Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor packageManager.js for less redundancy #433

Merged
merged 13 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@react-native-community/eslint-config": "^0.0.5",
"@types/jest": "^24.0.11",
"@types/node": "^11.13.0",
"@types/node-fetch": "^2.3.7",
"babel-jest": "^24.6.0",
"babel-plugin-module-resolver": "^3.2.0",
"chalk": "^2.4.2",
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/tools/__tests__/packageManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('npm', () => {

expect(execa).toHaveBeenCalledWith(
'npm',
['install', ...PACKAGES, '--save', '--save-exact'],
['install', '--save', '--save-exact', ...PACKAGES],
EXEC_OPTS,
);
});
Expand All @@ -65,7 +65,7 @@ describe('npm', () => {

expect(execa).toHaveBeenCalledWith(
'npm',
['install', ...PACKAGES, '--save-dev', '--save-exact'],
['install', '--save-dev', '--save-exact', ...PACKAGES],
EXEC_OPTS,
);
});
Expand All @@ -75,7 +75,7 @@ describe('npm', () => {

expect(execa).toHaveBeenCalledWith(
'npm',
['uninstall', ...PACKAGES, '--save'],
['uninstall', '--save', ...PACKAGES],
EXEC_OPTS,
);
});
Expand All @@ -87,7 +87,7 @@ it('should use npm if yarn is not available', () => {

expect(execa).toHaveBeenCalledWith(
'npm',
['install', ...PACKAGES, '--save', '--save-exact'],
['install', '--save', '--save-exact', ...PACKAGES],
EXEC_OPTS,
);
});
Expand All @@ -100,7 +100,7 @@ it('should use npm if project is not using yarn', () => {

expect(execa).toHaveBeenCalledWith(
'npm',
['install', ...PACKAGES, '--save', '--save-exact'],
['install', '--save', '--save-exact', ...PACKAGES],
EXEC_OPTS,
);
expect(yarn.isProjectUsingYarn).toHaveBeenCalledWith(PROJECT_ROOT);
Expand Down
50 changes: 30 additions & 20 deletions packages/cli/src/tools/packageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ type Options = {|

let projectDir;

const packageManagers = {
yarn: {
install: ['add'],
installDev: ['add', '-D'],
uninstall: ['remove'],
installAll: ['install'],
},
npm: {
install: ['install', '--save', '--save-exact'],
installDev: ['install', '--save-dev', '--save-exact'],
uninstall: ['uninstall', '--save'],
installAll: ['install'],
},
};

function configurePackageManager(
packageNames: Array<string>,
options?: Options,
action: 'install' | 'installDev' | 'installAll' | 'uninstall',
) {
const pm = shouldUseYarn(options) ? 'yarn' : 'npm';
const [executable, ...flags] = packageManagers[pm][action];
const args = [executable, ...flags, ...packageNames];
return executeCommand(pm, args, options);
}

function executeCommand(
command: string,
args: Array<string>,
Expand All @@ -36,33 +62,17 @@ export function setProjectDir(dir: string) {
}

export function install(packageNames: Array<string>, options?: Options) {
return shouldUseYarn(options)
? executeCommand('yarn', ['add', ...packageNames], options)
: executeCommand(
'npm',
['install', ...packageNames, '--save', '--save-exact'],
options,
);
return configurePackageManager(packageNames, options, 'install');
}

export function installDev(packageNames: Array<string>, options?: Options) {
return shouldUseYarn(options)
? executeCommand('yarn', ['add', '-D', ...packageNames], options)
: executeCommand(
'npm',
['install', ...packageNames, '--save-dev', '--save-exact'],
options,
);
return configurePackageManager(packageNames, options, 'installDev');
}

export function uninstall(packageNames: Array<string>, options?: Options) {
return shouldUseYarn(options)
? executeCommand('yarn', ['remove', ...packageNames], options)
: executeCommand('npm', ['uninstall', ...packageNames, '--save'], options);
return configurePackageManager(packageNames, options, 'uninstall');
}

export function installAll(options?: Options) {
return shouldUseYarn(options)
? executeCommand('yarn', ['install'], options)
: executeCommand('npm', ['install'], options);
return configurePackageManager([], options, 'installAll');
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,13 @@
dependencies:
"@types/node" "*"

"@types/node-fetch@^2.3.7":
version "2.3.7"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.3.7.tgz#b7212e895100f8642dbdab698472bab5f3c1d2f1"
integrity sha512-+bKtuxhj/TYSSP1r4CZhfmyA0vm/aDRQNo7vbAgf6/cZajn0SAniGGST07yvI4Q+q169WTa2/x9gEHfJrkcALw==
dependencies:
"@types/node" "*"

"@types/node@*":
version "12.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.0.tgz#d11813b9c0ff8aaca29f04cbc12817f4c7d656e5"
Expand Down