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