diff --git a/.changeset/khaki-clocks-wink.md b/.changeset/khaki-clocks-wink.md new file mode 100644 index 0000000..a750303 --- /dev/null +++ b/.changeset/khaki-clocks-wink.md @@ -0,0 +1,5 @@ +--- +"@rnm/tscx": patch +--- + +feat: support `--script` option diff --git a/packages/tscx/README.md b/packages/tscx/README.md index 2636cc9..202a398 100644 --- a/packages/tscx/README.md +++ b/packages/tscx/README.md @@ -27,13 +27,20 @@ Happy hacking! ## Highlight -- Same usages as `tsc` with few additional options. -- Remove output folder before every compilation. -- Copy non-ts files to output folder after every compilation. -- Execute js file after compilation success. +- Same usages as `tsc`. - Respect `tsconfig.json`. - ESM. +## Differences with `tsc` + +- ✅ Support option `--remove` for removing output folder before every compilation. +- ✅ Support option `--copyfiles` for copying non-ts files to output folder after every compilation. +- ✅ Support option `--script ` for running `npm run ` after compilation success. +- ✅ Support option `--exec ` for executing js file after compilation success. +- ❌ As for `tsc` built-in options, we only support options below. + - `--project` + - `--watch` + ## Install ```sh diff --git a/packages/tscx/src/bin/tscx.ts b/packages/tscx/src/bin/tscx.ts index 5af3d47..4ecd656 100644 --- a/packages/tscx/src/bin/tscx.ts +++ b/packages/tscx/src/bin/tscx.ts @@ -37,6 +37,10 @@ new Command() "Copy non-ts files to output folder after every compilation.", false, ) + .option( + "-s, --script ", + "Run 'npm run ' after every successful compilation. This will run before --exec option.", + ) .option( "-e, --exec ", "Execute or restart the specified js file after every successful compilation.", diff --git a/packages/tscx/src/cmd/index.ts b/packages/tscx/src/cmd/index.ts index 2c6a8f6..cdaa948 100644 --- a/packages/tscx/src/cmd/index.ts +++ b/packages/tscx/src/cmd/index.ts @@ -14,26 +14,31 @@ const TSC_PATH = path.resolve( "tsc", ); -function spawnNode(...args: string[]) { - return childProcess.spawn("node", args, { stdio: "inherit" }); +function spawn(cmd: "node" | "npm", ...args: string[]) { + return childProcess.spawn(cmd, args, { stdio: "inherit" }); } export function remove(filepath: string) { console.log("Remove", filepath); - return spawnNode(REMOVE_PATH, filepath); + return spawn("node", REMOVE_PATH, filepath); } export function tsc(options: { project: string }) { console.log("Tsc", options); - return spawnNode(TSC_PATH, "--project", options.project); + return spawn("node", TSC_PATH, "--project", options.project); } export function copyfiles(rootDir: string, outDir: string) { console.log("Copyfiles", rootDir, "=>", outDir); - return spawnNode(COPYFILES_PATH, rootDir, outDir); + return spawn("node", COPYFILES_PATH, rootDir, outDir); +} + +export function script(scr: string) { + console.log("Script", `npm run ${scr}`); + return spawn("npm", "run", scr); } export function exec(filepath: string) { console.log("Execute", filepath); - return spawnNode(filepath); + return spawn("node", filepath); } diff --git a/packages/tscx/src/compiler.ts b/packages/tscx/src/compiler.ts index 2428cc0..4f8e6ea 100644 --- a/packages/tscx/src/compiler.ts +++ b/packages/tscx/src/compiler.ts @@ -3,12 +3,13 @@ import childProcess from "node:child_process"; import path from "node:path"; import process from "node:process"; import type ts from "typescript"; -import { copyfiles, exec, remove, tsc } from "./cmd/index.js"; +import { copyfiles, exec, remove, script, tsc } from "./cmd/index.js"; export interface CompilerOptions { project: string; remove: boolean; copyfiles: boolean; + script?: string; exec?: string; } @@ -60,11 +61,18 @@ export class Compiler { } private getTasks(): Array<() => childProcess.ChildProcess> { - const { project, remove: rm, copyfiles: cp, exec: ex } = this.options; + const { + project, + remove: rm, + copyfiles: cp, + script: scr, + exec: ex, + } = this.options; return [ ...(rm ? [() => remove(this.outDir)] : []), () => tsc({ project }), ...(cp ? [() => copyfiles(this.rootDir, this.outDir)] : []), + ...(scr ? [() => script(scr)] : []), ...(ex ? [() => exec(ex)] : []), ]; }