Commit bb454395 authored by Phil Hughes's avatar Phil Hughes

Merge branch '44581-meta-use-prettier-ignore-file-in-our-save-script' into 'master'

Make use of the prettier ignore file during our save script

Closes #44581

See merge request gitlab-org/gitlab-ce!17960
parents 391732a2 d023244a
const glob = require('glob');
const prettier = require('prettier');
const fs = require('fs');
const path = require('path');
const prettierIgnore = require('ignore')();
const getStagedFiles = require('./frontend_script_utils').getStagedFiles;
......@@ -10,6 +12,10 @@ const allFiles = mode === 'check-all' || mode === 'save-all';
const config = {
patterns: ['**/*.js', '**/*.vue', '**/*.scss'],
/*
* The ignore patterns below are just to reduce search time with glob, as it includes the
* folders with the most ignored assets, the actual `.prettierignore` will be used later on
*/
ignore: ['**/node_modules/**', '**/vendor/**', '**/public/**'],
parsers: {
js: 'babylon',
......@@ -17,6 +23,20 @@ const config = {
scss: 'css',
},
};
/*
* Unfortunately the prettier API does not expose support for `.prettierignore` files, they however
* use the ignore package, so we do the same. We simply cannot use the glob package, because
* gitignore style is not compatible with globs ignore style.
*/
prettierIgnore.add(
fs
.readFileSync(path.join(__dirname, '../../', '.prettierignore'))
.toString()
.trim()
.split(/\r?\n/)
);
const availableExtensions = Object.keys(config.parsers);
console.log(`Loading ${allFiles ? 'All' : 'Staged'} Files ...`);
......@@ -44,6 +64,8 @@ if (allFiles) {
files = stagedFiles.filter(f => availableExtensions.includes(f.split('.').pop()));
}
files = prettierIgnore.filter(files);
if (!files.length) {
console.log('No Files found to process with Prettier');
return;
......
......@@ -4232,7 +4232,7 @@ ignore@^3.2.0:
version "3.3.3"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.3.tgz#432352e57accd87ab3110e82d3fea0e47812156d"
ignore@^3.3.5:
ignore@^3.3.5, ignore@^3.3.7:
version "3.3.7"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment