mirror of https://github.com/grpc/grpc-node.git
				
				
				
			
						commit
						c07fc056cb
					
				
							
								
								
									
										156
									
								
								gulpfile.js
								
								
								
								
							
							
						
						
									
										156
									
								
								gulpfile.js
								
								
								
								
							|  | @ -1,5 +1,6 @@ | ||||||
| const del = require('del'); | const del = require('del'); | ||||||
| const gulp = require('gulp'); | const _gulp = require('gulp'); | ||||||
|  | const help = require('gulp-help'); | ||||||
| const mocha = require('gulp-mocha'); | const mocha = require('gulp-mocha'); | ||||||
| const sourcemaps = require('gulp-sourcemaps'); | const sourcemaps = require('gulp-sourcemaps'); | ||||||
| const tslint = require('gulp-tslint'); | const tslint = require('gulp-tslint'); | ||||||
|  | @ -9,17 +10,35 @@ const merge2 = require('merge2'); | ||||||
| const path = require('path'); | const path = require('path'); | ||||||
| const through = require('through2'); | const through = require('through2'); | ||||||
| 
 | 
 | ||||||
| const tslintPath = './tslint.json' | // gulp-help monkeypatches tasks to have an additional description parameter
 | ||||||
|  | const gulp = help(_gulp); | ||||||
|  | 
 | ||||||
|  | const tslintPath = './node_modules/google-ts-style/tslint.json'; | ||||||
| const tsconfigPath = './tsconfig.json'; | const tsconfigPath = './tsconfig.json'; | ||||||
| const outDir = 'build'; | const outDir = 'build'; | ||||||
| 
 | 
 | ||||||
| function onError() {} | function onError() {} | ||||||
| 
 | 
 | ||||||
|  | // Coalesces all specified --file parameters into a single array
 | ||||||
|  | const files = !util.env.file ? [] : | ||||||
|  |   Array.isArray(util.env.file) ? util.env.file : [util.env.file]; | ||||||
|  | 
 | ||||||
|  | // If --dev is passed, override certain ts config options
 | ||||||
