microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1c81f4e7dc48c4e61d8c79bbce29eb416a2707e0

Branches

Tags

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

Clone

HTTPS

Download ZIP

gulpfile.js

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