Skip to content

Commit a51bf05

Browse files
committed
feat: codegen script
1 parent 1986a4a commit a51bf05

File tree

3 files changed

+198
-121
lines changed

3 files changed

+198
-121
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"changeset": "./scripts/changeset-wrapper.sh"
7+
"changeset": "./scripts/changeset-wrapper.sh",
8+
"codegen": "tsx scripts/liblab-gen.ts"
89
},
910
"devDependencies": {
1011
"@changesets/changelog-github": "^0.5.0",

scripts/liblab-gen.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { exec, execSync } from "node:child_process";
2+
import * as fs from "node:fs/promises";
3+
import * as path from "node:path";
4+
5+
import { replaceInFile } from "replace-in-file";
6+
7+
async function move(oldPath: string, newPath: string) {
8+
await fs.rm(newPath, { recursive: true, force: true });
9+
await fs.mkdir(path.dirname(newPath), { recursive: true });
10+
await fs.rename(oldPath, newPath);
11+
}
12+
13+
async function readJSON(path: string) {
14+
return fs
15+
.readFile(path, { encoding: "utf-8" })
16+
.then((data) => JSON.parse(data));
17+
}
18+
19+
function hasChangesInPath(path: string) {
20+
try {
21+
execSync(`git diff --quiet HEAD ${path}`, { stdio: "ignore" });
22+
return false;
23+
} catch (error) {
24+
return true;
25+
}
26+
}
27+
28+
async function fixExamples() {
29+
await replaceInFile({
30+
files: [
31+
"example/src/main/java/com/example/Main.java",
32+
"kotlin-example/src/main/kotlin/Main.kt",
33+
],
34+
from: ["page[before]", "page[after]"],
35+
to: "",
36+
});
37+
await exec("chmod +x example/run.sh kotlin-example/run.sh");
38+
}
39+
40+
async function build() {
41+
const pkgPath = `./packages/${path.basename(path.resolve("."))}`;
42+
43+
let pkgJson = await readJSON("./package.json");
44+
const liblabConfig = await readJSON("./liblab.config.json");
45+
console.log(
46+
`Building '${pkgJson.name}' using spec '${liblabConfig.specFilePath}'`
47+
);
48+
49+
execSync("npx -y liblab@latest build -y --skip-validation", {
50+
stdio: "inherit",
51+
});
52+
53+
await move("output/java/src", "./src");
54+
55+
if (!hasChangesInPath("./src")) {
56+
console.log("No changes detected in output.");
57+
return;
58+
}
59+
60+
const artifacts = [
61+
"pom.xml",
62+
"README.md",
63+
"documentation",
64+
"example",
65+
"kotlin-example",
66+
];
67+
for (const artifact of artifacts) {
68+
await move(`output/java/${artifact}`, `./${artifact}`);
69+
}
70+
71+
await fixExamples();
72+
}
73+
74+
void build().finally(() => {
75+
fs.rm("output", { recursive: true, force: true });
76+
});

0 commit comments

Comments
 (0)