-
Notifications
You must be signed in to change notification settings - Fork 148
/
install.js
162 lines (145 loc) · 4.45 KB
/
install.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
import { spawnSync } from 'child_process';
import path from 'path';
import stripAnsi from 'strip-ansi';
import semverLt from 'semver/functions/lt.js';
import { createOptions } from '../create-options.js';
import { spawn } from '../spawn.js';
import { timeout } from '../timeout.js';
const envSeparator = process.platform === 'win32' ? ';' : ':';
function getVersion(packageManager, context) {
const options = createOptions(
path.join(context.path, context.module.name),
context
);
let packageManagerBin = context.npmPath;
if (packageManager === 'yarn') {
packageManagerBin = context.yarnPath;
} else if (packageManager === 'pnpm') {
packageManagerBin = context.pnpmPath;
}
const binDirectory = path.dirname(packageManagerBin);
options.env.PATH = `${binDirectory}${envSeparator}${process.env.PATH}`;
options.encoding = 'utf8';
const proc = spawnSync(packageManagerBin, ['--version'], options);
if (proc.status !== 0) {
context.emit(
'data',
'warn',
`${context.module.name} ${packageManager}:`,
`Unable to determine ${packageManager} version:\n${proc.stdout}` +
`\n${proc.stderr}`
);
return undefined;
}
context.emit(
'data',
'verbose',
`${context.module.name} ${packageManager}:`,
`${packageManager} version ${proc.stdout}`
);
return proc.stdout;
}
export default function install(packageManager, context) {
return new Promise((resolve, reject) => {
const options = createOptions(
path.join(context.path, context.module.name),
context
);
let args = ['install'];
context.emit(
'data',
'info',
`${context.module.name} ${packageManager}:`,
`${packageManager} install started`
);
context.emit(
'data',
'verbose',
`${context.module.name} ${packageManager}:`,
`Using temp directory: "${options.env['npm_config_tmp']}"`
);
if (packageManager === 'npm') {
args.push('--no-audit');
args.push('--no-fund');
} else if (packageManager === 'yarn') {
// Tell yarn versions earlier than v2 to ignore the "engines" field in
// `package.json` (if present) to allow installation with unreleased
// versions of Node.js.
const version = getVersion(packageManager, context);
if (version && semverLt(version, '2.0.0', { includePrerelease: true })) {
options.env['YARN_IGNORE_ENGINES'] = 'true';
}
} else if (packageManager === 'pnpm') {
// No pnpm-specific options yet
}
if (context.module.install) {
args = context.module.install;
}
let packageManagerBin = context.npmPath;
if (packageManager === 'yarn') {
packageManagerBin = context.yarnPath;
} else if (packageManager === 'pnpm') {
packageManagerBin = context.pnpmPath;
}
const binDirectory = path.dirname(packageManagerBin);
options.env.PATH = `${binDirectory}${envSeparator}${process.env.PATH}`;
const proc = spawn(packageManagerBin, args, options);
const finish = timeout(
packageManager,
context,
proc,
(err) => {
if (err) {
if (context.testError.length === 0) {
// Because pnpm prints errors to stdout
context.testError = context.testOutput;
}
return reject(err);
}
resolve();
},
'Install'
);
proc.stderr.on('data', (chunk) => {
context.testError.append(chunk);
if (context.module.stripAnsi) {
chunk = stripAnsi(chunk.toString());
chunk = chunk.replace(/\r/g, '\n');
}
context.emit(
'data',
'warn',
`${context.module.name} ${packageManager}-install:`,
chunk.toString()
);
});
proc.stdout.on('data', (chunk) => {
context.testOutput.append(chunk);
if (context.module.stripAnsi) {
chunk = stripAnsi(chunk.toString());
chunk = chunk.replace(/\r/g, '\n');
}
context.emit(
'data',
'verbose',
`${context.module.name} ${packageManager}-install:`,
chunk.toString()
);
});
proc.on('error', () => {
return finish(new Error('Install Failed'));
});
proc.on('close', (code) => {
if (code > 0) {
return finish(Error('Install Failed'));
}
context.emit(
'data',
'info',
`${context.module.name} ${packageManager}:`,
`${packageManager} install successfully completed`
);
return finish(null, context);
});
});
}