microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulpfile.js
152lines · 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 sourcemaps = require('gulp-sourcemaps'); |
| 7 | var path = require('path'); |
| 8 | var preprocess = require('gulp-preprocess'); |
| 9 | var runSequence = require("run-sequence"); |
| 10 | var ts = require('gulp-typescript'); |
| 11 | var mocha = require('gulp-mocha'); |
| 12 | var GulpExtras = require("./tools/gulp-extras"); |
| 13 | var minimist = require('minimist'); |
| 14 | |
| 15 | var copyright = GulpExtras.checkCopyright; |
| 16 | var imports = GulpExtras.checkImports; |
| 17 | var executeCommand = GulpExtras.executeCommand; |
| 18 | |
| 19 | var srcPath = 'src'; |
| 20 | var outPath = 'out'; |
| 21 | |
| 22 | var sources = [ |
| 23 | srcPath, |
| 24 | ].map(function (tsFolder) { return tsFolder + '/**/*.ts'; }) |
| 25 | .concat(['test/*.ts']); |
| 26 | |
| 27 | var knownOptions = { |
| 28 | string: 'env', |
| 29 | default: { env: 'production' } |
| 30 | }; |
| 31 | |
| 32 | var options = minimist(process.argv.slice(2), knownOptions); |
| 33 | |
| 34 | function getArgumentIndex(argumentName) { |
| 35 | return process.argv.indexOf("--" + argumentName); |
| 36 | } |
| 37 | |
| 38 | function readArgument(argumentName) { |
| 39 | var argumentNameIndex = getArgumentIndex(argumentName); |
| 40 | var argumentValueIndex = argumentNameIndex + 1; |
| 41 | return argumentNameIndex > -1 && argumentValueIndex < process.argv.length |
| 42 | ? process.argv[argumentValueIndex] |
| 43 | : null; |
| 44 | } |
| 45 | |
| 46 | // TODO: The file property should point to the generated source (this implementation adds an extra folder to the path) |
| 47 | // We should also make sure that we always generate urls in all the path properties (We shouldn't have \\s. This seems to |
| 48 | // be an issue on Windows platforms) |
| 49 | gulp.task('build', ["check-imports", "check-copyright"], function () { |
| 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(ts(tsProject)) |
| 58 | .pipe(sourcemaps.write('.', { |
| 59 | includeContent: false, |
| 60 | sourceRoot: function (file) { |
| 61 | return path.relative(path.dirname(file.path), __dirname + '/src'); |
| 62 | } |
| 63 | })) |
| 64 | .pipe(gulp.dest(outPath)); |
| 65 | }); |
| 66 | |
| 67 | gulp.task('watch', ['build'], function (cb) { |
| 68 | log('Watching build sources...'); |
| 69 | return gulp.watch(sources, ['build']); |
| 70 | }); |
| 71 | |
| 72 | gulp.task('default', function (callback) { |
| 73 | runSequence("clean", "build", "tslint", callback); |
| 74 | }); |
| 75 | |
| 76 | var lintSources = [ |
| 77 | srcPath, |
| 78 | ].map(function (tsFolder) { return tsFolder + '/**/*.ts'; }); |
| 79 | lintSources = lintSources.concat([ |
| 80 | '!src/typings/**' |
| 81 | ]); |
| 82 | |
| 83 | var tslint = require('gulp-tslint'); |
| 84 | gulp.task('tslint', function () { |
| 85 | return gulp.src(lintSources, { base: '.' }) |
| 86 | .pipe(tslint()) |
| 87 | .pipe(tslint.report('verbose')); |
| 88 | }); |
| 89 | |
| 90 | function test() { |
| 91 | // Defaults |
| 92 | var invert = true; |
| 93 | |
| 94 | // Check if arguments were passed |
| 95 | var pattern = readArgument("pattern"); |
| 96 | if (pattern !== null) { |
| 97 | invert = false; |
| 98 | console.log("\nTesting cases that match pattern: " + pattern); |
| 99 | } else { |
| 100 | pattern = "extensionContext"; |
| 101 | console.log("\nTesting cases that don't match pattern: " + pattern); |
| 102 | } |
| 103 | |
| 104 | return gulp.src(['out/test/**/*.test.js', '!out/test/extension/**']) |
| 105 | .pipe(mocha({ |
| 106 | ui: 'tdd', |
| 107 | useColors: true, |
| 108 | invert: invert, |
| 109 | grep: pattern |
| 110 | })); |
| 111 | } |
| 112 | |
| 113 | gulp.task('build-test', ['build'], test); |
| 114 | gulp.task('test', test); |
| 115 | |
| 116 | gulp.task('check-imports', function (cb) { |
| 117 | var tsProject = ts.createProject('tsconfig.json'); |
| 118 | return tsProject.src() |
| 119 | .pipe(imports()); |
| 120 | }); |
| 121 | |
| 122 | gulp.task('check-copyright', function (cb) { |
| 123 | return gulp.src([ |
| 124 | "**/*.ts", |
| 125 | "**/*.js", |
| 126 | "!**/*.d.ts", |
| 127 | "!node_modules/**/*.*", |
| 128 | "!SampleApplication/**/*.js" |
| 129 | ]) |
| 130 | .pipe(copyright()); |
| 131 | }); |
| 132 | |
| 133 | gulp.task('watch-build-test', ['build', 'build-test'], function () { |
| 134 | return gulp.watch(sources, ['build', 'build-test']); |
| 135 | }); |
| 136 | |
| 137 | gulp.task("clean", function () { |
| 138 | var del = require("del"); |
| 139 | var pathsToDelete = [ |
| 140 | outPath, |
| 141 | ".vscode-test" |
| 142 | ].map(function (folder) { |
| 143 | return folder + "/**"; |
| 144 | }); |
| 145 | return del(pathsToDelete, { force: true }); |
| 146 | }); |
| 147 | |
| 148 | gulp.task("package", function (callback) { |
| 149 | var command = path.join(__dirname, "node_modules", ".bin", "vsce" + (process.platform === "win32" ? ".cmd" : "")); |
| 150 | var args = ["package"]; |
| 151 | executeCommand(command, args, callback); |
| 152 | }); |