|
| 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