-
Notifications
You must be signed in to change notification settings - Fork 28
/
install-spawn.js
90 lines (77 loc) · 2.64 KB
/
install-spawn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (C) 2015-2016 IBM Corporation and Others. All Rights Reserved.
// Install by using spawn
const path = require('path')
const fs = require('fs')
const spawnSync = require('child_process').spawnSync
const isglobal = process.env.npm_config_global === 'true'
const npmrc = '.npmrc'
if (!process.env.INIT_CWD) {
throw Error('INIT_CWD was not set- does not seem like we were launched from npm')
}
const npmrcPath = path.resolve(process.env.INIT_CWD, npmrc)
// uses semver regex from https://semver.org/
const YARN_REGEX = /yarn(-(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)?((.*cli)?\.c?js)?$/
module.exports = function npmInstallNpm (fullIcu, advice) {
const icupkg = fullIcu.icupkg
const icudat = fullIcu.icudat
let cmdPath = process.env.npm_node_execpath
const npmPath = process.env.npm_execpath
let args
if (YARN_REGEX.test(npmPath)) {
console.log('Looks like you are using yarn…')
args = [npmPath, 'add', icupkg, '--no-lockfile', '--ignore-scripts']
} else if (npmPath) {
args = [npmPath, 'install', icupkg]
} else {
// attempt to launch npm.
// do not try yarn here
console.log('(Hmm… doesn’t look like NPM called us. Let’s try calling NPM.)')
cmdPath = 'npm'
args = ['install', icupkg]
}
if (fs.existsSync(npmrcPath)) {
try {
fs.linkSync(npmrcPath, npmrc)
} catch (e) {
fs.symlinkSync(npmrcPath, npmrc)
}
}
console.log('full-icu$', cmdPath, args.join(' '))
const spawned = spawnSync(cmdPath, args, { stdio: 'inherit' })
if (fs.existsSync(npmrc)) {
try {
fs.unlinkSync(npmrc)
} catch (e) {
}
}
if (spawned.error) {
throw (spawned.error)
} else if (spawned.status !== 0) {
throw (Error(cmdPath + ' ' + args.join(' ') + ' --> status ' + spawned.status))
} else {
let datPath
if (isglobal) {
datPath = path.join('..', 'icu4c-data', icudat)
} else {
datPath = path.join('node_modules', 'icu4c-data', icudat)
}
if (fs.existsSync(icudat)) {
console.log(' √ ' + icudat + ' (existing link?)')
} else if (!fs.existsSync(datPath)) {
console.log(' • ' + ' (no ' + icudat + ' at ‘' + datPath + '’)')
} else {
try {
fs.linkSync(datPath, icudat)
console.log(' √ ' + icudat + ' (link)')
} catch (e) {
fs.symlinkSync(datPath, icudat)
console.log(' √ ' + icudat + ' (symlink)')
}
}
if (!fullIcu.haveDat()) {
throw Error('Somehow failed to install ' + icudat)
} else {
advice()
}
}
}