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 10 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
55 changes: 35 additions & 20 deletions packages/cli/src/tools/packageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ type Options = {|

let projectDir;

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

function configurePackageManager(
packageNames: Array<string>,
options?: Options,
action: 'add' | 'addDev' | 'remove',
) {
const pm = shouldUseYarn(options) ? 'yarn' : 'npm';
const pmConfig = packageManagers[pm];

const [executable, ...flags] = pmConfig[action];
const args = [executable, ...flags, ...packageNames];
return executeCommand(pm, args, options);
}

function executeCommand(
command: string,
args: Array<string>,
Expand All @@ -36,33 +64,20 @@ 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, 'add');
}

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, 'addDev');
}

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, 'remove');
}

export function installAll(options?: Options) {
return shouldUseYarn(options)
? executeCommand('yarn', ['install'], options)
: executeCommand('npm', ['install'], options);
const pm = shouldUseYarn(options) ? 'yarn' : 'npm';
const pmConfig = packageManagers[pm];

return executeCommand(pm, pmConfig.install, options);
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
}
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