Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Jun 21, 2019
1 parent 642cc03 commit 318bac0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "jest",
"test:ci:unit": "jest packages --ci --coverage",
"test:ci:e2e": "jest e2e --ci -i",
"lint": "eslint --ext .js,.ts . --cache --report-unused-disable-directives",
"lint": "eslint --fix --ext .js,.ts . --cache --report-unused-disable-directives",
"test:ci:cocoapods": "ruby packages/platform-ios/native_modules.rb",
"flow-check": "flow check",
"postinstall": "yarn build",
Expand Down
56 changes: 21 additions & 35 deletions packages/cli/src/tools/packageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@ type Options = {|

let projectDir;

const yarnConfig = {
dependencies: ['add'],
devDependencies: ['add', '-D'],
uninstallDependencies: ['remove'],
};

const npmConfig = {
dependencies: ['install', '--save', '--save-exact'],
devDependencies: ['install', '--save-dev', '--save-exact'],
uninstallDependencies: ['uninstall', '--save'],
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(
pmIsYarn: boolean,
packageNames: Array<string>,
options?: Options,
installType: string,
action: 'add' | 'addDev' | 'remove',
) {
const pm = pmIsYarn ? 'yarn' : 'npm';
const pmConfig = pm === 'npm' ? npmConfig : yarnConfig;
const pm = shouldUseYarn(options) ? 'yarn' : 'npm';
const pmConfig = packageManagers[pm];

const [executable, ...flags] = pmConfig[installType];
const [executable, ...flags] = pmConfig[action];
const args = [executable, ...packageNames, ...flags];
return executeCommand(pm, args, options);
}
Expand Down Expand Up @@ -62,33 +64,17 @@ export function setProjectDir(dir: string) {
}

export function install(packageNames: Array<string>, options?: Options) {
return configurePackageManager(
shouldUseYarn(options),
packageNames,
options,
'dependencies',
);
return configurePackageManager(packageNames, options, 'add');
}

export function installDev(packageNames: Array<string>, options?: Options) {
return configurePackageManager(
shouldUseYarn(options),
packageNames,
options,
'devDependencies',
);
return configurePackageManager(packageNames, options, 'addDev');
}

export function uninstall(packageNames: Array<string>, options?: Options) {
return configurePackageManager(
shouldUseYarn(options),
packageNames,
options,
'uninstallDependencies',
);
return configurePackageManager(packageNames, options, 'remove');
}

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

0 comments on commit 318bac0

Please sign in to comment.