Skip to content

Commit 65c7f1e

Browse files
committed
Add watch
1 parent b6be79e commit 65c7f1e

6 files changed

Lines changed: 179 additions & 41 deletions

File tree

packages/react-components/biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"tsconfig.app.json",
1717
"tsconfig.json",
1818
"tsconfig.node.json",
19-
"vite.config.ts"
19+
"vite.config.ts",
20+
"scripts/**"
2021
],
2122
"ignoreUnknown": false
2223
},

packages/react-components/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"scripts": {
2121
"build": "rm -rf dist && mkdir -p dist && copyfiles -e '**/*.ts' -e '**/*.tsx' 'src/**' dist && tsc -b --force && tailwindcss --cwd ./src -i ./full.css -o ../dist/bundle.css && echo success",
22-
"build:watch": "rm -rf dist && mkdir -p dist && copyfiles -e '**/*.ts' -e '**/*.tsx' 'src/**' dist && tsc -b --force --watch & tailwindcss --watch --cwd ./src -i ./full.css -o ../dist/bundle.css ; wait",
22+
"build:watch": "tsx scripts/watch.node.ts",
2323
"build-storybook": "storybook build",
2424
"format": "biome format",
2525
"format:check": "biome format",
@@ -99,6 +99,7 @@
9999
"copyfiles": "^2.4.1",
100100
"storybook": "^9.1.1",
101101
"tailwindcss": "^4.1.11",
102+
"tsx": "^4.20.4",
102103
"tw-animate-css": "^1.3.6",
103104
"typescript": "~5.8.3",
104105
"vite": "^7.0.4"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
4+
const EXTENSIONS = ["css"];
5+
6+
function makeGlob(extension: string): string {
7+
return `**/*.${extension}`;
8+
}
9+
10+
function makeGlobs(extensions: string[]): string[] {
11+
return extensions.map(makeGlob);
12+
}
13+
14+
if (!fs.existsSync("dist")) {
15+
fs.mkdirSync("dist");
16+
}
17+
18+
const suffixes = fs
19+
.globSync(makeGlobs(EXTENSIONS), {
20+
cwd: "./src",
21+
withFileTypes: true,
22+
})
23+
.filter((entry) => entry.isFile())
24+
.map((entry) =>
25+
path.join(
26+
"src",
27+
path.relative("src", path.join(entry.parentPath, entry.name)),
28+
),
29+
);
30+
31+
for (const srcPath of suffixes) {
32+
const dstPath = path.join("dist", srcPath);
33+
34+
const dstBasePath = path.dirname(dstPath);
35+
36+
if (!fs.existsSync(dstBasePath)) {
37+
console.log(`Create ${dstBasePath}`);
38+
fs.mkdirSync(dstBasePath, { recursive: true });
39+
}
40+
41+
if (!fs.existsSync(dstPath)) {
42+
console.log(`Copy ${srcPath} to ${dstPath}`);
43+
fs.copyFileSync(srcPath, dstPath);
44+
continue;
45+
}
46+
47+
const srcStat = fs.statSync(srcPath, { bigint: true });
48+
const dstStat = fs.statSync(dstPath, { bigint: true });
49+
50+
let shouldCopy = true;
51+
52+
if (!dstStat.isFile()) {
53+
console.log(`Remove non-file ${dstPath}`);
54+
fs.rmSync(dstPath, { recursive: true, force: true });
55+
} else if (dstStat.mtimeNs >= srcStat.mtimeNs) {
56+
console.log(`File up-to-date ${dstPath}`);
57+
shouldCopy = false;
58+
}
59+
60+
if (shouldCopy) {
61+
console.log(`Copy ${srcPath} to ${dstPath}`);
62+
fs.copyFileSync(srcPath, dstPath);
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
2+
3+
function spawnWithDecorations(
4+
command: string,
5+
args: string[],
6+
decorationOut: string,
7+
decorationErr: string,
8+
): ChildProcessWithoutNullStreams {
9+
const subprocess = spawn(command, args);
10+
subprocess.stdout.on("data", (data: Buffer) => {
11+
data
12+
.toString()
13+
.split("\n")
14+
.forEach((line) => console.log(decorationOut, line));
15+
});
16+
subprocess.stderr.on("data", (data: Buffer) => {
17+
data
18+
.toString()
19+
.split("\n")
20+
.forEach((line) => console.log(decorationErr, line));
21+
});
22+
return subprocess;
23+
}
24+
25+
spawnWithDecorations(
26+
"tsc",
27+
["-b", "--force", "--watch", "--preserveWatchOutput", "--pretty", "false"],
28+
" tsc> ",
29+
" tsc! ",
30+
);
31+
32+
spawnWithDecorations(
33+
"tailwindcss",
34+
["--watch", "--cwd", "./src", "-i", "./full.css", "-o", "../dist/bundle.css"],
35+
"tailwind> ",
36+
"tailwind! ",
37+
);
38+
39+
spawnWithDecorations(
40+
"tsx",
41+
["--watch-path=./src", "./scripts/copy.node.ts"],
42+
" copy> ",
43+
" copy! ",
44+
);

packages/react-components/tsconfig.node.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
"noFallthroughCasesInSwitch": true,
2525
"noUncheckedSideEffectImports": true
2626
},
27-
"include": ["vite.config.ts"]
27+
"include": ["vite.config.ts", "scripts/*"]
2828
}

0 commit comments

Comments
 (0)