@@ -49,6 +49,14 @@ const validateProjectName = require('validate-npm-package-name');
49
49
50
50
const packageJson = require ( './package.json' ) ;
51
51
52
+ function isUsingYarn ( ) {
53
+ // Yarn 1 and 2 set npm_execpath to a path ending with 'yarn' so we can detect
54
+ // that to determine if the script was run using yarn
55
+ // https://github.com/yarnpkg/yarn/blob/v1.22.11/src/util/execute-lifecycle-script.js#L81
56
+ // https://github.com/yarnpkg/berry/blob/%40yarnpkg/core%2F3.1.0-rc.2/packages/yarnpkg-core/sources/scriptUtils.ts#L113
57
+ return path . basename ( process . env . npm_execpath || '' , '.js' ) === 'yarn' ;
58
+ }
59
+
52
60
let projectName ;
53
61
54
62
function init ( ) {
@@ -69,7 +77,7 @@ function init() {
69
77
'--template <path-to-template>' ,
70
78
'specify a template for the created project'
71
79
)
72
- . option ( '--use-npm' )
80
+ . option ( '--use-yarn' , 'use yarn as the package manager' , isUsingYarn ( ) )
73
81
. option ( '--use-pnp' )
74
82
. allowUnknownOption ( )
75
83
. on ( '--help' , ( ) => {
@@ -228,14 +236,14 @@ function init() {
228
236
program . verbose ,
229
237
program . scriptsVersion ,
230
238
program . template ,
231
- program . useNpm ,
239
+ program . useYarn ,
232
240
program . usePnp
233
241
) ;
234
242
}
235
243
} ) ;
236
244
}
237
245
238
- function createApp ( name , verbose , version , template , useNpm , usePnp ) {
246
+ function createApp ( name , verbose , version , template , useYarn , usePnp ) {
239
247
const unsupportedNodeVersion = ! semver . satisfies (
240
248
// Coerce strings with metadata (i.e. `15.0.0-nightly`).
241
249
semver . coerce ( process . version ) ,
@@ -276,7 +284,6 @@ function createApp(name, verbose, version, template, useNpm, usePnp) {
276
284
JSON . stringify ( packageJson , null , 2 ) + os . EOL
277
285
) ;
278
286
279
- const useYarn = useNpm ? false : shouldUseYarn ( ) ;
280
287
const originalDirectory = process . cwd ( ) ;
281
288
process . chdir ( root ) ;
282
289
if ( ! useYarn && ! checkThatNpmCanReadCwd ( ) ) {
@@ -351,15 +358,6 @@ function createApp(name, verbose, version, template, useNpm, usePnp) {
351
358
) ;
352
359
}
353
360
354
- function shouldUseYarn ( ) {
355
- try {
356
- execSync ( 'yarnpkg --version' , { stdio : 'ignore' } ) ;
357
- return true ;
358
- } catch ( e ) {
359
- return false ;
360
- }
361
- }
362
-
363
361
function install ( root , useYarn , usePnp , dependencies , verbose , isOnline ) {
364
362
return new Promise ( ( resolve , reject ) => {
365
363
let command ;
0 commit comments