diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-17 18:09:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-17 18:09:21 +0300 |
commit | c982bb363b3a0390a274197f410a1609a4667760 (patch) | |
tree | 8be9521106e8e9af432d179f03c5b3af11a0e207 /scripts | |
parent | 75a4eaade04ee758bb3b253f27bf1c20c67991f0 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/frontend/execute-on-staged-files.sh | 25 | ||||
-rw-r--r-- | scripts/frontend/frontend_script_utils.js | 20 | ||||
-rw-r--r-- | scripts/frontend/prettier.js | 121 | ||||
-rwxr-xr-x | scripts/static-analysis | 4 |
4 files changed, 27 insertions, 143 deletions
diff --git a/scripts/frontend/execute-on-staged-files.sh b/scripts/frontend/execute-on-staged-files.sh new file mode 100755 index 00000000000..f218926f098 --- /dev/null +++ b/scripts/frontend/execute-on-staged-files.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +# The yarn run command we'd like to run +command="$1" +# The file types we'd like to target, use something like '(js|vue)' +file_types="$2" + +# Removing first two arguments +shift +shift + +# Read all staged non-deleted files into an array +staged_files=() +while IFS= read -r line; do + staged_files+=( "$line" ) +done < <( git diff --diff-filter=d --cached --name-only | { grep -E ".$file_types$" || true; }) + +if [ "${#staged_files[@]}" == "0" ]; then + echo "No staged '$file_types' files" +else + echo "Running $command on ${#staged_files[@]} staged '$file_types' files" + yarn run "$command" "$@" "${staged_files[@]}" +fi diff --git a/scripts/frontend/frontend_script_utils.js b/scripts/frontend/frontend_script_utils.js deleted file mode 100644 index 43016dce6a4..00000000000 --- a/scripts/frontend/frontend_script_utils.js +++ /dev/null @@ -1,20 +0,0 @@ -const execFileSync = require('child_process').execFileSync; - -const exec = (command, args) => { - const options = { - cwd: process.cwd(), - env: process.env, - encoding: 'utf-8', - }; - return execFileSync(command, args, options); -}; - -const execGitCmd = (args) => exec('git', args).trim().toString().split('\n').filter(Boolean); - -module.exports = { - getStagedFiles: (fileExtensionFilter) => { - const gitOptions = ['diff', '--name-only', '--cached', '--diff-filter=ACMRTUB']; - if (fileExtensionFilter) gitOptions.push(...fileExtensionFilter); - return execGitCmd(gitOptions); - }, -}; diff --git a/scripts/frontend/prettier.js b/scripts/frontend/prettier.js deleted file mode 100644 index f721e46f36b..00000000000 --- a/scripts/frontend/prettier.js +++ /dev/null @@ -1,121 +0,0 @@ -const glob = require('glob'); -const prettier = require('prettier'); -const fs = require('fs'); -const { getStagedFiles } = require('./frontend_script_utils'); - -const matchExtensions = ['js', 'vue', 'graphql']; - -// This will improve glob performance by excluding certain directories. -// The .prettierignore file will also be respected, but after the glob has executed. -const globIgnore = ['**/node_modules/**', 'vendor/**', 'public/**', 'fixtures/**']; - -const readFileAsync = (file, options) => - new Promise((resolve, reject) => { - fs.readFile(file, options, function (err, data) { - if (err) reject(err); - else resolve(data); - }); - }); - -const writeFileAsync = (file, data, options) => - new Promise((resolve, reject) => { - fs.writeFile(file, data, options, function (err) { - if (err) reject(err); - else resolve(); - }); - }); - -const mode = process.argv[2] || 'check'; -const shouldSave = mode === 'save' || mode === 'save-all'; -const allFiles = mode === 'check-all' || mode === 'save-all'; -let globDir = process.argv[3] || ''; -if (globDir && globDir.charAt(globDir.length - 1) !== '/') globDir += '/'; - -console.log( - `Loading all ${allFiles ? '' : 'staged '}files ${globDir ? `within ${globDir} ` : ''}...`, -); - -const globPatterns = matchExtensions.map((ext) => `${globDir}**/*.${ext}`); -const matchedFiles = allFiles - ? glob.sync(`{${globPatterns.join(',')}}`, { ignore: globIgnore }) - : getStagedFiles(globPatterns); -const matchedCount = matchedFiles.length; - -if (!matchedCount) { - console.log('No files found to process with prettier'); - process.exit(0); -} - -let didWarn = false; -let passedCount = 0; -let failedCount = 0; -let ignoredCount = 0; - -console.log(`${shouldSave ? 'Updating' : 'Checking'} ${matchedCount} file(s)`); - -const fixCommand = `yarn prettier-${allFiles ? 'all' : 'staged'}-save`; -const warningMessage = ` -=============================== -GitLab uses Prettier to format all JavaScript code. -Please format each file listed below or run "${fixCommand}" -=============================== -`; - -const checkFileWithOptions = (filePath, options) => - readFileAsync(filePath, 'utf8').then((input) => { - if (shouldSave) { - const output = prettier.format(input, options); - if (input === output) { - passedCount += 1; - } else { - return writeFileAsync(filePath, output, 'utf8').then(() => { - console.log(`Prettified : ${filePath}`); - failedCount += 1; - }); - } - } else { - if (prettier.check(input, options)) { - passedCount += 1; - } else { - if (!didWarn) { - // \x1b[31m make text red - // \x1b[1m make text bold - // %s warningMessage - // \x1b[0m reset text color (so logs after aren't red) - const redBoldText = '\x1b[1m\x1b[31;1m%s\x1b[0m'; - console.log(redBoldText, warningMessage); - didWarn = true; - } - console.log(`yarn prettier --write ${filePath}`); - failedCount += 1; - } - } - }); - -const checkFileWithPrettierConfig = (filePath) => - prettier - .getFileInfo(filePath, { ignorePath: '.prettierignore' }) - .then(({ ignored, inferredParser }) => { - if (ignored || !inferredParser) { - ignoredCount += 1; - return; - } - return prettier.resolveConfig(filePath).then((fileOptions) => { - const options = { ...fileOptions, parser: inferredParser }; - return checkFileWithOptions(filePath, options); - }); - }); - -Promise.all(matchedFiles.map(checkFileWithPrettierConfig)) - .then(() => { - const failAction = shouldSave ? 'fixed' : 'failed'; - console.log( - `\nSummary:\n ${matchedCount} files processed (${passedCount} passed, ${failedCount} ${failAction}, ${ignoredCount} ignored)\n`, - ); - - if (didWarn) process.exit(1); - }) - .catch((e) => { - console.log(`\nAn error occurred while processing files with prettier: ${e.message}\n`); - process.exit(1); - }); diff --git a/scripts/static-analysis b/scripts/static-analysis index febfdbd1da4..0f983840d5e 100755 --- a/scripts/static-analysis +++ b/scripts/static-analysis @@ -25,8 +25,8 @@ class StaticAnalysis # Most of the time, RuboCop finishes in 30 seconds, but sometimes it can take around 1200 seconds so we set a # duration of 300 to lower the likelihood that it will run in the same job as another long task... %w[bundle exec rubocop --parallel] => 300, - %w[yarn run eslint] => 197, - %w[yarn run prettier-all] => 124, + %w[yarn run lint:eslint] => 197, + %w[yarn run lint:prettier] => 124, %w[bin/rake gettext:lint] => 96, %w[bundle exec license_finder] => 49, %w[bin/rake scss_lint] => 38, |