Skip to content

Commit 5ea6e8d

Browse files
Merge pull request NativeScript#4992 from NativeScript/fatme/scopednpm
fix(npm/pacote): allow tns to be able to use npm configuration properly
2 parents 8b0d496 + 781f373 commit 5ea6e8d

File tree

8 files changed

+314
-105
lines changed

8 files changed

+314
-105
lines changed

lib/bootstrap.ts

+2
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,5 @@ $injector.require("applePortalApplicationService", "./services/apple-portal/appl
226226
$injector.require("watchIgnoreListService", "./services/watch-ignore-list-service");
227227

228228
$injector.requirePublicClass("initializeService", "./services/initialize-service");
229+
230+
$injector.require("npmConfigService", "./services/npm-config-service");

lib/declarations.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1053,4 +1053,8 @@ interface IWatchIgnoreListService {
10531053
addFileToIgnoreList(filePath: string): void;
10541054
removeFileFromIgnoreList(filePath: string): void;
10551055
isFileInIgnoreList(filePath: string): boolean;
1056+
}
1057+
1058+
interface INpmConfigService {
1059+
getConfig(): IDictionary<any>;
10561060
}

lib/definitions/libnpmconfig.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "libnpmconfig" {
2+
export function read(options?: Object): Object;
3+
}

lib/services/npm-config-service.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as npmconfig from "libnpmconfig";
2+
3+
export class NpmConfigService implements INpmConfigService {
4+
private config: IDictionary<any> = { };
5+
6+
constructor() {
7+
this.readConfig();
8+
}
9+
10+
public getConfig(): IDictionary<any> {
11+
return this.config;
12+
}
13+
14+
private readConfig(): void {
15+
const data = npmconfig.read();
16+
data.forEach((value: any, key: string) => {
17+
// replace env ${VARS} in strings with the process.env value
18+
this.config[key] = typeof value !== 'string' ? value : value.replace(/\${([^}]+)}/, (_, envVar) => process.env[envVar]);
19+
});
20+
}
21+
}
22+
$injector.register("npmConfigService", NpmConfigService);

lib/services/pacote-service.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export class PacoteService implements IPacoteService {
77
constructor(private $fs: IFileSystem,
88
private $injector: IInjector,
99
private $logger: ILogger,
10-
private $proxyService: IProxyService) { }
10+
private $npmConfigService: INpmConfigService,
11+
private $proxyService: IProxyService) {
12+
}
1113

1214
@cache()
1315
public get $packageManager(): INodePackageManager {
@@ -18,13 +20,16 @@ export class PacoteService implements IPacoteService {
1820
public async manifest(packageName: string, options?: IPacoteManifestOptions): Promise<any> {
1921
this.$logger.trace(`Calling pacoteService.manifest for packageName: '${packageName}' and options: ${options}`);
2022
const manifestOptions: IPacoteBaseOptions = await this.getPacoteBaseOptions();
23+
2124
if (options) {
2225
_.extend(manifestOptions, options);
2326
}
2427

2528
packageName = this.getRealPackageName(packageName);
2629
this.$logger.trace(`Calling pacote.manifest for packageName: ${packageName} and options: ${JSON.stringify(manifestOptions, null, 2)}`);
27-
return pacote.manifest(packageName, manifestOptions);
30+
const result = pacote.manifest(packageName, manifestOptions);
31+
32+
return result;
2833
}
2934

3035
public async extractPackage(packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> {
@@ -67,7 +72,10 @@ export class PacoteService implements IPacoteService {
6772
private async getPacoteBaseOptions(): Promise<IPacoteBaseOptions> {
6873
// 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.
6974
const cachePath = await this.$packageManager.getCachePath();
70-
const pacoteOptions = { cache: cachePath };
75+
76+
// Add NPM Configuration to our Manifest options
77+
const npmConfig = this.$npmConfigService.getConfig();
78+
const pacoteOptions = _.extend(npmConfig, { cache: cachePath });
7179
const proxySettings = await this.$proxyService.getCache();
7280
if (proxySettings) {
7381
_.extend(pacoteOptions, proxySettings);

0 commit comments

Comments
 (0)