Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @ninjutsu-build/tsc.getEntryPointsFromConfig #101

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion integration/src/tsc.test.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { beforeEach, test, describe } from "node:test";
import { strict as assert } from "node:assert";
import { NinjaBuilder, getInput, validations } from "@ninjutsu-build/core";
import { makeTSCRule, makeTypeCheckRule } from "@ninjutsu-build/tsc";
import {
getEntryPointsFromConfig,
makeTSCRule,
makeTypeCheckRule,
} from "@ninjutsu-build/tsc";
import { writeFileSync, mkdirSync, symlinkSync, existsSync } from "node:fs";
import { join } from "node:path/posix";
import {
Expand Down Expand Up @@ -269,6 +273,10 @@ describe("tsc", (suiteCtx) => {
"src/myOutput/script.mjs",
"src/myOutput/script.d.mts",
]);
assert.deepEqual(
await getEntryPointsFromConfig(ninja, "src/tsconfig.json"),
["src/script.mts"],
);

const typechecked = await typecheck({
tsConfig: { file: "src/tsconfig.json" },
Expand Down
2 changes: 1 addition & 1 deletion packages/tsc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ninjutsu-build/tsc",
"version": "0.13.0",
"version": "0.13.1",
"description": "Create a ninjutsu-build rule for running the TypeScript compiler (tsc)",
"author": "Elliot Goodrich",
"scripts": {
Expand Down
48 changes: 48 additions & 0 deletions packages/tsc/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,57 @@ async function showConfig(
join(ninja.outputDir, getInput(tsConfig)),
]);

// `--showConfig` displays valid JSON so use the built-in parser
return JSON.parse(stdout);
}

/**
* Return the list of entry points specified in the `tsconfig.json` file
* located at the specified `tsConfigPath`. This is a combination of
* the `files`, `include`, and `exclude` properties, see the
* [`tsconfig.json` documentation](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
* for further information.
*/
export async function getEntryPointsFromConfig(
ninja: NinjaBuilder,
tsConfigPath: Input<string>,
): Promise<string[]> {
const tsConfigPathRaw = join(ninja.outputDir, getInput(tsConfigPath));
const buffer = await readFile(tsConfigPathRaw);

// Use TypeScript's parsing to handle comments
let { config, error } = ts.parseConfigFileTextToJson(
tsConfigPathRaw,
buffer.toString(),
);
if (config === undefined) {
throw new Error(
error === undefined
? `Unknown error while parsing ${tsConfigPathRaw}`
: `${error.messageText}`,
);
}

// If we don't have any `include` property then `files` gives us
// exactly the entry points we care about
if (Array.isArray(config.include) && config.include.length > 0) {
// There is no public typescript method to get the full list of entry points
// so instead we call out to `tsc --showConfig`.
const { stdout } = await execFile(node, [
tsc,
getTSCPath(ninja),
"--showConfig",
"--project",
tsConfigPathRaw,
]);
// `--showConfig` displays valid JSON so use the built-in parser
config = JSON.parse(stdout);
}

const directory = dirname(getInput(tsConfigPath));
return config.files.map((f: string) => join(directory, f));
}

export type TypeCheckRuleFn = {
<O extends string>(a: {
in: readonly Input<string>[];
Expand Down