diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index b09ecc478f64f..158bef8b8c8bf 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -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') diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 9e9fac4f6dc34..c5ac7e787ea78 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -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: {