Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4207d6e467 | ||
|
|
a7f596aa0a | ||
|
|
364efbc1c1 | ||
|
|
fc81584878 | ||
|
|
0132be7586 |
@@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
dist
|
||||
build.json
|
||||
kit/.kit/modules/*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
- 基于 webdav 协议访问。包括**远程仓库存储方案**(基于 WebDAV 协议)与**本地客户端存储方案**。
|
||||
- 基于 webdav 协议访问。包括**远程仓库存储方案**(基于 WebDAV 协议)与**本地客户端存储方案**。
|
||||
- 支持存储多种操作系统(Linux、Windows)、多种架构(X86、aarch64)
|
||||
- 支持记录官网地址,方便定期从官网拉取新版本存储。
|
||||
|
||||
@@ -20,9 +20,10 @@
|
||||
|
||||
```
|
||||
/repo/ # WebDAV 服务暴露的根路径
|
||||
└─ {project-id}/ # 项目目录(唯一标识)
|
||||
└─ {group}/ # 分组目录,默认 /modules;空值等同 /
|
||||
└─ {project-id}/ # 项目目录(唯一标识),实际路径为 group/id
|
||||
├─ metadata.json # 项目级元数据文件
|
||||
└─ {semver}/ # 版本目录(如 1.2.3 或 v1.2.3)
|
||||
└─ {version}/ # 版本目录(如 1.0、1.1、2.0)
|
||||
├─ version.json # 版本级元数据文件(推荐)
|
||||
├─ any/any/ # 平台无关文件目录(保留字)
|
||||
│ └─ ... # 单文件或压缩包
|
||||
@@ -33,8 +34,9 @@
|
||||
```
|
||||
|
||||
#### 2.1 命名规则
|
||||
- `project-id`:小写字母、数字、连字符(-),长度 ≤ 64。
|
||||
- `semver`:符合 [Semantic Versioning 2.0]( 的版本号,推荐不含 `v` 前缀。
|
||||
- `project-id`:来自项目 `meta.json` 中的唯一 `id`,小写字母、数字、连字符(-),长度 ≤ 64。
|
||||
- `group`:来自项目 `meta.json`,默认 `/modules`;空字符串等同 `/`;支持 `/docker`、`/tmpls`、`/docker/net` 等子路径。
|
||||
- `version`:来自项目 `meta.json` 的 `version`;若为空,发布时根据远程 `metadata.json.version` 尾数 +1,远程不存在时初始为 `1.0`。
|
||||
- `os` 与 `arch` 使用标准化名称(见下表),`any` 为保留字,表示任意平台。
|
||||
|
||||
| 操作系统 | 标准名称 | 架构 | 标准名称 |
|
||||
@@ -58,12 +60,11 @@
|
||||
**字段说明**:
|
||||
|
||||
| 字段名 | 类型 | 必选 | 说明 |
|
||||
| ------------------ | ----------------- | --- | ----------------- |
|
||||
| `project_id` | string | 是 | 项目唯一标识,与目录名相同 |
|
||||
| `group` | string | 否 | 仓库分组路径,默认 `/modules`,空值等同 `/` |
|
||||
| `official_url` | string | 是 | 项目官网地址(用于定期拉取新版本) |
|
||||
| `description` | string | 否 | 项目简述 |
|
||||
| `latest_version` | string | 是 | 仓库中已存储的最新版本号 |
|
||||
| `versions` | array of strings | 是 | 仓库中已存储的所有版本号列表 |
|
||||
| `version` | string | 是 | 当前远程最新版本号;不再使用 `versions` 数组 |
|
||||
| `last_fetch_check` | string (ISO 8601) | 否 | 上次检查官网更新的时间戳 |
|
||||
| `fetch_rules` | array of objects | 否 | 自动拉取时的匹配规则(见 3.3) |
|
||||
| `extra` | object | 否 | 扩展字段 |
|
||||
@@ -72,10 +73,10 @@
|
||||
```json
|
||||
{
|
||||
"project_id": "myapp",
|
||||
"group": "/modules",
|
||||
"official_url": "",
|
||||
"description": "An example tool",
|
||||
"latest_version": "1.2.1",
|
||||
"versions": ["1.0.0", "1.2.0", "1.2.1"],
|
||||
"version": "1.2",
|
||||
"last_fetch_check": "2026-06-15T12:00:00Z",
|
||||
"fetch_rules": [...],
|
||||
"extra": { "license": "Apache-2.0" }
|
||||
@@ -207,9 +208,9 @@
|
||||
2. 访问 `official_url`,获取所有可用版本列表及最新版本。
|
||||
3. 对比已存储版本,找出缺失的新版本。
|
||||
4. 对于每个新版本,根据 `fetch_rules`(或内置逻辑)匹配下载 URL,下载对应的二进制/压缩包。
|
||||
5. 按 `os` / `arch` / `any` 规则存放到 `{project-id}/{version}/{os}/{arch}/` 下。
|
||||
5. 按 `os` / `arch` / `any` 规则存放到 `{group}/{project-id}/{version}/{os}/{arch}/` 下。
|
||||
6. 计算文件哈希,生成或更新该版本的 `version.json`。
|
||||
7. 更新项目级 `metadata.json`(`versions`、`latest_version`、`last_fetch_check`)。
|
||||
7. 更新项目级 `metadata.json`(`version`、`group`、`last_fetch_check`),不再写入 `versions`。
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
- 每个函数都要有中文注释, 包括功能说明、参数说明、返回值说明等,如果有注意事项,也要在注释中特别说明。
|
||||
- 每次需求修改都要同时更新文档、代码、测试代码。
|
||||
- 每条测试用例都要有一个唯一编号。例如 1-1、2-20 等。
|
||||
@@ -25,3 +25,18 @@ rollback myapp
|
||||
# 删除模块
|
||||
remove myapp
|
||||
```
|
||||
|
||||
## 发布规则
|
||||
|
||||
项目 `meta.json` 支持 `group` 字段:
|
||||
|
||||
- 默认值为 `/modules`。
|
||||
- 空字符串等同于 `/`。
|
||||
- 可配置为 `/docker`、`/tmpls` 等一级路径,也可配置为 `/docker/net` 等子路径。
|
||||
- 远程仓库整体上传路径为 `group/id`,其中 `id` 是项目唯一标识。
|
||||
|
||||
项目 `meta.json` 只使用 `version` 字段,不再使用 `versions`:
|
||||
|
||||
- `version` 有值时,按该版本上传;上传前检查远程 `group/id/version` 是否存在,已存在则报错。
|
||||
- `version` 为空时,先读取远程 `group/id/metadata.json` 中的 `version`,将尾数 +1 后上传。
|
||||
- 远程没有版本信息时,初始版本号为 `1.0`。
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
#!/usr/bin/env node
|
||||
const { execFileSync } = require("child_process");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function exitError(message, code = 1) {
|
||||
console.error("ERROR: " + message);
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
function run(command, args) {
|
||||
return execFileSync(command, args, { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
||||
}
|
||||
|
||||
function ensureDocker() {
|
||||
try {
|
||||
run("docker", ["version", "--format", "{{.Server.Version}}"]);
|
||||
} catch (e) {
|
||||
exitError("docker command is not executable, or Docker daemon is unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
function inspectContainer(container) {
|
||||
try {
|
||||
const text = run("docker", ["inspect", container]);
|
||||
const data = JSON.parse(text);
|
||||
if (!Array.isArray(data) || data.length === 0) {
|
||||
exitError("container not found: " + container);
|
||||
}
|
||||
return data[0];
|
||||
} catch (e) {
|
||||
if (e instanceof SyntaxError) {
|
||||
exitError("docker inspect output is not valid JSON");
|
||||
}
|
||||
exitError("docker inspect failed: " + container);
|
||||
}
|
||||
}
|
||||
|
||||
function quoteSh(value) {
|
||||
if (value === undefined || value === null) {
|
||||
return "''";
|
||||
}
|
||||
const text = String(value);
|
||||
if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(text)) {
|
||||
return text;
|
||||
}
|
||||
return "'" + text.replace(/'/g, "'\\''") + "'";
|
||||
}
|
||||
|
||||
function quoteMountSpec(value) {
|
||||
const text = String(value);
|
||||
if (text.startsWith("$(pwd)")) {
|
||||
const rest = text.slice("$(pwd)".length).replace(/(["\\`$])/g, "\\$1");
|
||||
return '"$(pwd)' + rest + '"';
|
||||
}
|
||||
return quoteSh(text);
|
||||
}
|
||||
|
||||
function normalizeContainerName(name) {
|
||||
return (name || "container").replace(/^\/+/, "") || "container";
|
||||
}
|
||||
|
||||
function isLikelyProjectPath(source) {
|
||||
if (!source) return false;
|
||||
const normalized = source.replace(/\\/g, "/").replace(/\/+$/, "");
|
||||
if (normalized === "/") return false;
|
||||
if (/^[A-Za-z]:$/i.test(normalized)) return false;
|
||||
|
||||
const specialPrefixes = [
|
||||
"/bin", "/boot", "/dev", "/etc", "/lib", "/lib64", "/proc", "/run", "/sbin", "/sys", "/tmp", "/usr", "/var",
|
||||
"/opt/docker", "/var/lib/docker", "/var/run/docker.sock",
|
||||
"C:/Program Files", "C:/Program Files (x86)", "C:/ProgramData", "C:/Windows"
|
||||
];
|
||||
return !specialPrefixes.some(prefix => normalized === prefix || normalized.startsWith(prefix + "/"));
|
||||
}
|
||||
|
||||
function relativeToPwdIfProject(source) {
|
||||
if (!isLikelyProjectPath(source)) {
|
||||
return source;
|
||||
}
|
||||
|
||||
const normalized = source.replace(/\\/g, "/").replace(/\/+$/, "");
|
||||
const base = path.posix.basename(normalized);
|
||||
if (!base || base === "." || base === "/") {
|
||||
return source;
|
||||
}
|
||||
return "$(pwd)/" + base;
|
||||
}
|
||||
|
||||
function collectEnv(config) {
|
||||
const env = Array.isArray(config.Env) ? config.Env : [];
|
||||
return env
|
||||
.filter(item => typeof item === "string" && item.length > 0)
|
||||
.map(item => " -e " + quoteSh(item) + " \\");
|
||||
}
|
||||
|
||||
function collectPorts(networkSettings, hostConfig) {
|
||||
const lines = [];
|
||||
const seen = new Set();
|
||||
const ports = Object.assign({}, networkSettings && networkSettings.Ports ? networkSettings.Ports : {});
|
||||
const bindings = hostConfig && hostConfig.PortBindings ? hostConfig.PortBindings : {};
|
||||
for (const containerPort of Object.keys(bindings)) {
|
||||
if (!ports[containerPort]) ports[containerPort] = bindings[containerPort];
|
||||
}
|
||||
|
||||
for (const containerPort of Object.keys(ports).sort()) {
|
||||
const entries = ports[containerPort];
|
||||
if (!Array.isArray(entries)) continue;
|
||||
for (const entry of entries) {
|
||||
if (!entry || !entry.HostPort) continue;
|
||||
const hostIp = entry.HostIp && entry.HostIp !== "0.0.0.0" && entry.HostIp !== "::" ? entry.HostIp + ":" : "";
|
||||
const spec = hostIp + entry.HostPort + ":" + containerPort;
|
||||
if (seen.has(spec)) continue;
|
||||
seen.add(spec);
|
||||
lines.push(" -p " + quoteSh(spec) + " \\");
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
function collectMounts(info) {
|
||||
const mounts = Array.isArray(info.Mounts) ? info.Mounts : [];
|
||||
const lines = [];
|
||||
for (const mount of mounts) {
|
||||
if (!mount || !mount.Destination) continue;
|
||||
if (mount.Type === "volume" && mount.Name) {
|
||||
let spec = mount.Name + ":" + mount.Destination;
|
||||
if (mount.RW === false) spec += ":ro";
|
||||
lines.push(" -v " + quoteMountSpec(spec) + " \\");
|
||||
continue;
|
||||
}
|
||||
if (mount.Source) {
|
||||
let spec = relativeToPwdIfProject(mount.Source) + ":" + mount.Destination;
|
||||
if (mount.RW === false) spec += ":ro";
|
||||
lines.push(" -v " + quoteMountSpec(spec) + " \\");
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
function collectNetworks(info) {
|
||||
const hostConfig = info.HostConfig || {};
|
||||
const networkMode = hostConfig.NetworkMode;
|
||||
const lines = [];
|
||||
if (networkMode && networkMode !== "default" && networkMode !== "bridge") {
|
||||
lines.push(" --network " + quoteSh(networkMode) + " \\");
|
||||
return lines;
|
||||
}
|
||||
|
||||
const networkSettings = info.NetworkSettings || {};
|
||||
const networks = networkSettings.Networks ? Object.keys(networkSettings.Networks) : [];
|
||||
if (networks.length === 1 && networks[0] !== "bridge") {
|
||||
lines.push(" --network " + quoteSh(networks[0]) + " \\");
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
function collectSimpleOptions(info) {
|
||||
const config = info.Config || {};
|
||||
const hostConfig = info.HostConfig || {};
|
||||
const lines = [];
|
||||
|
||||
if (hostConfig.RestartPolicy && hostConfig.RestartPolicy.Name && hostConfig.RestartPolicy.Name !== "no") {
|
||||
let restart = hostConfig.RestartPolicy.Name;
|
||||
if (restart === "on-failure" && hostConfig.RestartPolicy.MaximumRetryCount) {
|
||||
restart += ":" + hostConfig.RestartPolicy.MaximumRetryCount;
|
||||
}
|
||||
lines.push(" --restart " + quoteSh(restart) + " \\");
|
||||
}
|
||||
if (config.WorkingDir) lines.push(" -w " + quoteSh(config.WorkingDir) + " \\");
|
||||
if (config.User) lines.push(" -u " + quoteSh(config.User) + " \\");
|
||||
if (hostConfig.Privileged) lines.push(" --privileged \\");
|
||||
if (config.Tty) lines.push(" -t \\");
|
||||
if (config.OpenStdin) lines.push(" -i \\");
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
function commandTail(config) {
|
||||
let entrypoint = [];
|
||||
if (Array.isArray(config.Entrypoint)) {
|
||||
entrypoint = config.Entrypoint.filter(item => item !== null && item !== undefined).map(String);
|
||||
} else if (typeof config.Entrypoint === "string" && config.Entrypoint) {
|
||||
entrypoint = [config.Entrypoint];
|
||||
}
|
||||
|
||||
const cmd = Array.isArray(config.Cmd) ? config.Cmd : (typeof config.Cmd === "string" && config.Cmd ? [config.Cmd] : []);
|
||||
return {
|
||||
entrypoint: entrypoint.length > 0 ? entrypoint[0] : null,
|
||||
args: entrypoint.slice(1).concat(cmd)
|
||||
};
|
||||
}
|
||||
|
||||
function buildScript(info) {
|
||||
const config = info.Config || {};
|
||||
const hostConfig = info.HostConfig || {};
|
||||
const name = normalizeContainerName(info.Name);
|
||||
const image = config.Image || info.Image || "";
|
||||
const tail = commandTail(config);
|
||||
const lines = [
|
||||
"#!/usr/bin/env bash",
|
||||
"set -euo pipefail",
|
||||
"",
|
||||
"name=" + quoteSh(name),
|
||||
"img=" + quoteSh(image),
|
||||
"",
|
||||
"docker run -d \\",
|
||||
" --name \"$name\" \\",
|
||||
];
|
||||
|
||||
lines.push(...collectSimpleOptions(info));
|
||||
lines.push(...collectEnv(config));
|
||||
lines.push(...collectPorts(info.NetworkSettings || {}, hostConfig));
|
||||
lines.push(...collectMounts(info));
|
||||
lines.push(...collectNetworks(info));
|
||||
|
||||
if (tail.entrypoint) {
|
||||
lines.push(" --entrypoint " + quoteSh(tail.entrypoint) + " \\");
|
||||
}
|
||||
|
||||
const cmd = tail.args.map(quoteSh).join(" ");
|
||||
if (cmd) {
|
||||
lines.push(" \"$img\" " + cmd);
|
||||
} else {
|
||||
lines.push(" \"$img\"");
|
||||
}
|
||||
|
||||
lines.push("");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function outputFile(container, explicitOutput) {
|
||||
if (explicitOutput) return explicitOutput;
|
||||
const safe = container.replace(/[^A-Za-z0-9_.-]+/g, "_").replace(/^_+|_+$/g, "") || "container";
|
||||
return path.resolve(process.cwd(), "docker-run-" + safe + ".sh");
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length < 1 || args[0] === "-h" || args[0] === "--help") {
|
||||
console.log("Usage: node restore-run.js <container-id-or-name> [output-file]");
|
||||
process.exit(args.length < 1 ? 1 : 0);
|
||||
}
|
||||
|
||||
ensureDocker();
|
||||
const container = args[0];
|
||||
const info = inspectContainer(container);
|
||||
const script = buildScript(info);
|
||||
const file = outputFile(container, args[1]);
|
||||
fs.writeFileSync(file, script, { mode: 0o755 });
|
||||
console.log(file);
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildScript,
|
||||
main
|
||||
};
|
||||
@@ -172,6 +172,13 @@ parser.addCmdLine("freem [level]", "释放内存", function (cli) {
|
||||
});
|
||||
|
||||
|
||||
parser.addCmdLine("docker runsh <container> [--file]", "反向获取 docker 容器启动脚本", function (cli) {
|
||||
let container = cli.getParamValue("container")
|
||||
let file = cli.getParamValue("file")
|
||||
import("./docker/index.js").then(psl => psl.main(container, file))
|
||||
});
|
||||
|
||||
|
||||
parser.addCmdLine("killport <port>", "释放端口", function (cli) {
|
||||
let port = cli.getParamValue("port")
|
||||
net.killport( parseInt(port, 10) );
|
||||
|
||||
+71
-36
@@ -2,6 +2,15 @@
|
||||
const os = require("os");
|
||||
const kitfunction = require("./kitfunction");
|
||||
|
||||
async function getAvailableRemote() {
|
||||
const remote = await $context.get("remote");
|
||||
if (!(await remote.check())) {
|
||||
$logger.error("远程仓库不可用");
|
||||
return null;
|
||||
}
|
||||
return remote;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init : function () {
|
||||
return this;
|
||||
@@ -57,8 +66,6 @@ async function main(){
|
||||
const vm = require('vm');
|
||||
const script = new vm.Script(projectScript + ";" + step + "()");
|
||||
|
||||
// 注入全局变量
|
||||
const kitfunction = require("./kitfunction")
|
||||
const context = kitfunction;
|
||||
|
||||
script.runInNewContext(context, {
|
||||
@@ -83,53 +90,81 @@ async function main(){
|
||||
});
|
||||
|
||||
parser.addCmdLine("remote", "验证远程仓库可达;", async function () {
|
||||
let remote = await $context.get("remote")
|
||||
if (!(await remote.check())) {
|
||||
$logger.error("远程仓库不可用")
|
||||
return
|
||||
} else {
|
||||
$logger.info("远程仓库可用")
|
||||
const remote = await getAvailableRemote();
|
||||
if (remote) {
|
||||
$logger.info("远程仓库可用");
|
||||
}
|
||||
});
|
||||
|
||||
parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库", async function (cli) {
|
||||
parser.addCmdLine("check", "验证远程仓库可达;", async function () {
|
||||
const remote = await getAvailableRemote();
|
||||
if (remote) {
|
||||
$logger.info("远程仓库可用");
|
||||
}
|
||||
});
|
||||
|
||||
parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库; localFile 为空时使用当前目录 meta.json", async function (cli) {
|
||||
const fs = require("fs");
|
||||
let localFile = cli.getParamValue("localFile");
|
||||
if (!fs.existsSync(localFile)) {
|
||||
$logger.error("本地文件不存在")
|
||||
return
|
||||
let remotePath = cli.getParamValue("remotePath");
|
||||
|
||||
if (localFile && !fs.existsSync(localFile)) {
|
||||
$logger.error("本地文件不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
let remotePath = cli.getParamValue("remotePath");
|
||||
if (!remotePath) {
|
||||
$logger.info("使用本地配置路径")
|
||||
if (!fs.existsSync("meta.json")) {
|
||||
$logger.error("没有找到本地配置")
|
||||
return
|
||||
if (!localFile && !fs.existsSync("meta.json")) {
|
||||
$logger.error("没有找到本地配置 meta.json");
|
||||
return;
|
||||
}
|
||||
|
||||
const remote = await getAvailableRemote();
|
||||
if (!remote) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (remotePath) {
|
||||
$logger.info("发布到远程路径 " + remotePath);
|
||||
} else {
|
||||
$logger.info("发布到远程路径 " + remotePath)
|
||||
$logger.info("使用 meta.json 中的 group/id/version 规则发布");
|
||||
}
|
||||
let remote = await $context.get("remote")
|
||||
if (!(await remote.check())) {
|
||||
$logger.error("远程仓库不可用")
|
||||
return
|
||||
} else {
|
||||
$logger.info("远程仓库可用")
|
||||
}
|
||||
await remote.publish(localFile, remotePath)
|
||||
$logger.info("发布成功")
|
||||
await remote.publish(localFile, remotePath);
|
||||
$logger.info("发布成功");
|
||||
});
|
||||
|
||||
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) {
|
||||
let remote = await $context.get("remote")
|
||||
if (!(await remote.check())) {
|
||||
$logger.error("远程仓库不可用")
|
||||
return
|
||||
} else {
|
||||
$logger.info("远程仓库可用")
|
||||
parser.addCmdLine("install [module]", "安装/更新模块; 支持 myapp、myapp@1.0、/docker/net/myapp", async function (cli) {
|
||||
const remote = await getAvailableRemote();
|
||||
if (!remote) {
|
||||
return;
|
||||
}
|
||||
remote.install(cli.getParamValue("module"))
|
||||
await remote.install(cli.getParamValue("module"));
|
||||
});
|
||||
|
||||
parser.addCmdLine("list [module]", "列出本地已安装版本;", async function (cli) {
|
||||
let module = cli.getParamValue("module");
|
||||
if (!module) {
|
||||
module = "kit";
|
||||
}
|
||||
const remote = await $context.get("remote");
|
||||
await remote.list(module);
|
||||
});
|
||||
|
||||
parser.addCmdLine("use <module-version>", "切换到已安装版本,例如 myapp@1.0;", async function (cli) {
|
||||
const remote = await $context.get("remote");
|
||||
const version = await remote.use(cli.getParamValue("module-version"));
|
||||
$logger.info("当前版本 " + version);
|
||||
});
|
||||
|
||||
parser.addCmdLine("rollback <module>", "回滚到上一个已安装版本;", async function (cli) {
|
||||
const remote = await $context.get("remote");
|
||||
const version = await remote.rollback(cli.getParamValue("module"));
|
||||
$logger.info("当前版本 " + version);
|
||||
});
|
||||
|
||||
parser.addCmdLine("remove <module-version>", "删除本地已安装版本,例如 myapp@1.0;", async function (cli) {
|
||||
const remote = await $context.get("remote");
|
||||
await remote.remove(cli.getParamValue("module-version"));
|
||||
$logger.info("删除成功");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+51
-3
@@ -1,4 +1,4 @@
|
||||
const crypto = require("crypto");
|
||||
const crypto = require("crypto");
|
||||
const fs = require("fs");
|
||||
const os = require("os");
|
||||
const path = require("path");
|
||||
@@ -298,17 +298,65 @@ function writeJson(filePath, key, value) {
|
||||
return nextData;
|
||||
}
|
||||
|
||||
function resolvePlainPassword(params) {
|
||||
if (params.plainPwd !== undefined) {
|
||||
return params.plainPwd;
|
||||
}
|
||||
if (params.plainPassword !== undefined) {
|
||||
return params.plainPassword;
|
||||
}
|
||||
if (params.plaintextPwd !== undefined) {
|
||||
return params.plaintextPwd;
|
||||
}
|
||||
if (params.plaintextPassword !== undefined) {
|
||||
return params.plaintextPassword;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function resolveScpPassword(params) {
|
||||
const plainPassword = resolvePlainPassword(params);
|
||||
if (plainPassword !== undefined) {
|
||||
return plainPassword;
|
||||
}
|
||||
return resolveValue(params.pwd || params.password);
|
||||
}
|
||||
|
||||
function scp(from, to, params = {}) {
|
||||
if (!from || !to) {
|
||||
throw new Error("scp from and to are required");
|
||||
}
|
||||
platform.scp(from, to, Object.assign({}, params, { pwd: resolveValue(params.pwd) }));
|
||||
const pwd = resolveScpPassword(params);
|
||||
platform.scp(from, to, Object.assign({}, params, {
|
||||
pwd,
|
||||
password: pwd,
|
||||
key: resolveValue(params.key || params.identityFile),
|
||||
identityFile: resolveValue(params.identityFile || params.key)
|
||||
}));
|
||||
}
|
||||
|
||||
function exit(code = 0) {
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
function isSecretLogKey(key) {
|
||||
return /^(pwd|password|plainPwd|plainPassword|plaintextPwd|plaintextPassword)$/i.test(key);
|
||||
}
|
||||
|
||||
function redactLogValue(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(redactLogValue);
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
const result = {};
|
||||
for (const key of Object.keys(value)) {
|
||||
result[key] = isSecretLogKey(key) ? "******" : redactLogValue(value[key]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function stringifyLogValue(value) {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
@@ -320,7 +368,7 @@ function stringifyLogValue(value) {
|
||||
return `[Function ${value.name || "anonymous"}]`;
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
return JSON.stringify(redactLogValue(value));
|
||||
} catch (e) {
|
||||
return String(value);
|
||||
}
|
||||
|
||||
@@ -50,21 +50,68 @@ function zip(sourceDir, to, params = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
function isRemotePath(value) {
|
||||
if (typeof value !== "string") {
|
||||
return false;
|
||||
}
|
||||
if (/^[A-Za-z]:[\\/]/.test(value)) {
|
||||
return false;
|
||||
}
|
||||
return /^[^\s:]+:.+/.test(value);
|
||||
}
|
||||
|
||||
function resolveScpPath(value) {
|
||||
return isRemotePath(value) ? value : path.resolve(value);
|
||||
}
|
||||
|
||||
function appendScpParams(args, params) {
|
||||
if (params.recursive !== false) {
|
||||
args.push("-r");
|
||||
}
|
||||
if (params.port) {
|
||||
args.push("-P", String(params.port));
|
||||
}
|
||||
if (params.key) {
|
||||
args.push("-i", params.key);
|
||||
}
|
||||
if (params.preserve) {
|
||||
args.push("-p");
|
||||
}
|
||||
if (params.compress) {
|
||||
args.push("-C");
|
||||
}
|
||||
if (params.strictHostKeyChecking !== undefined) {
|
||||
args.push("-o", `StrictHostKeyChecking=${params.strictHostKeyChecking ? "yes" : "no"}`);
|
||||
}
|
||||
if (params.options) {
|
||||
for (const name of Object.keys(params.options)) {
|
||||
args.push("-o", `${name}=${params.options[name]}`);
|
||||
}
|
||||
}
|
||||
if (params.args) {
|
||||
args.push(...(Array.isArray(params.args) ? params.args : [params.args]));
|
||||
}
|
||||
}
|
||||
|
||||
function scp(from, to, params = {}) {
|
||||
const source = path.resolve(from);
|
||||
if (!fs.existsSync(source)) {
|
||||
const source = resolveScpPath(from);
|
||||
if (!isRemotePath(from) && !fs.existsSync(source)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const args = [];
|
||||
appendScpParams(args, params);
|
||||
args.push(source, resolveScpPath(to));
|
||||
|
||||
if (params.pwd) {
|
||||
if (!commandExists("sshpass")) {
|
||||
throw new Error("Linux scp with pwd requires sshpass in PATH");
|
||||
}
|
||||
run("sshpass", ["-p", params.pwd, "scp", "-r", source, to]);
|
||||
run("sshpass", ["-p", params.pwd, "scp", ...args]);
|
||||
return;
|
||||
}
|
||||
|
||||
run("scp", ["-r", source, to]);
|
||||
run("scp", args);
|
||||
}
|
||||
|
||||
module.exports = { zip, scp };
|
||||
|
||||
+43
-48
@@ -12,7 +12,15 @@ function normalizeRemotePath(value) {
|
||||
if (!value || value === "/") {
|
||||
return "/";
|
||||
}
|
||||
return path.posix.normalize("/" + String(value).replace(/\\/g, "/")).replace(/\/+$|^$/g, "") || "/";
|
||||
return path.posix.normalize("/" + String(value).replace(/\\/g, "/")).replace(/\/+$/g, "") || "/";
|
||||
}
|
||||
|
||||
function normalizeGroup(group) {
|
||||
return normalizeRemotePath(group == null ? "/modules" : group);
|
||||
}
|
||||
|
||||
function joinRemote(...items) {
|
||||
return normalizeRemotePath(items.filter((item) => item !== undefined && item !== null && item !== "").join("/"));
|
||||
}
|
||||
|
||||
function normalizeOs(value) {
|
||||
@@ -136,7 +144,8 @@ module.exports = class Remote {
|
||||
}
|
||||
|
||||
async list(projectId) {
|
||||
const state = this.getInstallState(projectId);
|
||||
const parsed = this.parseProjectVersion(projectId);
|
||||
const state = this.getInstallState(parsed.projectId);
|
||||
for (const version of state.installed_versions) {
|
||||
const current = version === state.current_version ? "*" : " ";
|
||||
$logger.info(`${current} ${version}${current === "*" ? " (current)" : ""}`);
|
||||
@@ -150,14 +159,15 @@ module.exports = class Remote {
|
||||
}
|
||||
|
||||
async rollback(projectId) {
|
||||
const state = this.getInstallState(projectId);
|
||||
const parsed = this.parseProjectVersion(projectId);
|
||||
const state = this.getInstallState(parsed.projectId);
|
||||
const history = [...state.history].reverse();
|
||||
const current = state.current_version;
|
||||
const previous = history.find((item) => item.version && item.version !== current && state.installed_versions.includes(item.version));
|
||||
if (!previous) {
|
||||
throw new Error(`没有可回滚版本 ${projectId}`);
|
||||
throw new Error(`没有可回滚版本 ${parsed.projectId}`);
|
||||
}
|
||||
return this.activate(projectId, previous.version, "rollback");
|
||||
return this.activate(parsed.projectId, previous.version, "rollback");
|
||||
}
|
||||
|
||||
async remove(projectVersion) {
|
||||
@@ -183,10 +193,15 @@ module.exports = class Remote {
|
||||
parseProjectVersion(value) {
|
||||
const text = String(value || "kit");
|
||||
const index = text.lastIndexOf("@");
|
||||
if (index <= 0) {
|
||||
return { projectId: text, version: undefined };
|
||||
const target = index <= 0 ? text : text.substring(0, index);
|
||||
const version = index <= 0 ? undefined : text.substring(index + 1);
|
||||
if (target.startsWith("/")) {
|
||||
const normalized = normalizeRemotePath(target);
|
||||
const projectId = path.posix.basename(normalized);
|
||||
const group = normalizeGroup(path.posix.dirname(normalized));
|
||||
return { projectId, version, group };
|
||||
}
|
||||
return { projectId: text.substring(0, index), version: text.substring(index + 1) };
|
||||
return { projectId: target, version, group: "/modules" };
|
||||
}
|
||||
|
||||
getLocalModuleRoot(projectId) {
|
||||
@@ -255,6 +270,10 @@ module.exports = class Remote {
|
||||
];
|
||||
}
|
||||
|
||||
getRemoteProjectRoot(projectId, group = "/modules") {
|
||||
return joinRemote(normalizeGroup(group), projectId);
|
||||
}
|
||||
|
||||
async ensureDir(remotePath) {
|
||||
const target = normalizeRemotePath(remotePath);
|
||||
if (target === "/") {
|
||||
@@ -299,51 +318,29 @@ module.exports = class Remote {
|
||||
return this.ensureDir(folder);
|
||||
}
|
||||
|
||||
async wGetModuleInfo(projectId) {
|
||||
return this.getProjectMetadata(projectId);
|
||||
async wGetModuleInfo(projectId, group = "/modules") {
|
||||
return this.getProjectMetadata(projectId, group);
|
||||
}
|
||||
|
||||
async getProjectMetadata(projectId) {
|
||||
const metadata = await this.getJson(`/${projectId}/metadata.json`, undefined);
|
||||
async getProjectMetadata(projectId, group = "/modules") {
|
||||
const metadata = await this.getJson(joinRemote(this.getRemoteProjectRoot(projectId, group), "metadata.json"), undefined);
|
||||
if (metadata) {
|
||||
metadata.group = normalizeGroup(metadata.group == null ? group : metadata.group);
|
||||
return metadata;
|
||||
}
|
||||
throw new Error(`远程模块不存在 ${normalizeGroup(group)}/${projectId}`);
|
||||
}
|
||||
|
||||
async getVersionMetadata(projectId, version, group = "/modules") {
|
||||
const metadata = await this.getJson(joinRemote(this.getRemoteProjectRoot(projectId, group), version, "version.json"), undefined);
|
||||
if (metadata) {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
const legacy = await this.getJson(`/modules/${projectId}/meta.json`, undefined);
|
||||
if (legacy) {
|
||||
return {
|
||||
project_id: legacy.id || projectId,
|
||||
description: legacy.desc || legacy.name || "",
|
||||
latest_version: legacy.latest && legacy.latest.version,
|
||||
versions: (legacy.versions || []).map((item) => item.version || item),
|
||||
extra: legacy
|
||||
};
|
||||
}
|
||||
throw new Error(`远程模块不存在 ${projectId}`);
|
||||
throw new Error(`远程版本不存在 ${normalizeGroup(group)}/${projectId}@${version}`);
|
||||
}
|
||||
|
||||
async getVersionMetadata(projectId, version) {
|
||||
const metadata = await this.getJson(`/${projectId}/${version}/version.json`, undefined);
|
||||
if (metadata) {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
const project = await this.getProjectMetadata(projectId);
|
||||
const legacyVersion = project.extra && project.extra.versions && project.extra.versions.find((item) => (item.version || item) === version);
|
||||
if (legacyVersion && legacyVersion.dist) {
|
||||
return {
|
||||
version,
|
||||
artifacts: legacyVersion.dist.map((dist) => ({
|
||||
path: `${normalizeOs(dist.os)}/${normalizeArch(dist.platform)}/${dist.filename}`,
|
||||
type: "executable"
|
||||
}))
|
||||
};
|
||||
}
|
||||
throw new Error(`远程版本不存在 ${projectId}@${version}`);
|
||||
}
|
||||
|
||||
async downloadArtifact(projectId, version, artifact, versionDir) {
|
||||
const remotePath = `/${projectId}/${version}/${artifact.path}`;
|
||||
async downloadArtifact(projectId, version, artifact, versionDir, group = "/modules") {
|
||||
const remotePath = joinRemote(this.getRemoteProjectRoot(projectId, group), version, artifact.path);
|
||||
const localPath = path.resolve(versionDir, artifact.path);
|
||||
await this.wDownload(remotePath, localPath);
|
||||
return localPath;
|
||||
@@ -362,5 +359,3 @@ module.exports = class Remote {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -50,13 +50,14 @@ function unpackArtifact(artifact, localPath, versionDir) {
|
||||
module.exports = async function install(remote, id) {
|
||||
const parsed = remote.parseProjectVersion(id || "kit");
|
||||
const projectId = parsed.projectId;
|
||||
const project = await remote.getProjectMetadata(projectId);
|
||||
const version = parsed.version || project.latest_version;
|
||||
const group = parsed.group || "/modules";
|
||||
const project = await remote.getProjectMetadata(projectId, group);
|
||||
const version = parsed.version || project.version;
|
||||
if (!version) {
|
||||
throw new Error(`远程模块没有可安装版本 ${projectId}`);
|
||||
}
|
||||
|
||||
const versionMeta = await remote.getVersionMetadata(projectId, version);
|
||||
const versionMeta = await remote.getVersionMetadata(projectId, version, project.group || group);
|
||||
const artifacts = remote.getMatchingArtifacts(versionMeta);
|
||||
if (artifacts.length < 1) {
|
||||
throw new Error(`没有匹配当前平台的文件 ${projectId}@${version}`);
|
||||
@@ -67,7 +68,7 @@ module.exports = async function install(remote, id) {
|
||||
fs.mkdirSync(versionDir, { recursive: true });
|
||||
|
||||
for (const artifact of artifacts) {
|
||||
const localPath = await remote.downloadArtifact(projectId, version, artifact, versionDir);
|
||||
const localPath = await remote.downloadArtifact(projectId, version, artifact, versionDir, project.group || group);
|
||||
verifyChecksum(localPath, artifact.checksum || (versionMeta.checksums && versionMeta.checksums[artifact.path]));
|
||||
unpackArtifact(artifact, localPath, versionDir);
|
||||
}
|
||||
|
||||
+60
-22
@@ -7,6 +7,21 @@ function now() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function normalizeRemotePath(value) {
|
||||
if (!value || value === "/") {
|
||||
return "/";
|
||||
}
|
||||
return path.posix.normalize("/" + String(value).replace(/\\/g, "/")).replace(/\/+$/g, "") || "/";
|
||||
}
|
||||
|
||||
function normalizeGroup(group) {
|
||||
return normalizeRemotePath(group == null ? "/modules" : group);
|
||||
}
|
||||
|
||||
function joinRemote(...items) {
|
||||
return normalizeRemotePath(items.filter((item) => item !== undefined && item !== null && item !== "").join("/"));
|
||||
}
|
||||
|
||||
function normalizeOs(value) {
|
||||
const name = String(value || "any").toLowerCase();
|
||||
if (["windows_nt", "win32", "windows"].includes(name)) {
|
||||
@@ -70,16 +85,6 @@ function archiveFormat(filePath) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function buildVersion(localMeta) {
|
||||
if (localMeta.release_version) {
|
||||
return localMeta.release_version;
|
||||
}
|
||||
if (localMeta.version) {
|
||||
return localMeta.version;
|
||||
}
|
||||
return new Date().toISOString().replace(/[-:TZ.]/g, "").slice(0, 14);
|
||||
}
|
||||
|
||||
function readLocalMeta(localFile) {
|
||||
const base = localFile ? path.resolve(localFile) : process.cwd();
|
||||
const metaFile = fs.statSync(base).isDirectory() ? path.resolve(base, "meta.json") : path.resolve(process.cwd(), "meta.json");
|
||||
@@ -93,7 +98,8 @@ function readLocalMeta(localFile) {
|
||||
|
||||
return {
|
||||
id: path.basename(localFile, path.extname(localFile)).toLowerCase(),
|
||||
version: buildVersion({}),
|
||||
group: "/modules",
|
||||
version: "",
|
||||
desc: "",
|
||||
dist: [{ path: localFile, os: "any", platform: "any", filename: path.basename(localFile) }]
|
||||
};
|
||||
@@ -123,6 +129,34 @@ function buildArtifacts(localMeta) {
|
||||
});
|
||||
}
|
||||
|
||||
function bumpTailVersion(version) {
|
||||
const text = String(version || "").trim();
|
||||
if (!text) {
|
||||
return "1.0";
|
||||
}
|
||||
const match = text.match(/^(.*?)(\d+)$/);
|
||||
if (!match) {
|
||||
return `${text}.1`;
|
||||
}
|
||||
return `${match[1]}${Number(match[2]) + 1}`;
|
||||
}
|
||||
|
||||
async function resolveVersion(remote, projectRoot, localMeta) {
|
||||
const explicitVersion = String(localMeta.version || "").trim();
|
||||
if (explicitVersion) {
|
||||
if (await remote.client.exists(joinRemote(projectRoot, explicitVersion))) {
|
||||
throw new Error(`远程版本已存在 ${projectRoot}@${explicitVersion}`);
|
||||
}
|
||||
return explicitVersion;
|
||||
}
|
||||
|
||||
const metadata = await remote.getJson(joinRemote(projectRoot, "metadata.json"), undefined);
|
||||
if (metadata && metadata.version) {
|
||||
return bumpTailVersion(metadata.version);
|
||||
}
|
||||
return "1.0";
|
||||
}
|
||||
|
||||
module.exports = async function publish(remote, localFile, remotePath) {
|
||||
if (remotePath) {
|
||||
const target = path.posix.resolve("/", remotePath.replace(/\\/g, "/"));
|
||||
@@ -137,9 +171,11 @@ module.exports = async function publish(remote, localFile, remotePath) {
|
||||
throw new Error("meta.json 缺少 project_id/id/name");
|
||||
}
|
||||
|
||||
const version = buildVersion(localMeta);
|
||||
const group = normalizeGroup(localMeta.group);
|
||||
const projectRoot = remote.getRemoteProjectRoot ? remote.getRemoteProjectRoot(projectId, group) : joinRemote(group, projectId);
|
||||
const version = await resolveVersion(remote, projectRoot, localMeta);
|
||||
const artifacts = buildArtifacts(localMeta);
|
||||
await remote.ensureDir(`/${projectId}/${version}`);
|
||||
await remote.ensureDir(joinRemote(projectRoot, version));
|
||||
|
||||
const versionMeta = {
|
||||
version,
|
||||
@@ -150,27 +186,29 @@ module.exports = async function publish(remote, localFile, remotePath) {
|
||||
};
|
||||
|
||||
for (const artifact of artifacts) {
|
||||
await remote.wUpload(artifact.localPath, `/${projectId}/${version}/${artifact.path}`);
|
||||
await remote.wUpload(artifact.localPath, joinRemote(projectRoot, version, artifact.path));
|
||||
}
|
||||
await remote.putJson(`/${projectId}/${version}/version.json`, versionMeta);
|
||||
await remote.putJson(joinRemote(projectRoot, version, "version.json"), versionMeta);
|
||||
|
||||
const metadata = await remote.getJson(`/${projectId}/metadata.json`, {
|
||||
const metadata = await remote.getJson(joinRemote(projectRoot, "metadata.json"), {
|
||||
project_id: projectId,
|
||||
group,
|
||||
official_url: localMeta.official_url || localMeta.vcs || "",
|
||||
description: localMeta.description || localMeta.desc || "",
|
||||
latest_version: version,
|
||||
versions: [],
|
||||
version,
|
||||
fetch_rules: localMeta.fetch_rules || [],
|
||||
extra: {}
|
||||
});
|
||||
|
||||
metadata.project_id = projectId;
|
||||
metadata.group = group;
|
||||
metadata.official_url = metadata.official_url || localMeta.official_url || localMeta.vcs || "";
|
||||
metadata.description = localMeta.description || localMeta.desc || metadata.description || "";
|
||||
metadata.latest_version = version;
|
||||
metadata.versions = Array.from(new Set([...(metadata.versions || []), version]));
|
||||
metadata.version = version;
|
||||
delete metadata.versions;
|
||||
delete metadata.latest_version;
|
||||
metadata.extra = Object.assign({}, metadata.extra || {}, localMeta.extra || {});
|
||||
|
||||
await remote.putJson(`/${projectId}/metadata.json`, metadata);
|
||||
$logger.info(`文件上传成功 ${projectId}@${version}`);
|
||||
await remote.putJson(joinRemote(projectRoot, "metadata.json"), metadata);
|
||||
$logger.info(`文件上传成功 ${group}/${projectId}@${version}`);
|
||||
};
|
||||
|
||||
@@ -48,9 +48,52 @@ function zip(sourceDir, to, params = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
function isRemotePath(value) {
|
||||
if (typeof value !== "string") {
|
||||
return false;
|
||||
}
|
||||
if (/^[A-Za-z]:[\\/]/.test(value)) {
|
||||
return false;
|
||||
}
|
||||
return /^[^\s:]+:.+/.test(value);
|
||||
}
|
||||
|
||||
function resolveScpPath(value) {
|
||||
return isRemotePath(value) ? value : path.resolve(value);
|
||||
}
|
||||
|
||||
function appendScpParams(args, params, supportOpenSshOptions) {
|
||||
if (params.recursive !== false) {
|
||||
args.push("-r");
|
||||
}
|
||||
if (params.port) {
|
||||
args.push("-P", String(params.port));
|
||||
}
|
||||
if (params.key) {
|
||||
args.push("-i", params.key);
|
||||
}
|
||||
if (params.preserve) {
|
||||
args.push("-p");
|
||||
}
|
||||
if (params.compress) {
|
||||
args.push("-C");
|
||||
}
|
||||
if (supportOpenSshOptions && params.strictHostKeyChecking !== undefined) {
|
||||
args.push("-o", `StrictHostKeyChecking=${params.strictHostKeyChecking ? "yes" : "no"}`);
|
||||
}
|
||||
if (supportOpenSshOptions && params.options) {
|
||||
for (const name of Object.keys(params.options)) {
|
||||
args.push("-o", `${name}=${params.options[name]}`);
|
||||
}
|
||||
}
|
||||
if (params.args) {
|
||||
args.push(...(Array.isArray(params.args) ? params.args : [params.args]));
|
||||
}
|
||||
}
|
||||
|
||||
function scp(from, to, params = {}) {
|
||||
const source = path.resolve(from);
|
||||
if (!fs.existsSync(source)) {
|
||||
const source = resolveScpPath(from);
|
||||
if (!isRemotePath(from) && !fs.existsSync(source)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -59,11 +102,20 @@ function scp(from, to, params = {}) {
|
||||
throw new Error("Windows scp with pwd requires pscp in PATH");
|
||||
}
|
||||
const bin = commandExists("pscp.exe") ? "pscp.exe" : "pscp";
|
||||
run(bin, ["-pw", params.pwd, "-r", source, to]);
|
||||
const args = ["-pw", params.pwd];
|
||||
appendScpParams(args, params, false);
|
||||
args.push(source, resolveScpPath(to));
|
||||
run(bin, args);
|
||||
return;
|
||||
}
|
||||
|
||||
run("scp.exe", ["-r", source, to]);
|
||||
if (!commandExists("scp.exe")) {
|
||||
throw new Error("Windows scp requires scp.exe in PATH");
|
||||
}
|
||||
const args = [];
|
||||
appendScpParams(args, params, true);
|
||||
args.push(source, resolveScpPath(to));
|
||||
run("scp.exe", args);
|
||||
}
|
||||
|
||||
module.exports = { zip, scp };
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const assert = require("assert");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { buildScript } = require("./restore-run");
|
||||
|
||||
const inspectPath = path.join(__dirname, "testcase-inspect.json");
|
||||
const expectedPath = path.join(__dirname, "testcase-run.sh");
|
||||
|
||||
const inspectData = JSON.parse(fs.readFileSync(inspectPath, "utf8"));
|
||||
const info = Array.isArray(inspectData) ? inspectData[0] : inspectData;
|
||||
const actual = buildScript(info);
|
||||
const expected = fs.readFileSync(expectedPath, "utf8").replace(/\r\n/g, "\n");
|
||||
|
||||
assert.strictEqual(actual, expected);
|
||||
assert.ok(actual.includes("name=freshrss"));
|
||||
assert.ok(actual.includes("img=freshrss/freshrss:latest"));
|
||||
assert.ok(actual.includes('-v "$(pwd)/data:/var/www/FreshRSS/data"'));
|
||||
assert.ok(actual.includes('-v "$(pwd)/extensions:/var/www/FreshRSS/extensions"'));
|
||||
assert.strictEqual((actual.match(/-p 8082:80\/tcp/g) || []).length, 1);
|
||||
|
||||
console.log("restore-run testcase passed");
|
||||
@@ -0,0 +1,272 @@
|
||||
[
|
||||
{
|
||||
"Id": "71ba6f6111c86b9cc72fd144717357a40a3c472794bae99cc17b7fd887648338",
|
||||
"Created": "2026-06-09T08:40:16.386743045Z",
|
||||
"Path": "./Docker/entrypoint.sh",
|
||||
"Args": [
|
||||
"/bin/bash",
|
||||
"-o",
|
||||
"pipefail",
|
||||
"-c",
|
||||
"([ -z \"$CRON_MIN\" ] || cron) && \t. /etc/apache2/envvars && \texec apache2 -D FOREGROUND $([ -n \"$OIDC_ENABLED\" ] && [ \"$OIDC_ENABLED\" -ne 0 ] && echo \"-D OIDC_ENABLED\")"
|
||||
],
|
||||
"State": {
|
||||
"Status": "running",
|
||||
"Running": true,
|
||||
"Paused": false,
|
||||
"Restarting": false,
|
||||
"OOMKilled": false,
|
||||
"Dead": false,
|
||||
"Pid": 2178,
|
||||
"ExitCode": 0,
|
||||
"Error": "",
|
||||
"StartedAt": "2026-06-11T03:36:17.818299057Z",
|
||||
"FinishedAt": "2026-06-11T03:36:11.0220238Z"
|
||||
},
|
||||
"Image": "sha256:fc6e4cf934a2d62c5da897c9fd011ef29dc3c83948ef3581d2a526fc90789622",
|
||||
"ResolvConfPath": "/var/lib/docker/containers/71ba6f6111c86b9cc72fd144717357a40a3c472794bae99cc17b7fd887648338/resolv.conf",
|
||||
"HostnamePath": "/var/lib/docker/containers/71ba6f6111c86b9cc72fd144717357a40a3c472794bae99cc17b7fd887648338/hostname",
|
||||
"HostsPath": "/var/lib/docker/containers/71ba6f6111c86b9cc72fd144717357a40a3c472794bae99cc17b7fd887648338/hosts",
|
||||
"LogPath": "/var/lib/docker/containers/71ba6f6111c86b9cc72fd144717357a40a3c472794bae99cc17b7fd887648338/71ba6f6111c86b9cc72fd144717357a40a3c472794bae99cc17b7fd887648338-json.log",
|
||||
"Name": "/freshrss",
|
||||
"RestartCount": 0,
|
||||
"Driver": "overlay2",
|
||||
"Platform": "linux",
|
||||
"MountLabel": "",
|
||||
"ProcessLabel": "",
|
||||
"AppArmorProfile": "docker-default",
|
||||
"ExecIDs": null,
|
||||
"HostConfig": {
|
||||
"Binds": [
|
||||
"/app/freshrss/extensions:/var/www/FreshRSS/extensions",
|
||||
"/app/freshrss/data:/var/www/FreshRSS/data"
|
||||
],
|
||||
"ContainerIDFile": "",
|
||||
"LogConfig": {
|
||||
"Type": "json-file",
|
||||
"Config": {
|
||||
"max-file": "3",
|
||||
"max-size": "10m"
|
||||
}
|
||||
},
|
||||
"NetworkMode": "bridge",
|
||||
"PortBindings": {
|
||||
"80/tcp": [
|
||||
{
|
||||
"HostIp": "",
|
||||
"HostPort": "8082"
|
||||
}
|
||||
]
|
||||
},
|
||||
"RestartPolicy": {
|
||||
"Name": "unless-stopped",
|
||||
"MaximumRetryCount": 0
|
||||
},
|
||||
"AutoRemove": false,
|
||||
"VolumeDriver": "",
|
||||
"VolumesFrom": null,
|
||||
"ConsoleSize": [
|
||||
30,
|
||||
118
|
||||
],
|
||||
"CapAdd": null,
|
||||
"CapDrop": null,
|
||||
"CgroupnsMode": "private",
|
||||
"Dns": [],
|
||||
"DnsOptions": [],
|
||||
"DnsSearch": [],
|
||||
"ExtraHosts": null,
|
||||
"GroupAdd": null,
|
||||
"IpcMode": "private",
|
||||
"Cgroup": "",
|
||||
"Links": null,
|
||||
"OomScoreAdj": 0,
|
||||
"PidMode": "",
|
||||
"Privileged": false,
|
||||
"PublishAllPorts": false,
|
||||
"ReadonlyRootfs": false,
|
||||
"SecurityOpt": null,
|
||||
"UTSMode": "",
|
||||
"UsernsMode": "",
|
||||
"ShmSize": 67108864,
|
||||
"Runtime": "runc",
|
||||
"Isolation": "",
|
||||
"CpuShares": 0,
|
||||
"Memory": 0,
|
||||
"NanoCpus": 0,
|
||||
"CgroupParent": "",
|
||||
"BlkioWeight": 0,
|
||||
"BlkioWeightDevice": [],
|
||||
"BlkioDeviceReadBps": [],
|
||||
"BlkioDeviceWriteBps": [],
|
||||
"BlkioDeviceReadIOps": [],
|
||||
"BlkioDeviceWriteIOps": [],
|
||||
"CpuPeriod": 0,
|
||||
"CpuQuota": 0,
|
||||
"CpuRealtimePeriod": 0,
|
||||
"CpuRealtimeRuntime": 0,
|
||||
"CpusetCpus": "",
|
||||
"CpusetMems": "",
|
||||
"Devices": [],
|
||||
"DeviceCgroupRules": null,
|
||||
"DeviceRequests": null,
|
||||
"MemoryReservation": 0,
|
||||
"MemorySwap": 0,
|
||||
"MemorySwappiness": null,
|
||||
"OomKillDisable": null,
|
||||
"PidsLimit": null,
|
||||
"Ulimits": [],
|
||||
"CpuCount": 0,
|
||||
"CpuPercent": 0,
|
||||
"IOMaximumIOps": 0,
|
||||
"IOMaximumBandwidth": 0,
|
||||
"MaskedPaths": [
|
||||
"/proc/asound",
|
||||
"/proc/acpi",
|
||||
"/proc/kcore",
|
||||
"/proc/keys",
|
||||
"/proc/latency_stats",
|
||||
"/proc/timer_list",
|
||||
"/proc/timer_stats",
|
||||
"/proc/sched_debug",
|
||||
"/proc/scsi",
|
||||
"/sys/firmware",
|
||||
"/sys/devices/virtual/powercap"
|
||||
],
|
||||
"ReadonlyPaths": [
|
||||
"/proc/bus",
|
||||
"/proc/fs",
|
||||
"/proc/irq",
|
||||
"/proc/sys",
|
||||
"/proc/sysrq-trigger"
|
||||
]
|
||||
},
|
||||
"GraphDriver": {
|
||||
"Data": {
|
||||
"LowerDir": "/var/lib/docker/overlay2/d9852f484fa657d12db8ed58e4f61fa9d937a63b2c02326bc859aa03f3c6a6f9-init/diff:/var/lib/docker/overlay2/e16b5110bbde48cd9070a8bea52ca1b49a6c7a6ae17abc23bc2f2152aa851dbb/diff:/var/lib/docker/overlay2/6d104177f8014ee6b291525597ac93c48cce1d6e3f45f8390330dbee8f536d89/diff:/var/lib/docker/overlay2/640758ad6ad59ac4f5825afc463879a4da93264dcbe96cfd06653d494689b1c4/diff:/var/lib/docker/overlay2/51e3461fd7fed56b0595c7c83e8c0edd6a67ae72ef9a59d2a29d6bb808847204/diff:/var/lib/docker/overlay2/b7de361f0da3dea53c4bcc25a9bbde40efaaf7933147da3b95ed8e7d4d718ad4/diff:/var/lib/docker/overlay2/1ee479d5e4ed919312b4792bec9fd9a2425ee199b4b7ff911572f272b2ba2449/diff:/var/lib/docker/overlay2/4d45f37889ffe243f232f86582a4c3b8125b13b397cacfc0193a79af39158190/diff",
|
||||
"MergedDir": "/var/lib/docker/overlay2/d9852f484fa657d12db8ed58e4f61fa9d937a63b2c02326bc859aa03f3c6a6f9/merged",
|
||||
"UpperDir": "/var/lib/docker/overlay2/d9852f484fa657d12db8ed58e4f61fa9d937a63b2c02326bc859aa03f3c6a6f9/diff",
|
||||
"WorkDir": "/var/lib/docker/overlay2/d9852f484fa657d12db8ed58e4f61fa9d937a63b2c02326bc859aa03f3c6a6f9/work"
|
||||
},
|
||||
"Name": "overlay2"
|
||||
},
|
||||
"Mounts": [
|
||||
{
|
||||
"Type": "bind",
|
||||
"Source": "/app/freshrss/data",
|
||||
"Destination": "/var/www/FreshRSS/data",
|
||||
"Mode": "",
|
||||
"RW": true,
|
||||
"Propagation": "rprivate"
|
||||
},
|
||||
{
|
||||
"Type": "bind",
|
||||
"Source": "/app/freshrss/extensions",
|
||||
"Destination": "/var/www/FreshRSS/extensions",
|
||||
"Mode": "",
|
||||
"RW": true,
|
||||
"Propagation": "rprivate"
|
||||
}
|
||||
],
|
||||
"Config": {
|
||||
"Hostname": "71ba6f6111c8",
|
||||
"Domainname": "",
|
||||
"User": "",
|
||||
"AttachStdin": false,
|
||||
"AttachStdout": false,
|
||||
"AttachStderr": false,
|
||||
"ExposedPorts": {
|
||||
"80/tcp": {}
|
||||
},
|
||||
"Tty": false,
|
||||
"OpenStdin": false,
|
||||
"StdinOnce": false,
|
||||
"Env": [
|
||||
"TZ=Asia/Shanghai",
|
||||
"CRON_MIN=*/30",
|
||||
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||
"COPY_LOG_TO_SYSLOG=On",
|
||||
"COPY_SYSLOG_TO_STDERR=On",
|
||||
"DATA_PATH=",
|
||||
"FRESHRSS_ENV=",
|
||||
"LISTEN=",
|
||||
"OIDC_ENABLED=",
|
||||
"TRUSTED_PROXY="
|
||||
],
|
||||
"Cmd": [
|
||||
"/bin/bash",
|
||||
"-o",
|
||||
"pipefail",
|
||||
"-c",
|
||||
"([ -z \"$CRON_MIN\" ] || cron) && \t. /etc/apache2/envvars && \texec apache2 -D FOREGROUND $([ -n \"$OIDC_ENABLED\" ] && [ \"$OIDC_ENABLED\" -ne 0 ] && echo \"-D OIDC_ENABLED\")"
|
||||
],
|
||||
"Image": "freshrss/freshrss:latest",
|
||||
"Volumes": null,
|
||||
"WorkingDir": "/var/www/FreshRSS",
|
||||
"Entrypoint": [
|
||||
"./Docker/entrypoint.sh"
|
||||
],
|
||||
"OnBuild": null,
|
||||
"Labels": {
|
||||
"org.opencontainers.image.created": "2026-05-20T17:58:29.110Z",
|
||||
"org.opencontainers.image.description": "A free, self-hostable news aggregator…",
|
||||
"org.opencontainers.image.documentation": "https://freshrss.github.io/FreshRSS/",
|
||||
"org.opencontainers.image.licenses": "AGPL-3.0",
|
||||
"org.opencontainers.image.revision": "b2c50115baa36c217e939ee3ea8ecfae52f91abd",
|
||||
"org.opencontainers.image.source": "https://github.com/FreshRSS/FreshRSS",
|
||||
"org.opencontainers.image.title": "FreshRSS",
|
||||
"org.opencontainers.image.url": "https://freshrss.org/",
|
||||
"org.opencontainers.image.vendor": "FreshRSS",
|
||||
"org.opencontainers.image.version": "1.29.1"
|
||||
}
|
||||
},
|
||||
"NetworkSettings": {
|
||||
"Bridge": "",
|
||||
"SandboxID": "78f7f8cccc0810fcdadb3d0e7bff0fe16156a46ab4b0feb7a4ebd6e80e5e330d",
|
||||
"SandboxKey": "/var/run/docker/netns/78f7f8cccc08",
|
||||
"Ports": {
|
||||
"80/tcp": [
|
||||
{
|
||||
"HostIp": "0.0.0.0",
|
||||
"HostPort": "8082"
|
||||
},
|
||||
{
|
||||
"HostIp": "::",
|
||||
"HostPort": "8082"
|
||||
}
|
||||
]
|
||||
},
|
||||
"HairpinMode": false,
|
||||
"LinkLocalIPv6Address": "",
|
||||
"LinkLocalIPv6PrefixLen": 0,
|
||||
"SecondaryIPAddresses": null,
|
||||
"SecondaryIPv6Addresses": null,
|
||||
"EndpointID": "14c4ae2de08b7c68dfe8a757933088247f4c4ffd8c3d1236ff919da0422a655c",
|
||||
"Gateway": "172.17.0.1",
|
||||
"GlobalIPv6Address": "",
|
||||
"GlobalIPv6PrefixLen": 0,
|
||||
"IPAddress": "172.17.0.3",
|
||||
"IPPrefixLen": 16,
|
||||
"IPv6Gateway": "",
|
||||
"MacAddress": "02:42:ac:11:00:03",
|
||||
"Networks": {
|
||||
"bridge": {
|
||||
"IPAMConfig": null,
|
||||
"Links": null,
|
||||
"Aliases": null,
|
||||
"MacAddress": "02:42:ac:11:00:03",
|
||||
"DriverOpts": null,
|
||||
"NetworkID": "2e5dc7af2729566328476edc3fe4594e9f6b39adabbdcda5b7cc26fcbe073224",
|
||||
"EndpointID": "14c4ae2de08b7c68dfe8a757933088247f4c4ffd8c3d1236ff919da0422a655c",
|
||||
"Gateway": "172.17.0.1",
|
||||
"IPAddress": "172.17.0.3",
|
||||
"IPPrefixLen": 16,
|
||||
"IPv6Gateway": "",
|
||||
"GlobalIPv6Address": "",
|
||||
"GlobalIPv6PrefixLen": 0,
|
||||
"DNSNames": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
name=freshrss
|
||||
img=freshrss/freshrss:latest
|
||||
|
||||
docker run -d \
|
||||
--name "$name" \
|
||||
--restart unless-stopped \
|
||||
-w /var/www/FreshRSS \
|
||||
-e TZ=Asia/Shanghai \
|
||||
-e 'CRON_MIN=*/30' \
|
||||
-e PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
|
||||
-e COPY_LOG_TO_SYSLOG=On \
|
||||
-e COPY_SYSLOG_TO_STDERR=On \
|
||||
-e DATA_PATH= \
|
||||
-e FRESHRSS_ENV= \
|
||||
-e LISTEN= \
|
||||
-e OIDC_ENABLED= \
|
||||
-e TRUSTED_PROXY= \
|
||||
-p 8082:80/tcp \
|
||||
-v "$(pwd)/data:/var/www/FreshRSS/data" \
|
||||
-v "$(pwd)/extensions:/var/www/FreshRSS/extensions" \
|
||||
--entrypoint ./Docker/entrypoint.sh \
|
||||
"$img" /bin/bash -o pipefail -c '([ -z "$CRON_MIN" ] || cron) && . /etc/apache2/envvars && exec apache2 -D FOREGROUND $([ -n "$OIDC_ENABLED" ] && [ "$OIDC_ENABLED" -ne 0 ] && echo "-D OIDC_ENABLED")'
|
||||
@@ -0,0 +1,260 @@
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
|
||||
const RUN_MANUAL = process.env.KIT_REMOTE_MANU_TEST === "1";
|
||||
const manualDescribe = RUN_MANUAL ? describe : describe.skip;
|
||||
const manualTest = RUN_MANUAL ? test : test.skip;
|
||||
|
||||
let Remote;
|
||||
if (RUN_MANUAL) {
|
||||
require("../src/init");
|
||||
Remote = require("../src/remote");
|
||||
}
|
||||
|
||||
async function createRemote() {
|
||||
const remote = new Remote();
|
||||
await remote.init();
|
||||
return remote;
|
||||
}
|
||||
|
||||
function writeFixtureProject(tempDir, version, content, options = {}) {
|
||||
const projectId = options.id || "myapp";
|
||||
const group = options.group === undefined ? "/modules" : options.group;
|
||||
const projectDir = path.resolve(tempDir, `fixture-${projectId}-${version || "auto"}-${Math.random().toString(16).slice(2)}`);
|
||||
fs.mkdirSync(projectDir, { recursive: true });
|
||||
|
||||
const filePath = path.resolve(projectDir, "myapp.txt");
|
||||
fs.writeFileSync(filePath, content, "utf-8");
|
||||
|
||||
fs.writeFileSync(path.resolve(projectDir, "meta.json"), JSON.stringify({
|
||||
id: projectId,
|
||||
group,
|
||||
version,
|
||||
desc: "test app",
|
||||
official_url: "https://example.test/myapp",
|
||||
dist: [
|
||||
{
|
||||
path: filePath,
|
||||
os: "any",
|
||||
platform: "any",
|
||||
filename: "myapp.txt",
|
||||
type: "data"
|
||||
}
|
||||
]
|
||||
}, null, 4), "utf-8");
|
||||
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
function normalizeRemotePath(value) {
|
||||
if (!value || value === "/") {
|
||||
return "/";
|
||||
}
|
||||
return path.posix.normalize("/" + String(value).replace(/\\/g, "/")).replace(/\/+$/g, "") || "/";
|
||||
}
|
||||
|
||||
function remoteProjectPath(group, projectId) {
|
||||
const normalizedGroup = group === undefined ? "/modules" : normalizeRemotePath(group);
|
||||
return normalizeRemotePath(path.posix.join(normalizedGroup, projectId));
|
||||
}
|
||||
|
||||
async function deleteRemotePath(remote, remotePath) {
|
||||
try {
|
||||
if (remote.client && remote.client.deleteFile && await remote.client.exists(remotePath)) {
|
||||
await remote.client.deleteFile(remotePath);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`cleanup remote failed ${remotePath}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
let tempDir;
|
||||
let localStore;
|
||||
let remote;
|
||||
let runId;
|
||||
let counter;
|
||||
let projectIds;
|
||||
let remoteRoots;
|
||||
|
||||
beforeEach(async () => {
|
||||
if (!RUN_MANUAL) {
|
||||
return;
|
||||
}
|
||||
tempDir = fs.mkdtempSync(path.resolve(os.tmpdir(), "kit-remote-manu-test-"));
|
||||
localStore = $sConfig.getLocalStorePath();
|
||||
remote = await createRemote();
|
||||
runId = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||||
counter = 0;
|
||||
projectIds = [];
|
||||
remoteRoots = [];
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (!RUN_MANUAL) {
|
||||
return;
|
||||
}
|
||||
for (const remoteRoot of remoteRoots.reverse()) {
|
||||
await deleteRemotePath(remote, remoteRoot);
|
||||
}
|
||||
for (const projectId of projectIds) {
|
||||
fs.rmSync(path.resolve(localStore, "modules", projectId), { recursive: true, force: true });
|
||||
}
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function nextProjectId() {
|
||||
const projectId = `myapp-${runId}-${++counter}`.toLowerCase();
|
||||
projectIds.push(projectId);
|
||||
return projectId;
|
||||
}
|
||||
|
||||
function trackRemote(group, projectId) {
|
||||
const root = remoteProjectPath(group, projectId);
|
||||
remoteRoots.push(root);
|
||||
return root;
|
||||
}
|
||||
|
||||
manualDescribe("remote module public API manual webdav", () => {
|
||||
manualTest("1-1 exposes documented public methods", async () => {
|
||||
expect(typeof remote.check).toBe("function");
|
||||
expect(typeof remote.publish).toBe("function");
|
||||
expect(typeof remote.install).toBe("function");
|
||||
expect(typeof remote.list).toBe("function");
|
||||
expect(typeof remote.use).toBe("function");
|
||||
expect(typeof remote.rollback).toBe("function");
|
||||
expect(typeof remote.remove).toBe("function");
|
||||
});
|
||||
|
||||
manualTest("1-2 check validates an available webdav client", async () => {
|
||||
expect(await remote.check()).toBe(true);
|
||||
});
|
||||
|
||||
manualTest("1-3 publish stores files under default group /modules/id", async () => {
|
||||
const projectId = nextProjectId();
|
||||
trackRemote("/modules", projectId);
|
||||
const projectDir = writeFixtureProject(tempDir, "1.0", "hello v1", { id: projectId });
|
||||
|
||||
await remote.publish(projectDir);
|
||||
|
||||
const metadata = JSON.parse(await remote.client.getFileContents(`/modules/${projectId}/metadata.json`, { format: "text" }));
|
||||
const version = JSON.parse(await remote.client.getFileContents(`/modules/${projectId}/1.0/version.json`, { format: "text" }));
|
||||
const artifact = await remote.client.getFileContents(`/modules/${projectId}/1.0/any/any/myapp.txt`, { format: "text" });
|
||||
|
||||
expect(metadata.project_id).toBe(projectId);
|
||||
expect(metadata.group).toBe("/modules");
|
||||
expect(metadata.version).toBe("1.0");
|
||||
expect(metadata.versions).toBeUndefined();
|
||||
expect(metadata.latest_version).toBeUndefined();
|
||||
expect(version.artifacts[0].path).toBe("any/any/myapp.txt");
|
||||
expect(artifact).toBe("hello v1");
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-4 publish supports custom nested group", async () => {
|
||||
const projectId = nextProjectId();
|
||||
const group = `/kit-manual-test/${runId}/docker/net`;
|
||||
trackRemote(group, projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "2.0", "docker net", { id: projectId, group }));
|
||||
|
||||
const metadata = JSON.parse(await remote.client.getFileContents(`${group}/${projectId}/metadata.json`, { format: "text" }));
|
||||
const artifact = await remote.client.getFileContents(`${group}/${projectId}/2.0/any/any/myapp.txt`, { format: "text" });
|
||||
|
||||
expect(metadata.group).toBe(group);
|
||||
expect(metadata.version).toBe("2.0");
|
||||
expect(artifact).toBe("docker net");
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-5 empty group stores project at repository root", async () => {
|
||||
const projectId = nextProjectId();
|
||||
trackRemote("", projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "root group", { id: projectId, group: "" }));
|
||||
|
||||
const metadata = JSON.parse(await remote.client.getFileContents(`/${projectId}/metadata.json`, { format: "text" }));
|
||||
expect(metadata.group).toBe("/");
|
||||
expect(await remote.client.getFileContents(`/${projectId}/1.0/any/any/myapp.txt`, { format: "text" })).toBe("root group");
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-6 explicit version fails when remote version already exists", async () => {
|
||||
const projectId = nextProjectId();
|
||||
trackRemote("/modules", projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "hello v1", { id: projectId }));
|
||||
|
||||
await expect(remote.publish(writeFixtureProject(tempDir, "1.0", "hello again", { id: projectId }))).rejects.toThrow("远程版本已存在");
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-7 blank version increments remote version tail or starts at 1.0", async () => {
|
||||
const projectId = nextProjectId();
|
||||
trackRemote("/modules", projectId);
|
||||
|
||||
await remote.publish(writeFixtureProject(tempDir, "", "first auto", { id: projectId }));
|
||||
let metadata = JSON.parse(await remote.client.getFileContents(`/modules/${projectId}/metadata.json`, { format: "text" }));
|
||||
expect(metadata.version).toBe("1.0");
|
||||
|
||||
await remote.publish(writeFixtureProject(tempDir, "", "second auto", { id: projectId }));
|
||||
metadata = JSON.parse(await remote.client.getFileContents(`/modules/${projectId}/metadata.json`, { format: "text" }));
|
||||
expect(metadata.version).toBe("1.1");
|
||||
expect(await remote.client.getFileContents(`/modules/${projectId}/1.1/any/any/myapp.txt`, { format: "text" })).toBe("second auto");
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-8 install downloads latest version from default group", async () => {
|
||||
const projectId = nextProjectId();
|
||||
trackRemote("/modules", projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "hello v1", { id: projectId }));
|
||||
|
||||
const installed = await remote.install(projectId);
|
||||
|
||||
const moduleRoot = path.resolve(localStore, "modules", projectId);
|
||||
const installJson = JSON.parse(fs.readFileSync(path.resolve(moduleRoot, ".meta", "install.json"), "utf-8"));
|
||||
const installedFile = fs.readFileSync(path.resolve(moduleRoot, "versions", "1.0", "any", "any", "myapp.txt"), "utf-8");
|
||||
|
||||
expect(installed).toBe("1.0");
|
||||
expect(installedFile).toBe("hello v1");
|
||||
expect(installJson.current_version).toBe("1.0");
|
||||
expect(installJson.installed_versions).toContain("1.0");
|
||||
expect(fs.existsSync(path.resolve(moduleRoot, "current"))).toBe(true);
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-9 install can target a custom group path", async () => {
|
||||
const projectId = nextProjectId();
|
||||
const group = `/kit-manual-test/${runId}/docker/net`;
|
||||
trackRemote(group, projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "docker install", { id: projectId, group }));
|
||||
|
||||
const installed = await remote.install(`${group}/${projectId}`);
|
||||
const installedFile = fs.readFileSync(path.resolve(localStore, "modules", projectId, "versions", "1.0", "any", "any", "myapp.txt"), "utf-8");
|
||||
|
||||
expect(installed).toBe("1.0");
|
||||
expect(installedFile).toBe("docker install");
|
||||
}, 30000);
|
||||
|
||||
manualTest("1-10 list use rollback and remove manage installed versions", async () => {
|
||||
const projectId = nextProjectId();
|
||||
trackRemote("/modules", projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "hello v1", { id: projectId }));
|
||||
await remote.install(projectId);
|
||||
await remote.publish(writeFixtureProject(tempDir, "2.0", "hello v2", { id: projectId }));
|
||||
await remote.install(projectId);
|
||||
|
||||
expect(await remote.list(projectId)).toEqual(["1.0", "2.0"]);
|
||||
|
||||
await remote.rollback(projectId);
|
||||
let installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", projectId, ".meta", "install.json"), "utf-8"));
|
||||
expect(installJson.current_version).toBe("1.0");
|
||||
|
||||
await remote.use(`${projectId}@2.0`);
|
||||
installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", projectId, ".meta", "install.json"), "utf-8"));
|
||||
expect(installJson.current_version).toBe("2.0");
|
||||
|
||||
await remote.remove(`${projectId}@1.0`);
|
||||
installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", projectId, ".meta", "install.json"), "utf-8"));
|
||||
expect(installJson.installed_versions).toEqual(["2.0"]);
|
||||
expect(fs.existsSync(path.resolve(localStore, "modules", projectId, "versions", "1.0"))).toBe(false);
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
if (!RUN_MANUAL) {
|
||||
describe("remote manual webdav tests", () => {
|
||||
test.skip("set KIT_REMOTE_MANU_TEST=1 and run bun test test/remote_manu_test.js", () => {});
|
||||
});
|
||||
}
|
||||
+214
-36
@@ -2,30 +2,155 @@
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import { Readable, Writable } from "stream";
|
||||
import Remote from "../src/remote";
|
||||
|
||||
require("../src/init")
|
||||
async function createRemote() {
|
||||
const Remote = require("../src/remote")
|
||||
class MemoryWebdavClient {
|
||||
constructor() {
|
||||
this.files = new Map();
|
||||
this.dirs = new Set(["/"]);
|
||||
}
|
||||
|
||||
normalize(remotePath) {
|
||||
const normalized = path.posix.normalize("/" + String(remotePath || "").replace(/\\/g, "/"));
|
||||
return normalized === "/" ? "/" : normalized.replace(/\/$/, "");
|
||||
}
|
||||
|
||||
async exists(remotePath) {
|
||||
const key = this.normalize(remotePath);
|
||||
return this.dirs.has(key) || this.files.has(key);
|
||||
}
|
||||
|
||||
async createDirectory(remotePath) {
|
||||
const key = this.normalize(remotePath);
|
||||
const parts = key.split("/").filter(Boolean);
|
||||
let current = "";
|
||||
for (const part of parts) {
|
||||
current += "/" + part;
|
||||
this.dirs.add(current);
|
||||
}
|
||||
}
|
||||
|
||||
async getDirectoryContents(remotePath) {
|
||||
const dir = this.normalize(remotePath);
|
||||
const prefix = dir === "/" ? "/" : dir + "/";
|
||||
const names = new Map();
|
||||
for (const item of [...this.dirs, ...this.files.keys()]) {
|
||||
if (item === dir || !item.startsWith(prefix)) {
|
||||
continue;
|
||||
}
|
||||
const rest = item.slice(prefix.length);
|
||||
const name = rest.split("/")[0];
|
||||
names.set(name, prefix + name);
|
||||
}
|
||||
return [...names].map(([basename, filename]) => ({ basename, filename, type: this.dirs.has(this.normalize(filename)) ? "directory" : "file" }));
|
||||
}
|
||||
|
||||
async putFileContents(remotePath, content) {
|
||||
const key = this.normalize(remotePath);
|
||||
await this.createDirectory(path.posix.dirname(key));
|
||||
this.files.set(key, Buffer.isBuffer(content) ? content : Buffer.from(String(content)));
|
||||
}
|
||||
|
||||
async getFileContents(remotePath, options = {}) {
|
||||
const key = this.normalize(remotePath);
|
||||
if (!this.files.has(key)) {
|
||||
throw new Error(`missing ${key}`);
|
||||
}
|
||||
const data = this.files.get(key);
|
||||
return options.format === "text" ? data.toString("utf-8") : data;
|
||||
}
|
||||
|
||||
createWriteStream(remotePath) {
|
||||
const key = this.normalize(remotePath);
|
||||
const chunks = [];
|
||||
const stream = new Writable({
|
||||
write(chunk, encoding, callback) {
|
||||
chunks.push(Buffer.from(chunk));
|
||||
callback();
|
||||
}
|
||||
});
|
||||
stream.on("finish", () => {
|
||||
this.createDirectory(path.posix.dirname(key));
|
||||
this.files.set(key, Buffer.concat(chunks));
|
||||
});
|
||||
return stream;
|
||||
}
|
||||
|
||||
createReadStream(remotePath) {
|
||||
const key = this.normalize(remotePath);
|
||||
if (!this.files.has(key)) {
|
||||
throw new Error(`missing ${key}`);
|
||||
}
|
||||
return Readable.from(this.files.get(key));
|
||||
}
|
||||
|
||||
async stat(remotePath) {
|
||||
const key = this.normalize(remotePath);
|
||||
const data = this.files.get(key);
|
||||
if (!data) {
|
||||
throw new Error(`missing ${key}`);
|
||||
}
|
||||
return { size: data.length };
|
||||
}
|
||||
}
|
||||
|
||||
function writeFixtureProject(tempDir, version, content, group = "/modules") {
|
||||
const projectDir = path.resolve(tempDir, `fixture-${version || "auto"}-${Math.random().toString(16).slice(2)}`);
|
||||
fs.mkdirSync(projectDir, { recursive: true });
|
||||
|
||||
const filePath = path.resolve(projectDir, "myapp.txt");
|
||||
fs.writeFileSync(filePath, content, "utf-8");
|
||||
|
||||
fs.writeFileSync(path.resolve(projectDir, "meta.json"), JSON.stringify({
|
||||
id: "myapp",
|
||||
group,
|
||||
version,
|
||||
desc: "test app",
|
||||
official_url: "https://example.test/myapp",
|
||||
dist: [
|
||||
{
|
||||
path: filePath,
|
||||
os: "any",
|
||||
platform: "any",
|
||||
filename: "myapp.txt",
|
||||
type: "data"
|
||||
}
|
||||
]
|
||||
}, null, 4), "utf-8");
|
||||
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
function createRemote() {
|
||||
const remote = new Remote();
|
||||
await remote.init()
|
||||
return remote
|
||||
remote.client = new MemoryWebdavClient();
|
||||
return remote;
|
||||
}
|
||||
|
||||
let tempDir;
|
||||
let localStore;
|
||||
let oldLogger;
|
||||
let oldConfig;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = fs.mkdtempSync(path.resolve(os.tmpdir(), "kit-remote-test"));
|
||||
localStore = $sConfig.getLocalStorePath();
|
||||
tempDir = fs.mkdtempSync(path.resolve(os.tmpdir(), "kit-remote-test-"));
|
||||
localStore = path.resolve(tempDir, "local-store");
|
||||
oldLogger = globalThis.$logger;
|
||||
oldConfig = globalThis.$sConfig;
|
||||
globalThis.$logger = { info() {}, warn() {}, error() {}, debug() {} };
|
||||
globalThis.$sConfig = { getLocalStorePath: () => localStore };
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.$logger = oldLogger;
|
||||
globalThis.$sConfig = oldConfig;
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("remote module public API", () => {
|
||||
test("exposes documented public methods", async() => {
|
||||
const remote = await createRemote();
|
||||
test("exposes documented public methods", () => {
|
||||
const remote = createRemote();
|
||||
expect(typeof remote.check).toBe("function");
|
||||
expect(typeof remote.publish).toBe("function");
|
||||
expect(typeof remote.install).toBe("function");
|
||||
@@ -36,65 +161,118 @@ describe("remote module public API", () => {
|
||||
});
|
||||
|
||||
test("check validates an available webdav client", async () => {
|
||||
const remote = await createRemote();
|
||||
const remote = createRemote();
|
||||
expect(await remote.check()).toBe(true);
|
||||
});
|
||||
|
||||
test("publish stores metadata and version artifacts in webdav layout", async () => {
|
||||
const remote = await createRemote();
|
||||
const projectDir = writeFixtureProject(tempDir, "1.0.0", "hello v1");
|
||||
test("publish stores files under default group /modules/id", async () => {
|
||||
const remote = createRemote();
|
||||
const projectDir = writeFixtureProject(tempDir, "1.0", "hello v1");
|
||||
|
||||
await remote.publish(projectDir);
|
||||
|
||||
const metadata = JSON.parse(await remote.client.getFileContents("/myapp/metadata.json", { format: "text" }));
|
||||
const version = JSON.parse(await remote.client.getFileContents("/myapp/1.0.0/version.json", { format: "text" }));
|
||||
const artifact = await remote.client.getFileContents("/myapp/1.0.0/any/any/myapp.txt", { format: "text" });
|
||||
const metadata = JSON.parse(await remote.client.getFileContents("/modules/myapp/metadata.json", { format: "text" }));
|
||||
const version = JSON.parse(await remote.client.getFileContents("/modules/myapp/1.0/version.json", { format: "text" }));
|
||||
const artifact = await remote.client.getFileContents("/modules/myapp/1.0/any/any/myapp.txt", { format: "text" });
|
||||
|
||||
expect(metadata.project_id).toBe("myapp");
|
||||
expect(metadata.latest_version).toBe("1.0.0");
|
||||
expect(metadata.versions).toContain("1.0.0");
|
||||
expect(metadata.group).toBe("/modules");
|
||||
expect(metadata.version).toBe("1.0");
|
||||
expect(metadata.versions).toBeUndefined();
|
||||
expect(metadata.latest_version).toBeUndefined();
|
||||
expect(version.artifacts[0].path).toBe("any/any/myapp.txt");
|
||||
expect(artifact).toBe("hello v1");
|
||||
});
|
||||
|
||||
test("install downloads latest matching artifact and records local state", async () => {
|
||||
const remote = await createRemote();
|
||||
const projectDir = writeFixtureProject(tempDir, "1.0.0", "hello v1");
|
||||
await remote.publish(projectDir);
|
||||
test("publish supports custom nested group", async () => {
|
||||
const remote = createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "2.0", "docker net", "/docker/net"));
|
||||
|
||||
const metadata = JSON.parse(await remote.client.getFileContents("/docker/net/myapp/metadata.json", { format: "text" }));
|
||||
const artifact = await remote.client.getFileContents("/docker/net/myapp/2.0/any/any/myapp.txt", { format: "text" });
|
||||
|
||||
expect(metadata.group).toBe("/docker/net");
|
||||
expect(metadata.version).toBe("2.0");
|
||||
expect(artifact).toBe("docker net");
|
||||
});
|
||||
|
||||
test("empty group stores project at repository root", async () => {
|
||||
const remote = createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "root group", ""));
|
||||
|
||||
const metadata = JSON.parse(await remote.client.getFileContents("/myapp/metadata.json", { format: "text" }));
|
||||
expect(metadata.group).toBe("/");
|
||||
expect(await remote.client.getFileContents("/myapp/1.0/any/any/myapp.txt", { format: "text" })).toBe("root group");
|
||||
});
|
||||
|
||||
test("explicit version fails when remote version already exists", async () => {
|
||||
const remote = createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "hello v1"));
|
||||
|
||||
await expect(remote.publish(writeFixtureProject(tempDir, "1.0", "hello again"))).rejects.toThrow("远程版本已存在");
|
||||
});
|
||||
|
||||
test("blank version increments remote version tail or starts at 1.0", async () => {
|
||||
const remote = createRemote();
|
||||
|
||||
await remote.publish(writeFixtureProject(tempDir, "", "first auto"));
|
||||
let metadata = JSON.parse(await remote.client.getFileContents("/modules/myapp/metadata.json", { format: "text" }));
|
||||
expect(metadata.version).toBe("1.0");
|
||||
|
||||
await remote.publish(writeFixtureProject(tempDir, "", "second auto"));
|
||||
metadata = JSON.parse(await remote.client.getFileContents("/modules/myapp/metadata.json", { format: "text" }));
|
||||
expect(metadata.version).toBe("1.1");
|
||||
expect(await remote.client.getFileContents("/modules/myapp/1.1/any/any/myapp.txt", { format: "text" })).toBe("second auto");
|
||||
});
|
||||
|
||||
test("install downloads latest version from default group", async () => {
|
||||
const remote = createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "hello v1"));
|
||||
|
||||
const installed = await remote.install("myapp");
|
||||
|
||||
const moduleRoot = path.resolve(localStore, "modules", "myapp");
|
||||
const installJson = JSON.parse(fs.readFileSync(path.resolve(moduleRoot, ".meta", "install.json"), "utf-8"));
|
||||
const installedFile = fs.readFileSync(path.resolve(moduleRoot, "versions", "1.0.0", "any", "any", "myapp.txt"), "utf-8");
|
||||
const installedFile = fs.readFileSync(path.resolve(moduleRoot, "versions", "1.0", "any", "any", "myapp.txt"), "utf-8");
|
||||
|
||||
expect(installed).toBe("1.0.0");
|
||||
expect(installed).toBe("1.0");
|
||||
expect(installedFile).toBe("hello v1");
|
||||
expect(installJson.current_version).toBe("1.0.0");
|
||||
expect(installJson.installed_versions).toContain("1.0.0");
|
||||
expect(installJson.current_version).toBe("1.0");
|
||||
expect(installJson.installed_versions).toContain("1.0");
|
||||
expect(fs.existsSync(path.resolve(moduleRoot, "current"))).toBe(true);
|
||||
});
|
||||
|
||||
test("install can target a custom group path", async () => {
|
||||
const remote = createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "docker install", "/docker/net"));
|
||||
|
||||
const installed = await remote.install("/docker/net/myapp");
|
||||
const installedFile = fs.readFileSync(path.resolve(localStore, "modules", "myapp", "versions", "1.0", "any", "any", "myapp.txt"), "utf-8");
|
||||
|
||||
expect(installed).toBe("1.0");
|
||||
expect(installedFile).toBe("docker install");
|
||||
});
|
||||
|
||||
test("list use rollback and remove manage installed versions", async () => {
|
||||
const remote = await createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0.0", "hello v1"));
|
||||
const remote = createRemote();
|
||||
await remote.publish(writeFixtureProject(tempDir, "1.0", "hello v1"));
|
||||
await remote.install("myapp");
|
||||
await remote.publish(writeFixtureProject(tempDir, "2.0.0", "hello v2"));
|
||||
await remote.publish(writeFixtureProject(tempDir, "2.0", "hello v2"));
|
||||
await remote.install("myapp");
|
||||
|
||||
expect(await remote.list("myapp")).toEqual(["1.0.0", "2.0.0"]);
|
||||
expect(await remote.list("myapp")).toEqual(["1.0", "2.0"]);
|
||||
|
||||
await remote.rollback("myapp");
|
||||
let installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", "myapp", ".meta", "install.json"), "utf-8"));
|
||||
expect(installJson.current_version).toBe("1.0.0");
|
||||
expect(installJson.current_version).toBe("1.0");
|
||||
|
||||
await remote.use("myapp@2.0.0");
|
||||
await remote.use("myapp@2.0");
|
||||
installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", "myapp", ".meta", "install.json"), "utf-8"));
|
||||
expect(installJson.current_version).toBe("2.0.0");
|
||||
expect(installJson.current_version).toBe("2.0");
|
||||
|
||||
await remote.remove("myapp@1.0.0");
|
||||
await remote.remove("myapp@1.0");
|
||||
installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", "myapp", ".meta", "install.json"), "utf-8"));
|
||||
expect(installJson.installed_versions).toEqual(["2.0.0"]);
|
||||
expect(fs.existsSync(path.resolve(localStore, "modules", "myapp", "versions", "1.0.0"))).toBe(false);
|
||||
expect(installJson.installed_versions).toEqual(["2.0"]);
|
||||
expect(fs.existsSync(path.resolve(localStore, "modules", "myapp", "versions", "1.0"))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user