|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { execSync, spawn } = require('child_process'); |
| 4 | +const { resolve } = require('path'); |
| 5 | +const { existsSync } = require('fs'); |
| 6 | +const { platform } = require('os'); |
| 7 | + |
| 8 | +function shouldUseYarn() { |
| 9 | + try { |
| 10 | + execSync('yarnpkg --version', { stdio: 'ignore' }); |
| 11 | + return true; |
| 12 | + } catch (e) { |
| 13 | + return false; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function shouldUseNpmConcurrently() { |
| 18 | + try { |
| 19 | + const versionString = execSync('npm --version'); |
| 20 | + const m = /^(\d+)[.]/.exec(versionString); |
| 21 | + // NPM >= 5 support concurrent installs |
| 22 | + return Number(m[1]) >= 5; |
| 23 | + } catch (e) { |
| 24 | + return false; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const yarn = shouldUseYarn(); |
| 29 | +const windows = platform() === 'win32'; |
| 30 | +const lerna = resolve( |
| 31 | + __dirname, |
| 32 | + 'node_modules', |
| 33 | + '.bin', |
| 34 | + windows ? 'lerna.cmd' : 'lerna' |
| 35 | +); |
| 36 | + |
| 37 | +if (!existsSync(lerna)) { |
| 38 | + if (yarn) { |
| 39 | + console.log('Cannot find lerna. Please run `yarn --check-files`.'); |
| 40 | + } else { |
| 41 | + console.log( |
| 42 | + 'Cannot find lerna. Please remove `node_modules` and run `npm install`.' |
| 43 | + ); |
| 44 | + } |
| 45 | + process.exit(1); |
| 46 | +} |
| 47 | + |
| 48 | +let child; |
| 49 | +if (yarn) { |
| 50 | + // Yarn does not support concurrency |
| 51 | + child = spawn(lerna, ['bootstrap', '--npm-client=yarn', '--concurrency=1'], { |
| 52 | + stdio: 'inherit', |
| 53 | + }); |
| 54 | +} else { |
| 55 | + let args = ['bootstrap']; |
| 56 | + if ( |
| 57 | + // The Windows filesystem does not handle concurrency well |
| 58 | + windows || |
| 59 | + // Only newer npm versions support concurrency |
| 60 | + !shouldUseNpmConcurrently() |
| 61 | + ) { |
| 62 | + args.push('--concurrency=1'); |
| 63 | + } |
| 64 | + child = spawn(lerna, args, { stdio: 'inherit' }); |
| 65 | +} |
| 66 | + |
| 67 | +child.on('close', code => process.exit(code)); |
0 commit comments