-
Notifications
You must be signed in to change notification settings - Fork 907
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Rework PackageManager implementation (#243)
Summary: --------- Removed nasty `class`, and replaced it with grouped methods. `projectDir` is always the same in `cli` lifecycle, so we set it once on the start. Based on the presence of `yarn.lock` we decide either to use yarn or not to use it. `preferYarn` is overriding this behavior, and can later be used in `init` command which lacks correct `projectDir`. Test Plan: ---------- - [x] - Unit tests passing - [x] - Manual testing
- Loading branch information
Showing
10 changed files
with
94 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
// @flow | ||
import {execSync} from 'child_process'; | ||
import yarn from './yarn'; | ||
import {getYarnVersionIfAvailable, isProjectUsingYarn} from './yarn'; | ||
|
||
type PackageManagerOptions = { | ||
forceNpm?: boolean, | ||
projectDir: string, | ||
}; | ||
type Options = {| | ||
preferYarn?: boolean, | ||
silent?: boolean, | ||
|}; | ||
|
||
export default class PackageManager { | ||
options: PackageManagerOptions; | ||
let projectDir; | ||
|
||
constructor(options: PackageManagerOptions) { | ||
this.options = options; | ||
} | ||
function executeCommand(command: string, options?: Options) { | ||
return execSync(command, { | ||
stdio: options && options.silent ? 'pipe' : 'inherit', | ||
}); | ||
} | ||
|
||
executeCommand(command: string, options?: {silent: boolean}) { | ||
return execSync(command, { | ||
stdio: options && options.silent ? 'pipe' : 'inherit', | ||
}); | ||
function shouldUseYarn(options?: Options) { | ||
if (options && options.preferYarn !== undefined) { | ||
return options.preferYarn && getYarnVersionIfAvailable(); | ||
} | ||
|
||
shouldCallYarn() { | ||
return ( | ||
!this.options.forceNpm && | ||
yarn.getYarnVersionIfAvailable() && | ||
yarn.isGlobalCliUsingYarn(this.options.projectDir) | ||
); | ||
} | ||
return isProjectUsingYarn(projectDir) && getYarnVersionIfAvailable(); | ||
} | ||
|
||
install(packageNames: Array<string>, options?: {silent: boolean}) { | ||
return this.shouldCallYarn() | ||
? this.executeCommand(`yarn add ${packageNames.join(' ')}`, options) | ||
: this.executeCommand( | ||
`npm install ${packageNames.join(' ')} --save --save-exact`, | ||
options, | ||
); | ||
} | ||
export function setProjectDir(dir: string) { | ||
projectDir = dir; | ||
} | ||
|
||
installDev(packageNames: Array<string>) { | ||
return this.shouldCallYarn() | ||
? this.executeCommand(`yarn add -D ${packageNames.join(' ')}`) | ||
: this.executeCommand( | ||
`npm install ${packageNames.join(' ')} --save-dev --save-exact`, | ||
); | ||
} | ||
export function install(packageNames: Array<string>, options?: Options) { | ||
return shouldUseYarn(options) | ||
? executeCommand(`yarn add ${packageNames.join(' ')}`, options) | ||
: executeCommand( | ||
`npm install ${packageNames.join(' ')} --save --save-exact`, | ||
options, | ||
); | ||
} | ||
|
||
uninstall(packageNames: Array<string>) { | ||
return this.shouldCallYarn() | ||
? this.executeCommand(`yarn remove ${packageNames.join(' ')}`) | ||
: this.executeCommand(`npm uninstall ${packageNames.join(' ')} --save`); | ||
} | ||
export function installDev(packageNames: Array<string>, options?: Options) { | ||
return shouldUseYarn(options) | ||
? executeCommand(`yarn add -D ${packageNames.join(' ')}`, options) | ||
: executeCommand( | ||
`npm install ${packageNames.join(' ')} --save-dev --save-exact`, | ||
options, | ||
); | ||
} | ||
|
||
export function uninstall(packageNames: Array<string>, options?: Options) { | ||
return shouldUseYarn(options) | ||
? executeCommand(`yarn remove ${packageNames.join(' ')}`, options) | ||
: executeCommand(`npm uninstall ${packageNames.join(' ')} --save`, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.