From a0adbf9f8f77531fcf81ae31bbc7102698765ee3 Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Fri, 20 Nov 2020 14:43:49 -0500 Subject: [PATCH] fix: npm init flat-options The `flatOptions` property exposed by `lib/npm.js` should not be redefined. This fixes an error in `npm init` by just avoiding trying to override the property there. Fixes: #2206 PR-URL: https://github.com/npm/cli/pull/2213 Credit: @ruyadorno Close: #2213 Reviewed-by: @nlf --- lib/init.js | 1 - test/lib/init.js | 33 +++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/init.js b/lib/init.js index ed476ef38cb28..ac49f54a7954d 100644 --- a/lib/init.js +++ b/lib/init.js @@ -43,7 +43,6 @@ const init = async args => { } } npm.config.set('package', []) - npm.flatOptions = { ...npm.flatOptions, package: [] } return new Promise((res, rej) => { npm.commands.exec([packageName, ...args.slice(1)], er => er ? rej(er) : res()) }) diff --git a/test/lib/init.js b/test/lib/init.js index cb15eac8fc2eb..e73cc4b30988c 100644 --- a/test/lib/init.js +++ b/test/lib/init.js @@ -29,7 +29,7 @@ t.afterEach(cb => { result = '' npm.config = { get: () => '', set () {} } npm.commands = {} - npm.flatOptions = {} + Object.defineProperty(npm, 'flatOptions', { value: {} }) npm.log = npmLog cb() }) @@ -52,9 +52,7 @@ t.test('classic npm init -y', t => { npm.config = { get: () => '~/.npm-init.js', } - npm.flatOptions = { - yes: true, - } + Object.defineProperty(npm, 'flatOptions', { value: { yes: true} }) npm.log = { ...npm.log } npm.log.silly = (title, msg) => { t.equal(title, 'package data', 'should print title') @@ -179,6 +177,33 @@ t.test('npm init exec error', t => { }) }) +t.test('should not rewrite flatOptions', t => { + t.plan(4) + Object.defineProperty(npm, 'flatOptions', { + get: () => ({}), + set () { + throw new Error('Should not set flatOptions') + }, + }) + npm.config = { + set (key, val) { + t.equal(key, 'package', 'should set package key') + t.deepEqual(val, [], 'should set empty array value') + }, + } + npm.commands.exec = (arr, cb) => { + t.deepEqual( + arr, + ['create-react-app', 'my-app'], + 'should npx with extra args' + ) + cb() + } + init(['react-app', 'my-app'], err => { + t.ifError(err, 'npm init react-app') + }) +}) + t.test('npm init cancel', t => { t.plan(3) const init = requireInject('../../lib/init.js', {