diff --git a/gulpfile.js b/gulpfile.js index 740845b2..9687aff5 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,162 +1,17 @@ -const del = require('del'); const _gulp = require('gulp'); const help = require('gulp-help'); -const mocha = require('gulp-mocha'); -const sourcemaps = require('gulp-sourcemaps'); -const tslint = require('gulp-tslint'); -const typescript = require('gulp-typescript'); -const util = require('gulp-util'); -const merge2 = require('merge2'); -const path = require('path'); -const through = require('through2'); // 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 outDir = 'build'; +require('./packages/grpc-js-core/gulpfile')(gulp); -function onError() {} +gulp.task('lint', 'Emit linting errors in source and test files', ['js.core.lint']); -// 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]; +gulp.task('build', 'Build packages', ['js.core.compile']); -// 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 - }; -} +gulp.task('clean', 'Delete generated files', ['js.core.clean']); -/** - * 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 - * to an output directory. - * @param {Object} globs - * @param {string=} globs.transpile The glob pattern for files to transpile. - * Defaults to match all *.ts files in baseDir (incl. subdirectories). - * @param {string=} globs.copy The glob pattern for files to transpile. - * Defaults to match all but *.ts files in baseDir (incl. subdirectories). - * @return A gulp task function. - */ -function makeCompileFn(globs) { - const transpileGlob = globs.transpile || '**/*.ts'; - const copyGlob = globs.copy || '!(**/*)'; - return () => { - const tsProject = typescript.createProject(tsconfigPath, tsDevOptions)(); - const { dts, js } = gulp.src(transpileGlob, { base: '.' }) - .pipe(sourcemaps.init()) - .pipe(tsProject) - .on('error', onError); - const jsmap = js.pipe(sourcemaps.write('.', { - includeContent: false, - sourceRoot: '..' - })); - const copy = gulp.src(copyGlob, { base: '.' }); - return merge2([ - js.pipe(gulp.dest(`${outDir}`)), - dts.pipe(gulp.dest(`${outDir}/types`)), - jsmap.pipe(gulp.dest(`${outDir}`)), - copy.pipe(gulp.dest(`${outDir}`)) - ]); - }; -} - -/** - * Runs tslint on files in src/, with linting rules defined in tslint.json. - */ -gulp.task('lint', 'Emits linting errors found in src/ and test/.', () => { - const program = require('tslint').Linter.createProgram(tsconfigPath); - gulp.src(['src/**/*.ts', 'test/**/*.ts']) - .pipe(tslint({ - configuration: tslintPath, - formatter: 'prose', - program - })) - .pipe(tslint.report()) - .on('warning', onError); -}); - -gulp.task('clean', 'Deletes transpiled code.', () => { - return del(outDir); -}); - -/** - * Transpiles TypeScript files in src/ to JavaScript according to the settings - * found in tsconfig.json. - * Currently, all errors are emitted twice. This is being tracked here: - * https://github.com/ivogabe/gulp-typescript/issues/438 - */ -gulp.task('compile', 'Transpiles src/.', - makeCompileFn({ transpile: ['*.ts', 'src/**/*.ts'] })); - -/** - * Transpiles TypeScript files in both src/ and test/. - */ -gulp.task('test.compile', 'After dep tasks, transpiles test/.', ['compile'], - makeCompileFn({ transpile: ['test/**/*.ts'], copy: 'test/**/!(*.ts)' })); - -/** - * Transpiles src/ and test/, and then runs all tests. - */ -gulp.task('test', 'After dep tasks, runs all tests.', - ['test.compile'], () => { - return gulp.src(`${outDir}/test/**/*.js`) - .pipe(mocha()); - } -); - -/** - * Transpiles individual files, specified by the --file flag. - */ -gulp.task('compile.single', 'Transpiles individual files specified by --file.', - makeCompileFn({ - transpile: files.map(f => path.relative('.', f)) - }) -); - -/** - * Run individual tests, specified by their pre-transpiled source path (as - * supplied through the '--file' flag). This is intended to be used as part of a - * VS Code "Gulp task" launch configuration; setting the "args" field to - * ["test.single", "--file", "${file}"] makes it possible for one to debug the - * currently open TS mocha test file in one step. - */ -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. - // Determine the path to the transpiled version of this TS file. - const getTranspiledPath = (file) => { - const dir = path.dirname(path.relative('.', file)); - const basename = path.basename(file, '.ts'); - return `${outDir}/${dir}/${basename}.js`; - }; - // Construct an instance of Mocha's runner API and feed it the path to the - // transpiled source. - return gulp.src(files.map(getTranspiledPath)) - .pipe(through.obj((file, enc, cb) => { - // Construct a new Mocha runner instance. - const Mocha = require('mocha'); - const runner = new Mocha(); - // Add the path to the test file to debug. - runner.addFile(file.path); - // Run the test suite. - runner.run((failures) => { - if (failures > 0) { - cb(new Error(`Mocha: ${failures} failures in ${file.path}]`)); - } else { - cb(null); - } - }); - })); - } -); +gulp.task('test', 'Run all tests', ['js.core.test']); gulp.task('default', ['help']); diff --git a/package.json b/package.json index 1c75eff9..f9b57cf2 100644 --- a/package.json +++ b/package.json @@ -1,55 +1,23 @@ { - "name": "grpc-js", + "name": "grpc-js-repository", "version": "0.1.0", - "description": "gRPC Library for Node - pure JS", - "homepage": "https://grpc.io/", - "main": "index.js", + "description": "Dummy package for the grpc-node repository", "private": true, - "engines": { - "node": ">=8.4" - }, "keywords": [], "author": { "name": "Google Inc." }, - "main": "build/src/index.js", - "types": "src/index.ts", "license": "Apache-2.0", "devDependencies": { - "@types/mocha": "^2.2.42", - "@types/node": "^8.0.25", - "clang-format": "^1.0.53", - "del": "^3.0.0", - "google-ts-style": "^0.2.0", "gulp": "^3.9.1", - "gulp-help": "^1.6.1", - "gulp-mocha": "^4.3.1", - "gulp-sourcemaps": "^2.6.1", - "gulp-tslint": "^8.1.1", - "gulp-typescript": "^3.2.2", - "gulp-util": "^3.0.8", - "merge2": "^1.1.0", - "mocha": "^3.5.0", - "through2": "^2.0.3", - "tslint": "^5.5.0", - "typescript": "^2.5.1" + "gulp-help": "^1.6.1" + }, + "scripts": { + "install": "cd packages/grpc-js-core && npm install" }, "contributors": [ { "name": "Google Inc." } - ], - "_id": "grpc-js@0.1.0", - "scripts": { - "build": "npm run compile", - "clean": "gulp clean", - "compile": "gulp compile", - "format": "clang-format -i -style=\"{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}\" src/*.ts test/*.ts", - "lint": "tslint -c node_modules/google-ts-style/tslint.json -p . -t codeFrame --type-check", - "test": "gulp test" - }, - "dependencies": { - "@types/lodash": "^4.14.73", - "lodash": "^4.17.4" - } + ] } diff --git a/packages/grpc-js-core/gulpfile.js b/packages/grpc-js-core/gulpfile.js new file mode 100644 index 00000000..7f7050aa --- /dev/null +++ b/packages/grpc-js-core/gulpfile.js @@ -0,0 +1,164 @@ +const del = require('del'); +const mocha = require('gulp-mocha'); +const sourcemaps = require('gulp-sourcemaps'); +const tslint = require('gulp-tslint'); +const typescript = require('gulp-typescript'); +const util = require('gulp-util'); +const merge2 = require('merge2'); +const path = require('path'); +const through = require('through2'); + +Error.stackTraceLimit = Infinity; + +module.exports = function(gulp) { + + const jsCoreDir = __dirname; + const tslintPath = path.resolve(jsCoreDir, 'node_modules/google-ts-style/tslint.json'); + const tsconfigPath = path.resolve(jsCoreDir, 'tsconfig.json'); + const outDir = path.resolve(jsCoreDir, 'build'); + const srcDir = path.resolve(jsCoreDir, 'src'); + const testDir = path.resolve(jsCoreDir, 'test'); + + 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 + * directory that match a certain glob pattern, transpiles them, and writes them + * to an output directory. + * @param {Object} globs + * @param {string=} globs.transpile The glob pattern for files to transpile. + * Defaults to match all *.ts files in baseDir (incl. subdirectories). + * @param {string=} globs.copy The glob pattern for files to transpile. + * Defaults to match all but *.ts files in baseDir (incl. subdirectories). + * @return A gulp task function. + */ + function makeCompileFn(globs) { + const transpileGlob = globs.transpile || `${srcDir}/**/*.ts`; + const copyGlob = globs.copy || '!(**/*)'; + return () => { + const tsProject = typescript.createProject(tsconfigPath, tsDevOptions)(); + const { dts, js } = gulp.src(transpileGlob, { base: jsCoreDir }) + .pipe(sourcemaps.init()) + .pipe(tsProject) + .on('error', onError); + const jsmap = js.pipe(sourcemaps.write(jsCoreDir, { + includeContent: false, + sourceRoot: path.resolve(jsCoreDir, '..') + })); + const copy = gulp.src(copyGlob, { base: jsCoreDir }); + return merge2([ + js.pipe(gulp.dest(`${outDir}`)), + dts.pipe(gulp.dest(`${outDir}/types`)), + jsmap.pipe(gulp.dest(`${outDir}`)), + copy.pipe(gulp.dest(`${outDir}`)) + ]); + }; + } + + /** + * Runs tslint on files in src/, with linting rules defined in tslint.json. + */ + gulp.task('js.core.lint', 'Emits linting errors found in src/ and test/.', () => { + const program = require('tslint').Linter.createProgram(tsconfigPath); + gulp.src([`${srcDir}/**/*.ts`, `${srcDir}/**/*.ts`]) + .pipe(tslint({ + configuration: tslintPath, + formatter: 'codeFrame', + program + })) + .pipe(tslint.report()) + .on('warning', onError); + }); + + gulp.task('js.core.clean', 'Deletes transpiled code.', () => { + return del(outDir); + }); + + /** + * Transpiles TypeScript files in src/ to JavaScript according to the settings + * found in tsconfig.json. + * Currently, all errors are emitted twice. This is being tracked here: + * https://github.com/ivogabe/gulp-typescript/issues/438 + */ + gulp.task('js.core.compile', 'Transpiles src/.', + makeCompileFn({ transpile: [`${srcDir}/**/*.ts`] })); + + /** + * Transpiles TypeScript files in both src/ and test/. + */ + gulp.task('js.core.test.compile', 'After dep tasks, transpiles test/.', ['js.core.compile'], + makeCompileFn({ transpile: [`${testDir}/**/*.ts`], copy: `${testDir}/**/!(*.ts)` })); + + /** + * Transpiles src/ and test/, and then runs all tests. + */ + gulp.task('js.core.test', 'After dep tasks, runs all tests.', + ['js.core.test.compile'], () => { + return gulp.src(`${outDir}/test/**/*.js`) + .pipe(mocha()); + } + ); + + /** + * Transpiles individual files, specified by the --file flag. + */ + gulp.task('js.core.compile.single', 'Transpiles individual files specified by --file.', + makeCompileFn({ + transpile: files.map(f => path.relative('.', f)) + }) + ); + + /** + * Run individual tests, specified by their pre-transpiled source path (as + * supplied through the '--file' flag). This is intended to be used as part of a + * VS Code "Gulp task" launch configuration; setting the "args" field to + * ["test.single", "--file", "${file}"] makes it possible for one to debug the + * currently open TS mocha test file in one step. + */ + gulp.task('js.core.test.single', 'After dep tasks, runs individual files specified ' + + 'by --file.', ['js.core.compile', 'js.core.compile.single'], () => { + // util.env contains CLI arguments for the gulp task. + // Determine the path to the transpiled version of this TS file. + const getTranspiledPath = (file) => { + const dir = path.dirname(path.relative('.', file)); + const basename = path.basename(file, '.ts'); + return `${outDir}/${dir}/${basename}.js`; + }; + // Construct an instance of Mocha's runner API and feed it the path to the + // transpiled source. + return gulp.src(files.map(getTranspiledPath)) + .pipe(through.obj((file, enc, cb) => { + // Construct a new Mocha runner instance. + const Mocha = require('mocha'); + const runner = new Mocha(); + // Add the path to the test file to debug. + runner.addFile(file.path); + // Run the test suite. + runner.run((failures) => { + if (failures > 0) { + cb(new Error(`Mocha: ${failures} failures in ${file.path}]`)); + } else { + cb(null); + } + }); + })); + } + ); + +}; diff --git a/packages/grpc-js-core/package.json b/packages/grpc-js-core/package.json new file mode 100644 index 00000000..3ae46ac2 --- /dev/null +++ b/packages/grpc-js-core/package.json @@ -0,0 +1,54 @@ +{ + "name": "grpc-js-core", + "version": "0.1.0", + "description": "gRPC Library for Node - pure JS core", + "homepage": "https://grpc.io/", + "main": "build/src/index.js", + "private": true, + "engines": { + "node": ">=8.4" + }, + "keywords": [], + "author": { + "name": "Google Inc." + }, + "types": "src/index.ts", + "license": "Apache-2.0", + "devDependencies": { + "@types/mocha": "^2.2.42", + "@types/node": "^8.0.25", + "clang-format": "^1.0.53", + "del": "^3.0.0", + "google-ts-style": "^0.2.0", + "gulp": "^3.9.1", + "gulp-help": "^1.6.1", + "gulp-mocha": "^4.3.1", + "gulp-sourcemaps": "^2.6.1", + "gulp-tslint": "^8.1.1", + "gulp-typescript": "^3.2.2", + "gulp-util": "^3.0.8", + "merge2": "^1.1.0", + "mocha": "^3.5.0", + "through2": "^2.0.3", + "tslint": "^5.5.0", + "typescript": "^2.5.1" + }, + "contributors": [ + { + "name": "Google Inc." + } + ], + "_id": "grpc-js-core@0.1.0", + "scripts": { + "build": "npm run compile", + "clean": "gulp clean", + "compile": "gulp compile", + "format": "clang-format -i -style=\"{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}\" src/*.ts test/*.ts", + "lint": "tslint -c node_modules/google-ts-style/tslint.json -p . -t codeFrame --type-check", + "test": "gulp test" + }, + "dependencies": { + "@types/lodash": "^4.14.73", + "lodash": "^4.17.4" + } +} diff --git a/src/call-credentials-filter.ts b/packages/grpc-js-core/src/call-credentials-filter.ts similarity index 100% rename from src/call-credentials-filter.ts rename to packages/grpc-js-core/src/call-credentials-filter.ts diff --git a/src/call-credentials.ts b/packages/grpc-js-core/src/call-credentials.ts similarity index 100% rename from src/call-credentials.ts rename to packages/grpc-js-core/src/call-credentials.ts diff --git a/src/call-stream.ts b/packages/grpc-js-core/src/call-stream.ts similarity index 100% rename from src/call-stream.ts rename to packages/grpc-js-core/src/call-stream.ts diff --git a/src/call.ts b/packages/grpc-js-core/src/call.ts similarity index 100% rename from src/call.ts rename to packages/grpc-js-core/src/call.ts diff --git a/src/channel-credentials.ts b/packages/grpc-js-core/src/channel-credentials.ts similarity index 100% rename from src/channel-credentials.ts rename to packages/grpc-js-core/src/channel-credentials.ts diff --git a/src/channel.ts b/packages/grpc-js-core/src/channel.ts similarity index 100% rename from src/channel.ts rename to packages/grpc-js-core/src/channel.ts diff --git a/src/client.ts b/packages/grpc-js-core/src/client.ts similarity index 100% rename from src/client.ts rename to packages/grpc-js-core/src/client.ts diff --git a/src/compression-filter.ts b/packages/grpc-js-core/src/compression-filter.ts similarity index 100% rename from src/compression-filter.ts rename to packages/grpc-js-core/src/compression-filter.ts diff --git a/src/constants.ts b/packages/grpc-js-core/src/constants.ts similarity index 100% rename from src/constants.ts rename to packages/grpc-js-core/src/constants.ts diff --git a/src/deadline-filter.ts b/packages/grpc-js-core/src/deadline-filter.ts similarity index 100% rename from src/deadline-filter.ts rename to packages/grpc-js-core/src/deadline-filter.ts diff --git a/src/filter-stack.ts b/packages/grpc-js-core/src/filter-stack.ts similarity index 100% rename from src/filter-stack.ts rename to packages/grpc-js-core/src/filter-stack.ts diff --git a/src/filter.ts b/packages/grpc-js-core/src/filter.ts similarity index 100% rename from src/filter.ts rename to packages/grpc-js-core/src/filter.ts diff --git a/src/index.ts b/packages/grpc-js-core/src/index.ts similarity index 100% rename from src/index.ts rename to packages/grpc-js-core/src/index.ts diff --git a/src/metadata.ts b/packages/grpc-js-core/src/metadata.ts similarity index 100% rename from src/metadata.ts rename to packages/grpc-js-core/src/metadata.ts diff --git a/src/object-stream.ts b/packages/grpc-js-core/src/object-stream.ts similarity index 100% rename from src/object-stream.ts rename to packages/grpc-js-core/src/object-stream.ts diff --git a/test/common.ts b/packages/grpc-js-core/test/common.ts similarity index 100% rename from test/common.ts rename to packages/grpc-js-core/test/common.ts diff --git a/test/fixtures/README b/packages/grpc-js-core/test/fixtures/README similarity index 100% rename from test/fixtures/README rename to packages/grpc-js-core/test/fixtures/README diff --git a/test/fixtures/ca.pem b/packages/grpc-js-core/test/fixtures/ca.pem similarity index 100% rename from test/fixtures/ca.pem rename to packages/grpc-js-core/test/fixtures/ca.pem diff --git a/test/fixtures/server1.key b/packages/grpc-js-core/test/fixtures/server1.key similarity index 100% rename from test/fixtures/server1.key rename to packages/grpc-js-core/test/fixtures/server1.key diff --git a/test/fixtures/server1.pem b/packages/grpc-js-core/test/fixtures/server1.pem similarity index 100% rename from test/fixtures/server1.pem rename to packages/grpc-js-core/test/fixtures/server1.pem diff --git a/test/test-call-credentials.ts b/packages/grpc-js-core/test/test-call-credentials.ts similarity index 100% rename from test/test-call-credentials.ts rename to packages/grpc-js-core/test/test-call-credentials.ts diff --git a/test/test-channel-credentials.ts b/packages/grpc-js-core/test/test-channel-credentials.ts similarity index 98% rename from test/test-channel-credentials.ts rename to packages/grpc-js-core/test/test-channel-credentials.ts index abd188bc..fac88110 100644 --- a/test/test-channel-credentials.ts +++ b/packages/grpc-js-core/test/test-channel-credentials.ts @@ -36,7 +36,7 @@ const readFile: (...args: any[]) => Promise = promisify(fs.readFile); // A promise which resolves to loaded files in the form { ca, key, cert } const pFixtures = Promise .all(['ca.pem', 'server1.key', 'server1.pem'].map( - (file) => readFile(`test/fixtures/${file}`))) + (file) => readFile(`${__dirname}/fixtures/${file}`))) .then((result) => { return {ca: result[0], key: result[1], cert: result[2]}; }); diff --git a/test/test-metadata.ts b/packages/grpc-js-core/test/test-metadata.ts similarity index 100% rename from test/test-metadata.ts rename to packages/grpc-js-core/test/test-metadata.ts diff --git a/tsconfig.json b/packages/grpc-js-core/tsconfig.json similarity index 100% rename from tsconfig.json rename to packages/grpc-js-core/tsconfig.json