diff --git a/.yarnrc b/.yarnrc index 7946fb1575..bc9f9c0fba 100644 --- a/.yarnrc +++ b/.yarnrc @@ -1 +1,2 @@ unsafe-disable-integrity-migration "false" +--run.silent true diff --git a/bin/format.js b/bin/format.js new file mode 100644 index 0000000000..fba005460f --- /dev/null +++ b/bin/format.js @@ -0,0 +1,21 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This is a script which can be run with or without --fix. +// It will run prettier with the right flags. + +const { spawnSync } = require('child_process'); + +const PRETTIER_FLAGS = [ + '--cache', + ...['--cache-strategy', 'content'], + ...['--cache-location', '.prettiercache'], + ...['--log-level', 'warn'], +]; + +const shouldFix = process.argv.includes('--fix'); +const writeOrCheck = shouldFix ? '--write' : '--check'; +const args = [writeOrCheck, '.', ...PRETTIER_FLAGS]; +const result = spawnSync('prettier', args, { stdio: 'inherit' }); +process.exitCode = result.status; diff --git a/bin/lint-or-lint-fix.js b/bin/lint-or-lint-fix.js new file mode 100644 index 0000000000..bffad76f6d --- /dev/null +++ b/bin/lint-or-lint-fix.js @@ -0,0 +1,10 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const { spawnSync } = require('child_process'); + +const shouldFix = process.argv.includes('--fix'); +const scriptToRun = shouldFix ? 'lint-fix' : 'lint-internal'; +const result = spawnSync('yarn', [scriptToRun], { stdio: 'inherit' }); +process.exitCode = result.status; diff --git a/bin/output-fixing-commands.js b/bin/output-fixing-commands.js deleted file mode 100644 index 262437fd81..0000000000 --- a/bin/output-fixing-commands.js +++ /dev/null @@ -1,40 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -// This file runs before linting commands and intercept errors so that more -// friendly errors can be output. - -const cp = require('child_process'); - -const fixingCommands = { - lint: 'lint-fix', - 'lint-js': 'lint-fix-js', - 'lint-css': 'lint-fix-css', - 'prettier-run': 'prettier-fix', - test: 'test -u', -}; - -const command = process.argv.slice(2); -const currentScriptName = process.env.npm_lifecycle_event; - -// Redirect the main lint command, but not individual commands. -if (currentScriptName === 'lint' && command.includes('--fix')) { - console.log(`🔧 Detected --fix flag, running: yarn lint-fix`); - const result = cp.spawnSync('yarn', ['lint-fix'], { stdio: 'inherit' }); - process.exitCode = result.status; - process.exit(); -} - -const result = cp.spawnSync(command[0], command.slice(1), { stdio: 'inherit' }); - -if (result.status !== 0) { - process.exitCode = result.status; - if (currentScriptName && currentScriptName in fixingCommands) { - console.log( - '💡 You might be able to fix the error by running `yarn ' + - fixingCommands[currentScriptName] + - '`' - ); - } -} diff --git a/bin/suggest-fixing-command.js b/bin/suggest-fixing-command.js new file mode 100644 index 0000000000..a0ae760d3b --- /dev/null +++ b/bin/suggest-fixing-command.js @@ -0,0 +1,25 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This file runs before linting commands and intercept errors so that more +// friendly errors can be output. + +const cp = require('child_process'); + +const scriptToRun = process.argv[2]; +const scriptToSuggest = process.argv[3]; +const extraArgs = process.argv.slice(4); + +const result = cp.spawnSync('yarn', [scriptToRun, ...extraArgs], { + stdio: 'inherit', +}); + +if (result.status !== 0) { + process.exitCode = result.status; + console.log( + '💡 You might be able to fix the error by running `yarn ' + + scriptToSuggest + + '`' + ); +} diff --git a/package.json b/package.json index f18daa7e6d..6624bb4f51 100644 --- a/package.json +++ b/package.json @@ -21,14 +21,20 @@ "build-photon": "webpack --config res/photon/webpack.config.js", "build-symbolicator-cli": "yarn build-symbolicator-cli:quiet --progress", "build-symbolicator-cli:quiet": "yarn build:clean && cross-env NODE_ENV=production webpack --config src/symbolicator-cli/webpack.config.js", - "lint": "node bin/output-fixing-commands.js run-p lint-js lint-css prettier-run", - "lint-fix": "run-p lint-fix-js lint-fix-css prettier-fix", - "lint-js": "node bin/output-fixing-commands.js eslint *.js bin src --report-unused-disable-directives --cache --cache-strategy content", - "lint-fix-js": "yarn lint-js --fix", - "lint-css": "node bin/output-fixing-commands.js stylelint \"src/**/*.css\" \"res/**/*.css\"", - "lint-fix-css": "yarn lint-css --fix", - "prettier-run": "node bin/output-fixing-commands.js prettier --check . --cache --cache-strategy content --cache-location .prettiercache", - "prettier-fix": "prettier --write . --cache --cache-strategy content --cache-location .prettiercache", + "lint": "node bin/suggest-fixing-command.js lint-or-lint-fix-internal 'lint --fix'", + "lint-js": "node bin/suggest-fixing-command.js lint-internal:js 'lint-js --fix'", + "lint-css": "node bin/suggest-fixing-command.js lint-internal:css 'lint-css --fix'", + "prettier-run": "node bin/suggest-fixing-command.js lint-internal:format fmt", + "fmt": "yarn lint-fix:format", + "lint-or-lint-fix-internal": "node bin/lint-or-lint-fix.js", + "lint-internal": "run-p lint-internal:**", + "lint-internal:js": "eslint *.js bin src --report-unused-disable-directives --cache --cache-strategy content", + "lint-internal:css": "stylelint \"src/**/*.css\" \"res/**/*.css\"", + "lint-internal:format": "node bin/format.js", + "lint-fix": "run-p lint-fix:**", + "lint-fix:js": "yarn lint-internal:js --fix", + "lint-fix:css": "yarn lint-internal:css --fix", + "lint-fix:format": "yarn lint-internal:format --fix", "ts": "tsc", "protoc": "npx -p protobufjs-cli pbjs -t static-module -w commonjs -o ./src/profile-logic/import/proto/simpleperf_report.js ./src/profile-logic/import/proto/simpleperf_report.proto && npx -p protobufjs-cli pbts -o ./src/profile-logic/import/proto/simpleperf_report.d.ts ./src/profile-logic/import/proto/simpleperf_report.js", "license-check": "devtools-license-check", @@ -42,11 +48,12 @@ "start-examples": "ws -d examples/ -s index.html -p 4244", "start-docs": "ws -d docs-user/ -p 3000", "start-photon": "node res/photon/server", - "test": "node bin/output-fixing-commands.js cross-env LC_ALL=C TZ=UTC NODE_ENV=test jest", + "test": "node bin/suggest-fixing-command.js test-internal 'test -u'", + "test-internal": "cross-env LC_ALL=C TZ=UTC NODE_ENV=test jest", "test-all": "run-p --max-parallel 4 ts license-check lint test test-alex test-lockfile", - "test-build-coverage": "yarn test --coverage --coverageReporters=html", - "test-serve-coverage": "ws -d coverage/ -p 4343", - "test-coverage": "run-s test-build-coverage test-serve-coverage", + "test-coverage": "run-s test-coverage:**", + "test-coverage:build": "yarn test --coverage --coverageReporters=html", + "test-coverage:serve": "ws -d coverage/ -p 4343", "test-alex": "alex ./docs-* CODE_OF_CONDUCT.md CONTRIBUTING.md README.md", "test-lockfile": "lockfile-lint --path yarn.lock --allowed-hosts yarn --validate-https", "test-debug": "cross-env LC_ALL=C TZ=UTC NODE_ENV=test node --inspect-brk node_modules/.bin/jest --runInBand",