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