mirror of https://github.com/grpc/grpc-node.git
commit
c07fc056cb
112
gulpfile.js
112
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,69 +94,59 @@ 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.',
|
||||||
|
['test.compile'], () => {
|
||||||
return gulp.src(`${outDir}/test/**/*.js`)
|
return gulp.src(`${outDir}/test/**/*.js`)
|
||||||
.pipe(mocha());
|
.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 ' +
|
||||||
|
'by --file.', ['compile', 'compile.single'], () => {
|
||||||
// util.env contains CLI arguments for the gulp task.
|
// util.env contains CLI arguments for the gulp task.
|
||||||
const { file } = util.env;
|
|
||||||
// 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 dir = path.dirname(path.relative('.', file));
|
||||||
const basename = path.basename(file, '.ts');
|
const basename = path.basename(file, '.ts');
|
||||||
const transpiledPath = `${outDir}/test/${dirWithinTest}/${basename}.js`;
|
return `${outDir}/${dir}/${basename}.js`;
|
||||||
|
};
|
||||||
// Construct an instance of Mocha's runner API and feed it the path to the
|
// Construct an instance of Mocha's runner API and feed it the path to the
|
||||||
// transpiled source.
|
// transpiled source.
|
||||||
return gulp.src(transpiledPath)
|
return gulp.src(files.map(getTranspiledPath))
|
||||||
.pipe(through.obj((file, enc, cb) => {
|
.pipe(through.obj((file, enc, cb) => {
|
||||||
// Construct a new Mocha runner instance.
|
// Construct a new Mocha runner instance.
|
||||||
const Mocha = require('mocha');
|
const Mocha = require('mocha');
|
||||||
const runner = new Mocha();
|
const runner = new Mocha();
|
||||||
// Add the path to the test file to debug.
|
// Add the path to the test file to debug.
|
||||||
runner.addFile(file.path);
|
runner.addFile(file.path);
|
||||||
// "Load" the test file.
|
|
||||||
// This is a no-op hack so that VS Code is informed that breakpoints set
|
|
||||||
// in this file should be hit.
|
|
||||||
// Because mocha's global-scope variables aren't loaded, this will throw.
|
|
||||||
try { require(file.path); } catch (e) {};
|
|
||||||
// Run the test suite.
|
// Run the test suite.
|
||||||
runner.run((failures) => {
|
runner.run((failures) => {
|
||||||
if (failures > 0) {
|
if (failures > 0) {
|
||||||
|
|
@ -147,6 +156,7 @@ gulp.task('test.single', ['compile', '.compileSingleTestFile'], () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
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