Skip to content

Commit 127d73b

Browse files
committed
feat(cli): prefer 'lint' and 'lint:ci' scripts when running 'compas lint'
1 parent 213e6be commit 127d73b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

docs/references/cli.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ ESLint is used for all JavaScript files and Prettier runs on JavaScript, JSON,
172172
Markdown, and YAML files. The default configuration can be initialized via
173173
'compas init --lint-config'.
174174

175+
If the 'lint' (or 'lint:ci') script exists, they are preferred over manually
176+
running ESLint and Prettier.
177+
175178
| Option | Description |
176179
| ----------------------- | -------------------------------------------------------------------------- |
177180
| --skip-prettier | Skip running Prettier. (boolean) |

packages/cli/src/compas/commands/lint.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { environment, spawn } from "@compas/stdlib";
1+
import { environment, isNil, spawn } from "@compas/stdlib";
2+
import { collectPackageScripts } from "../../utils.js";
23

34
/**
45
* @type {import("../../generated/common/types.js").CliCommandDefinitionInput}
@@ -10,6 +11,8 @@ export const cliDefinition = {
1011
1112
ESLint is used for all JavaScript files and Prettier runs on JavaScript, JSON, Markdown, and YAML files.
1213
The default configuration can be initialized via 'compas init --lint-config'.
14+
15+
If the 'lint' (or 'lint:ci') script exists, they are preferred over manually running ESLint and Prettier.
1316
`,
1417
flags: [
1518
{
@@ -46,6 +49,23 @@ The default configuration can be initialized via 'compas init --lint-config'.
4649
*/
4750
export async function cliExecutor(logger, state) {
4851
let exitCode = 0;
52+
const scripts = collectPackageScripts();
53+
const isCi = environment.CI === "true";
54+
const script = isCi ? "lint:ci" : "lint";
55+
56+
if (!isNil(scripts[script])) {
57+
const { exitCode } = await spawn(`npm`, ["run", script]);
58+
59+
if (exitCode !== 0) {
60+
return {
61+
exitStatus: "failed",
62+
};
63+
}
64+
65+
return {
66+
exitStatus: "passed",
67+
};
68+
}
4969

5070
if (state.flags.skipEslint !== true) {
5171
logger.info("Running ESLint...");

packages/cli/src/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ import { pathJoin } from "@compas/stdlib";
1919
* @returns {ScriptCollection}
2020
*/
2121
export function collectScripts() {
22+
/** @type {ScriptCollection} */
23+
return {
24+
...collectUserScripts(),
25+
...collectPackageScripts(),
26+
};
27+
}
28+
29+
function collectUserScripts() {
2230
/** @type {ScriptCollection} */
2331
const result = {};
2432

@@ -43,6 +51,16 @@ export function collectScripts() {
4351
}
4452
}
4553

54+
return result;
55+
}
56+
57+
/**
58+
* @returns {ScriptCollection}
59+
*/
60+
export function collectPackageScripts() {
61+
/** @type {ScriptCollection} */
62+
const result = {};
63+
4664
const pkgJsonPath = pathJoin(process.cwd(), "package.json");
4765
if (existsSync(pkgJsonPath)) {
4866
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));

0 commit comments

Comments
 (0)