-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f66b4e
feat() - [Yarn Support - Part 1] Yarn and Package Manager implementation
msc-ddiaz 666c4d8
refactor() - [Yarn Support - Part 2] - Replace npm with package manag…
msc-ddiaz fae38b7
Fix linting issue
msc-miguel e18fa02
Fix url not being contructed properly
msc-miguel 6e28e94
fix() - use spawnFromEvent to dedup and inject needed services to bas…
msc-ddiaz 80f2d64
Merge branch 'feat/pkg-mngr-impl' into refactor/pkg-mgr-replace
msc-ddiaz 1364bec
fix() - ensure settings fs read happens prior to calling member metho…
msc-ddiaz 90d6e95
Merge branch 'feat/pkg-mngr-impl' into refactor/pkg-mgr-replace
msc-ddiaz 7f14263
Merge pull request #10 from mflor35/refactor/pkg-mgr-replace
mflor35 646aa5d
fix() - ensure packageManager is used everywhere
msc-ddiaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { isInteractive } from "./common/helpers"; | ||
|
||
export class BasePackageManager { | ||
constructor( | ||
protected $childProcess: IChildProcess, | ||
private $hostInfo: IHostInfo, | ||
private packageManager: string | ||
) { } | ||
|
||
protected getPackageManagerExecutableName(): string { | ||
let npmExecutableName = this.packageManager; | ||
|
||
if (this.$hostInfo.isWindows) { | ||
npmExecutableName += ".cmd"; | ||
} | ||
|
||
return npmExecutableName; | ||
} | ||
|
||
protected async processPackageManagerInstall(params: string[], opts: { cwd: string }) { | ||
const npmExecutable = this.getPackageManagerExecutableName(); | ||
const stdioValue = isInteractive() ? "inherit" : "pipe"; | ||
return await this.$childProcess.spawnFromEvent(npmExecutable, params, "close", { cwd: opts.cwd, stdio: stdioValue }); | ||
} | ||
|
||
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(" "); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -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(); | ||
} | ||
@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); |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import * as path from "path"; | ||
import { BasePackageManager } from "./base-package-manager"; | ||
import { exported } from './common/decorators'; | ||
|
||
export class YarnPackageManager extends BasePackageManager implements INodePackageManager { | ||
|
||
constructor( | ||
$childProcess: IChildProcess, | ||
private $errors: IErrors, | ||
private $fs: IFileSystem, | ||
$hostInfo: IHostInfo, | ||
private $httpClient: Server.IHttpClient, | ||
private $logger: ILogger, | ||
private $pacoteService: IPacoteService | ||
) { | ||
super($childProcess, $hostInfo, 'yarn'); | ||
} | ||
|
||
@exported("yarn") | ||
public async install(packageName: string, pathToSave: string, config: INodePackageManagerInstallOptions): Promise<INpmInstallResultInfo> { | ||
if (config.disableNpmInstall) { | ||
return; | ||
} | ||
if (config.ignoreScripts) { | ||
config['ignore-scripts'] = true; | ||
} | ||
|
||
const packageJsonPath = path.join(pathToSave, 'package.json'); | ||
const jsonContentBefore = this.$fs.readJson(packageJsonPath); | ||
|
||
const flags = this.getFlagsString(config, true); | ||
let params = []; | ||
const isInstallingAllDependencies = packageName === pathToSave; | ||
if (!isInstallingAllDependencies) { | ||
params.push('add', packageName); | ||
} | ||
|
||
params = params.concat(flags); | ||
const cwd = pathToSave; | ||
|
||
try { | ||
await this.processPackageManagerInstall(params, { cwd }); | ||
|
||
if (isInstallingAllDependencies) { | ||
return null; | ||
} | ||
|
||
const packageMetadata = await this.$pacoteService.manifest(packageName, {}); | ||
return { | ||
name: packageMetadata.name, | ||
version: packageMetadata.version | ||
}; | ||
|
||
} catch (e) { | ||
this.$fs.writeJson(packageJsonPath, jsonContentBefore); | ||
throw e; | ||
} | ||
} | ||
|
||
@exported("yarn") | ||
public uninstall(packageName: string, config?: IDictionary<string | boolean>, path?: string): Promise<string> { | ||
const flags = this.getFlagsString(config, false); | ||
return this.$childProcess.exec(`yarn remove ${packageName} ${flags}`, { cwd: path }); | ||
} | ||
|
||
@exported("yarn") | ||
public async view(packageName: string, config: Object): Promise<any> { | ||
const wrappedConfig = _.extend({}, config, { json: true }); | ||
|
||
const flags = this.getFlagsString(wrappedConfig, false); | ||
let viewResult: any; | ||
try { | ||
viewResult = await this.$childProcess.exec(`yarn info ${packageName} ${flags}`); | ||
} catch (e) { | ||
this.$errors.failWithoutHelp(e.message); | ||
} | ||
return JSON.parse(viewResult); | ||
} | ||
|
||
@exported("yarn") | ||
public search(filter: string[], config: IDictionary<string | boolean>): Promise<string> { | ||
throw new Error("Method not implemented. Yarn does not support searching for packages in the registry."); | ||
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.
|
||
} | ||
|
||
public async searchNpms(keyword: string): Promise<INpmsResult> { | ||
const httpRequestResult = await this.$httpClient.httpRequest(`https://api.npms.io/v2/search?q=keywords:${keyword}`); | ||
const result: INpmsResult = JSON.parse(httpRequestResult.body); | ||
return result; | ||
} | ||
|
||
@exported("yarn") | ||
public async getRegistryPackageData(packageName: string): Promise<any> { | ||
const registry = await this.$childProcess.exec(`yarn config get registry`); | ||
const url = `${registry.trim()}/${packageName}`; | ||
this.$logger.trace(`Trying to get data from yarn registry for package ${packageName}, url is: ${url}`); | ||
const responseData = (await this.$httpClient.httpRequest(url)).body; | ||
this.$logger.trace(`Successfully received data from yarn registry for package ${packageName}. Response data is: ${responseData}`); | ||
const jsonData = JSON.parse(responseData); | ||
this.$logger.trace(`Successfully parsed data from yarn registry for package ${packageName}.`); | ||
return jsonData; | ||
} | ||
|
||
@exported("yarn") | ||
getCachePath(): Promise<string> { | ||
throw new Error("Method not implemented."); | ||
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.
|
||
} | ||
} | ||
|
||
$injector.register("yarn", YarnPackageManager); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This method is
async
so it is possible to call some method from the class andthis.packageManager
to be null.So I suggest an approach similar to this https://github.com/NativeScript/nativescript-cli/blob/master/lib/common/mobile/android/android-debug-bridge.ts#L56.
This way
this._determinePackageManager();
should be removed from constructor and all methods that relied onthis.packageManager
should be decorated with@invokeInit()
On the other side
init()
method is decorated withcache()
decorator so the value will be persisted andthis._determinePackageManager()
will be called only once.Also we can rewrite
_determinePackageManager
method: