Skip to content

Commit

Permalink
[build] Write a build script in bun to splatter dist into root
Browse files Browse the repository at this point in the history
Doing this because it simplifies the usage of the package and npm
doesn't seem to give me a way of telling it to do this. Could be wrong
and I hope I am.
  • Loading branch information
jmnuf committed Dec 11, 2024
1 parent a5f2b87 commit 3d85b8a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
{
"name": "pui-jsx",
"version": "0.2.0",
"main": "dist/index.js",
"main": "index.js",
"type": "module",
"types": "dist/index.d.ts",
"types": "index.d.ts",
"files": [
"dist",
"index.js",
"jsx-dev-runtime.js",
"jsx-node-builder.js",
"jsx-runtime.js",
"static",
"types.js",
"index.d.ts",
"jsx-dev-runtime.d.ts",
"jsx-node-builder.d.ts",
"jsx-runtime.d.ts",
"types.d.ts",
"README.md"
],
"author": "jmnuf <[email protected]>",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "bun test",
"pub": "bun run test && bun run build && npm publish"
"pub": "bun run publish.build.ts"
},
"devDependencies": {
"@types/bun": "latest"
Expand Down
80 changes: 80 additions & 0 deletions publish.build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import pkg from "./package.json";
import { readdir, mkdir, rm } from "node:fs/promises";

async function rm_rf(path: string) {
try {
await rm(path, { recursive: true, force: true });
return true;
} catch (err) {
console.error(err);
return false;
}
}

async function cpy(input: string, output: string) {
const f = Bun.file(input);
try {
await Bun.write(output, f);
return true;
} catch (err) {
console.error(err);
return false;
}
}

(async function main() {
const { $ } = Bun;
let shellResult;
// bun run test && bun run build && npm publish
shellResult = await $`bun run test`.nothrow();
if (shellResult.exitCode != 0) {
return;
}
shellResult = await $`bun run build`.nothrow();
if (shellResult.exitCode != 0) {
return;
}
const files = pkg.files;

try {
for (const f of files) {
if (f === "README.md") continue;
if (f === "static") {
await mkdir("./static");
const dir = await readdir(`./dist/${f}`);
for (const f of dir) {
console.log("[dist/static]", f, "exists:", await Bun.file(`./dist/static/${f}`).exists());
await cpy(`./dist/static/${f}`, `./static/${f}`);
}
continue;
}
console.log("[dist]", f, "exists:", await Bun.file(`./dist/${f}`).exists());
await cpy(`./dist/${f}`, `./${f}`);
}
} catch (err) {
console.error(err);
await rm_rf("./static");
return;
}

process.stdout.write("Press enter to continue... ");
for await (const _ of console) {
break;
}

// await $`npm publish`.nothrow();

try {
for (const f of files) {
if (f === "README.md") continue;
if (f === "static") {
await rm_rf("./static");
continue;
}
await rm(`./${f}`, { force: true });
}
} catch (err) {
console.error(err);
}

})()

0 comments on commit 3d85b8a

Please sign in to comment.