microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.11.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

gulpfile.js

308lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4const gulp = require("gulp");
5const log = require("fancy-log");
6const istanbul = require("gulp-istanbul");
7const isparta = require("isparta");
8const sourcemaps = require("gulp-sourcemaps");
9const path = require("path");
10const preprocess = require("gulp-preprocess");
11const ts = require("gulp-typescript");
12const mocha = require("gulp-mocha");
13const GulpExtras = require("./tools/gulp-extras");
14const minimist = require("minimist");
15const os = require("os");
16const fs = require("fs");
17const Q = require("q");
18const es = require("event-stream");
19const remapIstanbul = require("remap-istanbul/lib/gulpRemapIstanbul");
20const nls = require("vscode-nls-dev");
21const libtslint = require("tslint");
22const tslint = require("gulp-tslint");
23
24const copyright = GulpExtras.checkCopyright;
25const imports = GulpExtras.checkImports;
26const executeCommand = GulpExtras.executeCommand;
27
28const translationProjectName = "vscode-extensions";
29const translationExtensionName = "vscode-react-native";
30const defaultLanguages = [
31 { id: "zh-tw", folderName: "cht", transifexId: "zh-hant" },
32 { id: "zh-cn", folderName: "chs", transifexId: "zh-hans" },
33 { id: "ja", folderName: "jpn" },
34 { id: "ko", folderName: "kor" },
35 { id: "de", folderName: "deu" },
36 { id: "fr", folderName: "fra" },
37 { id: "es", folderName: "esn" },
38 { id: "ru", folderName: "rus" },
39 { id: "it", folderName: "ita" },
40
41 // These language-pack languages are included for VS but excluded from the vscode package
42 { id: "cs", folderName: "csy" },
43 { id: "tr", folderName: "trk" },
44 { id: "pt-br", folderName: "ptb", transifexId: "pt-BR" },
45 { id: "pl", folderName: "plk" }
46];
47
48const srcPath = "src";
49const testPath = "test";
50
51const sources = [srcPath, testPath].map((tsFolder) => tsFolder + "/**/*.ts");
52
53const knownOptions = {
54 string: "env",
55 default: { env: "production" }
56};
57
58const options = minimist(process.argv.slice(2), knownOptions);
59
60let lintSources = [srcPath, testPath].map((tsFolder) => tsFolder + "/**/*.ts");
61lintSources = lintSources.concat([
62 "!src/typings/**",
63 "!test/resources/sampleReactNative022Project/**",
64 "!test/smoke/**",
65 "!/SmokeTestLogs/**"
66]);
67
68function build(failOnError, buildNls) {
69 const tsProject = ts.createProject("tsconfig.json");
70 const isProd = options.env === "production";
71 const preprocessorContext = isProd ? { PROD: true } : { DEBUG: true };
72 let gotError = false;
73 log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`);
74 const tsResult = tsProject.src()
75 .pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line
76 .pipe(sourcemaps.init())
77 .pipe(tsProject());
78
79 return tsResult.js
80 .pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through())
81 .pipe(buildNls ? nls.createAdditionalLanguageFiles(defaultLanguages, "i18n", ".") : es.through())
82 .pipe(buildNls ? nls.bundleMetaDataFiles("msjsdiag.vscode-react-native", ".") : es.through())
83 .pipe(buildNls ? nls.bundleLanguageFiles() : es.through())
84 .pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "." }))
85 .pipe(gulp.dest((file) => file.cwd))
86 .once("error", () => {
87 gotError = true;
88 })
89 .once("finish", () => {
90 if (failOnError && gotError) {
91 process.exit(1);
92 }
93 });
94}
95
96function test() {
97 // Check if arguments were passed
98 if (options.pattern) {
99 log(`\nTesting cases that match pattern: ${options.pattern}`);
100 } else {
101 log("\nTesting cases that don't match pattern: extensionContext|localizationContext");
102 }
103
104 const testResultsPath = path.join(__dirname, "test", "DebuggerTests.xml");
105 process.env.MOCHA_FILE = testResultsPath;
106 return gulp.src(["test/**/*.test.js", "!test/extension/**"])
107 .pipe(mocha({
108 ui: "tdd",
109 useColors: true,
110 invert: !options.pattern,
111 grep: options.pattern || "(extensionContext|localizationContext)",
112 reporter: "mocha-multi-reporters",
113 reporterOptions: {
114 configFile: path.resolve("test/mochaReporterConfig.json"),
115 },
116 }));
117}
118
119gulp.task("check-imports", () => {
120 const tsProject = ts.createProject("tsconfig.json");
121 return tsProject.src()
122 .pipe(imports());
123});
124
125gulp.task("check-copyright", () => {
126 return gulp.src([
127 "**/*.ts",
128 "**/*.js",
129 "!**/*.d.ts",
130 "!coverage/**",
131 "!node_modules/**",
132 "!test/**/*.js",
133 "!SampleApplication/**",
134 "!test/resources/sampleReactNative022Project/**/*.js",
135 "!test/smoke/node_modules/**",
136 "!test/smoke/resources/**"
137 ])
138 .pipe(copyright());
139});
140
141gulp.task("tslint", () => {
142 const program = libtslint.Linter.createProgram("./tsconfig.json");
143 return gulp.src(lintSources, { base: "." })
144 .pipe(tslint({
145 formatter: "verbose",
146 program: program
147 }))
148 .pipe(tslint.report());
149});
150
151// TODO: The file property should point to the generated source (this implementation adds an extra folder to the path)
152// We should also make sure that we always generate urls in all the path properties (We shouldn"t have \\s. This seems to
153// be an issue on Windows platforms)
154gulp.task("build", gulp.series("check-imports", "check-copyright", "tslint", function runBuild(done) {
155 build(true, true)
156 .once("finish", () => {
157 done();
158 });
159}));
160
161gulp.task("build-dev", gulp.series("check-imports", "check-copyright", function runBuild(done) {
162 build(false, false)
163 .once("finish", () => {
164 done();
165 });
166}));
167
168gulp.task("quick-build", gulp.series("build-dev"));
169
170gulp.task("watch", gulp.series("build", function runWatch() {
171 log("Watching build sources...");
172 return gulp.watch(sources, gulp.series("build"));
173}));
174
175gulp.task("clean", () => {
176 const del = require("del");
177 const pathsToDelete = [
178 "src/**/*.js",
179 "src/**/*.js.map",
180 "test/**/*.js",
181 "test/**/*.js.map",
182 "out/",
183 "!test/resources/sampleReactNative022Project/**/*.js",
184 ".vscode-test/",
185 "nls.*.json",
186 "!test/smoke/resources/ReactNativeSample/App.js",
187 "!test/smoke/resources/ExpoSample/App.js",
188 "!test/smoke/resources/PureRNExpoSample/App.js",
189 ]
190 return del(pathsToDelete, { force: true });
191});
192
193gulp.task("default", gulp.series("clean", "build"));
194
195gulp.task("test", gulp.series("build", "tslint", test));
196
197gulp.task("coverage:instrument", () => {
198 return gulp.src(["src/**/*.js", "!test/**"])
199 .pipe(istanbul({
200 // Use the isparta instrumenter (code coverage for ES6)
201 instrumenter: isparta.Instrumenter,
202 includeUntested: true
203 }))
204 // Force `require` to return covered files
205 .pipe(istanbul.hookRequire());
206});
207
208gulp.task("coverage:report", () => {
209 return gulp.src(
210 ["src/**/*.js", "!test/**"],
211 { read: false }
212 )
213 .pipe(istanbul.writeReports({
214 reporters: ["json", "text-summary"]
215 }));
216});
217
218gulp.task("coverage:remap", () => {
219 return gulp.src("coverage/coverage-final.json")
220 .pipe(remapIstanbul({
221 reports: {
222 "json": "coverage/coverage.json",
223 "html": "coverage/html-report"
224 }
225 }));
226});
227
228gulp.task("test-no-build", test);
229
230gulp.task("test:coverage", gulp.series("quick-build", "coverage:instrument", "test-no-build", "coverage:report", "coverage:remap"));
231
232gulp.task("watch-build-test", gulp.series("build", "test", function runWatch() {
233 return gulp.watch(sources, gulp.series("build", "test"));
234}));
235
236gulp.task("package", (callback) => {
237 const command = path.join(__dirname, "node_modules", ".bin", "vsce");
238 const args = ["package"];
239 executeCommand(command, args, callback);
240});
241
242gulp.task("release", gulp.series("build", function prepareLicenses() {
243 const licenseFiles = ["LICENSE.txt", "ThirdPartyNotices.txt"];
244 const backupFolder = path.resolve(path.join(os.tmpdir(), "vscode-react-native"));
245 if (!fs.existsSync(backupFolder)) {
246 fs.mkdirSync(backupFolder);
247 }
248
249 return Q({})
250 .then(() => {
251 /* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */
252 log("Backing up license files to " + backupFolder + "...");
253 licenseFiles.forEach((fileName) => {
254 fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName));
255 });
256
257 /* copy over the release package license files */
258 log("Preparing license files for release...");
259 fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt"));
260 fs.writeFileSync("ThirdPartyNotices.txt", fs.readFileSync("release/ThirdPartyNotices.txt"));
261 }).then(() => {
262 log("Creating release package...");
263 var deferred = Q.defer();
264 // NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies.
265 executeCommand("vsce", ["package"], (arg) => { if (arg) { deferred.reject(arg); } deferred.resolve() }, { cwd: path.resolve(__dirname) });
266 return deferred.promise;
267 }).finally(() => {
268 /* restore backed up files */
269 log("Restoring modified files...");
270 licenseFiles.forEach((fileName) => {
271 fs.writeFileSync(path.join(__dirname, fileName), fs.readFileSync(path.join(backupFolder, fileName)));
272 });
273 });
274}));
275
276// Creates package.i18n.json files for all languages from {workspaceRoot}/i18n folder into project root
277gulp.task("add-i18n", () => {
278 return gulp.src(["package.nls.json"])
279 .pipe(nls.createAdditionalLanguageFiles(defaultLanguages, "i18n"))
280 .pipe(gulp.dest("."))
281});
282
283// Creates MLCP readable .xliff file and saves it locally
284gulp.task("translations-export", gulp.series("build", function runTranslationExport() {
285 return gulp.src(["package.nls.json", "nls.metadata.header.json", "nls.metadata.json"])
286 .pipe(nls.createXlfFiles(translationProjectName, translationExtensionName))
287 .pipe(gulp.dest(path.join("..", `${translationProjectName}-localization-export`)));
288}));
289
290// Imports localization from raw localized MLCP strings to VS Code .i18n.json files
291gulp.task("translations-import", (done) => {
292 var options = minimist(process.argv.slice(2), {
293 string: "location",
294 default: {
295 location: "../vscode-translations-import"
296 }
297 });
298 es.merge(defaultLanguages.map((language) => {
299 let id = language.transifexId || language.id;
300 log(path.join(options.location, id, 'vscode-extensions', `${translationExtensionName}.xlf`));
301 return gulp.src(path.join(options.location, id, 'vscode-extensions', `${translationExtensionName}.xlf`))
302 .pipe(nls.prepareJsonFiles())
303 .pipe(gulp.dest(path.join("./i18n", language.folderName)));
304 }))
305 .pipe(es.wait(() => {
306 done();
307 }));
308});
309