Skip to content

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

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

Closed
wants to merge 6 commits into from
Closed
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
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;
}
15 changes: 13 additions & 2 deletions lib/services/pacote-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import * as pacote from "pacote";
import * as tar from "tar";
import * as path from "path";
import { cache } from "../common/decorators";
import * as npmconfig from "libnpmconfig";

export class PacoteService implements IPacoteService {
private npmConfig: { [index: string]: any } = {};

constructor(private $fs: IFileSystem,
private $injector: IInjector,
private $logger: ILogger,
private $proxyService: IProxyService) { }
private $proxyService: IProxyService) {
npmconfig.read().forEach((value: any, key: string) => {
// replace env ${VARS} in strings with the process.env value
this.npmConfig[key] = typeof value !== 'string' ? value : value.replace(/\${([^}]+)}/, (_, envVar) => process.env[envVar] );
});
}

@cache()
public get $packageManager(): INodePackageManager {
Expand All @@ -18,6 +26,7 @@ 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);
}
Expand Down Expand Up @@ -67,7 +76,9 @@ 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 pacoteOptions = _.extend( this.npmConfig, {cache: cachePath });
const proxySettings = await this.$proxyService.getCache();
if (proxySettings) {
_.extend(pacoteOptions, proxySettings);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@
"nativescript-dev-xcode": "0.2.0",
"nativescript-doctor": "1.10.0",
"nativescript-preview-sdk": "0.3.4",
"libnpmconfig": "1.2.1",
"open": "0.0.5",
"ora": "2.0.0",
"osenv": "0.1.3",
"pacote": "8.1.6",
"pacote": "9.5.4",
"pako": "1.0.6",
"pbxproj-dom": "1.2.0",
"plist": "1.1.0",
Expand Down Expand Up @@ -139,4 +140,4 @@
"engines": {
"node": ">=10.0.0 <13.0.0"
}
}
}
33 changes: 28 additions & 5 deletions test/services/pacote-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { PacoteService } from '../../lib/services/pacote-service';
import { LoggerStub } from "../stubs";
import { sandbox, SinonSandbox, SinonStub } from "sinon";
import { EventEmitter } from "events";

const npmconfig = require("libnpmconfig");
const pacote = require("pacote");
const tar = require("tar");
const path = require("path");

const npmCachePath = "npmCachePath";
const defaultPacoteOpts: IPacoteBaseOptions = createPacoteOptions({});
const npmCachePath = defaultPacoteOpts['cache'];
const packageName = "testPackage";
const fullPath = `/Users/username/${packageName}`;
const destinationDir = "destinationDir";
const defaultPacoteOpts: IPacoteBaseOptions = { cache: npmCachePath };
const errorMessage = "error message";
const proxySettings: IProxySettings = {
hostname: "hostname",
Expand All @@ -36,6 +38,20 @@ interface ITestCase extends ITestSetup {
expectedArgs: any[];
}

function createPacoteOptions(source: Object): Object {
const options: { [index: string]: any } = {};
npmconfig.read().forEach((value: any, key: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a hardcoded config obj here instead of reading the real config from the file system and applying the same business logic in the unit tests.

This comment was marked as abuse.

This comment was marked as abuse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the details. As far as I understand, the problem is caused by libnpmconfig which is directly used in the Pacote service. In this way, we cannot inject a Stub returning a hardcoded value in the unit tests. If you refactor this part and extract the npmconfig related logic into a service, you will be able to mock it in the unit tests and avoid slow disk operations there. Also, have you checked this feature with yarn? Are they using the same config file?

This comment was marked as abuse.

This comment was marked as abuse.

// replace env ${VARS} in strings with the process.env value
options[key] = typeof value !== 'string' ? value : value.replace(/\${([^}]+)}/, (_, envVar) => process.env[envVar] );
});

// Copy any original source keys over our defaults
for (const key in source) {
options[key] = source[key];
}
return options;
}

const createTestInjector = (opts?: ITestSetup): IInjector => {
opts = opts || {};

Expand Down Expand Up @@ -103,8 +119,15 @@ describe("pacoteService", () => {
const setupTest = (opts?: ITestSetup): IPacoteService => {
opts = opts || {};
const testInjector = createTestInjector(opts);

if (opts.isLocalPackage) {
sandboxInstance.stub(path, "resolve").withArgs(packageName).returns(fullPath);
const oldPath = path.resolve;
sandboxInstance.stub(path, "resolve").callsFake((value:string) => {
if (value === packageName) {
return fullPath;
}
return oldPath(value);
});
}

return testInjector.resolve<IPacoteService>("pacoteService");
Expand All @@ -116,7 +139,7 @@ describe("pacoteService", () => {
const testData: ITestCase[] = [
{
name: "with 'cache' only when no opts are passed",
expectedArgs: [packageName, defaultPacoteOpts]
expectedArgs: [packageName, _.extend({}, defaultPacoteOpts)]
},
{
name: "with 'cache' and passed options",
Expand All @@ -137,7 +160,7 @@ describe("pacoteService", () => {
{
name: "with full path to file when it is local one",
isLocalPackage: true,
expectedArgs: [fullPath, defaultPacoteOpts]
expectedArgs: [fullPath, _.extend({}, defaultPacoteOpts)]
},
{
name: "with full path to file, 'cache' and passed options when local path is passed",
Expand Down