Skip to content

Commit a0adbf9

Browse files
ruyadornonlf
authored andcommitted
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: #2213 Credit: @ruyadorno Close: #2213 Reviewed-by: @nlf
1 parent 084a7b6 commit a0adbf9

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

lib/init.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const init = async args => {
4343
}
4444
}
4545
npm.config.set('package', [])
46-
npm.flatOptions = { ...npm.flatOptions, package: [] }
4746
return new Promise((res, rej) => {
4847
npm.commands.exec([packageName, ...args.slice(1)], er => er ? rej(er) : res())
4948
})

test/lib/init.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ t.afterEach(cb => {
2929
result = ''
3030
npm.config = { get: () => '', set () {} }
3131
npm.commands = {}
32-
npm.flatOptions = {}
32+
Object.defineProperty(npm, 'flatOptions', { value: {} })
3333
npm.log = npmLog
3434
cb()
3535
})
@@ -52,9 +52,7 @@ t.test('classic npm init -y', t => {
5252
npm.config = {
5353
get: () => '~/.npm-init.js',
5454
}
55-
npm.flatOptions = {
56-
yes: true,
57-
}
55+
Object.defineProperty(npm, 'flatOptions', { value: { yes: true} })
5856
npm.log = { ...npm.log }
5957
npm.log.silly = (title, msg) => {
6058
t.equal(title, 'package data', 'should print title')
@@ -179,6 +177,33 @@ t.test('npm init exec error', t => {
179177
})
180178
})
181179

180+
t.test('should not rewrite flatOptions', t => {
181+
t.plan(4)
182+
Object.defineProperty(npm, 'flatOptions', {
183+
get: () => ({}),
184+
set () {
185+
throw new Error('Should not set flatOptions')
186+
},
187+
})
188+
npm.config = {
189+
set (key, val) {
190+
t.equal(key, 'package', 'should set package key')
191+
t.deepEqual(val, [], 'should set empty array value')
192+
},
193+
}
194+
npm.commands.exec = (arr, cb) => {
195+
t.deepEqual(
196+
arr,
197+
['create-react-app', 'my-app'],
198+
'should npx with extra args'
199+
)
200+
cb()
201+
}
202+
init(['react-app', 'my-app'], err => {
203+
t.ifError(err, 'npm init react-app')
204+
})
205+
})
206+
182207
t.test('npm init cancel', t => {
183208
t.plan(3)
184209
const init = requireInject('../../lib/init.js', {

0 commit comments

Comments
 (0)