style: add gulp task to only format changed files (#24969)
Closes #24904 PR Close #24969
This commit is contained in:
parent
a08af77b70
commit
ac3252a73b
|
@ -29,6 +29,7 @@ function loadTask(fileName, taskName) {
|
|||
|
||||
gulp.task('format:enforce', loadTask('format', 'enforce'));
|
||||
gulp.task('format', loadTask('format', 'format'));
|
||||
gulp.task('format:changed', loadTask('format', 'format-changed'));
|
||||
gulp.task('build.sh', loadTask('build', 'all'));
|
||||
gulp.task('build.sh:no-bundle', loadTask('build', 'no-bundle'));
|
||||
gulp.task('lint', ['format:enforce', 'validate-commit-messages', 'tslint']);
|
||||
|
|
|
@ -88,7 +88,9 @@
|
|||
"gulp": "3.9.1",
|
||||
"gulp-clang-format": "1.0.23",
|
||||
"gulp-connect": "5.0.0",
|
||||
"gulp-conventional-changelog": "^2.0.3",
|
||||
"gulp-conventional-changelog": "1.1.0",
|
||||
"gulp-filter": "^5.1.0",
|
||||
"gulp-git": "^2.7.0",
|
||||
"gulp-tslint": "8.1.2",
|
||||
"hammerjs": "2.0.8",
|
||||
"husky": "^0.14.3",
|
||||
|
|
|
@ -29,6 +29,61 @@ const srcsToFmt = [
|
|||
'!tools/ts-api-guardian/test/fixtures/**',
|
||||
];
|
||||
|
||||
/**
|
||||
* Gulp stream that wraps the gulp-git status task
|
||||
* and converts the stdout into a stream of files
|
||||
*/
|
||||
function gulpStatus() {
|
||||
const Vinyl = require('vinyl');
|
||||
const path = require('path');
|
||||
const gulpGit = require('gulp-git');
|
||||
const through = require('through2');
|
||||
const srcStream = through.obj();
|
||||
|
||||
const opt = {cwd: process.cwd()};
|
||||
|
||||
// https://git-scm.com/docs/git-status#_short_format
|
||||
const RE_STATUS = /^((\s\w)|(\w+)|\?{0,2})\s([\w\+\-\/\\\.]+)(\s->\s)?([\w\+\-\/\\\.]+)*\n/gm;
|
||||
|
||||
gulpGit.status({args: '--porcelain', quiet: true}, function(err, stdout) {
|
||||
if (err) return srcStream.emit('error', err);
|
||||
|
||||
const data = stdout.toString();
|
||||
let currentMatch;
|
||||
|
||||
while ((currentMatch = RE_STATUS.exec(data)) !== null) {
|
||||
// This is necessary to avoid infinite loops with zero-width matches
|
||||
if (currentMatch.index === RE_STATUS.lastIndex) {
|
||||
RE_STATUS.lastIndex++;
|
||||
}
|
||||
|
||||
// status
|
||||
const status = currentMatch[1].trim().toLowerCase();
|
||||
|
||||
// File has been deleted
|
||||
if (status.includes('d')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// file path
|
||||
const currentFilePath = currentMatch[4];
|
||||
|
||||
// new file path in case its been moved
|
||||
const newFilePath = currentMatch[6];
|
||||
const filePath = newFilePath || currentFilePath;
|
||||
|
||||
srcStream.write(new Vinyl({
|
||||
path: path.resolve(opt.cwd, filePath),
|
||||
cwd: opt.cwd,
|
||||
}));
|
||||
}
|
||||
|
||||
srcStream.end();
|
||||
});
|
||||
|
||||
return srcStream;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
// Check source code for formatting errors (clang-format)
|
||||
enforce: (gulp) => () => {
|
||||
|
@ -45,5 +100,17 @@ module.exports = {
|
|||
return gulp.src(srcsToFmt, {base: '.'})
|
||||
.pipe(format.format('file', clangFormat))
|
||||
.pipe(gulp.dest('.'));
|
||||
},
|
||||
|
||||
// Format only the changed source code files with clang-format (see .clang-format)
|
||||
'format-changed': (gulp) => () => {
|
||||
const format = require('gulp-clang-format');
|
||||
const clangFormat = require('clang-format');
|
||||
const gulpFilter = require('gulp-filter');
|
||||
|
||||
return gulpStatus()
|
||||
.pipe(gulpFilter(srcsToFmt))
|
||||
.pipe(format.format('file', clangFormat))
|
||||
.pipe(gulp.dest('.'));
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue