Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃彈馃殌 Significantly speed up gulp prettify #25140

Merged
merged 1 commit into from Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
137 changes: 71 additions & 66 deletions build-system/tasks/prettify.js
Expand Up @@ -24,26 +24,22 @@

const argv = require('minimist')(process.argv.slice(2));
const globby = require('globby');
const gulp = require('gulp');
const log = require('fancy-log');
const path = require('path');
const prettier = require('gulp-prettier');
rsimha marked this conversation as resolved.
Show resolved Hide resolved
const {getFilesChanged, logOnSameLine} = require('../common/utils');
const {getOutput} = require('../common/exec');
const {green, cyan, red, yellow} = require('ansi-colors');
const {highlight} = require('cli-highlight');
const {isTravisBuild} = require('../common/travis');
const {maybeUpdatePackages} = require('./update-packages');
const {prettifyGlobs} = require('../test-configs/config');

const prettierCmd = 'node_modules/.bin/prettier';
const rootDir = path.dirname(path.dirname(__dirname));

/**
* Prettier exit codes. See https://prettier.io/docs/en/cli.html#exit-codes
* @enum {number}
*/
const PrettierResult = {
SUCCESS: 0,
FAILURE: 1,
ERROR: 2,
};
// Message header printed by gulp-prettier before the list of files with errors.
// See https://github.com/bhargavrpatel/gulp-prettier/blob/master/index.js#L90
const header =
'Code style issues found in the following file(s). Forgot to run Prettier?';

/**
* Logs the list of files that will be checked.
Expand All @@ -61,8 +57,10 @@ function logFiles(files) {

/**
* Checks files for formatting (and optionally fixes them) with Prettier.
*
* @return {!Promise}
*/
async function prettify() {
function prettify() {
maybeUpdatePackages();
let filesToCheck;
if (argv.files) {
Expand All @@ -79,23 +77,33 @@ async function prettify() {
} else {
filesToCheck = globby.sync(prettifyGlobs, {dot: true});
}
runPrettify(filesToCheck);
return runPrettify(filesToCheck);
}

/**
* Runs prettier on the given list of files. Returns the cumulative result to
* the `gulp` process via process.exitCode so all files can be checked / fixed.
* Runs prettier on the given list of files with gulp-prettier.
*
* @param {!Array<string>} filesToCheck
* @return {!Promise}
*/
function runPrettify(filesToCheck) {
if (!isTravisBuild()) {
log(green('Starting checks...'));
}
filesToCheck.forEach(checkFile);
if (process.exitCode == 1) {
logOnSameLine(red('ERROR: ') + 'Found errors in one or more files.');
if (!argv.fix) {
return new Promise((resolve, reject) => {
const onData = data => {
if (!isTravisBuild()) {
logOnSameLine(green('Checked: ') + path.relative(rootDir, data.path));
}
};

const rejectWithReason = reasonText => {
const reason = new Error(reasonText);
reason.showStack = false;
reject(reason);
};

const printFixMessages = () => {
log(
yellow('NOTE 1:'),
'You may be able to automatically fix some errors by running',
Expand All @@ -111,59 +119,56 @@ function runPrettify(filesToCheck) {
yellow('NOTE 3:'),
'For more information, read',
cyan(
'https://github.com/ampproject/amphtml/blob/master/contributing/getting-started-e2e.md#code-quality-and-style'
'https://github.com/ampproject/amphtml/blob/master/contributing/getting-started-e2e.md#code-quality-and-style\n'
)
);
}
} else {
if (!isTravisBuild()) {
logOnSameLine(green('SUCCESS: ') + 'No formatting errors found.');
}
}
}
};

/**
* Fixes the formatting of a single file
* @param {string} file
*/
function fixFile(file) {
const fixCmd = `${prettierCmd} --write ${file}`;
const fixResult = getOutput(fixCmd);
if (fixResult.status == PrettierResult.SUCCESS) {
logOnSameLine(green('Fixed: ') + file + '\n');
} else {
logOnSameLine(red('Could not fix: ') + file);
console.log(highlight(fixResult.stderr, {ignoreIllegals: true}), '\n');
process.exitCode = 1;
}
}
const onError = error => {
if (error.message.startsWith(header)) {
const filesWithErrors = error.message
.replace(header, '')
.trim()
.split('\n');
const reason = 'Found formatting errors in one or more files';
logOnSameLine(red('ERROR: ') + reason + ':');
filesWithErrors.forEach(file => {
log(cyan(file));
});
printFixMessages();
rejectWithReason(reason);
} else {
const reason = 'Found an unrecoverable error';
logOnSameLine(red('ERROR: ') + reason + ':');
log(error.message);
rejectWithReason(reason);
}
};

const onFinish = () => {
if (!isTravisBuild()) {
logOnSameLine('Checked ' + cyan(filesToCheck.length) + ' file(s)');
}
resolve();
};

/**
* Checks and optionally fixes the formatting of a single file.
* @param {string} file
*/
function checkFile(file) {
const checkCmd = `${prettierCmd} --list-different ${file}`;
const checkResult = getOutput(checkCmd);
if (checkResult.status == PrettierResult.SUCCESS) {
if (!isTravisBuild()) {
logOnSameLine(green('Checked: ') + file);
}
} else if (checkResult.status == PrettierResult.FAILURE) {
if (argv.fix) {
fixFile(file);
return gulp
.src(filesToCheck)
.pipe(prettier())
.on('data', onData)
.on('error', onError)
.pipe(gulp.dest(file => file.base))
.on('finish', onFinish);
} else {
logOnSameLine(red('Found errors in: ') + file + '\n');
process.exitCode = 1;
return gulp
.src(filesToCheck)
.pipe(prettier.check())
.on('data', onData)
.on('error', onError)
.on('finish', onFinish);
}
} else {
logOnSameLine(red('Could not parse: ') + file);
console.log(
highlight(checkResult.stderr.trim(), {ignoreIllegals: true}),
'\n'
);
process.exitCode = 1;
}
});
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -105,6 +105,7 @@
"gulp-jsonlint": "1.3.1",
"gulp-jsonminify": "1.1.0",
"gulp-nop": "0.0.3",
"gulp-prettier": "2.1.0",
"gulp-regexp-sourcemaps": "1.0.1",
"gulp-rename": "1.4.0",
"gulp-sourcemaps": "2.6.5",
Expand Down
12 changes: 11 additions & 1 deletion yarn.lock
Expand Up @@ -6244,6 +6244,16 @@ gulp-nop@0.0.3:
gulp-util "~2.2.14"
through "~2.3.4"

gulp-prettier@2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/gulp-prettier/-/gulp-prettier-2.1.0.tgz#6e7b2a12395315fb9e1ebd4ea9ed9514410aec15"
integrity sha512-6PvGPX+x0d1+PbP7tHF42o6zWzxCXqouTnpwZV1GjF47/wAgWBfPU1E6/6d4uAGM+NhmwWdKvIVumL3wMZZxDg==
dependencies:
plugin-error "^1.0.1"
prettier "^1.5.3"
safe-buffer "^5.1.2"
through2 "^3.0.0"

gulp-regexp-sourcemaps@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/gulp-regexp-sourcemaps/-/gulp-regexp-sourcemaps-1.0.1.tgz#a8812244852c10950683a948e1c0dad0b1249979"
Expand Down Expand Up @@ -10770,7 +10780,7 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

prettier@1.18.2:
prettier@1.18.2, prettier@^1.5.3:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
Expand Down