Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: save dep with tag if install with tag #408

Merged
merged 1 commit into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
], {
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();
});
});
});
}