Skip to content

Commit

Permalink
feat: allow all options that are allowed in tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Jan 13, 2025
1 parent a75dbdc commit 7c6ddbe
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-roses-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnm/tscx": minor
---

feat: allow all options that are allowed in `tsc`
7 changes: 3 additions & 4 deletions packages/tscx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ Now you can simplify your `package.json` by using this library 😄.
- ✅ Additionally support `--script <scr>` for running `npm run <scr>` after compilation success.
- ✅ Additionally support `--exec <path>` for executing js file after compilation success.
- 🚨 [outDir](https://www.typescriptlang.org/tsconfig/#outDir) is required in `tsconfig`.
- ⚠️ As for `tsc` built-in options, we only support these options below.
- `--project`
- `--watch`
- `--noCheck`

## Install

Expand All @@ -74,6 +70,9 @@ npm install typescript @rnm/tscx -D
# Equivalent to `npx tsc`
$ npx tscx

# Equivalent to `npx tsc --noEmit`
$ npx tscx --noEmit

# Equivalent to `npx tsc --project tsconfig.build.json --watch`
$ npx tscx --project tsconfig.build.json --watch

Expand Down
4 changes: 3 additions & 1 deletion packages/tscx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
},
"dependencies": {
"chokidar": "3.6.0",
"commander": "12.1.0"
"commander": "12.1.0",
"minimist": "1.2.8"
},
"devDependencies": {
"@types/minimist": "1.2.5",
"@types/node": "22.10.2",
"tsx": "4.19.2",
"vitest": "1.6.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/tscx/src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class Action {
private readonly compiler;
private watcher?: FSWatcher;
constructor(private readonly options: TscxOptions) {
this.compiler = new Compiler(options);
const { watch, ...compilerOptions } = options;
this.compiler = new Compiler(compilerOptions);
}

private watch(paths: string[]) {
Expand Down
12 changes: 5 additions & 7 deletions packages/tscx/src/bin/tscx.cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { Command } from "commander";
import minimist from "minimist";
import { Action } from "../action.js";

const version: string = JSON.parse(
Expand All @@ -27,11 +28,6 @@ new Command()
"Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.",
"tsconfig.json",
)
.option(
"--noCheck",
"Disable full type checking (only critical parse and emit errors will be reported).",
false,
)
.option("-w, --watch", "Watch input files.", false)
.option(
"-r, --remove",
Expand All @@ -51,12 +47,14 @@ new Command()
"-e, --exec <path>",
"Execute or restart the specified js file after every successful compilation.",
)
.action(async (options) => {
.allowUnknownOption()
.action(async (options, cmd) => {
const isDir = async (p: string) =>
(await fs.stat(path.resolve(process.cwd(), p))).isDirectory();
if (options.project && (await isDir(options.project))) {
options.project = path.join(options.project, "tsconfig.json");
}
new Action(options).start();
const { _, ...extraOptions } = minimist(cmd.args);
new Action({ ...options, ...extraOptions }).start();
})
.parse();
12 changes: 8 additions & 4 deletions packages/tscx/src/cmd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ export function remove(filepath: string) {
return spawn("node", REMOVE_PATH, filepath);
}

export function tsc(options: { project: string; noCheck: boolean }) {
export function tsc(options: Record<string, string | boolean>) {
console.log("Tsc", options);
const args = [
TSC_PATH,
"--project",
options.project,
...(options.noCheck ? ["--noCheck"] : []),
...Object.entries(options).flatMap(([key, value]) =>
value === false
? []
: value === true
? [`--${key}`]
: [`--${key}`, value],
),
];
return spawn("node", ...args);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/tscx/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import process from "node:process";
import type ts from "typescript";
import { copyfiles, exec, remove, script, tsc } from "./cmd/index.js";

export interface CompilerOptions {
export interface CompilerOptions extends Record<string, string | boolean> {
project: string;
noCheck: boolean;
remove: boolean;
copyfiles: boolean;
script?: string;
Expand Down Expand Up @@ -64,15 +63,15 @@ export class Compiler {
private getTasks(): Array<() => childProcess.ChildProcess> {
const {
project,
noCheck,
remove: rm,
copyfiles: cp,
script: scr,
exec: ex,
...others
} = this.options;
return [
...(rm ? [() => remove(this.outDir)] : []),
() => tsc({ project, noCheck }),
() => tsc({ project, ...others }),
...(cp ? [() => copyfiles(this.rootDir, this.outDir)] : []),
...(scr ? [() => script(scr)] : []),
...(ex ? [() => exec(ex)] : []),
Expand Down

0 comments on commit 7c6ddbe

Please sign in to comment.