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