Skip to content

Commit

Permalink
fix: npm init flat-options
Browse files Browse the repository at this point in the history
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: npm#2206
  • Loading branch information
ruyadorno committed Nov 20, 2020
1 parent e1a2837 commit 848ee0a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/init.js
Expand Up @@ -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())
})
Expand Down
33 changes: 29 additions & 4 deletions test/lib/init.js
Expand Up @@ -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()
})
Expand All @@ -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')
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit 848ee0a

Please sign in to comment.