summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Eipert <git@leipert.io>2018-03-23 16:21:03 +0100
committerLukas Eipert <git@leipert.io>2018-03-23 19:35:12 +0100
commitb24609b4ca35e4677a082170c969e206d2e751eb (patch)
tree11b2b2bb3e99458390cf5bdb98d8be313b42f4d0
parenta6507eed909093ed27b4f186f47311e610f08c06 (diff)
downloadgitlab-ce-b24609b4ca35e4677a082170c969e206d2e751eb.tar.gz
ignore files listed in the prettierignore file
-rw-r--r--package.json1
-rw-r--r--scripts/frontend/prettier.js45
-rw-r--r--yarn.lock2
3 files changed, 40 insertions, 8 deletions
diff --git a/package.json b/package.json
index c81020f631e..56fd2575e91 100644
--- a/package.json
+++ b/package.json
@@ -107,6 +107,7 @@
"eslint-plugin-jasmine": "^2.1.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-vue": "^4.0.1",
+ "ignore": "^3.3.7",
"istanbul": "^0.4.5",
"jasmine-core": "^2.9.0",
"jasmine-jquery": "^2.1.1",
diff --git a/scripts/frontend/prettier.js b/scripts/frontend/prettier.js
index 2708340b48e..5b185b8a70a 100644
--- a/scripts/frontend/prettier.js
+++ b/scripts/frontend/prettier.js
@@ -1,6 +1,9 @@
+/* eslint import/no-commonjs: "off", import/no-extraneous-dependencies: "off", no-console: "off" */
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,18 +13,33 @@ const allFiles = mode === 'check-all' || mode === 'save-all';
const config = {
patterns: ['**/*.js', '**/*.vue', '**/*.scss'],
- ignore: ['**/node_modules/**', '**/vendor/**', '**/public/**'],
parsers: {
js: 'babylon',
vue: 'vue',
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 ...`);
-const stagedFiles = allFiles ? null : getStagedFiles(availableExtensions.map(ext => `*.${ext}`));
+const stagedFiles = allFiles
+ ? null
+ : getStagedFiles(availableExtensions.map(ext => `*.${ext}`));
if (stagedFiles) {
if (!stagedFiles.length || (stagedFiles.length === 1 && !stagedFiles[0])) {
@@ -35,15 +53,28 @@ let didWarn = false;
let didError = false;
let files;
+
+// Read
if (allFiles) {
- const ignore = config.ignore;
const patterns = config.patterns;
- const globPattern = patterns.length > 1 ? `{${patterns.join(',')}}` : `${patterns.join(',')}`;
- files = glob.sync(globPattern, { ignore }).filter(f => allFiles || stagedFiles.includes(f));
+ /*
+ * The ignore patterns below are just to reduce search time on the system, as it includes the
+ * folders with the most ignored assets, the actual prettierignore will be used later on
+ */
+ const ignore = ['**/node_modules/**', '**/vendor/**', '**/public/**'];
+ const globPattern =
+ patterns.length > 1 ? `{${patterns.join(',')}}` : `${patterns.join(',')}`;
+ files = glob
+ .sync(globPattern, { ignore })
+ .filter(f => allFiles || stagedFiles.includes(f));
} else {
- files = stagedFiles.filter(f => availableExtensions.includes(f.split('.').pop()));
+ 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;
@@ -73,7 +104,7 @@ prettier
} else if (!prettier.check(input, options)) {
if (!didWarn) {
console.log(
- '\n===============================\nGitLab uses Prettier to format all JavaScript code.\nPlease format each file listed below or run "yarn prettier-staged-save"\n===============================\n'
+ '\n===============================\nGitLab uses Prettier to format all JavaScript code.\nPlease format each file listed below or run "yarn prettier-staged-save"\n===============================\n',
);
didWarn = true;
}
diff --git a/yarn.lock b/yarn.lock
index 36683a2a480..af7bda5d562 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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"