From 3f2c972001f4d8e657646122170bdfe714696c98 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Sat, 7 Sep 2024 13:53:34 +0800 Subject: [PATCH] feat(media): add prepare script --- packages/engine-media/package.json | 5 ++- packages/engine-media/scripts/prepare.cts | 40 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/engine-media/scripts/prepare.cts diff --git a/packages/engine-media/package.json b/packages/engine-media/package.json index ea8ceb2..829403a 100644 --- a/packages/engine-media/package.json +++ b/packages/engine-media/package.json @@ -14,12 +14,15 @@ "lib" ], "scripts": { - "build": "node -r esbuild-register ../../scripts/runOnChanged.cts ./src concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", + "prepare": "node -r esbuild-register scripts/prepare.cts", + "build": "yarn prepare && node -r esbuild-register ../../scripts/runOnChanged.cts ./src concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", "build:cjs": "node -r esbuild-register ../../scripts/build.cts", "build:dts": "tsc -p tsconfig.json" }, "devDependencies": { "@chronocat/shell": "workspace:packages/shell", + "@types/adm-zip": "^0.5.0", + "adm-zip": "^0.5.10", "concurrently": "^8.2.1", "esbuild-register": "^3.5.0", "ts-toolbelt": "^9.6.0", diff --git a/packages/engine-media/scripts/prepare.cts b/packages/engine-media/scripts/prepare.cts new file mode 100644 index 0000000..2b9e140 --- /dev/null +++ b/packages/engine-media/scripts/prepare.cts @@ -0,0 +1,40 @@ +import AdmZip from 'adm-zip' +import { access, mkdir } from 'node:fs/promises' +import { join, resolve } from 'node:path' + +const filenames = ['ntsilk-win32-x64.exe', 'ntsilk-linux-x64'] +const dstDir = resolve(__dirname, '../../../build/caches') +const dstPath = join(dstDir, 'ntsilk.zip') + +export async function exists(path: string): Promise { + try { + await access(path) + } catch (_) { + return false + } + return true +} + +void (async () => { + await mkdir(dstDir, { + recursive: true, + }) + + if (await exists(dstPath)) return + + const zip = new AdmZip() + + await Promise.all( + filenames.map((name) => + fetch( + `https://raw.githubusercontent.com/ntsilk-userland/binaries/master/${name}`, + ) + .then((response) => response.arrayBuffer()) + .then((data) => { + zip.addFile(name, Buffer.from(data)) + }), + ), + ) + + await zip.writeZipPromise(dstPath) +})()