Skip to content

Commit

Permalink
Add exit code util
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Feb 8, 2024
1 parent 856e8b6 commit 2984c12
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .changeset/nervous-carrots-yell.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@arethetypeswrong/cli": patch
---

Expose internal renderer API
Expose internal renderer and exit code API
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"devDependencies": {
"@changesets/cli": "^2.27.1",
"prettier": "^3.0.3",
"typescript": "5.3.3"
"typescript": "5.4.0-dev.20240207"
},
"engines": {
"node": ">=18",
"pnpm": ">=8"
},
"pnpm": {
"overrides": {
"ts-expose-internals-conditionally": "$typescript"
"ts-expose-internals-conditionally": "5.3.3"
}
}
}
5 changes: 3 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"attw": "./dist/index.js"
},
"exports": {
"./internal/getExitCode": "./dist/getExitCode.js",
"./internal/render": "./dist/render/index.js"
},
"publishConfig": {
Expand All @@ -47,8 +48,8 @@
"@types/marked-terminal": "^3.1.3",
"@types/node": "^20.2.5",
"@types/semver": "^7.5.3",
"ts-expose-internals": "5.3.2",
"typescript": "5.3.2"
"ts-expose-internals-conditionally": "1.0.0-empty.0",
"typescript": "5.4.0-dev.20240207"
},
"dependencies": {
"@arethetypeswrong/core": "0.13.6",
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/getExitCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { CheckResult } from "@arethetypeswrong/core";
import { problemFlags } from "./problemUtils.js";
import type { RenderOptions } from "./render/index.js";

export function getExitCode(analysis: CheckResult, opts?: RenderOptions): number {
if (!analysis.types) {
return 0;
}
if (!opts?.ignoreRules) {
return analysis.problems.length > 0 ? 1 : 0;
}
return analysis.problems.some((problem) => !opts.ignoreRules!.includes(problemFlags[problem.kind])) ? 1 : 0;
}
22 changes: 10 additions & 12 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { problemFlags } from "./problemUtils.js";
import { readConfig } from "./readConfig.js";
import * as render from "./render/index.js";
import { major, minor } from "semver";
import { getExitCode } from "./getExitCode.js";

const packageJson = createRequire(import.meta.url)("../package.json");
const version = packageJson.version;
Expand Down Expand Up @@ -206,25 +207,22 @@ particularly ESM-related module resolution issues.`,

console.log(JSON.stringify(result));

if (
analysis.types &&
analysis.problems.some((problem) => !opts.ignoreRules?.includes(problemFlags[problem.kind]))
)
process.exit(1);
if (deleteTgz) {
await unlink(deleteTgz);
}

const exitCode = getExitCode(analysis, opts);
if (exitCode) {
process.exit(exitCode);
}

return;
}

console.log();
if (analysis.types) {
console.log(await render.typed(analysis, opts));

if (
analysis.types &&
analysis.problems.some((problem) => !opts.ignoreRules?.includes(problemFlags[problem.kind]))
) {
process.exitCode = 1;
}
process.exitCode = getExitCode(analysis, opts);
} else {
console.log(render.untyped(analysis as core.UntypedResult));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"module": "nodenext",
"rootDir": "src",
"types": ["ts-expose-internals", "node"],
"types": ["ts-expose-internals-conditionally", "node"],
"outDir": "./dist",
"sourceMap": true
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"fflate": "^0.7.4",
"semver": "^7.5.4",
"ts-expose-internals-conditionally": "1.0.0-empty.0",
"typescript": "5.3.3",
"typescript": "5.4.0-dev.20240207",
"validate-npm-package-name": "^5.0.0"
},
"devDependencies": {
Expand Down
31 changes: 13 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2984c12

Please sign in to comment.