forked from microsoft/vscode-react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
613 lines (556 loc) · 19.5 KB
/
gulpfile.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
const gulp = require("gulp");
const log = require("fancy-log");
const istanbul = require("gulp-istanbul");
const isparta = require("isparta");
const sourcemaps = require("gulp-sourcemaps");
const path = require("path");
const preprocess = require("gulp-preprocess");
const ts = require("gulp-typescript");
const mocha = require("gulp-mocha");
const GulpExtras = require("./tools/gulp-extras");
const minimist = require("minimist");
const os = require("os");
const fs = require("fs");
const es = require("event-stream");
const remapIstanbul = require("remap-istanbul/lib/gulpRemapIstanbul");
const nls = require("vscode-nls-dev");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const filter = require("gulp-filter");
const del = require("del");
const vscodeTest = require("vscode-test");
const cp = require("child_process");
const copyright = GulpExtras.checkCopyright;
const imports = GulpExtras.checkImports;
const executeCommand = GulpExtras.executeCommand;
const tsProject = ts.createProject("tsconfig.json");
/**
* Whether we're running a nightly build.
*/
const isNightly = process.argv.includes("--nightly");
const fullExtensionName = isNightly
? "msjsdiag.vscode-react-native-preview"
: "msjsdiag.vscode-react-native";
const extensionName = isNightly ? "vscode-react-native-preview" : "vscode-react-native";
const translationProjectName = "vscode-extensions";
const defaultLanguages = [
{ id: "zh-tw", folderName: "cht", transifexId: "zh-hant" },
{ id: "zh-cn", folderName: "chs", transifexId: "zh-hans" },
{ id: "ja", folderName: "jpn" },
{ id: "ko", folderName: "kor" },
{ id: "de", folderName: "deu" },
{ id: "fr", folderName: "fra" },
{ id: "es", folderName: "esn" },
{ id: "ru", folderName: "rus" },
{ id: "it", folderName: "ita" },
// These language-pack languages are included for VS but excluded from the vscode package
{ id: "cs", folderName: "csy" },
{ id: "tr", folderName: "trk" },
{ id: "pt-br", folderName: "ptb", transifexId: "pt-BR" },
{ id: "pl", folderName: "plk" },
];
const srcPath = "src";
const testPath = "test";
const buildDir = "src";
const distDir = "dist";
const distSrcDir = `${distDir}/src`;
const sources = [srcPath, testPath].map(tsFolder => tsFolder + "/**/*.ts");
const knownOptions = {
string: "env",
default: { env: "production" },
};
const options = minimist(process.argv.slice(2), knownOptions);
let lintSources = [srcPath, testPath].map(tsFolder => tsFolder + "/**/*.ts");
lintSources = lintSources.concat([
"!src/typings/**",
"!test/resources/sampleReactNative022Project/**",
"!test/smoke/**",
"!/SmokeTestLogs/**",
]);
async function runWebpack({
packages = [],
devtool = false,
compileInPlace = false,
mode = process.argv.includes("watch") ? "development" : "production",
} = options) {
let configs = [];
for (const { entry, library, filename } of packages) {
const config = {
mode,
target: "node",
entry: path.resolve(entry),
output: {
path: compileInPlace ? path.resolve(path.dirname(entry)) : path.resolve(distDir),
filename: filename || path.basename(entry).replace(".js", ".bundle.js"),
devtoolModuleFilenameTemplate: "../[resource-path]",
},
devtool: devtool,
resolve: {
extensions: [".js", ".ts", ".json"],
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
// vscode-nls-dev loader:
// * rewrite nls-calls
loader: "vscode-nls-dev/lib/webpack-loader",
options: {
base: path.join(__dirname),
},
},
{
// configure TypeScript loader:
// * enable sources maps for end-to-end source maps
loader: "ts-loader",
options: {
compilerOptions: {
sourceMap: true,
},
},
},
],
},
],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: /^\**!|@preserve/i,
},
},
extractComments: false,
}),
],
},
node: {
__dirname: false,
__filename: false,
},
externals: {
vscode: "commonjs vscode",
},
};
if (library) {
config.output.libraryTarget = "commonjs2";
}
if (process.argv.includes("--analyze-size")) {
config.plugins = [
new (require("webpack-bundle-analyzer").BundleAnalyzerPlugin)({
analyzerMode: "static",
reportFilename: path.resolve(distSrcDir, path.basename(entry) + ".html"),
}),
];
}
configs.push(config);
}
await new Promise((resolve, reject) =>
webpack(configs, (err, stats) => {
if (err) {
reject(err);
} else if (stats.hasErrors()) {
reject(stats);
} else {
resolve();
}
}),
);
}
// Generates ./dist/nls.bundle.<language_id>.json from files in ./i18n/** *//<src_path>/<filename>.i18n.json
// Localized strings are read from these files at runtime.
const generateSrcLocBundle = () => {
// Transpile the TS to JS, and let vscode-nls-dev scan the files for calls to localize.
return tsProject
.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.js.pipe(nls.createMetaDataFiles())
.pipe(nls.createAdditionalLanguageFiles(defaultLanguages, "i18n"))
.pipe(nls.bundleMetaDataFiles(fullExtensionName, "dist"))
.pipe(nls.bundleLanguageFiles())
.pipe(
filter([
"**/nls.bundle.*.json",
"**/nls.metadata.header.json",
"**/nls.metadata.json",
"!src/**",
]),
)
.pipe(gulp.dest("dist"));
};
function build(failOnError, buildNls) {
const isProd = options.env === "production";
const preprocessorContext = isProd ? { PROD: true } : { DEBUG: true };
let gotError = false;
log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`);
const tsResult = tsProject
.src()
.pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line
.pipe(sourcemaps.init())
.pipe(tsProject());
return tsResult.js
.pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through())
.pipe(
buildNls
? nls.createAdditionalLanguageFiles(defaultLanguages, "i18n", ".")
: es.through(),
)
.pipe(buildNls ? nls.bundleMetaDataFiles(fullExtensionName, ".") : es.through())
.pipe(buildNls ? nls.bundleLanguageFiles() : es.through())
.pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "." }))
.pipe(gulp.dest(file => file.cwd))
.once("error", () => {
gotError = true;
})
.once("finish", () => {
if (failOnError && gotError) {
process.exit(1);
}
});
}
async function test() {
// Check if arguments were passed
if (options.pattern) {
log(`\nTesting cases that match pattern: ${options.pattern}`);
} else {
log(`\nTesting cases that don't match pattern: extensionContext|localizationContext`);
}
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = __dirname;
// The path to the extension test runner script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "test", "index");
console.log(extensionTestsPath);
// Download VS Code, unzip it and run the integration test
await vscodeTest.runTests({
extensionDevelopmentPath,
extensionTestsPath,
});
} catch (err) {
console.error(err);
console.error("Failed to run tests");
process.exit(1);
}
}
gulp.task("check-imports", () => {
const tsProject = ts.createProject("tsconfig.json");
return tsProject.src().pipe(imports());
});
gulp.task("check-copyright", () => {
return gulp
.src([
"**/*.ts",
"**/*.js",
"!**/*.d.ts",
"!coverage/**",
"!node_modules/**",
"!test/**/*.js",
"!SampleApplication/**",
"!test/resources/sampleReactNative022Project/**/*.js",
"!test/smoke/package/node_modules/**",
"!test/smoke/automation/node_modules/**",
"!test/smoke/resources/**",
"!test/smoke/vscode/**",
"!dist/**",
])
.pipe(copyright());
});
const runPrettier = (onlyStaged, fix, callback) => {
const child = cp.fork(
"./node_modules/@mixer/parallel-prettier/dist/index.js",
[
fix ? "--write" : "--list-different",
"src/**/*.ts",
"test/**/*.ts",
"gulpfile.js",
"*.md",
"!CHANGELOG.md",
"!test/smoke/**",
"!src/**/*.d.ts",
],
{
stdio: "inherit",
},
);
child.on("exit", code => (code ? callback(`Prettier exited with code ${code}`) : callback()));
};
const runEslint = (fix, callback) => {
let args = ["--color", "src/**/*.ts"];
if (fix) {
args.push("--fix");
}
const child = cp.fork("./node_modules/eslint/bin/eslint.js", args, {
stdio: "inherit",
cwd: __dirname,
});
child.on("exit", code => (code ? callback(`Eslint exited with code ${code}`) : callback()));
};
gulp.task("format:prettier", callback => runPrettier(false, true, callback));
gulp.task("format:eslint", callback => runEslint(true, callback));
gulp.task("format", gulp.series("format:prettier", "format:eslint"));
gulp.task("lint:prettier", callback => runPrettier(false, false, callback));
gulp.task("lint:eslint", callback => runEslint(false, callback));
gulp.task("lint", gulp.parallel("lint:prettier", "lint:eslint"));
/** Run webpack to bundle the extension output files */
gulp.task("webpack-bundle", async () => {
const packages = [
{
entry: `${buildDir}/extension/rn-extension.ts`,
filename: "rn-extension.js",
library: true,
},
];
return runWebpack({ packages });
});
gulp.task("clean", () => {
const pathsToDelete = [
"src/**/*.js",
"src/**/*.js.map",
"test/**/*.js",
"test/**/*.js.map",
"out/",
"dist",
"!test/resources/sampleReactNative022Project/**/*.js",
".vscode-test/",
"nls.*.json",
"!test/smoke/**/*",
];
return del(pathsToDelete, { force: true });
});
// TODO: The file property should point to the generated source (this implementation adds an extra folder to the path)
// We should also make sure that we always generate urls in all the path properties (We shouldn"t have \\s. This seems to
// be an issue on Windows platforms)
gulp.task(
"build",
gulp.series("check-imports", "lint", function runBuild(done) {
build(true, true).once("finish", () => {
done();
});
}),
);
gulp.task(
"build-dev",
gulp.series("check-imports", "lint", function runBuild(done) {
build(false, false).once("finish", () => {
done();
});
}),
);
gulp.task("quick-build", gulp.series("build-dev"));
gulp.task(
"watch",
gulp.series("build", function runWatch() {
log("Watching build sources...");
return gulp.watch(sources, gulp.series("build"));
}),
);
gulp.task("prod-build", gulp.series("clean", "webpack-bundle", generateSrcLocBundle));
gulp.task("default", gulp.series("prod-build"));
gulp.task("test", gulp.series("build", "lint", test));
gulp.task("coverage:instrument", () => {
return (
gulp
.src(["src/**/*.js", "!test/**"])
.pipe(
istanbul({
// Use the isparta instrumenter (code coverage for ES6)
instrumenter: isparta.Instrumenter,
includeUntested: true,
}),
)
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
);
});
gulp.task("coverage:report", () => {
return gulp.src(["src/**/*.js", "!test/**"], { read: false }).pipe(
istanbul.writeReports({
reporters: ["json", "text-summary"],
}),
);
});
gulp.task("coverage:remap", () => {
return gulp.src("coverage/coverage-final.json").pipe(
remapIstanbul({
reports: {
json: "coverage/coverage.json",
html: "coverage/html-report",
},
}),
);
});
gulp.task("test-no-build", test);
gulp.task(
"test:coverage",
gulp.series(
"quick-build",
"coverage:instrument",
"test-no-build",
"coverage:report",
"coverage:remap",
),
);
gulp.task(
"watch-build-test",
gulp.series("build", "test", function runWatch() {
return gulp.watch(sources, gulp.series("build", "test"));
}),
);
gulp.task("package", callback => {
const command = path.join(__dirname, "node_modules", ".bin", "vsce");
const args = ["package"];
executeCommand(command, args, callback);
});
function readJson(file) {
const contents = fs.readFileSync(path.join(__dirname, file), "utf-8").toString();
return JSON.parse(contents);
}
function writeJson(file, jsonObj) {
const content = JSON.stringify(jsonObj, null, 2);
fs.writeFileSync(path.join(__dirname, file), content);
}
/**
* Generate version number for a nightly build.
*/
const getVersionNumber = () => {
const date = new Date(new Date().toLocaleString("en-US", { timeZone: "America/Los_Angeles" }));
return [
// YY
date.getFullYear(),
// MM,
date.getMonth() + 1,
//DDHH
`${date.getDate()}${String(date.getHours()).padStart(2, "0")}`,
].join(".");
};
gulp.task("release", function prepareLicenses() {
const backupFiles = [
"LICENSE.txt",
"ThirdPartyNotices.txt",
"package.json",
"package-lock.json",
];
const backupFolder = path.resolve(path.join(os.tmpdir(), "vscode-react-native"));
if (!fs.existsSync(backupFolder)) {
fs.mkdirSync(backupFolder);
}
return Promise.resolve()
.then(() => {
/* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */
log("Backing up license files to " + backupFolder + "...");
backupFiles.forEach(fileName => {
fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName));
});
/* copy over the release package license files */
log("Preparing license files for release...");
fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt"));
fs.writeFileSync(
"ThirdPartyNotices.txt",
fs.readFileSync("release/ThirdPartyNotices.txt"),
);
})
.then(() => {
let packageJson = readJson("package.json");
packageJson.main = "./dist/rn-extension";
if (isNightly) {
log("Performing nightly release...");
packageJson.version = getVersionNumber();
packageJson.name = extensionName;
packageJson.preview = true;
packageJson.displayName += " (Preview)";
}
writeJson("package.json", packageJson);
log("Creating release package...");
return new Promise((resolve, reject) => {
// NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies.
executeCommand(
"vsce",
["package"],
arg => {
if (arg) {
reject(arg);
}
resolve();
},
{ cwd: path.resolve(__dirname) },
);
});
})
.finally(() => {
/* restore backed up files */
log("Restoring modified files...");
backupFiles.forEach(fileName => {
fs.writeFileSync(
path.join(__dirname, fileName),
fs.readFileSync(path.join(backupFolder, fileName)),
);
});
});
});
// Creates package.i18n.json files for all languages from {workspaceRoot}/i18n folder into project root
gulp.task("add-i18n", () => {
return gulp
.src(["package.nls.json"])
.pipe(nls.createAdditionalLanguageFiles(defaultLanguages, "i18n"))
.pipe(gulp.dest("."));
});
// Creates MLCP readable .xliff file and saves it locally
gulp.task(
"translations-export",
gulp.series("build", function runTranslationExport() {
return gulp
.src(["package.nls.json", "nls.metadata.header.json", "nls.metadata.json"])
.pipe(nls.createXlfFiles(translationProjectName, fullExtensionName))
.pipe(gulp.dest(path.join("..", `${translationProjectName}-localization-export`)));
}),
);
// Imports localization from raw localized MLCP strings to VS Code .i18n.json files
gulp.task(
"translations-import",
gulp.series(done => {
var options = minimist(process.argv.slice(2), {
string: "location",
default: {
location: "../vscode-translations-import",
},
});
es.merge(
defaultLanguages.map(language => {
let id = language.transifexId || language.id;
log(
path.join(
options.location,
id,
"vscode-extensions",
`${fullExtensionName}.xlf`,
),
);
return gulp
.src(
path.join(
options.location,
id,
"vscode-extensions",
`${fullExtensionName}.xlf`,
),
)
.pipe(nls.prepareJsonFiles())
.pipe(gulp.dest(path.join("./i18n", language.folderName)));
}),
).pipe(
es.wait(() => {
done();
}),
);
}, "add-i18n"),
);