From d4a798d9d48629812fc2bc2e6d67d151e8bed000 Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Sun, 17 Nov 2024 20:11:57 +0900 Subject: [PATCH] base: refine utils for dnt. --- packages/chacha20poly1305/dnt.ts | 33 +++------------- packages/core/dnt.ts | 30 +++----------- packages/dhkem-secp256k1/dnt.ts | 33 +++------------- packages/dhkem-x25519/dnt.ts | 30 +++----------- packages/dhkem-x448/dnt.ts | 30 +++----------- packages/hpke-js/dnt.ts | 30 +++----------- packages/hybridkem-x-wing/dnt.ts | 36 +++-------------- packages/hybridkem-x25519-kyber768/dnt.ts | 36 +++-------------- packages/ml-kem/dnt.ts | 30 +++----------- utils/dntCommon.ts | 48 +++++++++++++++++++++++ utils/misc.ts | 24 ------------ 11 files changed, 93 insertions(+), 267 deletions(-) create mode 100644 utils/dntCommon.ts delete mode 100644 utils/misc.ts diff --git a/packages/chacha20poly1305/dnt.ts b/packages/chacha20poly1305/dnt.ts index e66eb68be..625682695 100644 --- a/packages/chacha20poly1305/dnt.ts +++ b/packages/chacha20poly1305/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/chacha20poly1305"); -await emptyDir("../../npm/samples/chacha20poly1305"); -await emptyDir("../../npm/test/chacha20poly1305/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("chacha20poly1305"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/chacha20poly1305", @@ -69,20 +62,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/chacha20poly1305", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/chacha20poly1305/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/chacha20poly1305/LICENSE"); -Deno.copyFileSync( - "README.md", - "../../npm/packages/chacha20poly1305/README.md", -); +afterBuild("chacha20poly1305"); diff --git a/packages/core/dnt.ts b/packages/core/dnt.ts index 16b0fccef..000ede85c 100644 --- a/packages/core/dnt.ts +++ b/packages/core/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/core"); -await emptyDir("../../npm/samples/core"); -await emptyDir("../../npm/test/core/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("core"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/core", @@ -67,17 +60,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/core", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/core/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/core/LICENSE"); -Deno.copyFileSync("README.md", "../../npm/packages/core/README.md"); +afterBuild("core"); diff --git a/packages/dhkem-secp256k1/dnt.ts b/packages/dhkem-secp256k1/dnt.ts index e96df1446..7221130c5 100644 --- a/packages/dhkem-secp256k1/dnt.ts +++ b/packages/dhkem-secp256k1/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/dhkem-secp256k1"); -await emptyDir("../../npm/samples/dhkem-secp256k1"); -await emptyDir("../../npm/test/dhkem-secp256k1/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("dhkem-secp256k1"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/dhkem-secp256k1", @@ -69,20 +62,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/dhkem-secp256k1", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/dhkem-secp256k1/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/dhkem-secp256k1/LICENSE"); -Deno.copyFileSync( - "README.md", - "../../npm/packages/dhkem-secp256k1/README.md", -); +afterBuild("dhkem-secp256k1"); diff --git a/packages/dhkem-x25519/dnt.ts b/packages/dhkem-x25519/dnt.ts index cd3e2ce1b..4372efa35 100644 --- a/packages/dhkem-x25519/dnt.ts +++ b/packages/dhkem-x25519/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "jsr:@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/dhkem-x25519"); -await emptyDir("../../npm/samples/dhkem-x25519"); -await emptyDir("../../npm/test/dhkem-x25519/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("dhkem-x25519"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/dhkem-x25519", @@ -69,17 +62,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/dhkem-x25519", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/dhkem-x25519/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/dhkem-x25519/LICENSE"); -Deno.copyFileSync("README.md", "../../npm/packages/dhkem-x25519/README.md"); +afterBuild("dhkem-x25519"); diff --git a/packages/dhkem-x448/dnt.ts b/packages/dhkem-x448/dnt.ts index ed4340e07..aeef2c78b 100644 --- a/packages/dhkem-x448/dnt.ts +++ b/packages/dhkem-x448/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/dhkem-x448"); -await emptyDir("../../npm/samples/dhkem-x448"); -await emptyDir("../../npm/test/dhkem-x448/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("dhkem-x448"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/dhkem-x448", @@ -69,17 +62,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/dhkem-x448", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/dhkem-x448/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/dhkem-x448/LICENSE"); -Deno.copyFileSync("README.md", "../../npm/packages/dhkem-x448/README.md"); +afterBuild("dhkem-x448"); diff --git a/packages/hpke-js/dnt.ts b/packages/hpke-js/dnt.ts index d1b13d151..c28a40f07 100644 --- a/packages/hpke-js/dnt.ts +++ b/packages/hpke-js/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/hpke-js"); -await emptyDir("../../npm/samples/hpke-js"); -await emptyDir("../../npm/test/hpke-js/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("hpke-js"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/hpke-js", @@ -73,17 +66,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/hpke-js", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/hpke-js/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/hpke-js/LICENSE"); -Deno.copyFileSync("README.md", "../../npm/packages/hpke-js/README.md"); +afterBuild("hpke-js"); diff --git a/packages/hybridkem-x-wing/dnt.ts b/packages/hybridkem-x-wing/dnt.ts index b54edb278..fb3bfddeb 100644 --- a/packages/hybridkem-x-wing/dnt.ts +++ b/packages/hybridkem-x-wing/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/hybridkem-x-wing"); -await emptyDir("../../npm/samples/hybridkem-x-wing"); -await emptyDir("../../npm/test/hybridkem-x-wing/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("hybridkem-x-wing"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/hybridkem-x-wing", @@ -70,23 +63,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/hybridkem-x-wing", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/hybridkem-x-wing/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync( - "LICENSE", - "../../npm/packages/hybridkem-x-wing/LICENSE", -); -Deno.copyFileSync( - "README.md", - "../../npm/packages/hybridkem-x-wing/README.md", -); +afterBuild("hybridkem-x-wing"); diff --git a/packages/hybridkem-x25519-kyber768/dnt.ts b/packages/hybridkem-x25519-kyber768/dnt.ts index 413d9c98f..59400a02a 100644 --- a/packages/hybridkem-x25519-kyber768/dnt.ts +++ b/packages/hybridkem-x25519-kyber768/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/hybridkem-x25519-kyber768"); -await emptyDir("../../npm/samples/hybridkem-x25519-kyber768"); -await emptyDir("../../npm/test/hybridkem-x25519-kyber768/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("hybridkem-x25519-kyber768"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/hybridkem-x25519-kyber768", @@ -69,23 +62,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/hybridkem-x25519-kyber768", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/hybridkem-x25519-kyber768/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync( - "LICENSE", - "../../npm/packages/hybridkem-x25519-kyber768/LICENSE", -); -Deno.copyFileSync( - "README.md", - "../../npm/packages/hybridkem-x25519-kyber768/README.md", -); +afterBuild("hybridkem-x25519-kyber768"); diff --git a/packages/ml-kem/dnt.ts b/packages/ml-kem/dnt.ts index 067590912..48dd6719a 100644 --- a/packages/ml-kem/dnt.ts +++ b/packages/ml-kem/dnt.ts @@ -1,17 +1,10 @@ -import { build, emptyDir } from "@deno/dnt"; -import { copySync } from "@std/fs"; -import { removeNodeModules } from "../../utils/misc.ts"; - -// clean up dist -await emptyDir("../../npm/packages/ml-kem"); -await emptyDir("../../npm/samples/ml-kem"); -await emptyDir("../../npm/test/ml-kem/runtimes/cloudflare"); - -// clean up node_modules -await removeNodeModules(); +import { build } from "@deno/dnt"; +import { afterBuild, beforeBuild } from "../../utils/dntCommon.ts"; const denoPkg = JSON.parse(await Deno.readTextFile("./deno.json")); +await beforeBuild("ml-kem"); + await build({ entryPoints: ["./mod.ts"], outDir: "../../npm/packages/ml-kem", @@ -68,17 +61,4 @@ await build({ }, }); -copySync( - "samples/node", - "../../npm/samples/ml-kem", - { overwrite: true }, -); -copySync( - "test/runtimes/cloudflare", - "../../npm/test/ml-kem/runtimes/cloudflare", - { overwrite: true }, -); - -// post build steps -Deno.copyFileSync("LICENSE", "../../npm/packages/ml-kem/LICENSE"); -Deno.copyFileSync("README.md", "../../npm/packages/ml-kem/README.md"); +afterBuild("ml-kem"); diff --git a/utils/dntCommon.ts b/utils/dntCommon.ts new file mode 100644 index 000000000..acab87252 --- /dev/null +++ b/utils/dntCommon.ts @@ -0,0 +1,48 @@ +import { emptyDir } from "@deno/dnt"; +import { copySync } from "@std/fs"; + +export async function beforeBuild(name: string): Promise { + // Clean up dist + await emptyDir(`../../npm/packages/${name}`); + await emptyDir(`../../npm/samples/${name}`); + await emptyDir(`../../npm/test/${name}/runtimes/cloudflare`); + + // Remove node_modules + try { + await Deno.remove("test/runtimes/browsers/node_modules", { + recursive: true, + }); + } catch { + // ignore + } + try { + await Deno.remove("test/runtimes/bun/node_modules", { + recursive: true, + }); + } catch { + // ignore + } + try { + await Deno.remove("test/runtimes/cloudflare/node_modules", { + recursive: true, + }); + } catch { + // ignore + } + return; +} + +export function afterBuild(name: string) { + copySync( + "samples/node", + `../../npm/samples/${name}`, + { overwrite: true }, + ); + copySync( + "test/runtimes/cloudflare", + `../../npm/test/${name}/runtimes/cloudflare`, + { overwrite: true }, + ); + Deno.copyFileSync("LICENSE", `../../npm/packages/${name}/LICENSE`); + Deno.copyFileSync("README.md", `../../npm/packages/${name}/README.md`); +} diff --git a/utils/misc.ts b/utils/misc.ts deleted file mode 100644 index 7a3f39247..000000000 --- a/utils/misc.ts +++ /dev/null @@ -1,24 +0,0 @@ -export async function removeNodeModules() { - try { - await Deno.remove("test/runtimes/browsers/node_modules", { - recursive: true, - }); - } catch { - // ignore - } - try { - await Deno.remove("test/runtimes/bun/node_modules", { - recursive: true, - }); - } catch { - // ignore - } - try { - await Deno.remove("test/runtimes/cloudflare/node_modules", { - recursive: true, - }); - } catch { - // ignore - } - return; -}