|  | let tsDevOptions = {}; | ||||||
|  | if (util.env.dev) { | ||||||
|  |   tsDevOptions = { | ||||||
|  |     allowUnreachableCode: true, | ||||||
|  |     noUnusedParameters: false, | ||||||
|  |     noImplicitAny: false, | ||||||
|  |     noImplicitThis: false, | ||||||
|  |     noEmitOnError: false | ||||||
|  |   }; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Helper function that creates a gulp task function that opens files in a |  * Helper function that creates a gulp task function that opens files in a | ||||||
|  * directory that match a certain glob pattern, transpiles them, and writes them |  * directory that match a certain glob pattern, transpiles them, and writes them | ||||||
|  * to an output directory. |  * to an output directory. | ||||||
|  * @param {string} baseDir The name of the directory containing the files. |  | ||||||
|  * @param {Object} globs |  * @param {Object} globs | ||||||
|  * @param {string=} globs.transpile The glob pattern for files to transpile. |  * @param {string=} globs.transpile The glob pattern for files to transpile. | ||||||
|  *   Defaults to match all *.ts files in baseDir (incl. subdirectories). |  *   Defaults to match all *.ts files in baseDir (incl. subdirectories). | ||||||
|  | @ -27,25 +46,25 @@ function onError() {} | ||||||
|  *   Defaults to match all but *.ts files in baseDir (incl. subdirectories). |  *   Defaults to match all but *.ts files in baseDir (incl. subdirectories). | ||||||
|  * @return A gulp task function. |  * @return A gulp task function. | ||||||
|  */ |  */ | ||||||
| function makeCompileFn(baseDir, globs) { | function makeCompileFn(globs) { | ||||||
|   const transpileGlob = globs.transpile || '**/*.ts'; |   const transpileGlob = globs.transpile || '**/*.ts'; | ||||||
|   const copyGlob = globs.copy || '**/!(*.ts)'; |   const copyGlob = globs.copy || '!(**/*)'; | ||||||
|   return () => { |   return () => { | ||||||
|     const tsProject = typescript.createProject(tsconfigPath)(); |     const tsProject = typescript.createProject(tsconfigPath, tsDevOptions)(); | ||||||
|     const { dts, js } = gulp.src(`${baseDir}/${transpileGlob}`) |     const { dts, js } = gulp.src(transpileGlob, { base: '.' }) | ||||||
|       .pipe(sourcemaps.init()) |       .pipe(sourcemaps.init()) | ||||||
|       .pipe(tsProject) |       .pipe(tsProject) | ||||||
|       .on('error', onError); |       .on('error', onError); | ||||||
|     const jsmap = js.pipe(sourcemaps.write('.', { |     const jsmap = js.pipe(sourcemaps.write('.', { | ||||||
|       includeContent: false, |       includeContent: false, | ||||||
|       sourceRoot: path.relative(`${outDir}/${baseDir}`, baseDir) |       sourceRoot: '..' | ||||||
|     })); |     })); | ||||||
|     const copy = gulp.src(`${baseDir}/${copyGlob}`); |     const copy = gulp.src(copyGlob, { base: '.' }); | ||||||
|     return merge2([ |     return merge2([ | ||||||
|       js.pipe(gulp.dest(`${outDir}/${baseDir}`)), |       js.pipe(gulp.dest(`${outDir}`)), | ||||||
|       dts.pipe(gulp.dest(`${outDir}/types`)), |       dts.pipe(gulp.dest(`${outDir}/types`)), | ||||||
|       jsmap.pipe(gulp.dest(`${outDir}/${baseDir}`)), |       jsmap.pipe(gulp.dest(`${outDir}`)), | ||||||
|       copy.pipe(gulp.dest(`${outDir}/${baseDir}`)) |       copy.pipe(gulp.dest(`${outDir}`)) | ||||||
|     ]); |     ]); | ||||||
|   }; |   }; | ||||||
| } | } | ||||||
|  | @ -53,9 +72,9 @@ function makeCompileFn(baseDir, globs) { | ||||||
| /** | /** | ||||||
|  * Runs tslint on files in src/, with linting rules defined in tslint.json. |  * Runs tslint on files in src/, with linting rules defined in tslint.json. | ||||||
|  */ |  */ | ||||||
| gulp.task('lint', () => { | gulp.task('lint', 'Emits linting errors found in src/ and test/.', () => { | ||||||
|   const program = require('tslint').Linter.createProgram(tsconfigPath); |   const program = require('tslint').Linter.createProgram(tsconfigPath); | ||||||
|   gulp.src(srcGlob) |   gulp.src(['src/**/*.ts', 'test/**/*.ts']) | ||||||
|     .pipe(tslint({ |     .pipe(tslint({ | ||||||
|       configuration: tslintPath, |       configuration: tslintPath, | ||||||
|       formatter: 'prose', |       formatter: 'prose', | ||||||
|  | @ -65,7 +84,7 @@ gulp.task('lint', () => { | ||||||
|     .on('warning', onError); |     .on('warning', onError); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| gulp.task('clean', () => { | gulp.task('clean', 'Deletes transpiled code.', () => { | ||||||
|   return del(outDir); |   return del(outDir); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | @ -75,78 +94,69 @@ gulp.task('clean', () => { | ||||||
|  * Currently, all errors are emitted twice. This is being tracked here: |  * Currently, all errors are emitted twice. This is being tracked here: | ||||||
|  * https://github.com/ivogabe/gulp-typescript/issues/438
 |  * https://github.com/ivogabe/gulp-typescript/issues/438
 | ||||||
|  */ |  */ | ||||||
| gulp.task('compile', makeCompileFn('src', | gulp.task('compile', 'Transpiles src/.', | ||||||
|   { transpile: '**/*.ts' })); |   makeCompileFn({ transpile: ['*.ts', 'src/**/*.ts'] })); | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Transpiles TypeScript files in both src/ and test/. |  * Transpiles TypeScript files in both src/ and test/. | ||||||
|  */ |  */ | ||||||
| gulp.task('test.compile', ['compile'], | gulp.task('test.compile', 'After dep tasks, transpiles test/.', ['compile'], | ||||||
|   makeCompileFn('test', { transpile: '**/*.ts' })); |   makeCompileFn({ transpile: ['test/**/*.ts'], copy: 'test/**/!(*.ts)' })); | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Starts watching files in src/, running the 'compile' step whenever a file |  | ||||||
|  * changes. |  | ||||||
|  */ |  | ||||||
| gulp.task('watch', () => { |  | ||||||
|   gulp.start(['compile']); |  | ||||||
|   return gulp.watch(srcGlob, ['compile']); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Transpiles src/ and test/, and then runs all tests. |  * Transpiles src/ and test/, and then runs all tests. | ||||||
|  */ |  */ | ||||||
| gulp.task('test', ['test.compile'], () => { | gulp.task('test', 'After dep tasks, runs all tests.', | ||||||
|   return gulp.src(`${outDir}/test/**/*.js`) |   ['test.compile'], () => { | ||||||
|     .pipe(mocha()); |     return gulp.src(`${outDir}/test/**/*.js`) | ||||||
| }); |       .pipe(mocha()); | ||||||
|  |   } | ||||||
|  | ); | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Compiles a single test file. Only intended as a pre-requisite for |  * Transpiles individual files, specified by the --file flag. | ||||||
|  * 'test.single'. |  | ||||||
|  * @private |  | ||||||
|  */ |  */ | ||||||
| gulp.task('.compileSingleTestFile', util.env.file ? | gulp.task('compile.single', 'Transpiles individual files specified by --file.', | ||||||
|   makeCompileFn('test', { transpile: path.relative('test', util.env.file) }) : |   makeCompileFn({ | ||||||
|   () => { throw new Error('No file specified'); }); |     transpile: files.map(f => path.relative('.', f)) | ||||||
|  |   }) | ||||||
|  | ); | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Run a single test, specified by its pre-transpiled source path (as supplied |  * Run individual tests, specified by their pre-transpiled source path (as | ||||||
|  * through the '--file' flag). This is intended to be used as part of a VS Code |  * supplied through the '--file' flag). This is intended to be used as part of a | ||||||
|  * "Gulp task" launch configuration; setting the "args" field to |  * VS Code "Gulp task" launch configuration; setting the "args" field to | ||||||
|  * ["test.single", "--file", "${file}"] makes it possible for one to debug the |  * ["test.single", "--file", "${file}"] makes it possible for one to debug the | ||||||
|  * currently open TS mocha test file in one step. |  * currently open TS mocha test file in one step. | ||||||
|  */ |  */ | ||||||
| gulp.task('test.single', ['compile', '.compileSingleTestFile'], () => { | gulp.task('test.single', 'After dep tasks, runs individual files specified ' + | ||||||
|   // util.env contains CLI arguments for the gulp task.
 |   'by --file.', ['compile', 'compile.single'], () => { | ||||||
|   const { file } = util.env; |     // util.env contains CLI arguments for the gulp task.
 | ||||||
|   // Determine the path to the transpiled version of this TS file.
 |     // Determine the path to the transpiled version of this TS file.
 | ||||||
|   const dirWithinTest = path.dirname(path.relative('./test', file)); |     const getTranspiledPath = (file) => { | ||||||
|   const basename = path.basename(file, '.ts'); |       const dir = path.dirname(path.relative('.', file)); | ||||||
|   const transpiledPath = `${outDir}/test/${dirWithinTest}/${basename}.js`; |       const basename = path.basename(file, '.ts'); | ||||||
|   // Construct an instance of Mocha's runner API and feed it the path to the
 |       return `${outDir}/${dir}/${basename}.js`; | ||||||
|   // transpiled source.
 |     }; | ||||||
|   return gulp.src(transpiledPath) |     // Construct an instance of Mocha's runner API and feed it the path to the
 | ||||||
|     .pipe(through.obj((file, enc, cb) => { |     // transpiled source.
 | ||||||
|       // Construct a new Mocha runner instance.
 |     return gulp.src(files.map(getTranspiledPath)) | ||||||
|       const Mocha = require('mocha'); |       .pipe(through.obj((file, enc, cb) => { | ||||||
|       const runner = new Mocha(); |         // Construct a new Mocha runner instance.
 | ||||||
|       // Add the path to the test file to debug.
 |         const Mocha = require('mocha'); | ||||||
|       runner.addFile(file.path); |         const runner = new Mocha(); | ||||||
|       // "Load" the test file.
 |         // Add the path to the test file to debug.
 | ||||||
|       // This is a no-op hack so that VS Code is informed that breakpoints set
 |         runner.addFile(file.path); | ||||||
|       // in this file should be hit.
 |         // Run the test suite.
 | ||||||
|       // Because mocha's global-scope variables aren't loaded, this will throw.
 |         runner.run((failures) => { | ||||||
|       try { require(file.path); } catch (e) {}; |           if (failures > 0) { | ||||||
|       // Run the test suite.
 |             cb(new Error(`Mocha: ${failures} failures in ${file.path}]`)); | ||||||
|       runner.run((failures) => { |           } else { | ||||||
|         if (failures > 0) { |             cb(null); | ||||||
|           cb(new Error(`Mocha: ${failures} failures in ${file.path}]`)); |           } | ||||||
|         } else { |         }); | ||||||
|           cb(null); |       })); | ||||||
|         } |   } | ||||||
|       }); | ); | ||||||
|     })); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| gulp.task('default', ['compile']); | gulp.task('default', ['help']); | ||||||
|  |  | ||||||
|  | @ -20,6 +20,7 @@ | ||||||
|     "del": "^3.0.0", |     "del": "^3.0.0", | ||||||
|     "google-ts-style": "latest", |     "google-ts-style": "latest", | ||||||
|     "gulp": "^3.9.1", |     "gulp": "^3.9.1", | ||||||
|  |     "gulp-help": "^1.6.1", | ||||||
|     "gulp-mocha": "^4.3.1", |     "gulp-mocha": "^4.3.1", | ||||||
|     "gulp-sourcemaps": "^2.6.0", |     "gulp-sourcemaps": "^2.6.0", | ||||||
|     "gulp-tslint": "^8.1.1", |     "gulp-tslint": "^8.1.1", | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue