diff --git a/build/gulpfile.hygiene.js b/build/gulpfile.hygiene.js index 5bf1069ffc6e8c8adb390adf08e611fc9eecd033..87f80d9b194abed3db8df136354eaa4628f3572b 100644 --- a/build/gulpfile.hygiene.js +++ b/build/gulpfile.hygiene.js @@ -12,9 +12,11 @@ const gulptslint = require('gulp-tslint'); const gulpeslint = require('gulp-eslint'); const tsfmt = require('typescript-formatter'); const tslint = require('tslint'); +const VinylFile = require('vinyl'); const vfs = require('vinyl-fs'); const path = require('path'); const fs = require('fs'); +const pall = require('p-all'); /** * Hygiene works by creating cascading subsets of all our files and @@ -157,7 +159,7 @@ gulp.task('tslint', () => { .pipe(gulptslint.default.report(options)); }); -const hygiene = exports.hygiene = (some, options) => { +function hygiene(some, options) { options = options || {}; let errorCount = 0; @@ -279,7 +281,15 @@ const hygiene = exports.hygiene = (some, options) => { this.emit('data', file); }); - const result = vfs.src(some || all, { base: '.', follow: true, allowEmpty: true }) + let input; + + if (Array.isArray(some) || typeof some === 'string' || !some) { + input = vfs.src(some || all, { base: '.', follow: true, allowEmpty: true }); + } else { + input = some; + } + + const result = input .pipe(filter(f => !f.stat.isDirectory())) .pipe(filter(eolFilter)) .pipe(options.skipEOL ? es.through() : eol) @@ -331,9 +341,39 @@ const hygiene = exports.hygiene = (some, options) => { this.emit('end'); } })); -}; +} + +function createGitIndexVinyls(paths) { + const cp = require('child_process'); + const repositoryPath = process.cwd(); + + const fns = paths.map(relativePath => () => new Promise((c, e) => { + const fullPath = path.join(repositoryPath, relativePath); + + fs.stat(fullPath, (err, stat) => { + if (err) { + return e(err); + } + + cp.exec(`git show :${relativePath}`, { maxBuffer: 2000 * 1024, encoding: 'buffer' }, (err, out) => { + if (err) { + return e(err); + } + + c(new VinylFile({ + path: fullPath, + base: repositoryPath, + contents: out, + stat + })); + }); + }); + })); + + return pall(fns, { concurrency: 4 }); +} -gulp.task('hygiene', () => hygiene('')); +gulp.task('hygiene', () => hygiene()); // this allows us to run hygiene as a git pre-commit hook if (require.main === module) { @@ -360,6 +400,7 @@ if (require.main === module) { console.error(); console.error(err); process.exit(1); + return; } const some = out @@ -367,11 +408,17 @@ if (require.main === module) { .filter(l => !!l); if (some.length > 0) { - hygiene(some, { skipEOL: skipEOL }).on('error', err => { - console.error(); - console.error(err); - process.exit(1); - }); + console.log('Reading git index versions...'); + + createGitIndexVinyls(some) + .then(vinyls => new Promise((c, e) => hygiene(es.readArray(vinyls), { skipEOL: skipEOL }) + .on('end', () => c()) + .on('error', e))) + .catch(err => { + console.error(); + console.error(err); + process.exit(1); + }); } }); }); diff --git a/package.json b/package.json index ce61c2c02b89c11fc4c61113035f43c41460664c..e0b2fddc1b4a0909459b39ded7719770b708e905 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "mocha": "^2.2.5", "object-assign": "^4.0.1", "optimist": "0.3.5", + "p-all": "^1.0.0", "pump": "^1.0.1", "queue": "3.0.6", "remap-istanbul": "^0.6.4", diff --git a/yarn.lock b/yarn.lock index 62f828b3932b3359bf33cc0a70eb5bbcfc0863c5..063db3f398a1abb964d141a04e22bc94b9262385 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4160,6 +4160,12 @@ osenv@^0.1.3: os-homedir "^1.0.0" os-tmpdir "^1.0.0" +p-all@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/p-all/-/p-all-1.0.0.tgz#93bdf53a55a23821fdfa98b4174a99bf7f31df8d" + dependencies: + p-map "^1.0.0" + p-finally@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" @@ -4176,6 +4182,10 @@ p-locate@^2.0.0: dependencies: p-limit "^1.1.0" +p-map@^1.0.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/p-map/-/p-map-1.2.0.tgz#e4e94f311eabbc8633a1e79908165fca26241b6b" + p-try@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-try/-/p-try-1.0.0.tgz#cbc79cdbaf8fd4228e13f621f2b1a237c1b207b3"