Skip to content

Commit

Permalink
feat: save dep with tag if install with tag (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Jun 25, 2022
1 parent 0432f13 commit 91ced98
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ for (const name of argv._) {
version: p.fetchSpec || p.rawSpec,
type: p.type,
alias: aliasPackageName,
arg: p,
});
}

Expand Down Expand Up @@ -460,7 +461,6 @@ function getIgnoreScripts() {
}

async function updateDependencies(root, pkgs, propName, saveExact, remoteNames) {
const savePrefix = saveExact ? '' : getVersionSavePrefix();
const pkgFile = path.join(root, 'package.json');
const pkg = await utils.readJSON(pkgFile);
const deps = pkg[propName] = pkg[propName] || {};
Expand All @@ -476,7 +476,16 @@ async function updateDependencies(root, pkgs, propName, saveExact, remoteNames)
} 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'));
deps[itemPkg.name] = `${savePrefix}${itemPkg.version}`;

let saveSpec;
// If install with `cnpm i foo`, the type is tag but rawSpec is empty string
if (item.arg.type === 'tag' && item.arg.rawSpec) {
saveSpec = item.arg.rawSpec;
} else {
const savePrefix = saveExact ? '' : getVersionSavePrefix();
saveSpec = `${savePrefix}${itemPkg.version}`;
}
deps[itemPkg.name] = saveSpec;
}
}
// sort pkg[propName]
Expand Down
16 changes: 16 additions & 0 deletions test/installSaveDeps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,21 @@ if (process.platform !== 'win32') {
done();
});
});

it('install with dist-tag should update dependencies with tag', done => {
coffee.fork(helper.npminstall, [
'pedding@latest',
], {
cwd: tmp,
})
.expect('code', 0)
.end(err => {
assert(!err, err && err.message);
const deps = JSON.parse(fs.readFileSync(path.join(tmp, 'package.json'))).dependencies;
assert(deps);
assert(deps.pedding === 'latest');
done();
});
});
});
}

0 comments on commit 91ced98

Please sign in to comment.