-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
231 lines (208 loc) · 5.68 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict'
const path = require('path')
const { execFile } = require('child_process')
const fs = require('fs-extra')
const nv = require('@pkgjs/nv')
const semver = require('semver')
// @TODO get things merged to nvm
// const NVM_INSTALL_SRC = 'https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh'
const NVM_INSTALL_SRC = 'https://raw.githubusercontent.com/wesleytodd/nvm/wip/install.sh'
const NVM_SRC = 'https://github.com/wesleytodd/nvm.git'
module.exports.NVM = class NVM {
constructor (opts = {}) {
this._installed = false
this.env = env(opts.env)
this.nvmSrc = opts.nvmSrc || NVM_SRC
this.nvmInstallSrc = opts.nvmInstallSrc || NVM_INSTALL_SRC
this.nvmDirectory = opts.nvmDirectory || path.join(__dirname, '.nvm')
this.nvmBin = opts.nvmBin || path.join(__dirname, 'bin', 'nvm')
this.nvmInstallBin = opts.nvmInstallBin || path.join(__dirname, 'bin', 'install')
this.nvmSearchDirectories = opts.nvmSearchDirectories || [
opts.nvmDirectory,
process.env.NVM_DIR,
this.nvmDirectory
].filter((v) => typeof v !== 'undefined')
}
async findExistingNvm () {
// Find existing nvm installs
const found = await this.nvmSearchDirectories.reduce(async (p, dir) => {
// Already found or dir is falsy
const found = await p
if (found || !dir) {
return found
}
// Try to get the installed nvm version
const ver = await this.version({
env: {
NVM_DIR: dir
}
})
if (ver) {
return [dir, ver]
}
}, Promise.resolve(false))
if (found) {
this._installed = Promise.resolve(found)
}
return found || [null, null]
}
installNvm () {
if (this._installed) {
return this._installed
}
this._installed = new Promise((resolve, reject) => {
const dir = this.nvmDirectory
fs.ensureDir(dir).then(() => {
execFile(this.nvmInstallBin, [], {
shell: '/bin/bash',
env: {
...this.env,
NVM_DIR: dir,
// Change the install script source
NVM_SOURCE: this.nvmSrc,
// @TODO https://github.com/nvm-sh/nvm/pull/2132
PROFILE: '/dev/null',
// @TODO https://github.com/nvm-sh/nvm/pull/2132
NVM_NOUSE: 'true'
}
}, async (err, stdout, stderr) => {
if (err) {
return reject(err)
}
const ver = await this.version({
env: {
NVM_DIR: dir
}
})
if (!ver) {
return reject(new Error(`failed to install: ${dir}`))
}
resolve([dir, ver])
})
}, reject)
})
return this._installed
}
nvm (args = [], opts = {}) {
return run(this, args, opts)
}
async install (version, opts = {}) {
const v = (await nv(version)).pop()
const args = ['install', (v && v.version) || version]
if (opts.noUse === true) {
args.push('--no-use')
delete opts.noUse
}
const o = await run(this, args, opts)
// Parse out versions
let nodeVer
let npmVer
const mo = o.stdout.match(/Now using node (v[0-9]+\.[0-9]+\.[0-9]+) \(npm (v[0-9]+\.[0-9]+\.[0-9]+)\)/)
if (mo) {
nodeVer = semver.parse(mo[1])
npmVer = semver.parse(mo[2])
} else {
const me = o.stderr.match(/(v[0-9]+\.[0-9]+\.[0-9]+) is already installed./)
if (!me) {
throw Object.assign(new Error('Unable to parse nvm output'), o)
}
nodeVer = semver.parse(me[1])
}
// Bin path
const bin = await this.which(nodeVer.version)
return Object.assign(o, {
node: {
path: bin,
version: nodeVer.version,
major: nodeVer.major,
minor: nodeVer.minor,
patch: nodeVer.patch
},
npm: npmVer
})
}
async installNpm (version, opts = {}) {
// @TODO
}
async run (_args, opts = {}) {
const args = [
'run',
(opts.silent) ? '--silent' : '',
opts.node || 'current',
..._args
]
return run(this, args, opts)
}
async exec (_args, opts = {}) {
const args = [
'exec',
(opts.silent) ? '--silent' : '',
opts.node || 'current',
..._args
]
return run(this, args, opts)
}
async version (opts) {
try {
const { stdout } = await run(this, ['--version'], opts)
return stdout.trim()
} catch (e) {
/* ignore */
}
return null
}
async which (version, opts = {}) {
try {
const { stdout } = await run(this, ['which', version || 'current'], opts)
return stdout.trim()
} catch (e) {
/* ignore */
}
return null
}
async current (opts) {
try {
const { stdout } = await run(this, ['current'], opts)
return stdout.trim()
} catch (e) {
console.log(e)
/* ignore */
}
return null
}
}
function env (overrides = {}) {
const env = process.env
return Object.keys(env).reduce((e, key) => {
if (key.startsWith('npm_') || key.startsWith('NVM_') || env[key] === undefined) {
return e
}
e[key] = e[key] || env[key]
return e
}, { ...overrides })
}
function run (nvm, args = [], opts = {}) {
return new Promise((resolve, reject) => {
const cp = execFile(nvm.nvmBin, args, {
...opts,
env: {
...nvm.env,
...(opts.env || {}),
NVM_DIR: nvm.nvmDirectory
}
}, (err, stdout, stderr) => {
const o = {
error: err,
exitCode: cp.exitCode,
stdout: stdout,
stderr: stderr
}
if (err && opts.rejectOnError !== false) {
Error.captureStackTrace(err, run)
Object.assign(err, o)
return reject(err)
}
resolve(o)
})
})
}