Skip to content

Commit

Permalink
feat: support --script option
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Apr 28, 2024
1 parent 17b51ca commit bd20506
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-clocks-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnm/tscx": patch
---

feat: support `--script` option
15 changes: 11 additions & 4 deletions packages/tscx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <scr>` for running `npm run <scr>` after compilation success.
- ✅ Support option `--exec <path>` for executing js file after compilation success.
- ❌ As for `tsc` built-in options, we only support options below.
- `--project`
- `--watch`

## Install

```sh
Expand Down
4 changes: 4 additions & 0 deletions packages/tscx/src/bin/tscx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ new Command()
"Copy non-ts files to output folder after every compilation.",
false,
)
.option(
"-s, --script <scr>",
"Run 'npm run <scr>' after every successful compilation. This will run before --exec option.",
)
.option(
"-e, --exec <path>",
"Execute or restart the specified js file after every successful compilation.",
Expand Down
17 changes: 11 additions & 6 deletions packages/tscx/src/cmd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 10 additions & 2 deletions packages/tscx/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)] : []),
];
}
Expand Down

0 comments on commit bd20506

Please sign in to comment.