Skip to content

Commit

Permalink
chore(_tools): add npm build script
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Sep 30, 2022
1 parent 920215f commit a6afe30
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
23 changes: 23 additions & 0 deletions _tools/build_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { build, emptyDir } from "https://deno.land/x/dnt@0.30.0/mod.ts";
import { join } from "https://deno.land/std@0.157.0/path/mod.ts";
import { makeOptions } from "./meta.ts";

async function buildPkg(version: string): Promise<void> {
await emptyDir("./npm");
const pkg = makeOptions(version);
await Deno.copyFile("LICENSE", join(pkg.outDir, "LICENSE"));
Deno.copyFile(
join(".", "README.md"),
join(pkg.outDir, "README.md"),
);
await build(pkg);
}

if (import.meta.main) {
const version = Deno.args[0];
if (!version) {
console.error("argument is required");
Deno.exit(1);
}
await buildPkg(version);
}
38 changes: 38 additions & 0 deletions _tools/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BuildOptions } from "https://deno.land/x/dnt@0.30.0/mod.ts";

export const makeOptions = (version: string): BuildOptions => ({
test: false,
shims: {},
typeCheck: true,
entryPoints: ["./mod.ts"],
outDir: "./npm",
package: {
name: "resultjs",
version,
description: "The standard API for result in JavaScript",
keywords: [
"result",
"ok",
"error",
"container",
"handle",
"throw",
"throws",
"handling",
"match",
"unsafe",
],
license: "MIT",
homepage: "https://github.com/TomokiMiyauci/result-js",
repository: {
type: "git",
url: "git+https://github.com/TomokiMiyauci/result-js.git",
},
bugs: {
url: "https://github.com/TomokiMiyauci/result-js/issues",
},
sideEffects: false,
type: "module",
},
packageManager: "pnpm",
});
26 changes: 26 additions & 0 deletions _tools/publish_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { prerelease, valid } from "https://deno.land/x/semver@v1.4.0/mod.ts";
import { makeOptions } from "./meta.ts";

if (import.meta.main) {
const version = Deno.args[0];
if (!version) {
console.error("arg of version is required");
Deno.exit(1);
}
if (!valid(version)) {
console.error("The argument of version is invalid");
Deno.exit(1);
}

const isPrerelease = prerelease(version);
const tag = isPrerelease?.[0] ?? "latest";

const pkg = makeOptions(version);
const result = await Deno.run({
cmd: ["npm", "publish", pkg.outDir, "--tag", String(tag)],
stdout: "piped",
})
.output();

console.log(new TextDecoder().decode(result));
}

0 comments on commit a6afe30

Please sign in to comment.