Skip to content

Commit

Permalink
fix(get-package-name): resolve paths properly before matching (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
smackfu authored Jan 5, 2024
1 parent c5acbb2 commit 28aefa6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
9 changes: 8 additions & 1 deletion __tests__/utils/get-package-name.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
* under the License.
*/

const path = require('path');
const getPackageName = require('../../src/utils/get-package-name');

jest.mock('fs', () => ({
readFileSync: () => ({
dependencies: {
anotherDepName: '1.0.0',
templateDepName: 'file:../some/path/my-template.tgz',
},
}),
Expand All @@ -41,7 +43,12 @@ describe('getPackageName', () => {
expect(getPackageName('@somescope/my-template')).toEqual('@somescope/my-template');
});

it('retrieves name from installed package', () => {
it('retrieves name from installed package on relative path', () => {
expect(getPackageName('../some/path/my-template.tgz')).toEqual('templateDepName');
});

it('retrieves name from installed package on absolute path', () => {
const resolvedAbsolutePath = path.resolve(__dirname, '../..', '../some/path/my-template.tgz');
expect(getPackageName(resolvedAbsolutePath)).toEqual('templateDepName');
});
});
11 changes: 9 additions & 2 deletions src/utils/get-package-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ const path = require('path');

// This only works if tarball has been installed
const getPackageName = (templateName) => {
if (templateName.match(/^\..+\.(tgz|tar(\.gz){0,1})$/)) {
if (templateName.match(/.+\.(tgz|tar(\.gz){0,1})$/)) {
const templatePath = path.resolve(templateName);
const { dependencies } = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../package.json')));
return Object.entries(dependencies)
.find(([, version]) => version.match(templateName))[0];
.find(([, version]) => {
if (version.startsWith('file:')) {
const dependencyPath = path.resolve(__dirname, '../../', version.slice(5));
return dependencyPath === templatePath;
}
return false;
})[0];
}
return templateName.charAt(0) + templateName.slice(1).split('@')[0];
};
Expand Down
12 changes: 4 additions & 8 deletions src/utils/install-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ const path = require('path');
const fs = require('fs');
const runNpmInstall = require('./run-npm-install');

const resolveRelativeTarball = (templateName) => {
// matches if a relative path to tarball
if (templateName.match(/^\..+\.(tgz|tar(\.gz){0,1})$/)) {
const templatePath = path.resolve(templateName);
return fs.existsSync(templatePath) ? templatePath : templateName;
}
return templateName;
const resolveLocalTarball = (templateName) => {
const templatePath = path.resolve(templateName);
return fs.existsSync(templatePath) ? templatePath : templateName;
};

const installTemplate = (templateName) => runNpmInstall(__dirname, [
resolveRelativeTarball(templateName),
resolveLocalTarball(templateName),
'--ignore-scripts',
'--save',
'--save-exact',
Expand Down

0 comments on commit 28aefa6

Please sign in to comment.