Skip to content
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
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
  • Loading branch information
ruyadorno committed Nov 20, 2020
commit 848ee0a5cc53aaddd8bb221028dc9fb471f8dec7
1 change: 0 additions & 1 deletion lib/init.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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