Skip to content

Commit

Permalink
style: update gulp task to format untracked and diff files separately
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Sep 27, 2018
1 parent 1c82003 commit 0e287cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Expand Up @@ -29,9 +29,9 @@ function loadTask(fileName, taskName) {

gulp.task('format:enforce', loadTask('format', 'enforce'));
gulp.task('format', loadTask('format', 'format'));
gulp.task('format:changes', loadTask('format', 'format-changes'));
gulp.task('format:untracked', loadTask('format', 'format-untracked'));
gulp.task('format:diff', loadTask('format', 'format-diff'));
gulp.task('format:changed', ['format:changes', 'format:diff']);
gulp.task('format:changed', ['format:untracked', 'format:diff']);
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']);
Expand Down
22 changes: 10 additions & 12 deletions tools/gulp-tasks/format.js
Expand Up @@ -30,8 +30,9 @@ const srcsToFmt = [
];

/**
* Gulp stream that wraps the gulp-git status task
* and converts the stdout into a stream of files
* Gulp stream that wraps the gulp-git status,
* only returns untracked files, and converts
* the stdout into a stream of files.
*/
function gulpStatus() {
const Vinyl = require('vinyl');
Expand All @@ -43,7 +44,7 @@ function gulpStatus() {
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;
const RE_STATUS = /((\s\w)|(\w+)|\?{0,2})\s([\w\+\-\/\\\.]+)(\s->\s)?([\w\+\-\/\\\.]+)*\n{0,1}/gm;

gulpGit.status({args: '--porcelain', quiet: true}, function(err, stdout) {
if (err) return srcStream.emit('error', err);
Expand All @@ -52,16 +53,11 @@ function gulpStatus() {
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')) {
// We only care about untracked files and renamed files
if (!new RegExp(/r|\?/i).test(status)) {
continue;
}

Expand All @@ -76,6 +72,8 @@ function gulpStatus() {
path: path.resolve(opt.cwd, filePath),
cwd: opt.cwd,
}));

RE_STATUS.lastIndex++;
}

srcStream.end();
Expand All @@ -102,8 +100,8 @@ module.exports = {
.pipe(gulp.dest('.'));
},

// Format only the changed source code files with clang-format (see .clang-format)
'format-changes': (gulp) => () => {
// Format only the untracked source code files with clang-format (see .clang-format)
'format-untracked': (gulp) => () => {
const format = require('gulp-clang-format');
const clangFormat = require('clang-format');
const gulpFilter = require('gulp-filter');
Expand Down

0 comments on commit 0e287cb

Please sign in to comment.