Skip to content
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

refactor: add pacote to resolve template name instead of arborist workaround #1115

Closed
wants to merge 11 commits into from
12 changes: 7 additions & 5 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const filenamify = require('filenamify');
const git = require('simple-git');
const log = require('loglevel');
const Arborist = require('@npmcli/arborist');
const pacote = require('pacote');
const Config = require('@npmcli/config');
const requireg = require('requireg');
const npmPath = requireg.resolve('npm').replace('index.js','');
Expand Down Expand Up @@ -607,14 +608,15 @@ class Generator {
saveType: 'prod',
save: false
});

const manifest = await pacote.manifest(this.templateName);
if (!manifest) throw new Error('Unable to resolve the name of the added package. It was most probably not added to node_modules successfully');

const addResult = arb[Symbol.for('resolvedAdd')];
if (!addResult) throw new Error('Unable to resolve the name of the added package. It was most probably not added to node_modules successfully');

const packageName = addResult[0].name;
const packageName = manifest.name;
const packageVersion = installResult.children.get(packageName).version;
const packagePath = installResult.children.get(packageName).path;

const packagePath = installResult.children.get(packageName).path;

if (!isFileSystemPath(this.templateName)) log.debug(logMessage.templateSuccessfullyInstalled(packageName, packagePath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));

Expand Down
1 change: 1 addition & 0 deletions apps/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"minimatch": "^3.0.4",
"node-fetch": "^2.6.0",
"nunjucks": "^3.2.0",
"pacote": "^15.1.1",
"requireg": "^0.2.2",
"resolve-from": "^5.0.0",
"resolve-pkg": "^2.0.0",
Expand Down
2 changes: 0 additions & 2 deletions apps/generator/test/__mocks__/@npmcli/arborist.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const arb = jest.genMockFromModule('@npmcli/arborist');

arb.prototype[Symbol.for('resolvedAdd')] = [{name: 'test'}];

arb.prototype.reify = jest.fn(async (opt) => {
const childrenMap = new Map();
childrenMap.set('test', {path: './test'});
Expand Down
9 changes: 9 additions & 0 deletions apps/generator/test/__mocks__/pacote/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const pacote = jest.genMockFromModule('pacote');

pacote.manifest = jest.fn(async (templateName) => {
return {
name: 'test'
};
});

module.exports = pacote;
Loading
Loading