Skip to content

Commit

Permalink
fix: fix save alias error (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
gemwuu authored Jul 5, 2021
1 parent c4bdcd1 commit 1e365c8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
6 changes: 4 additions & 2 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { parsePackageName } = require('../lib/alias');
const {
LOCAL_TYPES,
REMOTE_TYPES,
ALIAS_TYPES,
} = require('../lib/npa_types');

const orignalArgv = process.argv.slice(2);
Expand Down Expand Up @@ -146,9 +147,8 @@ if (process.env.NPMINSTALL_BY_UPDATE) {
for (const name of argv._) {
const [
aliasPackageName,
realPackageName,
] = parsePackageName(name);
const p = npa(String(realPackageName), argv.root);
const p = npa(name, argv.root);
pkgs.push({
name: p.name,
// `mozilla/nunjucks#0f8b21b8df7e8e852b2e1889388653b7075f0d09` should be rawSpec
Expand Down Expand Up @@ -454,6 +454,8 @@ async function updateDependencies(root, pkgs, propName, saveExact, remoteNames)
item.name
? deps[item.name] = item.version
: deps[remoteNames[item.version]] = item.version;
} else if (item.type === ALIAS_TYPES) {
deps[item.name] = item.version;
} else {
const pkgDir = LOCAL_TYPES.includes(item.type) ? item.version : path.join(root, 'node_modules', item.name);
const itemPkg = await utils.readJSON(path.join(pkgDir, 'package.json'));
Expand Down
2 changes: 1 addition & 1 deletion lib/download/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ module.exports = async (pkg, options) => {
return await git(pkg, options);
}

// type is `tag | version | range`
// type is `tag | version | range | alias`
return await npm(pkg, options);
};
2 changes: 1 addition & 1 deletion lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const utils = require('../utils');
const config = require('../cnpm_config');

module.exports = async (pkg, options) => {
const realPkg = await resolve(pkg, options);
const realPkg = await resolve(pkg.subSpec || pkg, options);
// download tarball and unzip
const info = await download(realPkg, options);
info.package = realPkg;
Expand Down
2 changes: 2 additions & 0 deletions lib/npa_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ exports.REMOTE_TYPES = [
'remote',
'git',
];

exports.ALIAS_TYPES = 'alias';
27 changes: 27 additions & 0 deletions test/installSaveDeps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ if (process.platform !== 'win32') {
});
});

it('should install --save alias and update dependencies', done => {
coffee.fork(helper.npminstall, [
'--save',
'lodash-has-v3@npm:lodash.has@^3',
'lodash-has-v4@npm:lodash.has@^4',
], {
cwd: tmp,
})
// .debug()
.expect('code', 0)
.end(err => {
assert(!err, err && err.message);
const deps = JSON.parse(fs.readFileSync(path.join(tmp, 'package.json'))).dependencies;
const lodashHashV3 = JSON.parse(fs.readFileSync(path.join(tmp, 'node_modules/lodash-has-v3', 'package.json')));
const lodashHashV4 = JSON.parse(fs.readFileSync(path.join(tmp, 'node_modules/lodash-has-v4', 'package.json')));

assert(deps);
assert.strictEqual(deps['lodash-has-v3'], 'npm:lodash.has@^3');
assert.strictEqual(deps['lodash-has-v4'], 'npm:lodash.has@^4');
assert(/^3\.\d+\.\d+$/.test(lodashHashV3.version));
assert.strictEqual(lodashHashV3.name, 'lodash.has');
assert(/^4\.\d+\.\d+$/.test(lodashHashV4.version));
assert.strictEqual(lodashHashV4.name, 'lodash.has');
done();
});
});

it('should install --save-optional pedding and update optionalDependencies', done => {
coffee.fork(helper.npminstall, [
'--save-optional',
Expand Down

0 comments on commit 1e365c8

Please sign in to comment.