From d93316be1588a7f19d2cba654cf084c7fce4a05a Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 17 Jun 2026 11:13:02 +0800 Subject: [PATCH] publish --- .gitignore | 1 + kit/package.json | 2 + kit/publish.js | 116 ++++++++++++++++++++++++++++++++++++++++++ kit/src/index.js | 2 +- kit/src/kitcommand.js | 1 + 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 kit/publish.js diff --git a/.gitignore b/.gitignore index 52bf1b4..e06baaa 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules dist build.json kit/.kit/modules/* +kit/publish/* diff --git a/kit/package.json b/kit/package.json index 463e6a4..3df0c66 100644 --- a/kit/package.json +++ b/kit/package.json @@ -4,6 +4,7 @@ "bin": "src/index.js", "scripts": { "test": "bun src/index.js", + "publish": "node publish.js", "linux": "bun build.js && bun build ./src/index.js --compile --bytecode --minify --target=bun-linux-x64-baseline --outfile=./dist/linux/kit", "windows": "bun build.js && bun build ./src/index.js --compile --bytecode --minify --target=bun-windows-x64-baseline --outfile=./dist/windows/kit.exe", "pkg-alpine": "bun build.js && pkg --compress GZip . -t node14-alpine-arm64 -o ./dist/alpine/kit", @@ -27,3 +28,4 @@ "webdav": "^5.3.1" } } + diff --git a/kit/publish.js b/kit/publish.js new file mode 100644 index 0000000..b376abc --- /dev/null +++ b/kit/publish.js @@ -0,0 +1,116 @@ +const fs = require('fs'); +const path = require('path'); +const { spawnSync } = require('child_process'); +const JSON5 = require('json5'); + +const root = __dirname; +const publishDir = path.resolve(root, 'publish'); +const artifacts = [ + { + script: 'pkg-windows', + source: path.resolve(root, 'dist/windows/kit.exe'), + target: path.resolve(publishDir, 'windows/x86_64/kit.exe'), + localPath: 'publish/windows/x86_64/kit.exe', + os: 'windows', + arch: 'x86_64', + filename: 'kit.exe' + }, + { + script: 'pkg-linux', + source: path.resolve(root, 'dist/linux/kit'), + target: path.resolve(publishDir, 'linux/x86_64/kit'), + localPath: 'publish/linux/x86_64/kit', + os: 'linux', + arch: 'x86_64', + filename: 'kit' + } +]; + +function run(command, args) { + console.log(`> ${command} ${args.join(' ')}`); + const result = spawnSync(command, args, { + cwd: root, + stdio: 'inherit', + shell: process.platform === 'win32' + }); + + if (result.error) { + throw result.error; + } + + if (result.status !== 0) { + process.exit(result.status || 1); + } +} + +function readJson(file) { + return JSON5.parse(fs.readFileSync(file, 'utf-8')); +} + +function readPackage() { + return readJson(path.resolve(root, 'package.json')); +} + +function readSourceMeta() { + const candidates = [ + path.resolve(root, 'meta.json'), + path.resolve(root, '..', 'meta.json') + ]; + + for (const file of candidates) { + if (fs.existsSync(file)) { + return readJson(file); + } + } + + return {}; +} + +function copyFile(source, target) { + if (!fs.existsSync(source)) { + throw new Error(`Build artifact not found: ${source}`); + } + + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.copyFileSync(source, target); +} + +function writeMeta() { + const pkg = readPackage(); + const sourceMeta = readSourceMeta(); + const meta = { + id: sourceMeta.id || pkg.name || 'kit', + name: sourceMeta.name || pkg.name || 'kit', + desc: sourceMeta.desc || sourceMeta.description || '', + group: sourceMeta.group || '/modules', + dist: artifacts.map((artifact) => ({ + path: artifact.localPath, + os: artifact.os, + arch: artifact.arch, + filename: artifact.filename, + type: 'executable' + })) + }; + + fs.mkdirSync(publishDir, { recursive: true }); + fs.writeFileSync(path.resolve(publishDir, 'meta.json'), JSON.stringify(meta, null, 4), 'utf-8'); +} + +function preparePublishDir() { + fs.rmSync(publishDir, { recursive: true, force: true }); + + for (const artifact of artifacts) { + copyFile(artifact.source, artifact.target); + } + + writeMeta(); +} + +for (const artifact of artifacts) { + run('npm', ['run', artifact.script]); +} + +preparePublishDir(); +run('kit3', ['publish', './publish']); + +console.log('Publish completed.'); diff --git a/kit/src/index.js b/kit/src/index.js index 9d56a9e..8dca2b6 100644 --- a/kit/src/index.js +++ b/kit/src/index.js @@ -172,7 +172,7 @@ parser.addCmdLine("freem [level]", "释放内存", function (cli) { }); -parser.addCmdLine("docker runsh [--file]", "反向获取 docker 容器启动脚本", function (cli) { +parser.addCmdLine("dockersh [--file]", "反向获取 docker 容器启动脚本", function (cli) { let container = cli.getParamValue("container") let file = cli.getParamValue("file") import("./docker/index.js").then(psl => psl.main(container, file)) diff --git a/kit/src/kitcommand.js b/kit/src/kitcommand.js index 6f0ae0c..28c5f2d 100644 --- a/kit/src/kitcommand.js +++ b/kit/src/kitcommand.js @@ -144,6 +144,7 @@ async function main(){ } await remote.publish(localFile, remotePath); $logger.info("发布成功"); + process.exit(0); }); parser.addCmdLine("install [module]", "安装/更新模块; 支持 myapp、myapp@1.0、/docker/net/myapp", async function (cli) {