@zirkelc/typecheck 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Chris
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @zirkelc/typecheck
2
+
3
+ TypeScript type checker for monorepo packages. Runs `tsc --noEmit` using the TypeScript compiler API and filters out diagnostics from files outside the current package directory.
4
+
5
+ This solves the problem of [internal packages](https://turborepo.dev/docs/core-concepts/internal-packages) exporting raw `.ts` files:
6
+
7
+ > Errors in internal dependencies will be reported: When directly exporting TypeScript, type-checking in a dependent package will fail if code in an internal dependency has TypeScript errors. You may find this confusing or problematic in some situations.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ pnpm add -D @zirkelc/typecheck
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Add a script to your monorepo packages `package.json`:
18
+
19
+ ```json
20
+ {
21
+ "scripts": {
22
+ "typecheck": "typecheck"
23
+ }
24
+ }
25
+ ```
26
+
27
+ Then run in your monorepo packages:
28
+
29
+ ```sh
30
+ # Uses tsconfig.json by default
31
+ pnpm typecheck
32
+
33
+ # Use a custom tsconfig
34
+ pnpm typecheck --project tsconfig.build.json
35
+ ```
36
+
37
+ ## How it works
38
+
39
+ 1. Reads the `tsconfig.json` (or `--project` target) in `process.cwd()`
40
+ 2. Creates a `ts.Program` and collects all diagnostics via `ts.getPreEmitDiagnostics()`
41
+ 3. Filters diagnostics: only keeps errors where the source file is inside the current directory
42
+ 4. Formats remaining errors with colors and code context using `ts.formatDiagnosticsWithColorAndContext()`
43
+ 5. Exits with code 1 if local errors exist, 0 otherwise
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env node
2
+ import * as path from "node:path";
3
+ import * as ts from "typescript";
4
+
5
+ //#region src/typecheck.ts
6
+ const cwd = process.cwd();
7
+ const isCI = process.env.GITHUB_ACTIONS === `true`;
8
+ const formatHost = {
9
+ getCanonicalFileName: (f) => f,
10
+ getCurrentDirectory: () => cwd,
11
+ getNewLine: () => ts.sys.newLine
12
+ };
13
+ /** Parse --project flag, default to tsconfig.json */
14
+ const projectArgIndex = process.argv.indexOf(`--project`);
15
+ const tsconfigName = projectArgIndex !== -1 ? process.argv[projectArgIndex + 1] : `tsconfig.json`;
16
+ /** Find and parse tsconfig */
17
+ const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigName);
18
+ if (!configPath) {
19
+ console.error(`Could not find ${tsconfigName} in ${cwd}`);
20
+ process.exit(1);
21
+ }
22
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
23
+ if (configFile.error) {
24
+ console.error(ts.formatDiagnosticsWithColorAndContext([configFile.error], formatHost));
25
+ process.exit(1);
26
+ }
27
+ const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, cwd);
28
+ /** Create program and collect diagnostics */
29
+ const program = ts.createProgram(parsed.fileNames, parsed.options);
30
+ const diagnostics = ts.getPreEmitDiagnostics(program);
31
+ /**
32
+ * Filter: keep only diagnostics from files inside cwd.
33
+ * Use cwd + path.sep to avoid /packages/api matching /packages/api-chat.
34
+ * Diagnostics without a file (global/config errors) are always kept.
35
+ */
36
+ const cwdPrefix = cwd + path.sep;
37
+ const filtered = diagnostics.filter((d) => {
38
+ if (!d.file) return true;
39
+ return path.resolve(d.file.fileName).startsWith(cwdPrefix);
40
+ });
41
+ const externalCount = diagnostics.length - filtered.length;
42
+ if (filtered.length > 0) {
43
+ if (isCI) for (const d of filtered) {
44
+ const message = ts.flattenDiagnosticMessageText(d.messageText, ` `);
45
+ if (d.file && d.start !== void 0) {
46
+ const { line, character } = d.file.getLineAndCharacterOfPosition(d.start);
47
+ const relPath = path.relative(cwd, d.file.fileName);
48
+ console.log(`::error file=${relPath},line=${line + 1},col=${character + 1}::TS${d.code}: ${message}`);
49
+ } else console.log(`::error ::TS${d.code}: ${message}`);
50
+ }
51
+ console.log(ts.formatDiagnosticsWithColorAndContext(filtered, formatHost));
52
+ const parts = [`Found ${filtered.length} error(s)`];
53
+ if (externalCount > 0) parts.push(`(${externalCount} external error(s) filtered)`);
54
+ console.log(parts.join(` `));
55
+ process.exit(1);
56
+ }
57
+ if (externalCount > 0) console.log(`No errors in current package. ${externalCount} external error(s) filtered from dependencies.`);
58
+ else console.log(`No errors found.`);
59
+
60
+ //#endregion
61
+ export { };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@zirkelc/typecheck",
3
+ "version": "1.0.0",
4
+ "description": "Typecheck for Monorepos",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "bin": {
10
+ "typecheck": "./dist/typecheck.mjs"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "keywords": [],
16
+ "author": "Chris Cook",
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/zirkelc/typecheck"
21
+ },
22
+ "peerDependencies": {
23
+ "typescript": "^5.9.3"
24
+ },
25
+ "devDependencies": {
26
+ "@arethetypeswrong/cli": "^0.18.2",
27
+ "@biomejs/biome": "^2.3.14",
28
+ "@total-typescript/tsconfig": "^1.0.4",
29
+ "@types/node": "^25.2.0",
30
+ "husky": "^9.1.7",
31
+ "pkg-pr-new": "^0.0.63",
32
+ "publint": "^0.3.17",
33
+ "tsdown": "^0.20.3",
34
+ "tsx": "^4.21.0",
35
+ "typescript": "^5.9.3",
36
+ "vitest": "^4.0.18"
37
+ },
38
+ "exports": {
39
+ ".": "./dist/typecheck.mjs",
40
+ "./package.json": "./package.json"
41
+ },
42
+ "scripts": {
43
+ "build": "tsdown",
44
+ "test": "vitest",
45
+ "lint": "biome check . --write"
46
+ }
47
+ }