-
-
Notifications
You must be signed in to change notification settings - Fork 197
feat() - [Yarn Support - Part 1 and 2] Yarn and Package Manager implementation #4050
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
Changes from 3 commits
0f66b4e
666c4d8
fae38b7
e18fa02
6e28e94
80f2d64
1364bec
90d6e95
7f14263
646aa5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as child_process from 'child_process'; | ||
|
||
export class BasePackageManager { | ||
constructor(private packageManager: string) { } | ||
|
||
protected getNpmExecutableName(isWindows: boolean): string { | ||
let npmExecutableName = this.packageManager; | ||
|
||
if (isWindows) { | ||
npmExecutableName += ".cmd"; | ||
} | ||
|
||
return npmExecutableName; | ||
} | ||
|
||
protected async processPackageManagerInstall( | ||
childProcess: child_process.ChildProcess, | ||
isWindows: boolean, | ||
params: string[], | ||
): Promise<ISpawnResult> { | ||
return new Promise<ISpawnResult>((resolve, reject) => { | ||
let isFulfilled = false; | ||
let capturedOut = ""; | ||
let capturedErr = ""; | ||
|
||
const npmExecutable = this.getNpmExecutableName(isWindows); | ||
|
||
if (childProcess.stdout) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is duplicated to this https://github.com/NativeScript/nativescript-cli/blob/master/lib/common/child-process.ts#L57
These 2 methods should be deleted in this case: |
||
childProcess.stdout.on("data", (data: string) => { | ||
capturedOut += data; | ||
}); | ||
} | ||
|
||
if (childProcess.stderr) { | ||
childProcess.stderr.on("data", (data: string) => { | ||
capturedErr += data; | ||
}); | ||
} | ||
|
||
childProcess.on("close", (arg: any) => { | ||
const exitCode = typeof arg === "number" ? arg : arg && arg.code; | ||
|
||
if (exitCode === 0) { | ||
isFulfilled = true; | ||
const result = { | ||
stdout: capturedOut, | ||
stderr: capturedErr, | ||
exitCode | ||
}; | ||
|
||
resolve(result); | ||
} else { | ||
let errorMessage = `Command ${npmExecutable} ${params && params.join(" ")} failed with exit code ${exitCode}`; | ||
if (capturedErr) { | ||
errorMessage += ` Error output: \n ${capturedErr}`; | ||
} | ||
|
||
if (!isFulfilled) { | ||
isFulfilled = true; | ||
reject(new Error(errorMessage)); | ||
} | ||
} | ||
}); | ||
|
||
childProcess.on("error", (err: Error) => { | ||
if (!isFulfilled) { | ||
isFulfilled = true; | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
protected getFlagsString(config: any, asArray: boolean): any { | ||
const array: Array<string> = []; | ||
for (const flag in config) { | ||
if (flag === "global" && this.packageManager !== 'yarn') { | ||
array.push(`--${flag}`); | ||
array.push(`${config[flag]}`); | ||
} else if (config[flag]) { | ||
if (flag === "dist-tags" || flag === "versions") { | ||
array.push(` ${flag}`); | ||
continue; | ||
} | ||
array.push(`--${flag}`); | ||
} | ||
} | ||
if (asArray) { | ||
return array; | ||
} | ||
|
||
return array.join(" "); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
import { exported } from './common/decorators'; | ||
export class PackageManager implements INodePackageManager { | ||
private packageManager: INodePackageManager; | ||
|
||
constructor( | ||
private $errors: IErrors, | ||
private $npm: INodePackageManager, | ||
private $options: IOptions, | ||
private $yarn: INodePackageManager, | ||
private $userSettingsService: IUserSettingsService | ||
) { | ||
this._determinePackageManager(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is
This way On the other side Also we can rewrite
|
||
} | ||
@exported("packageManager") | ||
public install(packageName: string, pathToSave: string, config: INodePackageManagerInstallOptions): Promise<INpmInstallResultInfo> { | ||
return this.packageManager.install(packageName, pathToSave, config); | ||
} | ||
@exported("packageManager") | ||
public uninstall(packageName: string, config?: IDictionary<string | boolean>, path?: string): Promise<string> { | ||
return this.packageManager.uninstall(packageName, config, path); | ||
} | ||
@exported("packageManager") | ||
public view(packageName: string, config: Object): Promise<any> { | ||
return this.packageManager.view(packageName, config); | ||
} | ||
@exported("packageManager") | ||
public search(filter: string[], config: IDictionary<string | boolean>): Promise<string> { | ||
return this.packageManager.search(filter, config); | ||
} | ||
public searchNpms(keyword: string): Promise<INpmsResult> { | ||
return this.packageManager.searchNpms(keyword); | ||
} | ||
public getRegistryPackageData(packageName: string): Promise<any> { | ||
return this.packageManager.getRegistryPackageData(packageName); | ||
} | ||
public getCachePath(): Promise<string> { | ||
return this.packageManager.getCachePath(); | ||
} | ||
|
||
private _determinePackageManager(): void { | ||
this.$userSettingsService.getSettingValue('packageManager') | ||
.then((pm: string) => { | ||
if (pm === 'yarn' || this.$options.yarn) { | ||
this.packageManager = this.$yarn; | ||
} else { | ||
this.packageManager = this.$npm; | ||
} | ||
}) | ||
.catch((err) => { | ||
this.$errors.fail(`Unable to read package manager config from user settings ${err}`); | ||
}); | ||
} | ||
} | ||
|
||
$injector.register('packageManager', PackageManager); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to provide
isWindows
as param. You can determine it inside the method. Just need to inject$hostInfo
.You'll need to change this https://github.com/NativeScript/nativescript-cli/pull/4050/files#diff-5d0fca739bb2fbd7cab602dbeaa30b89R17 as well
This also should be changed https://github.com/NativeScript/nativescript-cli/pull/4050/files#diff-5d0fca739bb2fbd7cab602dbeaa30b89L11
NOTE: No
private
before$hostInfo
.