Skip to content

fix(npm/pacote): allow tns to be able to use npm configuration properly #4992

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 7 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,5 @@ $injector.require("applePortalApplicationService", "./services/apple-portal/appl
$injector.require("watchIgnoreListService", "./services/watch-ignore-list-service");

$injector.requirePublicClass("initializeService", "./services/initialize-service");

$injector.require("npmConfigService", "./services/npm-config-service");
4 changes: 4 additions & 0 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,4 +1053,8 @@ interface IWatchIgnoreListService {
addFileToIgnoreList(filePath: string): void;
removeFileFromIgnoreList(filePath: string): void;
isFileInIgnoreList(filePath: string): boolean;
}

interface INpmConfigService {
getConfig(): IDictionary<any>;
}
3 changes: 3 additions & 0 deletions lib/definitions/libnpmconfig.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "libnpmconfig" {
export function read(options?: Object): Object;
}
22 changes: 22 additions & 0 deletions lib/services/npm-config-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as npmconfig from "libnpmconfig";

export class NpmConfigService implements INpmConfigService {
private config: IDictionary<any> = { };

constructor() {
this.readConfig();
}

public getConfig(): IDictionary<any> {
return this.config;
}

private readConfig(): void {
const data = npmconfig.read();
data.forEach((value: any, key: string) => {
// replace env ${VARS} in strings with the process.env value
this.config[key] = typeof value !== 'string' ? value : value.replace(/\${([^}]+)}/, (_, envVar) => process.env[envVar]);
});
}
}
$injector.register("npmConfigService", NpmConfigService);
14 changes: 11 additions & 3 deletions lib/services/pacote-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export class PacoteService implements IPacoteService {
constructor(private $fs: IFileSystem,
private $injector: IInjector,
private $logger: ILogger,
private $proxyService: IProxyService) { }
private $npmConfigService: INpmConfigService,
private $proxyService: IProxyService) {
}

@cache()
public get $packageManager(): INodePackageManager {
Expand All @@ -18,13 +20,16 @@ export class PacoteService implements IPacoteService {
public async manifest(packageName: string, options?: IPacoteManifestOptions): Promise<any> {
this.$logger.trace(`Calling pacoteService.manifest for packageName: '${packageName}' and options: ${options}`);
const manifestOptions: IPacoteBaseOptions = await this.getPacoteBaseOptions();

if (options) {
_.extend(manifestOptions, options);
}

packageName = this.getRealPackageName(packageName);
this.$logger.trace(`Calling pacote.manifest for packageName: ${packageName} and options: ${JSON.stringify(manifestOptions, null, 2)}`);
return pacote.manifest(packageName, manifestOptions);
const result = pacote.manifest(packageName, manifestOptions);

return result;
}

public async extractPackage(packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> {
Expand Down Expand Up @@ -67,7 +72,10 @@ export class PacoteService implements IPacoteService {
private async getPacoteBaseOptions(): Promise<IPacoteBaseOptions> {
// In case `tns create myapp --template https://github.com/NativeScript/template-hello-world.git` command is executed, pacote module throws an error if cache option is not provided.
const cachePath = await this.$packageManager.getCachePath();
const pacoteOptions = { cache: cachePath };

// Add NPM Configuration to our Manifest options
const npmConfig = this.$npmConfigService.getConfig();
const pacoteOptions = _.extend(npmConfig, { cache: cachePath });
const proxySettings = await this.$proxyService.getCache();
if (proxySettings) {
_.extend(pacoteOptions, proxySettings);
Expand Down
Loading