Skip to content

Commit 91ced98

Browse files
authored
feat: save dep with tag if install with tag (#408)
Follow npm/rfcs#547
1 parent 0432f13 commit 91ced98

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

bin/install.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ for (const name of argv._) {
164164
version: p.fetchSpec || p.rawSpec,
165165
type: p.type,
166166
alias: aliasPackageName,
167+
arg: p,
167168
});
168169
}
169170

@@ -460,7 +461,6 @@ function getIgnoreScripts() {
460461
}
461462

462463
async function updateDependencies(root, pkgs, propName, saveExact, remoteNames) {
463-
const savePrefix = saveExact ? '' : getVersionSavePrefix();
464464
const pkgFile = path.join(root, 'package.json');
465465
const pkg = await utils.readJSON(pkgFile);
466466
const deps = pkg[propName] = pkg[propName] || {};
@@ -476,7 +476,16 @@ async function updateDependencies(root, pkgs, propName, saveExact, remoteNames)
476476
} else {
477477
const pkgDir = LOCAL_TYPES.includes(item.type) ? item.version : path.join(root, 'node_modules', item.name);
478478
const itemPkg = await utils.readJSON(path.join(pkgDir, 'package.json'));
479-
deps[itemPkg.name] = `${savePrefix}${itemPkg.version}`;
479+
480+
let saveSpec;
481+
// If install with `cnpm i foo`, the type is tag but rawSpec is empty string
482+
if (item.arg.type === 'tag' && item.arg.rawSpec) {
483+
saveSpec = item.arg.rawSpec;
484+
} else {
485+
const savePrefix = saveExact ? '' : getVersionSavePrefix();
486+
saveSpec = `${savePrefix}${itemPkg.version}`;
487+
}
488+
deps[itemPkg.name] = saveSpec;
480489
}
481490
}
482491
// sort pkg[propName]

test/installSaveDeps.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,21 @@ if (process.platform !== 'win32') {
245245
done();
246246
});
247247
});
248+
249+
it('install with dist-tag should update dependencies with tag', done => {
250+
coffee.fork(helper.npminstall, [
251+
'pedding@latest',
252+
], {
253+
cwd: tmp,
254+
})
255+
.expect('code', 0)
256+
.end(err => {
257+
assert(!err, err && err.message);
258+
const deps = JSON.parse(fs.readFileSync(path.join(tmp, 'package.json'))).dependencies;
259+
assert(deps);
260+
assert(deps.pedding === 'latest');
261+
done();
262+
});
263+
});
248264
});
249265
}

0 commit comments

Comments
 (0)