Add npm publishing of JS libs (#702)

* feat: publish js libs to npm

* changed license type

* updated readme

* added id

* added exclusion for web and deno packages

* update comment

* updating env var
This commit is contained in:
Dan Ditomaso
2025-07-10 12:28:23 -04:00
committed by GitHub
parent 66b839742a
commit 7f177f6b83
20 changed files with 317 additions and 78 deletions
+101
View File
@@ -0,0 +1,101 @@
import { build, emptyDir } from "@deno/dnt";
import { join } from "jsr:@std/path@1/join";
interface DenoJsonConfig {
name: string;
version: string;
description: string;
imports?: Record<string, string>;
exports?: Record<string, string>;
}
async function getJson(filePath: string) {
try {
return JSON.parse(await Deno.readTextFile(filePath));
} catch (e) {
if (e instanceof Error) {
throw new Error(`Error reading or parsing ${filePath}: ${e.message}`);
}
}
}
if (Deno.args.length !== 1) {
console.error("Usage: deno task build:npm <path-to-package>");
console.error(
"Example: deno task build:npm packages/core",
);
Deno.exit(1);
}
const packagePath = Deno.args[0];
const denoJsonPath = join(packagePath, "deno.json");
const outDir = join(packagePath, "npm");
// Read the deno.json file to get the package metadata.
let jsonContent: DenoJsonConfig;
try {
jsonContent = await getJson(denoJsonPath);
} catch (error) {
console.log(`Error reading or parsing ${denoJsonPath}:`, error);
if (error instanceof Deno.errors.NotFound) {
console.error(`Error: Config file not found at ${denoJsonPath}`);
} else {
console.error(`Error reading or parsing ${denoJsonPath}:`, error);
}
Deno.exit(1);
}
const { name, version, description } = jsonContent;
if (!name || !version || !description) {
console.error(
`Error: 'name', 'version', and 'description' must be defined in ${denoJsonPath}`,
);
Deno.exit(1);
}
console.log(`Building ${name}@${version} from ${packagePath}...`);
// Clean the output directory before building.
await emptyDir(outDir);
try {
await build({
entryPoints: [join(packagePath, "mod.ts")],
outDir,
test: false,
shims: {
deno: true,
},
package: {
name,
version,
description,
license: "GPL-3.0-only",
repository: {
type: "git",
url: "git+https://github.com/meshtastic/web.git",
},
bugs: {
url: "https://github.com/meshtastic/web/issues",
},
},
compilerOptions: {
lib: ["DOM", "ESNext"],
},
postBuild() {
Deno.copyFileSync("LICENSE", join(outDir, "LICENSE"));
Deno.copyFileSync(
join(packagePath, "README.md"),
join(outDir, "README.md"),
);
},
});
} catch (error) {
console.error(`Error building ${name}@${version}:`, error);
Deno.exit(1);
}
console.log(`✅ Successfully built ${name}@${version} to ${outDir}`);