-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to use tools-common's gulp tasks as a library (#5)
* More work to make tools-common librarified * Add version to package.json * Use cwd rather than __dirname * Use the tools-common .eslintrc.json if no project-specific one is found * Use tools-common tslint as a fallback too * Update gulp-typings * Document the "if main" section of gulpfile.js * Document the subtle gulp cwd constraint * Gulp as peer dependency, gulp tasks as gulp-tasks.js * Add generateCompleteTaskgraph, fix style nits and improve docs
- Loading branch information
Showing
4 changed files
with
194 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | ||
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
* Code distributed by Google as part of the polymer project is also | ||
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs-extra'); | ||
const gulp = require('gulp'); | ||
const mocha = require('gulp-mocha'); | ||
const tslint_lib = require('gulp-tslint'); | ||
const typescript = require('gulp-typescript'); | ||
const typings = require('gulp-typings'); | ||
const mergeStream = require('merge-stream'); | ||
const path = require('path'); | ||
const runSequence = require('run-sequence'); | ||
|
||
function task(name, deps, impl) { | ||
if (gulp.hasTask(name)) { | ||
throw new Error( | ||
`A task with the name ${JSON.stringify(name)} already exists!`); | ||
} | ||
gulp.task(name, deps, impl); | ||
} | ||
|
||
module.exports.init = function() { | ||
task('init', () => gulp.src('./typings.json').pipe(typings())); | ||
} | ||
|
||
module.exports.depcheck = function depcheck(options) { | ||
const depcheck_lib = require('depcheck'); | ||
const defaultOptions = {stickyDeps: new Set()}; | ||
options = Object.assign({}, defaultOptions, options); | ||
|
||
task('depcheck', () => { | ||
return new Promise((resolve, reject) => { | ||
// Note that process.cwd() in a gulp task is the directory of the | ||
// running gulpfile. See e.g. https://github.com/gulpjs/gulp/issues/523 | ||
depcheck_lib(process.cwd(), {ignoreDirs: []}, resolve); | ||
}).then((result) => { | ||
const invalidFiles = Object.keys(result.invalidFiles) || []; | ||
const invalidJsFiles = invalidFiles.filter((f) => f.endsWith('.js')); | ||
|
||
if (invalidJsFiles.length > 0) { | ||
console.log('Invalid files:', result.invalidFiles); | ||
throw new Error('Invalid files'); | ||
} | ||
|
||
const unused = new Set(result.dependencies); | ||
for (const falseUnused of options.stickyDeps) { | ||
unused.delete(falseUnused); | ||
} | ||
if (unused.size > 0) { | ||
console.log('Unused dependencies:', unused); | ||
throw new Error('Unused dependencies'); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
module.exports.lint = function(options) { | ||
module.exports.tslint(options); | ||
module.exports.eslint(options); | ||
module.exports.depcheck(options); | ||
task('lint', ['tslint', 'eslint', 'depcheck']); | ||
} | ||
|
||
function getJsonConfig(filename) { | ||
var placesToLook = [ | ||
process.cwd(), | ||
__dirname, | ||
]; | ||
for (const directory of placesToLook) { | ||
try { | ||
return JSON.parse( | ||
fs.readFileSync(path.join(directory, filename), 'utf-8')); | ||
} catch (error) { /* don't care */ } | ||
} | ||
throw new Error('Could not find a .eslintrc.json. This should never happen.'); | ||
} | ||
|
||
module.exports.tslint = function(options) { | ||
const defaultOptions = {tsSrcs: gulp.src('src/**/*.ts')}; | ||
options = Object.assign({}, defaultOptions, options); | ||
const tslintConfig = getJsonConfig('tslint.json'); | ||
task('tslint', () => | ||
options.tsSrcs | ||
.pipe(tslint_lib({ | ||
configuration: tslintConfig, | ||
})) | ||
.pipe(tslint_lib.report('verbose'))); | ||
} | ||
|
||
module.exports.eslint = function(options) { | ||
const eslint_lib = require('gulp-eslint'); | ||
const defaultOptions = {jsSrcs: gulp.src(['test/**/*.js', 'gulpfile.js'])}; | ||
options = Object.assign({}, defaultOptions, options); | ||
const eslintConfig = getJsonConfig('.eslintrc.json'); | ||
task('eslint', () => | ||
options.jsSrcs | ||
.pipe(eslint_lib(eslintConfig)) | ||
.pipe(eslint_lib.format()) | ||
.pipe(eslint_lib.failAfterError())); | ||
} | ||
|
||
module.exports.build = function(options) { | ||
const defaultOptions = { | ||
tsSrcs: gulp.src('src/**/*.ts'), | ||
dataSrcs: gulp.src(['src/**/*', '!src/**/*.ts']), | ||
}; | ||
options = Object.assign({}, defaultOptions, options); | ||
|
||
const tsProject = typescript.createProject('tsconfig.json'); | ||
|
||
task('build', () => | ||
mergeStream( | ||
options.tsSrcs.pipe(typescript(tsProject)), | ||
options.dataSrcs | ||
).pipe(gulp.dest('lib')) | ||
); | ||
} | ||
|
||
module.exports.clean = function(options) { | ||
const defaultOptions = {buildArtifacts: ['lib/', 'typings/']}; | ||
options = Object.assign({}, defaultOptions, options); | ||
|
||
task('clean', () => { | ||
for (const buildArtifact of options.buildArtifacts) { | ||
fs.removeSync(path.join(process.cwd(), buildArtifact)); | ||
} | ||
}); | ||
} | ||
|
||
|
||
module.exports.buildAll = function(options) { | ||
module.exports.clean(options); | ||
module.exports.init(options); | ||
module.exports.lint(options); | ||
module.exports.build(options); | ||
|
||
task('build-all', (done) => { | ||
runSequence('clean', 'init', 'lint', 'build', done); | ||
}); | ||
} | ||
|
||
module.exports.test = function(options) { | ||
module.exports.buildAll(options); | ||
|
||
task('test', ['build'], () => | ||
gulp.src('test/**/*_test.js', {read: false}) | ||
.pipe(mocha({ | ||
ui: 'tdd', | ||
reporter: 'spec', | ||
})) | ||
); | ||
} | ||
|
||
module.exports.generateCompleteTaskgraph = function(options) { | ||
module.exports.test(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters