From 4566ad0b9694a53f19b3ef90f3de3b4d98109f78 Mon Sep 17 00:00:00 2001 From: Tomoki Miyauchi Date: Sun, 4 Jun 2023 01:24:24 +0900 Subject: [PATCH] chore(_tools): add npm build script --- .releaserc | 25 +++++++++++++++++++++++++ _tools/build_npm.ts | 23 +++++++++++++++++++++++ _tools/meta.ts | 41 +++++++++++++++++++++++++++++++++++++++++ _tools/publish_npm.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 .releaserc create mode 100644 _tools/build_npm.ts create mode 100644 _tools/meta.ts create mode 100644 _tools/publish_npm.ts diff --git a/.releaserc b/.releaserc new file mode 100644 index 0000000..e7eb04b --- /dev/null +++ b/.releaserc @@ -0,0 +1,25 @@ +{ + "branches": [ + "main", + { + "name": "beta", + "prerelease": true + } + ], + "plugins": [ + "@semantic-release/commit-analyzer", + "@semantic-release/release-notes-generator", + "@semantic-release/changelog", + "@semantic-release/github", + [ + "@semantic-release/git", + { + "assets": [ + "CHANGELOG.md" + ], + "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" + } + ] + ], + "tagFormat": "${version}" +} diff --git a/_tools/build_npm.ts b/_tools/build_npm.ts new file mode 100644 index 0000000..8bb479a --- /dev/null +++ b/_tools/build_npm.ts @@ -0,0 +1,23 @@ +import { build, emptyDir } from "https://deno.land/x/dnt@0.34.0/mod.ts"; +import { join } from "https://deno.land/std@0.190.0/path/mod.ts"; +import { makeOptions } from "./meta.ts"; + +async function buildPkg(version: string): Promise { + 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); +} diff --git a/_tools/meta.ts b/_tools/meta.ts new file mode 100644 index 0000000..81ca329 --- /dev/null +++ b/_tools/meta.ts @@ -0,0 +1,41 @@ +import { BuildOptions } from "https://deno.land/x/dnt@0.34.0/mod.ts"; + +export const makeOptions = (version: string): BuildOptions => ({ + test: false, + shims: {}, + typeCheck: true, + entryPoints: ["./mod.ts"], + outDir: "./npm", + package: { + name: "@miyauci/format", + version, + description: "Formatting and printing string utilities", + keywords: [ + "format", + "fmt", + "formatter", + "print", + "sprintf", + "format-string", + "replace", + "replacement", + "template", + "interpolate", + ], + license: "MIT", + homepage: "https://github.com/TomokiMiyauci/format", + repository: { + type: "git", + url: "git+https://github.com/TomokiMiyauci/format.git", + }, + bugs: { + url: "https://github.com/TomokiMiyauci/format/issues", + }, + sideEffects: false, + type: "module", + publishConfig: { + access: "public", + }, + }, + packageManager: "pnpm", +}); diff --git a/_tools/publish_npm.ts b/_tools/publish_npm.ts new file mode 100644 index 0000000..c304aa1 --- /dev/null +++ b/_tools/publish_npm.ts @@ -0,0 +1,27 @@ +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 command = new Deno.Command("npm", { + args: ["publish", pkg.outDir, "--tag", String(tag)], + }); + const result = await command.output(); + + if (!result.success) { + console.error(new TextDecoder().decode(result.stderr)); + } +}