Skip to content

Commit 6dc114c

Browse files
feat: detect yarn according to yarn.lock (#872)
* feat: detect yarn according to yarn.lock * Move autoyarn logic to initOptions. * Allow cli to override autoyarn. Co-authored-by: Raine Revere <[email protected]>
1 parent 5b8b582 commit 6dc114c

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

bin/cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const combinedArguments = rcResult
8282

8383
program.parse(combinedArguments)
8484

85-
// filter out undefined program options and combine with config file options
85+
// filter out undefined program options and combine cli options with config file options
8686
const options = {
8787
...rcResult && Object.keys(rcResult.config).length > 0
8888
? { rcConfigPath: rcResult.filePath }
@@ -93,4 +93,6 @@ const options = {
9393
cli: true,
9494
}
9595

96+
// NOTE: Options handling and defaults go in initOptions in index.js
97+
9698
ncu.run(options)

lib/cli-options.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ const cliOptions = [
182182
long: 'packageManager',
183183
short: 'p',
184184
arg: 'name',
185-
description: 'npm, yarn',
186-
default: 'npm'
185+
// manual default to allow overriding auto yarn detection
186+
description: 'npm, yarn (default: "npm")'
187187
},
188188
{
189189
long: 'pre',

lib/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ function initOptions(options) {
297297
...options.ownerChanged ? ['ownerChanged'] : []
298298
]
299299

300+
// autodetect yarn
301+
const files = fs.readdirSync(options.cwd || '.')
302+
const autoYarn = !options.packageManager && files.includes('yarn.lock') && !files.includes('package-lock.json')
303+
if (autoYarn) {
304+
print(options, 'Using yarn')
305+
}
306+
300307
return {
301308
...options,
302309
...options.deep ? { packageFile: `${deepPatternPrefix}${getPackageFileName(options)}` } : null,
@@ -314,6 +321,7 @@ function initOptions(options) {
314321
target,
315322
// imply upgrade in interactive mode when json is not specified as the output
316323
...options.interactive && options.upgrade === undefined ? { upgrade: !json } : null,
324+
...!options.packageManager && { packageManager: autoYarn ? 'yarn' : 'npm' },
317325
}
318326
}
319327

0 commit comments

Comments
 (0)