Skip to content

Commit

Permalink
feat: use pacote for package name resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
achaljhawar committed Dec 20, 2024
1 parent 8bc3a78 commit c0b4ed4
Show file tree
Hide file tree
Showing 5 changed files with 1,697 additions and 34 deletions.
8 changes: 3 additions & 5 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const log = require('loglevel');
const Arborist = require('@npmcli/arborist');
const Config = require('@npmcli/config');
const requireg = require('requireg');
const pacote = require('pacote');
const npmPath = requireg.resolve('npm').replace('index.js','');

const { isAsyncAPIDocument } = require('@asyncapi/parser/cjs/document');
Expand Down Expand Up @@ -604,11 +605,8 @@ class Generator {
saveType: 'prod',
save: false
});

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 manifest = await pacote.manifest(this.templateName);
const packageName = manifest.name;
const packageVersion = installResult.children.get(packageName).version;
const packagePath = installResult.children.get(packageName).path;

Expand Down
3 changes: 2 additions & 1 deletion apps/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@
"fs.extra": "^1.3.2",
"global-dirs": "^3.0.0",
"jmespath": "^0.15.0",
"pacote": "^15.1.1",
"js-yaml": "^3.13.1",
"levenshtein-edit-distance": "^2.0.5",
"levenshtein-edit-distance": "^2.0.5pacote",
"loglevel": "^1.6.8",
"minimatch": "^3.0.4",
"node-fetch": "^2.6.0",
Expand Down
3 changes: 0 additions & 3 deletions apps/generator/test/__mocks__/@npmcli/arborist.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
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

0 comments on commit c0b4ed4

Please sign in to comment.