microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fd2c4686c9bd6ee2309c4bd9d78a9514dc0ba2ba

Branches

Tags

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

Clone

HTTPS

Download ZIP

gulpfile.js

235lines · 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
4var gulp = require("gulp");
5var log = require("gulp-util").log;
6var istanbul = require('gulp-istanbul');
7var isparta = require('isparta');
8var sourcemaps = require("gulp-sourcemaps");
9var path = require("path");
10var preprocess = require("gulp-preprocess");
11var install = require("gulp-install");
12var runSequence = require("run-sequence");
13var ts = require("gulp-typescript");
14var mocha = require("gulp-mocha");
15var GulpExtras = require("./tools/gulp-extras");
16var minimist = require("minimist");
17var os = require("os");
18var fs = require("fs");
19var Q = require("q");
20var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
21var execSync = require('child_process').execSync;
22
23var copyright = GulpExtras.checkCopyright;
24var imports = GulpExtras.checkImports;
25var executeCommand = GulpExtras.executeCommand;
26
27
28var srcPath = "src";
29var testPath = "test";
30
31var sources = [
32 srcPath,
33 testPath,
34].map(function (tsFolder) { return tsFolder + "/**/*.ts"; });
35
36var knownOptions = {
37 string: "env",
38 default: { env: "production" }
39};
40
41var options = minimist(process.argv.slice(2), knownOptions);
42
43var tsProject = ts.createProject("tsconfig.json");
44
45// TODO: The file property should point to the generated source (this implementation adds an extra folder to the path)
46// We should also make sure that we always generate urls in all the path properties (We shouldn't have \\s. This seems to
47// be an issue on Windows platforms)
48gulp.task("build", ["check-imports", "check-copyright"], build);
49
50gulp.task("quick-build", build);
51
52function build(callback) {
53 var tsProject = ts.createProject("tsconfig.json");
54 var isProd = options.env === "production";
55 var preprocessorContext = isProd ? { PROD: true } : { DEBUG: true };
56 log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`);
57 return tsProject.src()
58 .pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line
59 .pipe(sourcemaps.init())
60 .pipe(tsProject())
61 .on("error", function (e) {
62 callback(e);
63 })
64 .pipe(sourcemaps.write(".", {
65 includeContent: false,
66 sourceRoot: "."
67 }))
68 .pipe(gulp.dest(function (file) {
69 return file.cwd;
70 }));
71}
72
73gulp.task("watch", ["build"], function (cb) {
74 log("Watching build sources...");
75 return gulp.watch(sources, ["build"]);
76});
77
78gulp.task("default", function (callback) {
79 runSequence("clean", "build", "tslint", callback);
80});
81
82var lintSources = [
83 srcPath,
84 testPath
85].map(function (tsFolder) { return tsFolder + "/**/*.ts"; });
86lintSources = lintSources.concat([
87 "!src/typings/**",
88 "!test/resources/sampleReactNative022Project/**"
89]);
90
91var libtslint = require("tslint");
92var tslint = require("gulp-tslint");
93gulp.task("tslint", function () {
94 var program = libtslint.Linter.createProgram("./tsconfig.json");
95 return gulp.src(lintSources, { base: "." })
96 .pipe(tslint({
97 formatter: "verbose",
98 program: program
99 }))
100 .pipe(tslint.report());
101});
102
103function test() {
104 // Check if arguments were passed
105 if (options.pattern) {
106 console.log("\nTesting cases that match pattern: " + options.pattern);
107 } else {
108 console.log("\nTesting cases that don't match pattern: extensionContext");
109 }
110
111 return gulp.src(["test/**/*.test.js", "!test/extension/**"])
112 .pipe(mocha({
113 ui: "tdd",
114 useColors: true,
115 invert: !options.pattern,
116 grep: options.pattern || "extensionContext"
117 }));
118}
119
120gulp.task("test", ["build", "tslint"], test);
121
122gulp.task('coverage:instrument', function () {
123 return gulp.src(["src/**/*.js", "!test/**"])
124 .pipe(istanbul({
125 // Use the isparta instrumenter (code coverage for ES6)
126 instrumenter: isparta.Instrumenter,
127 includeUntested: true
128 }))
129 // Force `require` to return covered files
130 .pipe(istanbul.hookRequire());
131});
132
133gulp.task('coverage:report', function (done) {
134 return gulp.src(
135 ["src/**/*.js", "!test/**"],
136 { read: false }
137 )
138 .pipe(istanbul.writeReports({
139 reporters: ['json', 'text-summary']
140 }));
141});
142
143gulp.task('coverage:remap', function () {
144 return gulp.src('coverage/coverage-final.json')
145 .pipe(remapIstanbul({
146 reports: {
147 'json': 'coverage/coverage.json',
148 'html': 'coverage/html-report'
149 }
150 }));
151});
152
153gulp.task("test:coverage", function (done) {
154 runSequence("quick-build", 'coverage:instrument',
155 "test-no-build", 'coverage:report', 'coverage:remap', done);
156});
157
158gulp.task("test-no-build", test);
159
160gulp.task("check-imports", function (cb) {
161 return tsProject.src()
162 .pipe(imports());
163});
164
165gulp.task("check-copyright", function (cb) {
166 return gulp.src([
167 "**/*.ts",
168 "**/*.js",
169 "!**/*.d.ts",
170 "!coverage/**",
171 "!node_modules/**",
172 "!test/**/*.js",
173 "!SampleApplication/**",
174 "!test/resources/sampleReactNative022Project/**/*.js"
175 ])
176 .pipe(copyright());
177});
178
179gulp.task("watch-build-test", ["build", "build-test"], function () {
180 return gulp.watch(sources, ["build", "build-test"]);
181});
182
183gulp.task("clean", function () {
184 var del = require("del");
185 var pathsToDelete = [
186 "src/**/*.js",
187 "src/**/*.js.map",
188 "test/**/*.js",
189 "test/**/*.js.map",
190 "out/",
191 "!test/resources/sampleReactNative022Project/**/*.js",
192 ".vscode-test/"
193 ]
194 return del(pathsToDelete, { force: true });
195});
196
197gulp.task("package", function (callback) {
198 var command = path.join(__dirname, "node_modules", ".bin", "vsce");
199 var args = ["package"];
200 executeCommand(command, args, callback);
201});
202
203gulp.task("release", ["build"], function () {
204 var licenseFiles = ["LICENSE.txt", "ThirdPartyNotices.txt"];
205 var backupFolder = path.resolve(path.join(os.tmpdir(), "vscode-react-native"));
206 if (!fs.existsSync(backupFolder)) {
207 fs.mkdirSync(backupFolder);
208 }
209
210 return Q({})
211 .then(function () {
212 /* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */
213 console.log("Backing up license files to " + backupFolder + "...");
214 licenseFiles.forEach(function (fileName) {
215 fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName));
216 });
217
218 /* copy over the release package license files */
219 console.log("Preparing license files for release...");
220 fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt"));
221 fs.writeFileSync("ThirdPartyNotices.txt", fs.readFileSync("release/ThirdPartyNotices.txt"));
222 }).then(() => {
223 console.log("Creating release package...");
224 var deferred = Q.defer();
225 // NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies.
226 executeCommand("vsce", ["package"], function (arg) { if (arg) { deferred.reject(arg); } deferred.resolve() }, { cwd: path.resolve(__dirname) });
227 return deferred.promise;
228 }).finally(function () {
229 /* restore backed up files */
230 console.log("Restoring modified files...");
231 licenseFiles.forEach(function (fileName) {
232 fs.writeFileSync(path.join(__dirname, fileName), fs.readFileSync(path.join(backupFolder, fileName)));
233 });
234 });
235});