Skip to content

Commit

Permalink
feat(cli): prefer 'lint' and 'lint:ci' scripts when running 'compas l…
Browse files Browse the repository at this point in the history
…int'
  • Loading branch information
dirkdev98 committed Jul 11, 2024
1 parent 213e6be commit 127d73b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/references/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ ESLint is used for all JavaScript files and Prettier runs on JavaScript, JSON,
Markdown, and YAML files. The default configuration can be initialized via
'compas init --lint-config'.

If the 'lint' (or 'lint:ci') script exists, they are preferred over manually
running ESLint and Prettier.

| Option | Description |
| ----------------------- | -------------------------------------------------------------------------- |
| --skip-prettier | Skip running Prettier. (boolean) |
Expand Down
22 changes: 21 additions & 1 deletion packages/cli/src/compas/commands/lint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { environment, spawn } from "@compas/stdlib";
import { environment, isNil, spawn } from "@compas/stdlib";
import { collectPackageScripts } from "../../utils.js";

/**
* @type {import("../../generated/common/types.js").CliCommandDefinitionInput}
Expand All @@ -10,6 +11,8 @@ export const cliDefinition = {
ESLint is used for all JavaScript files and Prettier runs on JavaScript, JSON, Markdown, and YAML files.
The default configuration can be initialized via 'compas init --lint-config'.
If the 'lint' (or 'lint:ci') script exists, they are preferred over manually running ESLint and Prettier.
`,
flags: [
{
Expand Down Expand Up @@ -46,6 +49,23 @@ The default configuration can be initialized via 'compas init --lint-config'.
*/
export async function cliExecutor(logger, state) {
let exitCode = 0;
const scripts = collectPackageScripts();
const isCi = environment.CI === "true";
const script = isCi ? "lint:ci" : "lint";

if (!isNil(scripts[script])) {
const { exitCode } = await spawn(`npm`, ["run", script]);

if (exitCode !== 0) {
return {
exitStatus: "failed",
};
}

return {
exitStatus: "passed",
};
}

if (state.flags.skipEslint !== true) {
logger.info("Running ESLint...");
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import { pathJoin } from "@compas/stdlib";
* @returns {ScriptCollection}
*/
export function collectScripts() {
/** @type {ScriptCollection} */
return {
...collectUserScripts(),
...collectPackageScripts(),
};
}

function collectUserScripts() {
/** @type {ScriptCollection} */
const result = {};

Expand All @@ -43,6 +51,16 @@ export function collectScripts() {
}
}

return result;
}

/**
* @returns {ScriptCollection}
*/
export function collectPackageScripts() {
/** @type {ScriptCollection} */
const result = {};

const pkgJsonPath = pathJoin(process.cwd(), "package.json");
if (existsSync(pkgJsonPath)) {
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
Expand Down

0 comments on commit 127d73b

Please sign in to comment.