Skip to content

Commit e1619d1

Browse files
committed
feat: add watch mode
1 parent b68bb3c commit e1619d1

6 files changed

Lines changed: 76 additions & 34 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"format": "biome check --write --linter-enabled=false",
3030
"build:app": "bun run --cwd ./apps/registry build",
3131
"build:cli": "bun run --cwd ./packages/usts build",
32+
"build": "bun run build:cli",
3233
"test": "bun run --cwd ./packages/usts test"
3334
},
3435
"devDependencies": {

packages/usts/src/cli/build/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { resolveConfig } from "~/config/resolve";
22
import { buildUserscript } from "~/core/build";
3+
import { watchUserscript } from "~/core/build/watch";
34

4-
async function build(): Promise<void> {
5+
async function build(options: { watch?: boolean }): Promise<void> {
56
const { userscriptConfig, root } = await resolveConfig();
67

78
const outDir = userscriptConfig.outDir;
@@ -12,7 +13,13 @@ async function build(): Promise<void> {
1213
);
1314
}
1415

15-
await buildUserscript(userscriptConfig, { write: true });
16+
if (!options.watch) {
17+
await buildUserscript(userscriptConfig, { write: true });
18+
}
19+
20+
if (options.watch) {
21+
watchUserscript(userscriptConfig);
22+
}
1623
}
1724

1825
export { build };

packages/usts/src/cli/index.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ import { parseArgs } from "node:util";
22

33
type CLICommand = "help" | "build";
44

5-
type Flags = {} & Record<string, never>;
5+
type Flags = { watch: boolean };
66

77
interface Args {
88
values: Flags;
99
positionals: string[];
1010
}
1111

1212
async function printHelp() {
13-
console.log("Run `usts build` to build a userscript");
13+
console.log(`
14+
usts build - build a userscript
15+
usts build --watch - build userscript in watch mode
16+
`);
17+
}
18+
19+
function isSupportedCommand<T extends CLICommand>(
20+
supportedCommands: T[],
21+
cmd: string,
22+
): cmd is T {
23+
return new Set<string>(supportedCommands).has(cmd);
1424
}
1525

1626
function resolveCommand(parsedArgs: Args): CLICommand {
@@ -20,30 +30,33 @@ function resolveCommand(parsedArgs: Args): CLICommand {
2030
return "help";
2131
}
2232

23-
const supportedCommands = new Set(["build"]);
24-
if (supportedCommands.has(cmd)) {
25-
return cmd as CLICommand;
33+
if (isSupportedCommand(["build"], cmd)) {
34+
return cmd;
2635
}
2736

2837
return "help";
2938
}
3039

31-
async function runCommand(cmd: CLICommand) {
40+
async function runCommand(cmd: CLICommand, flags: Flags) {
3241
switch (cmd) {
3342
case "help": {
3443
await printHelp();
3544
return;
3645
}
3746
case "build": {
3847
const { build } = await import("./build/index.js");
39-
await build();
48+
await build({ watch: flags.watch });
4049
return;
4150
}
4251
}
4352
}
4453

4554
export async function cli(argv: string[]): Promise<void> {
46-
const parsedArgs = parseArgs({ args: argv, allowPositionals: true });
55+
const parsedArgs = parseArgs({
56+
args: argv,
57+
allowPositionals: true,
58+
options: { watch: { type: "boolean", default: false } },
59+
});
4760
const cmd = resolveCommand(parsedArgs);
48-
await runCommand(cmd);
61+
await runCommand(cmd, parsedArgs.values);
4962
}

packages/usts/src/core/build/index.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
1-
import * as path from "node:path";
2-
31
import * as rolldown from "rolldown";
42

53
import type { ResolvedUserscriptConfig } from "~/config/schema";
64

7-
import { serializeMetaHeader } from "./meta-header";
5+
import { resolveOptions } from "./options";
86

97
async function buildUserscript(
108
config: ResolvedUserscriptConfig,
11-
options?: { write?: boolean; watch?: boolean },
9+
options?: { write?: boolean },
1210
): Promise<string> {
13-
const header = serializeMetaHeader(config.header);
14-
15-
const USERSCRIPT_OUTPUT_FILE_NAME = "index.user.js";
16-
const outFile = path.join(config.outDir, USERSCRIPT_OUTPUT_FILE_NAME);
17-
18-
const result = await rolldown.build({
19-
input: config.entryPoint,
20-
tsconfig: true,
21-
plugins: [config.plugins],
22-
output: {
23-
format: "iife",
24-
sourcemap: false,
25-
minify: "dce-only",
26-
postBanner: `${header}\n`,
27-
cleanDir: config.clean,
28-
file: outFile,
29-
},
30-
write: options?.write ?? false,
31-
});
11+
const result = await rolldown.build(resolveOptions(config, options));
3212

3313
if (result.output.length !== 1) {
3414
throw new Error(`❌ Unexpected userscript build output`);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as path from "node:path";
2+
import type { BuildOptions } from "rolldown";
3+
import type { ResolvedUserscriptConfig } from "~/config/schema";
4+
import { serializeMetaHeader } from "./meta-header";
5+
6+
export function resolveOptions(
7+
config: ResolvedUserscriptConfig,
8+
options?: { write?: boolean },
9+
): BuildOptions {
10+
const header = serializeMetaHeader(config.header);
11+
12+
const USERSCRIPT_OUTPUT_FILE_NAME = "index.user.js";
13+
const outFile = path.join(config.outDir, USERSCRIPT_OUTPUT_FILE_NAME);
14+
15+
return {
16+
input: config.entryPoint,
17+
tsconfig: true,
18+
plugins: [config.plugins],
19+
output: {
20+
format: "iife",
21+
sourcemap: false,
22+
minify: "dce-only",
23+
postBanner: `${header}\n`,
24+
cleanDir: config.clean,
25+
file: outFile,
26+
},
27+
write: options?.write ?? false,
28+
};
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as rolldown from "rolldown";
2+
3+
import type { ResolvedUserscriptConfig } from "~/config/schema";
4+
5+
import { resolveOptions } from "./options";
6+
7+
function watchUserscript(config: ResolvedUserscriptConfig): void {
8+
const options = resolveOptions(config);
9+
rolldown.watch(options);
10+
}
11+
12+
export { watchUserscript };

0 commit comments

Comments
 (0)