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

fix: npm link <pkg> should not save package.json #2245

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,23 @@ const linkInstall = async args => {
)
}

// npm link should not save=true by default unless you're
// using any of --save-dev or other types
const save =
Boolean(npm.config.find('save') !== 'default' || npm.flatOptions.saveType)

// create a new arborist instance for the local prefix and
// reify all the pending names as symlinks there
const localArb = new Arborist({
...npm.flatOptions,
path: npm.prefix,
save,
})
await localArb.reify({
...npm.flatOptions,
path: npm.prefix,
add: names.map(l => `file:${resolve(globalTop, 'node_modules', l)}`),
save,
})

await reifyFinish(localArb)
Expand Down
12 changes: 11 additions & 1 deletion test/lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const npm = {
get () {
return false
},
find () {},
},
}
const printLinks = async (opts) => {
Expand Down Expand Up @@ -196,7 +197,7 @@ t.test('link global linked pkg to local nm when using args', (t) => {
})

t.test('link pkg already in global space', (t) => {
t.plan(2)
t.plan(3)

const testdir = t.testdir({
'global-prefix': {
Expand Down Expand Up @@ -224,17 +225,26 @@ t.test('link pkg already in global space', (t) => {
npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules')
npm.prefix = resolve(testdir, 'my-project')

npm.config.find = () => 'default'

const _cwd = process.cwd()
process.chdir(npm.prefix)

reifyOutput = async () => {
reifyOutput = undefined
process.chdir(_cwd)
npm.config.find = () => null

const links = await printLinks({
path: npm.prefix,
})

t.equal(
require(resolve(testdir, 'my-project', 'package.json')).dependencies,
undefined,
'should not save to package.json upon linking'
)

t.matchSnapshot(links, 'should create a local symlink to global pkg')
}

Expand Down