forked from stoplightio/vscode-spectral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
286 lines (230 loc) · 8.76 KB
/
make.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* global mkdir, pushd, popd, rm, target, cp */
'use strict';
require('shelljs/make');
const fs = require('fs').promises;
const path = require('path');
const ncp = require('child_process');
const jsonpath = require('jsonpath');
const semver = require('semver');
const packageJsonPath = path.join(__dirname, 'package.json');
/** Build output paths. */
const outputPath = {
artifacts: path.join(__dirname, 'artifacts'),
dist: path.join(__dirname, '.dist'),
log: path.join(__dirname, 'artifacts', 'log'),
};
target.allDev = async () => {
banner('Target: AllDev');
const backupPath = await patchPackageJsonUpdateVersion();
try {
await target.package();
} finally {
await revertToOriginalPackageJson(backupPath);
}
e2e();
};
target.package = async () => {
banner('Target: Package');
await buildAll();
pushd(outputPath.dist);
run(`yarn vsce package -o ${outputPath.artifacts}`);
popd();
};
target.publish = async (args) => {
if (!args || !args.length || args.length !== 1) {
console.log('node make.js publish -- yourpublishtoken');
return;
}
const token = args[0].trim();
const vsixFiles = (await fs.readdir(outputPath.artifacts, { withFileTypes: true }))
.filter((f) => !f.isDirectory())
.filter((f) => f.name.endsWith('.vsix'));
if (!vsixFiles || !vsixFiles.length) {
console.log(`No .vsix found in the '${outputPath.artifacts}' folder.`);
return;
}
if (vsixFiles.length > 1) {
console.log(`More than one .vsix found in the '${outputPath.artifacts}' folder.`);
return;
}
// https://code.visualstudio.com/api/working-with-extensions/publishing-extension
run(`yarn vsce publish --packagePath ${path.join(outputPath.artifacts, vsixFiles[0].name)} -p ${token}`);
};
const buildAll = async () => {
banner('Target: BuildAll');
clean();
compile();
test();
await prepublish();
};
const clean = () => {
banner('Target: Clean');
run('yarn clean');
};
const compile = () => {
banner('Target: Compile');
run('yarn');
run('yarn lint');
run('yarn compile');
};
const prepublish = async () => {
banner('Target: PrePublish');
try {
await fs.access(outputPath.artifacts);
} catch (err) {
mkdir(outputPath.artifacts);
}
run(`node node_modules/webpack-cli/bin/cli.js --mode production --config ./client/webpack.config.js`);
await preparePackageStructure();
await generateServerPackagingReports();
};
const e2e = () => {
banner('Target: e2e tests');
run(`yarn test:e2e`);
};
const test = () => {
banner('Target: Test');
run('yarn test');
};
/**
* Writes a visible banner with top/bottom bars for build logging.
* @param {string} message - The message to display.
*/
function banner(message) {
console.log();
console.log('------------------------------------------------------------');
console.log(message);
console.log('------------------------------------------------------------');
}
/**
* Executes a command in a child process.
* @param {string} cl - The command line to execute.
* @param {boolean} capture - True to capture and return the output; false to simply echo to console.
* @return {string|undefined} The output from the command.
*/
function run(cl, capture = false) {
console.log();
console.log('> ' + cl);
let output;
try {
// Exec needs to be synchronous or tasks continue on without waiting for the
// external process to finish. It gets weird.
const options = {
stdio: capture ? 'pipe' : 'inherit',
};
output = ncp.execSync(cl, options);
} catch (err) {
console.error(err.output ? err.output.toString() : err.message);
throw err;
}
return (output || '').toString().trim();
}
/**
* Generates reports to help 'manually webpack' the server plugin.
*/
async function generateServerPackagingReports() {
// Deletes the .mjs file from decimal.js because because Webpack messes up the VSIX when it's present. Deleting it fixes the issue
// This seems related: https://github.com/MikeMcl/decimal.js/issues/59"
run('yarn rimraf server/node_modules/decimal.js/decimal.mjs');
console.log('Using webpack to identify the list of modules used by the server.');
run(`node node_modules/webpack-cli/bin/cli.js --config ./server/webpack.config.js --profile --json > ${outputPath.artifacts}/server-modules.json`);
console.log('Extracting the list of modules used by the server.');
const serverModules = require(`${outputPath.artifacts}/server-modules.json`);
const names = jsonpath
.query(serverModules, '$..name')
.filter((v) => v.indexOf('node_modules') >= 0)
.map((v) => v.replace(/.*node_modules\/([^/]+).*/, '$1'))
.filter((v, i, a) => a.indexOf(v) === i) // unique items
.sort();
await fs.writeFile(path.join(outputPath.artifacts, 'server-module-names.txt'), names.join('\n'));
console.log('Generating potential .vscodeignore items for server.');
const potentialIgnores = [];
const serverNodeModules = await fs.readdir(path.join(__dirname, 'server', 'node_modules'), { withFileTypes: true });
serverNodeModules.filter((v) => v.isDirectory())
.map((v) => v.name)
.filter((v) => names.indexOf(v) === -1)
.forEach((v) => {
potentialIgnores.push(`server/node_modules/${v}/`);
});
await fs.writeFile(path.join(outputPath.artifacts, 'server-vscodeignore.txt'), potentialIgnores.join('\n'));
await fs.appendFile(path.join(outputPath.dist, '.vscodeignore'), potentialIgnores.join('\n'));
}
/**
* Temporarily patches the package.json version
* in order to make local integration testing easier
*/
async function patchPackageJsonUpdateVersion() {
const now = Math.floor(Date.now() / 1000);
const backupPath = `${packageJsonPath}.${now}`;
console.log(`Backing up "package.json" to "${backupPath}"`);
cp(packageJsonPath, backupPath);
const packageJsonContent = await fs.readFile(packageJsonPath, { encoding: 'utf8' });
const jsonPackage = JSON.parse(packageJsonContent);
const current = jsonPackage.version;
const version = semver.parse(current);
version.patch = now;
version.inc('prerelease', 'dev');
const next = version.toString();
console.log(`Temporarily patching "package.json" version from "${current}" to "${next}"`);
jsonPackage.version = next;
await fs.writeFile(packageJsonPath, JSON.stringify(jsonPackage, undefined, 2));
return backupPath;
}
/**
* Reverts patched package.json file
* @param {string} backupPath - The path to the original "package.json" file.
*/
async function revertToOriginalPackageJson(backupPath) {
console.log('Restoring original "package.json" file');
cp(backupPath, packageJsonPath);
console.log(`Removing temporary backup "${backupPath}" file`);
rm(backupPath);
}
/**
* Remove the workspaces property from the top package.json file
* so that /server ends up with a sub 'node_modules' folder on install
* @param {string} path - The path to the root "package.json" file.
*/
async function patchPackageJsonRemoveWorkspaces(path) {
console.log(`Removing workspaces from "${path}"`);
const packageJsonContent = await fs.readFile(path, { encoding: 'utf8' });
const jsonPackage = JSON.parse(packageJsonContent);
delete jsonPackage.workspaces;
delete jsonPackage.private; // required to exist and to equal "false" for workspaces
await fs.writeFile(path, JSON.stringify(jsonPackage, undefined, 2));
}
/**
* Prepare package structure
*/
async function preparePackageStructure() {
console.log(`Generating package tree structure in "${outputPath.dist}"`);
mkdir(outputPath.dist);
cp(path.join(__dirname, 'tools', '.vscodeignore'), outputPath.dist);
cp(path.join(__dirname, 'package.json'), outputPath.dist);
cp(path.join(__dirname, 'yarn.lock'), outputPath.dist);
const distPackage = path.join(outputPath.dist, 'package.json');
await patchPackageJsonRemoveWorkspaces(distPackage);
pushd(outputPath.dist);
run('yarn install');
popd();
const distClient = path.join(outputPath.dist, 'client');
mkdir(distClient);
const distServer = path.join(outputPath.dist, 'server');
mkdir(distServer);
cp(path.join(__dirname, 'README.md'), outputPath.dist);
cp(path.join(__dirname, 'CHANGELOG.md'), outputPath.dist);
cp(path.join(__dirname, 'LICENSE.txt'), outputPath.dist);
cp(path.join(__dirname, 'icon.png'), outputPath.dist);
cp(path.join(__dirname, 'client', 'wbpkd', 'index.js'), distClient);
cp(path.join(__dirname, 'server', 'out', '*.js'), distServer);
cp(path.join(__dirname, 'server', 'package.json'), distServer);
cp(path.join(__dirname, 'yarn.lock'), distServer);
// Install the server node_modules, filtering as much as possible all the useless cruft
cp(path.join(__dirname, 'tools', '.yarnclean'), distServer);
pushd(distServer);
run('yarn --offline --production');
rm(path.join(distServer, '.yarnclean'));
rm(path.join(distServer, 'node_modules', '.yarn-integrity'));
rm(path.join(distServer, 'yarn.lock'));
popd();
}