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: issue-5280 Ensure save flags update package.json #7233

Open
wants to merge 6 commits into
base: latest
Choose a base branch
from
6 changes: 6 additions & 0 deletions workspaces/config/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ class Config {
for (const { data } of this.data.values()) {
this.#flatten(data, this.#flatOptions)
}

// Only set 'save' to true if a saveType has been specified
// and if 'save' is false due to non-default config
if (!this.isDefault('save') && this.#flatOptions.saveType) {
this.#flatOptions.save = true
}
this.#flatOptions.nodeBin = this.execPath
this.#flatOptions.npmBin = this.npmBin
process.emit('timeEnd', 'config:load:flatten')
Expand Down
39 changes: 39 additions & 0 deletions workspaces/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,45 @@ t.test('finding the global prefix', t => {
t.end()
})

t.test('manages the save flag when flat is retrieved', t => {
const npmPath = __dirname
const buildConfig = async (args = [], envSave = false) => {
const c = new Config({
argv: [process.execPath, __filename, ...args],
shorthands,
definitions,
npmPath,
flatten,
env: {
save: envSave,
},
})
await c.load()
// Ensure test runner environment's npm settings do not change test outcomes
c.set('save', envSave, 'user')
c.set('save', envSave, 'global')
c.set('save', envSave, 'project')
return c
}
t.test('does not override save to true if a save flag is not passed', async t => {
const c = await buildConfig([], false)
t.equal(c.flat.save, false)
})
t.test('does not override save to true if a negative save flag is passed', async t => {
const c = await buildConfig(['--save-dev=false'], false)
t.equal(c.flat.save, false)
})
t.test('overrides save to true if a save flag is passed', async t => {
const c = await buildConfig(['--save-prod'], false)
t.equal(c.flat.save, true)
})
t.test('does not overwrite save if --no-save is present', async t => {
const c = await buildConfig(['--no-save'], true)
t.equal(c.flat.save, false)
})
t.end()
})

t.test('finding the local prefix', t => {
const path = t.testdir({
hasNM: {
Expand Down