@pmate/cli 0.50.4 → 0.50.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +0 -1
- package/dist/index.js +796 -632
- package/dist/launcher.js +3 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -8884,6 +8884,9 @@ __export(index_exports, {
|
|
|
8884
8884
|
});
|
|
8885
8885
|
module.exports = __toCommonJS(index_exports);
|
|
8886
8886
|
|
|
8887
|
+
// src/commandGuide.ts
|
|
8888
|
+
var import_node_util = require("util");
|
|
8889
|
+
|
|
8887
8890
|
// src/serviceHelp.ts
|
|
8888
8891
|
function serviceHelp(argv) {
|
|
8889
8892
|
const end = argv.indexOf("--");
|
|
@@ -8908,6 +8911,111 @@ Other service actions: rename, move, delete, init, ssl, env, env-vars, apk. Run
|
|
|
8908
8911
|
Help performs no login, registry request or deployment.`;
|
|
8909
8912
|
}
|
|
8910
8913
|
|
|
8914
|
+
// src/commandGuide.ts
|
|
8915
|
+
var guidance = {
|
|
8916
|
+
list: "\u6309\u8DEF\u5F84\u5217\u51FA\u9879\u76EE\u3002\u9009\u62E9\u9879\u76EE\u540E\u7528 pmate dev <\u5E94\u7528\u540D> --cwd <\u76EE\u5F55> \u542F\u52A8\u672C\u5730\u670D\u52A1\u3002",
|
|
8917
|
+
login: "\u767B\u5F55\u5B8C\u6210\u540E\uFF0C\u7528 pmate session status --app <\u5E94\u7528> \u6838\u5BF9\u8D26\u53F7\u4E0E\u6709\u6548\u671F\u3002",
|
|
8918
|
+
session: "\u67E5\u8BE2\u72B6\u6001\u7528 pmate session status --app <\u5E94\u7528>\u3002read \u4F1A\u8F93\u51FA\u4F1A\u8BDD\u51ED\u636E\u3002",
|
|
8919
|
+
"add-dns": "\u8BB0\u5F55\u5199\u5165\u540E\uFF0C\u6309\u8FD4\u56DE\u7684\u8BB0\u5F55\u540D\u548C\u7C7B\u578B\u68C0\u67E5\u89E3\u6790\u3002",
|
|
8920
|
+
sts: "\u8F93\u51FA\u662F\u6709\u65F6\u9650\u7684\u8BBF\u95EE\u51ED\u636E\uFF1B\u7528\u8FD4\u56DE\u7684\u5230\u671F\u65F6\u95F4\u5224\u65AD\u662F\u5426\u9700\u8981\u91CD\u65B0\u7533\u8BF7\u3002",
|
|
8921
|
+
bucket: "\u521B\u5EFA\u524D\u786E\u5B9A bucket \u540D\u548C\u76EE\u6807\u57DF\u540D\uFF0C\u521B\u5EFA\u540E\u6309\u8FD4\u56DE\u7684 bucket \u540D\u4F7F\u7528\u3002",
|
|
8922
|
+
delete: "\u5220\u9664\u9488\u5BF9\u6240\u9009\u76EE\u5F55\u548C\u5E94\u7528\u3002\u6267\u884C\u524D\u7528 pmate list <\u76EE\u5F55> \u6838\u5BF9\u76EE\u6807\u3002",
|
|
8923
|
+
dev: "\u8FDB\u7A0B\u542F\u52A8\u540E\uFF0C\u4F7F\u7528\u65E5\u5FD7\u91CC\u7684\u5B9E\u9645 URL \u68C0\u67E5\u9875\u9762\u662F\u5426\u5DF2\u5C31\u7EEA\u3002",
|
|
8924
|
+
env: "\u67E5\u770B\u5F53\u524D\u5E94\u7528\u7684\u73AF\u5883\u914D\u7F6E\uFF1Apmate env <\u5E94\u7528\u540D> --list\u3002",
|
|
8925
|
+
commit: "\u8BB0\u5F55\u5F53\u524D\u4ED3\u5E93\u7684\u6539\u52A8\uFF1B--push \u624D\u4F1A\u63A8\u9001\u3002\u67E5\u770B\u7ED3\u679C\u7528 git log -1 --oneline\u3002",
|
|
8926
|
+
summary: "\u7528 --pr \u9009\u62E9\u8981\u603B\u7ED3\u7684 PR\uFF0C--cwd \u6307\u5411\u5B83\u6240\u5C5E\u7684\u4ED3\u5E93\u3002",
|
|
8927
|
+
linear: "\u5148\u7528 pmate linear whoami \u6838\u5BF9\u8EAB\u4EFD\uFF1B\u62FF\u5230 issue \u6807\u8BC6\u540E\u7528 pmate linear view <issue> \u67E5\u770B\u3002",
|
|
8928
|
+
notion: "\u62FF\u5230\u9875\u9762 ID \u6216 URL \u540E\uFF0C\u7528 pmate notion view <\u9875\u9762> \u67E5\u770B\u5185\u5BB9\u3002",
|
|
8929
|
+
oss: "\u8BB0\u5F55\u8F93\u51FA\u7684\u5BF9\u8C61\u8DEF\u5F84\uFF0C\u7528\u4E8E\u5B9A\u4F4D\u4E0A\u4F20\u7684\u6587\u4EF6\u3002",
|
|
8930
|
+
qrcode: "\u7F16\u7801\u7ED3\u679C\u5BF9\u5E94\u8F93\u5165\u7684\u6587\u672C\u6216 URL\u3002\u6587\u4EF6\u8F93\u51FA\u4EE5\u8FD4\u56DE\u7684\u8DEF\u5F84\u4E3A\u51C6\u3002",
|
|
8931
|
+
random: "\u7ED3\u679C\u53EF\u76F4\u63A5\u4F7F\u7528\uFF1B\u518D\u6B21\u6267\u884C\u4F1A\u91CD\u65B0\u751F\u6210\u3002",
|
|
8932
|
+
collect: "\u5148\u786E\u8BA4\u76EE\u6807\u7EBF\u7A0B\u548C\u6765\u6E90\uFF0C\u518D\u4F7F\u7528 sync\u3002",
|
|
8933
|
+
im1: "\u67E5\u770B\u53EF\u7528\u7EBF\u7A0B\uFF1Apmate im1 threads\uFF1B\u67E5\u770B\u539F\u6D88\u606F\uFF1Apmate im1 messages --thread <\u7EBF\u7A0B>\u3002",
|
|
8934
|
+
"chat-room": "\u6839\u636E\u64CD\u4F5C\u9009\u62E9 bot \u548C\u76EE\u6807\u4F1A\u8BDD\uFF1B\u53D1\u9001\u7ED3\u679C\u4E2D\u7684\u6D88\u606F\u6807\u8BC6\u7528\u4E8E\u5B9A\u4F4D\u672C\u6B21\u53D1\u9001\u3002",
|
|
8935
|
+
function: "\u5217\u8868\u8FD4\u56DE Function ID\u3002\u7528 pmate function get <namespace:function> \u67E5\u770B\u5B9A\u4E49\u3002",
|
|
8936
|
+
worker: "\u5217\u8868\u8FD4\u56DE Worker ID\u3002\u7528 pmate worker get <namespace:worker> \u68C0\u67E5\u6CE8\u518C\u4FE1\u606F\u3002",
|
|
8937
|
+
call: "\u6309 Function \u7684\u8F93\u5165\u89C4\u8303\u4F20 payload\u3002\u8C03\u7528\u8D85\u65F6\u540E\uFF0C\u5148\u6838\u5BF9\u539F\u8C03\u7528\u7684\u7ED3\u679C\u3002",
|
|
8938
|
+
ns: "\u4F7F\u7528\u8FD4\u56DE\u7684\u547D\u540D\u7A7A\u95F4\u6807\u8BC6\u7EE7\u7EED\u67E5\u8BE2\u6216\u7BA1\u7406\u5176\u4E2D\u7684\u8D44\u6E90\u3002",
|
|
8939
|
+
role: "\u64CD\u4F5C\u524D\u6838\u5BF9\u89D2\u8272\u548C\u8303\u56F4\u3002\u53EF\u7528 pmate role check \u67E5\u770B\u6307\u5B9A\u6743\u9650\u3002",
|
|
8940
|
+
account: "\u4F7F\u7528 account ID \u5B9A\u4F4D\u8D26\u53F7\uFF1B\u7528 pmate account view --account <id> \u8BFB\u53D6\u5F53\u524D\u8BB0\u5F55\u3002",
|
|
8941
|
+
org: "\u67E5\u770B\u7EC4\u7EC7\u5B9A\u4E49\u7528 pmate org get <\u7EC4\u7EC7\u6807\u8BC6>\u3002",
|
|
8942
|
+
store: "\u5148\u786E\u8BA4\u76EE\u6807 store\u3001chain \u548C\u8868\u3002\u8BFB\u5199\u540E\u4EE5\u5BF9\u5E94\u8BB0\u5F55\u7684\u56DE\u6267\u4E3A\u51C6\u3002",
|
|
8943
|
+
app: "\u5148\u53D6\u5F97\u5E94\u7528\u6807\u8BC6\uFF0C\u518D\u7528 pmate app view <\u5E94\u7528\u6807\u8BC6> \u68C0\u67E5\u914D\u7F6E\u3002",
|
|
8944
|
+
install: "\u5B89\u88C5\u56DE\u6267\u662F\u672C\u673A\u5305\u7684\u7248\u672C\u3002\u5DF2\u8FD0\u884C\u7684\u8FDB\u7A0B\u9700\u8981\u5355\u72EC\u6838\u5BF9\u7248\u672C\u3002",
|
|
8945
|
+
service: "\u7528 pmate service get <\u670D\u52A1\u6807\u8BC6> \u67E5\u770B\u6CE8\u518C\u914D\u7F6E\uFF1B\u90E8\u7F72\u540E\u7528 pmate service log <\u670D\u52A1\u6807\u8BC6> \u68C0\u67E5\u8FD0\u884C\u60C5\u51B5\u3002",
|
|
8946
|
+
logs: "\u5148\u7528 schema \u786E\u8BA4\u5B57\u6BB5\u3002\u67E5\u8BE2\u65F6\u6307\u5B9A\u65F6\u95F4\u8303\u56F4\u548C topic\uFF0C\u4F7F\u7528 trace \u6807\u8BC6\u8DDF\u8E2A\u4E00\u6B21\u8BF7\u6C42\u3002",
|
|
8947
|
+
vercel: "\u5148\u7528 pmate vercel projects \u786E\u8BA4\u9879\u76EE\u540D\uFF0C\u518D\u7528 pmate vercel deploys --project <\u540D\u79F0>\u3002",
|
|
8948
|
+
host: "\u6838\u5BF9\u76EE\u6807\u4E3B\u673A\uFF1B\u4FEE\u6539\u73AF\u5883\u540E\uFF0C\u6309\u9700\u4F7F\u7528 apply \u4F7F\u914D\u7F6E\u751F\u6548\u3002",
|
|
8949
|
+
quiz: "\u7528\u56DE\u6267\u7684 paper ID \u67E5\u770B\u8349\u7A3F\uFF1Apmate quiz view --paper-id <id>\u3002"
|
|
8950
|
+
};
|
|
8951
|
+
function groupName(argv) {
|
|
8952
|
+
const group = argv[0] === "namespace" ? "ns" : argv[0];
|
|
8953
|
+
return group && Object.prototype.hasOwnProperty.call(guidance, group) ? group : void 0;
|
|
8954
|
+
}
|
|
8955
|
+
function commandGuide(argv, catalogue) {
|
|
8956
|
+
const group = groupName(argv);
|
|
8957
|
+
const end = argv.indexOf("--"), args = end < 0 ? argv : argv.slice(0, end);
|
|
8958
|
+
if (!group || !args.some((x) => x === "-h" || x === "--help")) return;
|
|
8959
|
+
const service = serviceHelp(args);
|
|
8960
|
+
if (service) return service + "\n\n" + guidance[group];
|
|
8961
|
+
if (group === "session")
|
|
8962
|
+
return "pmate session status|read|logout --app <\u5E94\u7528>\n" + guidance.session;
|
|
8963
|
+
const lines = (0, import_node_util.stripVTControlCharacters)(catalogue).split("\n");
|
|
8964
|
+
const index = lines.findIndex(
|
|
8965
|
+
(s) => s.startsWith(" " + group + " ") || s === " " + group
|
|
8966
|
+
);
|
|
8967
|
+
if (index < 0) return "pmate " + group + "\n" + guidance[group];
|
|
8968
|
+
const options = [];
|
|
8969
|
+
for (const line of lines.slice(index + 1)) {
|
|
8970
|
+
if (!/^\s{4}/.test(line)) break;
|
|
8971
|
+
options.push(line.trim());
|
|
8972
|
+
}
|
|
8973
|
+
const prefix = args.slice(1).filter((x) => !x.startsWith("-"))[0];
|
|
8974
|
+
const selected = prefix ? options.filter((line, i) => {
|
|
8975
|
+
let start = i;
|
|
8976
|
+
while (start > 0 && options[start].startsWith("[")) start--;
|
|
8977
|
+
return options[start].split(/\s+/, 1)[0]?.split("|").includes(prefix);
|
|
8978
|
+
}) : options;
|
|
8979
|
+
return [
|
|
8980
|
+
"pmate " + lines[index].trim(),
|
|
8981
|
+
...(selected.length ? selected : options).map((line) => " " + line),
|
|
8982
|
+
"",
|
|
8983
|
+
guidance[group]
|
|
8984
|
+
].join("\n");
|
|
8985
|
+
}
|
|
8986
|
+
var shellArg = (s) => /^[\w./:@-]+$/.test(s) ? s : "'" + s.replace(/'/g, "'\\''") + "'";
|
|
8987
|
+
function nextRead(argv) {
|
|
8988
|
+
if (argv.some((x) => ["--json", "--help", "-h", "--version", "-v"].includes(x)))
|
|
8989
|
+
return;
|
|
8990
|
+
const [group, op, id] = argv;
|
|
8991
|
+
if (group === "commit")
|
|
8992
|
+
return "\u6838\u5BF9\u672C\u6B21\u63D0\u4EA4\uFF1Agit " + (argv.includes("--cwd") ? "-C " + shellArg(argv[argv.indexOf("--cwd") + 1] ?? ".") + " " : "") + "log -1 --oneline";
|
|
8993
|
+
if (group === "login" && argv.includes("--app")) {
|
|
8994
|
+
const app = argv[argv.indexOf("--app") + 1];
|
|
8995
|
+
if (app) return "\u6838\u5BF9\u767B\u5F55\u72B6\u6001\uFF1Apmate session status --app " + shellArg(app);
|
|
8996
|
+
}
|
|
8997
|
+
const scope = (flag) => {
|
|
8998
|
+
const pos = argv.indexOf(flag);
|
|
8999
|
+
return pos >= 0 && argv[pos + 1] ? " " + flag + " " + shellArg(argv[pos + 1]) : "";
|
|
9000
|
+
};
|
|
9001
|
+
if (!op) return;
|
|
9002
|
+
if (op === "list" && group && ["service", "app", "function", "worker", "org"].includes(group))
|
|
9003
|
+
return "\u67E5\u770B\u4E00\u6761\u8BB0\u5F55\uFF1Apmate " + group + " get '<\u4E0A\u65B9\u7684\u5B8C\u6574\u6807\u8BC6>'" + scope("--base-url");
|
|
9004
|
+
if (group === "im1" && op === "threads")
|
|
9005
|
+
return "\u67E5\u770B\u4F1A\u8BDD\u6D88\u606F\uFF1Apmate im1 messages --thread '<\u4F1A\u8BDD\u6807\u8BC6>' --text";
|
|
9006
|
+
if (!id || id.startsWith("-")) return;
|
|
9007
|
+
if (group === "service" && op === "deploy" && !argv.includes("--dry-run")) {
|
|
9008
|
+
const env = argv.indexOf("--env");
|
|
9009
|
+
return "\u67E5\u770B\u90E8\u7F72\u65E5\u5FD7\uFF1Apmate service log " + shellArg(id) + (env >= 0 ? " --env " + shellArg(argv[env + 1] ?? "prod") : "");
|
|
9010
|
+
}
|
|
9011
|
+
if (["service", "app", "function", "worker", "org"].includes(group) && op === "update")
|
|
9012
|
+
return "\u67E5\u770B\u5F53\u524D\u8BB0\u5F55\uFF1Apmate " + group + " get " + shellArg(id) + scope("--base-url");
|
|
9013
|
+
if (group === "linear" && op === "update")
|
|
9014
|
+
return "\u67E5\u770B\u66F4\u65B0\u540E\u7684 issue\uFF1Apmate linear view " + shellArg(id) + scope("--cwd");
|
|
9015
|
+
if (group === "notion" && op === "update")
|
|
9016
|
+
return "\u67E5\u770B\u66F4\u65B0\u540E\u7684\u9875\u9762\uFF1Apmate notion view " + shellArg(id);
|
|
9017
|
+
}
|
|
9018
|
+
|
|
8911
9019
|
// src/sessionCommand.ts
|
|
8912
9020
|
init_sessionStore();
|
|
8913
9021
|
function sessionCommand(args) {
|
|
@@ -8938,88 +9046,461 @@ var import_node_path38 = __toESM(require("path"));
|
|
|
8938
9046
|
|
|
8939
9047
|
// src/services/PackageInstaller.ts
|
|
8940
9048
|
var import_node_child_process = require("child_process");
|
|
8941
|
-
var
|
|
9049
|
+
var import_node_util2 = require("util");
|
|
9050
|
+
var import_node_fs3 = require("fs");
|
|
9051
|
+
var import_node_os3 = require("os");
|
|
9052
|
+
var import_node_path3 = __toESM(require("path"));
|
|
9053
|
+
|
|
9054
|
+
// src/config.ts
|
|
8942
9055
|
var import_node_fs2 = require("fs");
|
|
8943
|
-
var import_node_os2 = require("os");
|
|
9056
|
+
var import_node_os2 = __toESM(require("os"));
|
|
8944
9057
|
var import_node_path2 = __toESM(require("path"));
|
|
8945
|
-
var
|
|
8946
|
-
|
|
8947
|
-
|
|
9058
|
+
var import_ajv = __toESM(require("ajv"));
|
|
9059
|
+
var import_yaml = __toESM(require("yaml"));
|
|
9060
|
+
var cachedConfig;
|
|
9061
|
+
var CONFIG = new Proxy({}, {
|
|
9062
|
+
get(_target, prop) {
|
|
9063
|
+
return loadPmateConfig()[prop];
|
|
9064
|
+
}
|
|
9065
|
+
});
|
|
9066
|
+
var PMATE_USER_CONFIG_SCHEMA = {
|
|
9067
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
9068
|
+
type: "object",
|
|
9069
|
+
additionalProperties: false,
|
|
9070
|
+
properties: {
|
|
9071
|
+
aliyun: {
|
|
9072
|
+
type: "object",
|
|
9073
|
+
additionalProperties: false,
|
|
9074
|
+
properties: {
|
|
9075
|
+
ak: { type: "string" },
|
|
9076
|
+
sk: { type: "string" },
|
|
9077
|
+
region: { type: "string" },
|
|
9078
|
+
domain: { type: "string" }
|
|
9079
|
+
}
|
|
9080
|
+
},
|
|
9081
|
+
notion: {
|
|
9082
|
+
type: "object",
|
|
9083
|
+
additionalProperties: false,
|
|
9084
|
+
properties: {
|
|
9085
|
+
token: { type: "string" }
|
|
9086
|
+
}
|
|
9087
|
+
},
|
|
9088
|
+
openAI: {
|
|
9089
|
+
type: "object",
|
|
9090
|
+
additionalProperties: false,
|
|
9091
|
+
properties: {
|
|
9092
|
+
apiKey: { type: "string" },
|
|
9093
|
+
baseUrl: { type: "string" }
|
|
9094
|
+
}
|
|
9095
|
+
},
|
|
9096
|
+
linear: {
|
|
9097
|
+
type: "object",
|
|
9098
|
+
additionalProperties: false,
|
|
9099
|
+
properties: {
|
|
9100
|
+
teamId: { type: "string" },
|
|
9101
|
+
teamKey: { type: "string" },
|
|
9102
|
+
accounts: {
|
|
9103
|
+
type: "array",
|
|
9104
|
+
items: {
|
|
9105
|
+
type: "object",
|
|
9106
|
+
additionalProperties: false,
|
|
9107
|
+
required: ["title", "apiKey"],
|
|
9108
|
+
properties: {
|
|
9109
|
+
title: { type: "string" },
|
|
9110
|
+
apiKey: { type: "string" },
|
|
9111
|
+
teamId: { type: "string" },
|
|
9112
|
+
teamKey: { type: "string" }
|
|
9113
|
+
}
|
|
9114
|
+
}
|
|
9115
|
+
}
|
|
9116
|
+
}
|
|
9117
|
+
},
|
|
9118
|
+
oss: {
|
|
9119
|
+
type: "object",
|
|
9120
|
+
additionalProperties: false,
|
|
9121
|
+
properties: {
|
|
9122
|
+
public: {
|
|
9123
|
+
type: "object",
|
|
9124
|
+
additionalProperties: false,
|
|
9125
|
+
properties: {
|
|
9126
|
+
region: { type: "string" },
|
|
9127
|
+
bucket: { type: "string" }
|
|
9128
|
+
}
|
|
9129
|
+
},
|
|
9130
|
+
private: {
|
|
9131
|
+
type: "object",
|
|
9132
|
+
additionalProperties: false,
|
|
9133
|
+
properties: {
|
|
9134
|
+
region: { type: "string" },
|
|
9135
|
+
bucket: { type: "string" }
|
|
9136
|
+
}
|
|
9137
|
+
}
|
|
9138
|
+
}
|
|
9139
|
+
},
|
|
9140
|
+
ssl: {
|
|
9141
|
+
type: "object",
|
|
9142
|
+
additionalProperties: false,
|
|
9143
|
+
properties: {
|
|
9144
|
+
pub: { type: "string" },
|
|
9145
|
+
prv: { type: "string" }
|
|
9146
|
+
}
|
|
9147
|
+
},
|
|
9148
|
+
github: {
|
|
9149
|
+
type: "object",
|
|
9150
|
+
additionalProperties: false,
|
|
9151
|
+
properties: {
|
|
9152
|
+
token: { type: "string" }
|
|
9153
|
+
}
|
|
9154
|
+
},
|
|
9155
|
+
test: {
|
|
9156
|
+
type: "object",
|
|
9157
|
+
additionalProperties: false,
|
|
9158
|
+
properties: {
|
|
9159
|
+
authKey: { type: "string" }
|
|
9160
|
+
}
|
|
9161
|
+
},
|
|
9162
|
+
vercel: {
|
|
9163
|
+
type: "object",
|
|
9164
|
+
additionalProperties: false,
|
|
9165
|
+
properties: {
|
|
9166
|
+
scope: { type: "string" }
|
|
9167
|
+
}
|
|
9168
|
+
}
|
|
9169
|
+
}
|
|
8948
9170
|
};
|
|
8949
|
-
var
|
|
8950
|
-
|
|
8951
|
-
|
|
8952
|
-
|
|
8953
|
-
|
|
8954
|
-
|
|
8955
|
-
|
|
8956
|
-
|
|
8957
|
-
|
|
8958
|
-
const p = value;
|
|
8959
|
-
if (!p || p.id !== id || p.id !== p.namespace + ":" + p.name || !semver.test(p.version) || !/^[-a-zA-Z0-9_.]+\/[-a-zA-Z0-9_.]+$/.test(p.repository) || !relative(p.directory) || !relative(p.manifest) || !Array.isArray(p.platforms) || !Array.isArray(p.prepare) || p.prepare.length > 20 || !p.bins || typeof p.bins !== "object" || !Object.keys(p.bins).length) throw Error("Invalid package registry metadata");
|
|
8960
|
-
if (p.replaces !== void 0 && (!Array.isArray(p.replaces) || p.replaces.length > 20 || p.replaces.some((id2) => typeof id2 !== "string" || packageRef(id2).id !== id2 || packageRef(id2).namespace !== p.namespace || id2 === p.id))) throw Error("Invalid replaced package identities");
|
|
8961
|
-
for (const [name, file] of Object.entries(p.bins)) if (!segment.test(name) || !relative(file)) throw Error("Invalid package bin");
|
|
8962
|
-
for (const step of [...p.prepare, ...Object.values(p.lifecycle ?? {})]) if (!step || typeof step.command !== "string" || !step.command || step.command.includes("\0") || !Array.isArray(step.args) || step.args.some((a) => typeof a !== "string" || a.includes("\0")) || step.cwd !== void 0 && !relative(step.cwd)) throw Error("Invalid package command");
|
|
8963
|
-
if (p.lifecycle && !["health,start,status,stop", "drain,health,start,status,stop"].includes(Object.keys(p.lifecycle).sort().join(","))) throw Error("Incomplete package lifecycle");
|
|
8964
|
-
return p;
|
|
8965
|
-
}
|
|
8966
|
-
var PackageInstaller = class {
|
|
8967
|
-
constructor(ports = {}) {
|
|
8968
|
-
this.ports = ports;
|
|
8969
|
-
this.directory = import_node_path2.default.join(ports.home ?? (0, import_node_os2.homedir)(), ".pmate/packages");
|
|
8970
|
-
this.binDirectory = import_node_path2.default.join(ports.home ?? (0, import_node_os2.homedir)(), ".local/bin");
|
|
9171
|
+
var ajv = new import_ajv.default({
|
|
9172
|
+
allErrors: true,
|
|
9173
|
+
allowUnionTypes: true,
|
|
9174
|
+
validateSchema: false
|
|
9175
|
+
});
|
|
9176
|
+
var validatePmateUserConfig = ajv.compile(PMATE_USER_CONFIG_SCHEMA);
|
|
9177
|
+
function loadPmateConfig() {
|
|
9178
|
+
if (cachedConfig !== void 0) {
|
|
9179
|
+
return cachedConfig ?? {};
|
|
8971
9180
|
}
|
|
8972
|
-
|
|
8973
|
-
|
|
9181
|
+
const pmateDir = import_node_path2.default.join(import_node_os2.default.homedir(), ".pmate");
|
|
9182
|
+
const yamlPath = import_node_path2.default.join(pmateDir, "config.yaml");
|
|
9183
|
+
let yamlConfig = null;
|
|
9184
|
+
if ((0, import_node_fs2.existsSync)(yamlPath)) {
|
|
9185
|
+
const raw = (0, import_node_fs2.readFileSync)(yamlPath, "utf8");
|
|
9186
|
+
try {
|
|
9187
|
+
const parsed = import_yaml.default.parse(raw);
|
|
9188
|
+
yamlConfig = validateParsedConfig(parsed, yamlPath);
|
|
9189
|
+
} catch (error) {
|
|
9190
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
9191
|
+
throw new Error(`Invalid YAML in ${yamlPath}: ${message}`);
|
|
9192
|
+
}
|
|
8974
9193
|
}
|
|
8975
|
-
|
|
8976
|
-
|
|
9194
|
+
cachedConfig = yamlConfig ?? {};
|
|
9195
|
+
return cachedConfig;
|
|
9196
|
+
}
|
|
9197
|
+
function loadRepoConfig(cwd) {
|
|
9198
|
+
const paths = findRepoConfigPaths(cwd, true);
|
|
9199
|
+
if (!paths) {
|
|
9200
|
+
return null;
|
|
8977
9201
|
}
|
|
8978
|
-
|
|
8979
|
-
|
|
8980
|
-
|
|
8981
|
-
|
|
8982
|
-
|
|
8983
|
-
|
|
9202
|
+
const parsed = loadMergedRepoYaml(paths);
|
|
9203
|
+
return normalizeRepoConfig(parsed, paths.configPath);
|
|
9204
|
+
}
|
|
9205
|
+
function loadAppConfig(cwd, options = {}) {
|
|
9206
|
+
const searchParents = options.searchParents ?? true;
|
|
9207
|
+
const paths = findRepoConfigPaths(cwd, searchParents);
|
|
9208
|
+
if (!paths) {
|
|
9209
|
+
const suffix = searchParents ? " or parent directories" : "";
|
|
9210
|
+
throw new Error(`No .pmate.yaml found in ${cwd}${suffix}`);
|
|
8984
9211
|
}
|
|
8985
|
-
|
|
8986
|
-
|
|
8987
|
-
|
|
8988
|
-
|
|
9212
|
+
const parsed = loadMergedRepoYaml(paths);
|
|
9213
|
+
const appConfig = parsed;
|
|
9214
|
+
if (!appConfig || typeof appConfig !== "object" || !Array.isArray(appConfig.apps)) {
|
|
9215
|
+
throw new Error(
|
|
9216
|
+
`Invalid .pmate.yaml: missing apps array (${describeRepoConfigPaths(paths)})`
|
|
9217
|
+
);
|
|
8989
9218
|
}
|
|
8990
|
-
|
|
8991
|
-
|
|
8992
|
-
|
|
8993
|
-
|
|
8994
|
-
|
|
9219
|
+
return {
|
|
9220
|
+
config: appConfig,
|
|
9221
|
+
rootDir: paths.rootDir,
|
|
9222
|
+
configPath: paths.configPath,
|
|
9223
|
+
localConfigPath: paths.localConfigPath
|
|
9224
|
+
};
|
|
9225
|
+
}
|
|
9226
|
+
function describeLoadedAppConfig({
|
|
9227
|
+
configPath,
|
|
9228
|
+
localConfigPath
|
|
9229
|
+
}) {
|
|
9230
|
+
return localConfigPath ? `${configPath} + ${localConfigPath}` : configPath;
|
|
9231
|
+
}
|
|
9232
|
+
function validateParsedConfig(value, configPath) {
|
|
9233
|
+
if (value === null || value === void 0) {
|
|
9234
|
+
return {};
|
|
8995
9235
|
}
|
|
8996
|
-
|
|
8997
|
-
const
|
|
8998
|
-
|
|
9236
|
+
if (!validatePmateUserConfig(value)) {
|
|
9237
|
+
const detail = ajv.errorsText(validatePmateUserConfig.errors, {
|
|
9238
|
+
separator: "\n",
|
|
9239
|
+
dataVar: "config"
|
|
9240
|
+
});
|
|
9241
|
+
throw new Error(`Invalid config schema in ${configPath}:
|
|
9242
|
+
${detail}`);
|
|
8999
9243
|
}
|
|
9000
|
-
|
|
9001
|
-
|
|
9002
|
-
|
|
9003
|
-
|
|
9004
|
-
|
|
9005
|
-
|
|
9244
|
+
return value;
|
|
9245
|
+
}
|
|
9246
|
+
function findRepoConfigPaths(cwd, searchParents) {
|
|
9247
|
+
let current = import_node_path2.default.resolve(cwd);
|
|
9248
|
+
while (true) {
|
|
9249
|
+
const candidate = import_node_path2.default.join(current, ".pmate.yaml");
|
|
9250
|
+
if ((0, import_node_fs2.existsSync)(candidate)) {
|
|
9251
|
+
const localCandidate = import_node_path2.default.join(current, ".pmate.local.yaml");
|
|
9252
|
+
return {
|
|
9253
|
+
rootDir: current,
|
|
9254
|
+
configPath: candidate,
|
|
9255
|
+
localConfigPath: (0, import_node_fs2.existsSync)(localCandidate) ? localCandidate : void 0
|
|
9256
|
+
};
|
|
9006
9257
|
}
|
|
9007
|
-
|
|
9008
|
-
|
|
9009
|
-
const candidates = [records[metadata.id], ...(metadata.replaces ?? []).map((id) => records[id])].filter((p) => !!p);
|
|
9010
|
-
const owner = candidates.find((p) => p.bins[name]);
|
|
9011
|
-
if (owner && owner.id !== metadata.id) {
|
|
9012
|
-
if (owner.definition.lifecycle) throw Error("Cannot replace a package with managed services");
|
|
9013
|
-
if (Object.keys(owner.bins).some((bin) => !metadata.bins[bin])) throw Error("Replacement must include every old command");
|
|
9014
|
-
const roots = [import_node_path2.default.join(this.directory, "repos", ...owner.definition.repository.split("/")), import_node_path2.default.join(this.directory, "releases", ...owner.definition.repository.split("/"), owner.definition.name + "-" + owner.version)];
|
|
9015
|
-
if (!roots.includes(owner.root) || !(0, import_node_fs2.realpathSync)(owner.bins[name]).startsWith((0, import_node_fs2.realpathSync)(owner.root) + import_node_path2.default.sep)) throw Error("Replaced command is outside its managed installation");
|
|
9258
|
+
if (!searchParents) {
|
|
9259
|
+
return null;
|
|
9016
9260
|
}
|
|
9017
|
-
|
|
9018
|
-
|
|
9019
|
-
|
|
9020
|
-
|
|
9261
|
+
const parent = import_node_path2.default.dirname(current);
|
|
9262
|
+
if (parent === current) {
|
|
9263
|
+
return null;
|
|
9264
|
+
}
|
|
9265
|
+
current = parent;
|
|
9021
9266
|
}
|
|
9022
|
-
|
|
9267
|
+
}
|
|
9268
|
+
function loadMergedRepoYaml(paths) {
|
|
9269
|
+
const base = parseYamlFile(paths.configPath);
|
|
9270
|
+
if (!paths.localConfigPath) {
|
|
9271
|
+
return base;
|
|
9272
|
+
}
|
|
9273
|
+
const local = parseYamlFile(paths.localConfigPath);
|
|
9274
|
+
return mergeRepoConfigValues(base, local);
|
|
9275
|
+
}
|
|
9276
|
+
function parseYamlFile(configPath) {
|
|
9277
|
+
const raw = (0, import_node_fs2.readFileSync)(configPath, "utf8");
|
|
9278
|
+
try {
|
|
9279
|
+
return import_yaml.default.parse(raw);
|
|
9280
|
+
} catch (error) {
|
|
9281
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
9282
|
+
throw new Error(`Invalid YAML in ${configPath}: ${message}`);
|
|
9283
|
+
}
|
|
9284
|
+
}
|
|
9285
|
+
function describeRepoConfigPaths(paths) {
|
|
9286
|
+
return paths.localConfigPath ? `${paths.configPath} + ${paths.localConfigPath}` : paths.configPath;
|
|
9287
|
+
}
|
|
9288
|
+
function mergeRepoConfigValues(base, local) {
|
|
9289
|
+
if (Array.isArray(base) && Array.isArray(local)) {
|
|
9290
|
+
return [...base, ...local];
|
|
9291
|
+
}
|
|
9292
|
+
if (isPlainObject(base) && isPlainObject(local)) {
|
|
9293
|
+
const merged = { ...base };
|
|
9294
|
+
for (const [key, localValue] of Object.entries(local)) {
|
|
9295
|
+
const baseValue = merged[key];
|
|
9296
|
+
if (key === "apps" && Array.isArray(baseValue) && Array.isArray(localValue)) {
|
|
9297
|
+
merged[key] = mergeApps(baseValue, localValue);
|
|
9298
|
+
continue;
|
|
9299
|
+
}
|
|
9300
|
+
merged[key] = mergeRepoConfigValues(baseValue, localValue);
|
|
9301
|
+
}
|
|
9302
|
+
return merged;
|
|
9303
|
+
}
|
|
9304
|
+
return local === void 0 ? base : local;
|
|
9305
|
+
}
|
|
9306
|
+
function mergeApps(baseApps, localApps) {
|
|
9307
|
+
const merged = [...baseApps];
|
|
9308
|
+
for (const localApp of localApps) {
|
|
9309
|
+
const localName = getAppName(localApp);
|
|
9310
|
+
if (!localName) {
|
|
9311
|
+
merged.push(localApp);
|
|
9312
|
+
continue;
|
|
9313
|
+
}
|
|
9314
|
+
const baseIndex = merged.findIndex((entry) => getAppName(entry) === localName);
|
|
9315
|
+
if (baseIndex === -1) {
|
|
9316
|
+
merged.push(localApp);
|
|
9317
|
+
continue;
|
|
9318
|
+
}
|
|
9319
|
+
merged[baseIndex] = mergeRepoConfigValues(merged[baseIndex], localApp);
|
|
9320
|
+
}
|
|
9321
|
+
return merged;
|
|
9322
|
+
}
|
|
9323
|
+
function getAppName(value) {
|
|
9324
|
+
if (!isPlainObject(value) || typeof value.app !== "string") {
|
|
9325
|
+
return void 0;
|
|
9326
|
+
}
|
|
9327
|
+
const app = value.app.trim();
|
|
9328
|
+
return app || void 0;
|
|
9329
|
+
}
|
|
9330
|
+
function isPlainObject(value) {
|
|
9331
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
9332
|
+
}
|
|
9333
|
+
function normalizeRepoConfig(value, configPath) {
|
|
9334
|
+
if (!value || typeof value !== "object") {
|
|
9335
|
+
return null;
|
|
9336
|
+
}
|
|
9337
|
+
const config = value;
|
|
9338
|
+
const hasRepoKeys = "githubUser" in config || "linear" in config;
|
|
9339
|
+
if (!hasRepoKeys) {
|
|
9340
|
+
return null;
|
|
9341
|
+
}
|
|
9342
|
+
const repoConfig = {};
|
|
9343
|
+
if (config.githubUser !== void 0) {
|
|
9344
|
+
if (typeof config.githubUser !== "string" || !config.githubUser.trim()) {
|
|
9345
|
+
throw new Error(
|
|
9346
|
+
`Invalid repo config in ${configPath}: githubUser must be a non-empty string.`
|
|
9347
|
+
);
|
|
9348
|
+
}
|
|
9349
|
+
repoConfig.githubUser = config.githubUser.trim();
|
|
9350
|
+
}
|
|
9351
|
+
if (config.linear !== void 0) {
|
|
9352
|
+
if (!config.linear || typeof config.linear !== "object") {
|
|
9353
|
+
throw new Error(
|
|
9354
|
+
`Invalid repo config in ${configPath}: linear must be an object.`
|
|
9355
|
+
);
|
|
9356
|
+
}
|
|
9357
|
+
const linear = config.linear;
|
|
9358
|
+
const teamId = getOptionalString(linear.teamId);
|
|
9359
|
+
const teamKey = getOptionalString(linear.teamKey);
|
|
9360
|
+
const rawAccount = linear.account ?? linear.accounts;
|
|
9361
|
+
if (!rawAccount) {
|
|
9362
|
+
throw new Error(
|
|
9363
|
+
`Invalid repo config in ${configPath}: linear account is required.`
|
|
9364
|
+
);
|
|
9365
|
+
}
|
|
9366
|
+
const account = normalizeRepoLinearAccount(rawAccount, configPath);
|
|
9367
|
+
repoConfig.linear = {
|
|
9368
|
+
teamId,
|
|
9369
|
+
teamKey,
|
|
9370
|
+
account
|
|
9371
|
+
};
|
|
9372
|
+
}
|
|
9373
|
+
return repoConfig;
|
|
9374
|
+
}
|
|
9375
|
+
function normalizeRepoLinearAccount(value, configPath) {
|
|
9376
|
+
const account = Array.isArray(value) ? value : [value];
|
|
9377
|
+
if (account.length !== 1) {
|
|
9378
|
+
throw new Error(
|
|
9379
|
+
`Invalid repo config in ${configPath}: linear account must be a single entry.`
|
|
9380
|
+
);
|
|
9381
|
+
}
|
|
9382
|
+
const entry = account[0];
|
|
9383
|
+
if (!entry || typeof entry !== "object") {
|
|
9384
|
+
throw new Error(
|
|
9385
|
+
`Invalid repo config in ${configPath}: linear account must be an object.`
|
|
9386
|
+
);
|
|
9387
|
+
}
|
|
9388
|
+
const raw = entry;
|
|
9389
|
+
const apiKey = getOptionalString(raw.apiKey);
|
|
9390
|
+
if (!apiKey) {
|
|
9391
|
+
throw new Error(
|
|
9392
|
+
`Invalid repo config in ${configPath}: linear account apiKey is required.`
|
|
9393
|
+
);
|
|
9394
|
+
}
|
|
9395
|
+
const title = getOptionalString(raw.title) || "Repo";
|
|
9396
|
+
const teamId = getOptionalString(raw.teamId);
|
|
9397
|
+
const teamKey = getOptionalString(raw.teamKey);
|
|
9398
|
+
return {
|
|
9399
|
+
title,
|
|
9400
|
+
apiKey,
|
|
9401
|
+
teamId,
|
|
9402
|
+
teamKey
|
|
9403
|
+
};
|
|
9404
|
+
}
|
|
9405
|
+
function getOptionalString(value) {
|
|
9406
|
+
if (typeof value !== "string") {
|
|
9407
|
+
return void 0;
|
|
9408
|
+
}
|
|
9409
|
+
const trimmed = value.trim();
|
|
9410
|
+
return trimmed ? trimmed : void 0;
|
|
9411
|
+
}
|
|
9412
|
+
|
|
9413
|
+
// src/services/PackageInstaller.ts
|
|
9414
|
+
var execFileAsync = (0, import_node_util2.promisify)(import_node_child_process.execFile);
|
|
9415
|
+
var run = async (file, args, cwd) => {
|
|
9416
|
+
const env = { ...process.env, GIT_TERMINAL_PROMPT: "0" };
|
|
9417
|
+
if (file === "git" && (args[0] === "clone" || args[0] === "pull")) {
|
|
9418
|
+
const token = loadPmateConfig().github?.token?.trim();
|
|
9419
|
+
if (token) {
|
|
9420
|
+
const count = Number(env.GIT_CONFIG_COUNT ?? 0);
|
|
9421
|
+
if (!Number.isSafeInteger(count) || count < 0) throw Error("Invalid Git configuration environment");
|
|
9422
|
+
env.GIT_CONFIG_COUNT = String(count + 1);
|
|
9423
|
+
env[`GIT_CONFIG_KEY_${count}`] = "http.https://github.com/.extraheader";
|
|
9424
|
+
env[`GIT_CONFIG_VALUE_${count}`] = `Authorization: Basic ${Buffer.from(`x-access-token:${token}`).toString("base64")}`;
|
|
9425
|
+
}
|
|
9426
|
+
}
|
|
9427
|
+
const { stdout } = await execFileAsync(file, args, { cwd, env, timeout: 6e5, maxBuffer: 8 * 1024 * 1024 });
|
|
9428
|
+
return stdout.trim();
|
|
9429
|
+
};
|
|
9430
|
+
var semver = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
|
|
9431
|
+
var segment = /^[a-z][a-z0-9-]{0,62}$/;
|
|
9432
|
+
var relative = (p) => typeof p === "string" && !!p && !import_node_path3.default.isAbsolute(p) && !p.includes("\\") && !p.includes("\0") && !p.split("/").some((x) => !x || x === "..");
|
|
9433
|
+
function packageRef(value) {
|
|
9434
|
+
const parts = value.includes(":") ? value.split(":") : ["pmate", value];
|
|
9435
|
+
if (parts.length !== 2 || parts.some((p) => !segment.test(p))) throw Error("Use a package name or namespace:name");
|
|
9436
|
+
return { namespace: parts[0], name: parts[1], id: parts.join(":") };
|
|
9437
|
+
}
|
|
9438
|
+
function validateInstallMetadata(value, id) {
|
|
9439
|
+
const p = value;
|
|
9440
|
+
if (!p || p.id !== id || p.id !== p.namespace + ":" + p.name || !semver.test(p.version) || !/^[-a-zA-Z0-9_.]+\/[-a-zA-Z0-9_.]+$/.test(p.repository) || !relative(p.directory) || !relative(p.manifest) || !Array.isArray(p.platforms) || !Array.isArray(p.prepare) || p.prepare.length > 20 || !p.bins || typeof p.bins !== "object" || !Object.keys(p.bins).length) throw Error("Invalid package registry metadata");
|
|
9441
|
+
if (p.replaces !== void 0 && (!Array.isArray(p.replaces) || p.replaces.length > 20 || p.replaces.some((id2) => typeof id2 !== "string" || packageRef(id2).id !== id2 || packageRef(id2).namespace !== p.namespace || id2 === p.id))) throw Error("Invalid replaced package identities");
|
|
9442
|
+
for (const [name, file] of Object.entries(p.bins)) if (!segment.test(name) || !relative(file)) throw Error("Invalid package bin");
|
|
9443
|
+
for (const step of [...p.prepare, ...Object.values(p.lifecycle ?? {})]) if (!step || typeof step.command !== "string" || !step.command || step.command.includes("\0") || !Array.isArray(step.args) || step.args.some((a) => typeof a !== "string" || a.includes("\0")) || step.cwd !== void 0 && !relative(step.cwd)) throw Error("Invalid package command");
|
|
9444
|
+
if (p.lifecycle && !["health,start,status,stop", "drain,health,start,status,stop"].includes(Object.keys(p.lifecycle).sort().join(","))) throw Error("Incomplete package lifecycle");
|
|
9445
|
+
return p;
|
|
9446
|
+
}
|
|
9447
|
+
var PackageInstaller = class {
|
|
9448
|
+
constructor(ports = {}) {
|
|
9449
|
+
this.ports = ports;
|
|
9450
|
+
this.directory = import_node_path3.default.join(ports.home ?? (0, import_node_os3.homedir)(), ".pmate/packages");
|
|
9451
|
+
this.binDirectory = import_node_path3.default.join(ports.home ?? (0, import_node_os3.homedir)(), ".local/bin");
|
|
9452
|
+
}
|
|
9453
|
+
execute(file, args, cwd) {
|
|
9454
|
+
return (this.ports.run ?? run)(file, args, cwd);
|
|
9455
|
+
}
|
|
9456
|
+
say(message) {
|
|
9457
|
+
this.ports.progress?.(message);
|
|
9458
|
+
}
|
|
9459
|
+
records() {
|
|
9460
|
+
const file = import_node_path3.default.join(this.directory, "installed.json");
|
|
9461
|
+
if (!(0, import_node_fs3.existsSync)(file)) return {};
|
|
9462
|
+
const value = JSON.parse((0, import_node_fs3.readFileSync)(file, "utf8"));
|
|
9463
|
+
if (value.schemaVersion !== 1 || !value.packages || typeof value.packages !== "object") throw Error("Invalid installed package registry");
|
|
9464
|
+
return value.packages;
|
|
9465
|
+
}
|
|
9466
|
+
save(packages) {
|
|
9467
|
+
const file = import_node_path3.default.join(this.directory, "installed.json"), temporary = file + ".tmp-" + process.pid;
|
|
9468
|
+
(0, import_node_fs3.writeFileSync)(temporary, JSON.stringify({ schemaVersion: 1, packages }, null, 2) + "\n", { mode: 384 });
|
|
9469
|
+
(0, import_node_fs3.renameSync)(temporary, file);
|
|
9470
|
+
}
|
|
9471
|
+
local(root, relativePath) {
|
|
9472
|
+
if (!relative(relativePath)) throw Error("Invalid package path");
|
|
9473
|
+
const file = import_node_path3.default.resolve(root, relativePath), base = (0, import_node_fs3.realpathSync)(root);
|
|
9474
|
+
if ((0, import_node_fs3.realpathSync)(file) !== base && !(0, import_node_fs3.realpathSync)(file).startsWith(base + import_node_path3.default.sep)) throw Error("Package path escapes repository");
|
|
9475
|
+
return file;
|
|
9476
|
+
}
|
|
9477
|
+
async step(root, step) {
|
|
9478
|
+
const cwd = step.cwd ? this.local(root, step.cwd) : root;
|
|
9479
|
+
return this.execute(step.command, step.args, cwd);
|
|
9480
|
+
}
|
|
9481
|
+
validateReplacements(metadata, records) {
|
|
9482
|
+
for (const id of metadata.replaces ?? []) {
|
|
9483
|
+
const old = records[id];
|
|
9484
|
+
if (!old) continue;
|
|
9485
|
+
if (old.definition.lifecycle || Object.keys(old.bins).some((bin) => !metadata.bins[bin])) throw Error("Replacement must include every old command and cannot replace managed services");
|
|
9486
|
+
for (const bin of Object.keys(old.bins)) this.commandOwner({ ...metadata, id: "replacement-validation" }, { [id]: old }, bin);
|
|
9487
|
+
}
|
|
9488
|
+
}
|
|
9489
|
+
commandOwner(metadata, records, name) {
|
|
9490
|
+
const candidates = [records[metadata.id], ...(metadata.replaces ?? []).map((id) => records[id])].filter((p) => !!p);
|
|
9491
|
+
const owner = candidates.find((p) => p.bins[name]);
|
|
9492
|
+
if (owner && owner.id !== metadata.id) {
|
|
9493
|
+
if (owner.definition.lifecycle) throw Error("Cannot replace a package with managed services");
|
|
9494
|
+
if (Object.keys(owner.bins).some((bin) => !metadata.bins[bin])) throw Error("Replacement must include every old command");
|
|
9495
|
+
const roots = [import_node_path3.default.join(this.directory, "repos", ...owner.definition.repository.split("/")), import_node_path3.default.join(this.directory, "releases", ...owner.definition.repository.split("/"), owner.definition.name + "-" + owner.version)];
|
|
9496
|
+
if (!roots.includes(owner.root) || !(0, import_node_fs3.realpathSync)(owner.bins[name]).startsWith((0, import_node_fs3.realpathSync)(owner.root) + import_node_path3.default.sep)) throw Error("Replaced command is outside its managed installation");
|
|
9497
|
+
}
|
|
9498
|
+
return owner;
|
|
9499
|
+
}
|
|
9500
|
+
retireReplaced(metadata, records) {
|
|
9501
|
+
for (const id of metadata.replaces ?? []) if (records[id]) delete records[id];
|
|
9502
|
+
}
|
|
9503
|
+
async healthy(root, step) {
|
|
9023
9504
|
for (let attempt = 0; attempt < 15; attempt++) {
|
|
9024
9505
|
try {
|
|
9025
9506
|
const result = JSON.parse(await this.step(root, step));
|
|
@@ -9037,62 +9518,62 @@ var PackageInstaller = class {
|
|
|
9037
9518
|
if (!response.ok || !body.success) throw Error(body.message ?? "Package registry unavailable");
|
|
9038
9519
|
return validateInstallMetadata(body.data, ref.id);
|
|
9039
9520
|
}
|
|
9040
|
-
async install(name
|
|
9521
|
+
async install(name) {
|
|
9041
9522
|
const metadata = await this.metadata(name);
|
|
9042
9523
|
if (!metadata.platforms.includes(this.ports.platform ?? process.platform)) throw Error("Package is not supported on this platform");
|
|
9043
|
-
(0,
|
|
9044
|
-
if ((0,
|
|
9045
|
-
const lock =
|
|
9524
|
+
(0, import_node_fs3.mkdirSync)(this.directory, { recursive: true, mode: 448 });
|
|
9525
|
+
if ((0, import_node_fs3.lstatSync)(this.directory).isSymbolicLink()) throw Error("Package directory must not be a symlink");
|
|
9526
|
+
const lock = import_node_path3.default.join(this.directory, "install.lock");
|
|
9046
9527
|
try {
|
|
9047
|
-
(0,
|
|
9048
|
-
(0,
|
|
9528
|
+
(0, import_node_fs3.mkdirSync)(lock);
|
|
9529
|
+
(0, import_node_fs3.writeFileSync)(import_node_path3.default.join(lock, "pid"), String(process.pid));
|
|
9049
9530
|
} catch {
|
|
9050
9531
|
throw Error("Another package installation is running; inspect ~/.pmate/packages/install.lock");
|
|
9051
9532
|
}
|
|
9052
9533
|
try {
|
|
9053
9534
|
const old = this.records()[metadata.id];
|
|
9054
|
-
return
|
|
9535
|
+
return old?.root.startsWith(import_node_path3.default.join(this.directory, "releases") + import_node_path3.default.sep) ? await this.installVersioned(metadata) : await this.installLocked(metadata);
|
|
9055
9536
|
} finally {
|
|
9056
|
-
(0,
|
|
9537
|
+
(0, import_node_fs3.rmSync)(lock, { recursive: true, force: true });
|
|
9057
9538
|
}
|
|
9058
9539
|
}
|
|
9059
9540
|
/** Prepare a separate source tree, then switch command links. Never touch running processes. */
|
|
9060
|
-
async
|
|
9541
|
+
async installVersioned(metadata) {
|
|
9061
9542
|
const records = this.records();
|
|
9062
9543
|
this.validateReplacements(metadata, records);
|
|
9063
9544
|
const old = records[metadata.id];
|
|
9064
|
-
const root =
|
|
9065
|
-
(0,
|
|
9545
|
+
const root = import_node_path3.default.join(this.directory, "releases", ...metadata.repository.split("/"), metadata.name + "-" + metadata.version);
|
|
9546
|
+
(0, import_node_fs3.mkdirSync)(this.binDirectory, { recursive: true });
|
|
9066
9547
|
for (const name of Object.keys(metadata.bins)) {
|
|
9067
|
-
const owner = this.commandOwner(metadata, records, name), link =
|
|
9068
|
-
if (stat && (!owner?.bins[name] || !stat.isSymbolicLink() || (0,
|
|
9548
|
+
const owner = this.commandOwner(metadata, records, name), link = import_node_path3.default.join(this.binDirectory, name), stat = (0, import_node_fs3.lstatSync)(link, { throwIfNoEntry: false });
|
|
9549
|
+
if (stat && (!owner?.bins[name] || !stat.isSymbolicLink() || (0, import_node_fs3.realpathSync)(link) !== (0, import_node_fs3.realpathSync)(owner.bins[name]))) throw Error("Refusing to replace unmanaged command: " + name);
|
|
9069
9550
|
}
|
|
9070
9551
|
if (old?.version === metadata.version) {
|
|
9071
|
-
const manifest2 = JSON.parse((0,
|
|
9552
|
+
const manifest2 = JSON.parse((0, import_node_fs3.readFileSync)(this.local(old.root, import_node_path3.default.posix.join(metadata.directory, metadata.manifest)), "utf8"));
|
|
9072
9553
|
if (manifest2.version !== old.version) throw Error("Installed manifest does not match the recorded version");
|
|
9073
|
-
for (const [name, file] of Object.entries(old.bins)) if (!(0,
|
|
9554
|
+
for (const [name, file] of Object.entries(old.bins)) if (!(0, import_node_fs3.existsSync)(import_node_path3.default.join(this.binDirectory, name)) || (0, import_node_fs3.realpathSync)(import_node_path3.default.join(this.binDirectory, name)) !== (0, import_node_fs3.realpathSync)(file)) throw Error("Installed bin is missing or changed: " + name);
|
|
9074
9555
|
return { ...old, changed: false };
|
|
9075
9556
|
}
|
|
9076
9557
|
if (old && old.version.split(".").map(BigInt).some((part, i, parts) => part > BigInt(metadata.version.split(".")[i]) && parts.slice(0, i).every((p, j) => p === BigInt(metadata.version.split(".")[j])))) throw Error("Published package version is older than the installed version");
|
|
9077
|
-
if ((0,
|
|
9078
|
-
(0,
|
|
9558
|
+
if ((0, import_node_fs3.existsSync)(root)) throw Error("Release directory already exists; inspect it before retrying");
|
|
9559
|
+
(0, import_node_fs3.mkdirSync)(import_node_path3.default.dirname(root), { recursive: true, mode: 448 });
|
|
9079
9560
|
this.say("Preparing isolated release " + metadata.id + " " + metadata.version + "; running processes are unchanged");
|
|
9080
|
-
await this.execute("git", ["clone", "--branch", "main", "https://github.com/" + metadata.repository + ".git", root],
|
|
9081
|
-
const manifest = JSON.parse((0,
|
|
9561
|
+
await this.execute("git", ["clone", "--branch", "main", "https://github.com/" + metadata.repository + ".git", root], import_node_path3.default.dirname(root));
|
|
9562
|
+
const manifest = JSON.parse((0, import_node_fs3.readFileSync)(this.local(root, import_node_path3.default.posix.join(metadata.directory, metadata.manifest)), "utf8"));
|
|
9082
9563
|
if (manifest.version !== metadata.version) throw Error("Repository component version does not match the published package version");
|
|
9083
9564
|
for (const step of metadata.prepare) await this.step(root, step);
|
|
9084
9565
|
const bins = {};
|
|
9085
9566
|
for (const [name, file] of Object.entries(metadata.bins)) {
|
|
9086
9567
|
const target = this.local(root, file);
|
|
9087
|
-
if (!(0,
|
|
9088
|
-
(0,
|
|
9568
|
+
if (!(0, import_node_fs3.lstatSync)(target).isFile()) throw Error("Package bin is not a regular file");
|
|
9569
|
+
(0, import_node_fs3.chmodSync)(target, 493);
|
|
9089
9570
|
bins[name] = target;
|
|
9090
9571
|
}
|
|
9091
9572
|
const commit = await this.execute("git", ["rev-parse", "HEAD"], root);
|
|
9092
9573
|
for (const [name, target] of Object.entries(bins)) {
|
|
9093
|
-
const link =
|
|
9094
|
-
(0,
|
|
9095
|
-
(0,
|
|
9574
|
+
const link = import_node_path3.default.join(this.binDirectory, name), temporary = link + ".install-" + process.pid;
|
|
9575
|
+
(0, import_node_fs3.symlinkSync)(target, temporary);
|
|
9576
|
+
(0, import_node_fs3.renameSync)(temporary, link);
|
|
9096
9577
|
}
|
|
9097
9578
|
const record = { id: metadata.id, version: metadata.version, root, directory: metadata.directory, manifest: metadata.manifest, bins, metadataRevision: metadata.revision, commit, installedAt: (/* @__PURE__ */ new Date()).toISOString(), definition: metadata };
|
|
9098
9579
|
this.retireReplaced(metadata, records);
|
|
@@ -9104,24 +9585,24 @@ var PackageInstaller = class {
|
|
|
9104
9585
|
async installLocked(metadata) {
|
|
9105
9586
|
const records = this.records();
|
|
9106
9587
|
this.validateReplacements(metadata, records);
|
|
9107
|
-
const old = records[metadata.id], root =
|
|
9108
|
-
(0,
|
|
9588
|
+
const old = records[metadata.id], root = import_node_path3.default.join(this.directory, "repos", ...metadata.repository.split("/"));
|
|
9589
|
+
(0, import_node_fs3.mkdirSync)(import_node_path3.default.dirname(root), { recursive: true, mode: 448 });
|
|
9109
9590
|
if (old && old.root !== root) throw Error("Installed repository location differs; migration required");
|
|
9110
|
-
if ((0,
|
|
9111
|
-
if ((0,
|
|
9591
|
+
if ((0, import_node_fs3.existsSync)(root)) {
|
|
9592
|
+
if ((0, import_node_fs3.lstatSync)(root).isSymbolicLink()) throw Error("Repository directory must not be a symlink");
|
|
9112
9593
|
const remote = (await this.execute("git", ["remote", "get-url", "origin"], root)).replace(/\.git$/, "");
|
|
9113
9594
|
if (!remote.endsWith("/" + metadata.repository) && !remote.endsWith(":" + metadata.repository)) throw Error("Installed repository origin differs");
|
|
9114
9595
|
if (await this.execute("git", ["status", "--porcelain"], root)) throw Error("Installed repository contains local changes; preserve them before updating");
|
|
9115
9596
|
}
|
|
9116
|
-
(0,
|
|
9597
|
+
(0, import_node_fs3.mkdirSync)(this.binDirectory, { recursive: true });
|
|
9117
9598
|
for (const name of Object.keys(metadata.bins)) {
|
|
9118
|
-
const owner = this.commandOwner(metadata, records, name), file =
|
|
9119
|
-
if (stat && (!owner?.bins[name] || !stat.isSymbolicLink() || (0,
|
|
9599
|
+
const owner = this.commandOwner(metadata, records, name), file = import_node_path3.default.join(this.binDirectory, name), stat = (0, import_node_fs3.lstatSync)(file, { throwIfNoEntry: false });
|
|
9600
|
+
if (stat && (!owner?.bins[name] || !stat.isSymbolicLink() || (0, import_node_fs3.realpathSync)(file) !== (0, import_node_fs3.realpathSync)(owner.bins[name]))) throw Error("Refusing to replace unmanaged command: " + name);
|
|
9120
9601
|
}
|
|
9121
|
-
if (old?.version === metadata.version && (0,
|
|
9122
|
-
const manifest = JSON.parse((0,
|
|
9602
|
+
if (old?.version === metadata.version && (0, import_node_fs3.existsSync)(root)) {
|
|
9603
|
+
const manifest = JSON.parse((0, import_node_fs3.readFileSync)(this.local(root, import_node_path3.default.posix.join(metadata.directory, metadata.manifest)), "utf8"));
|
|
9123
9604
|
if (manifest.version !== old.version) throw Error("Installed manifest does not match the recorded version");
|
|
9124
|
-
for (const [name, file] of Object.entries(old.bins)) if (!(0,
|
|
9605
|
+
for (const [name, file] of Object.entries(old.bins)) if (!(0, import_node_fs3.existsSync)(import_node_path3.default.join(this.binDirectory, name)) || (0, import_node_fs3.realpathSync)(import_node_path3.default.join(this.binDirectory, name)) !== (0, import_node_fs3.realpathSync)(file)) throw Error("Installed bin is missing or changed: " + name);
|
|
9125
9606
|
this.say(metadata.id + " is already installed (" + metadata.version + ")");
|
|
9126
9607
|
return { ...old, changed: false };
|
|
9127
9608
|
}
|
|
@@ -9146,534 +9627,172 @@ var PackageInstaller = class {
|
|
|
9146
9627
|
}
|
|
9147
9628
|
if (wasRunning) {
|
|
9148
9629
|
await this.step(root, lifecycle.stop);
|
|
9149
|
-
stopped = true;
|
|
9150
|
-
}
|
|
9151
|
-
} catch (error) {
|
|
9152
|
-
if (drained && !stopped) await this.step(root, lifecycle.start);
|
|
9153
|
-
throw error;
|
|
9154
|
-
}
|
|
9155
|
-
}
|
|
9156
|
-
const previousCommit = (0, import_node_fs2.existsSync)(root) ? await this.execute("git", ["rev-parse", "HEAD"], root) : void 0;
|
|
9157
|
-
try {
|
|
9158
|
-
if (!(0, import_node_fs2.existsSync)(root)) {
|
|
9159
|
-
this.say("Cloning " + metadata.repository);
|
|
9160
|
-
await this.execute("git", ["clone", "--branch", "main", "https://github.com/" + metadata.repository + ".git", root], import_node_path2.default.dirname(root));
|
|
9161
|
-
} else {
|
|
9162
|
-
this.say("Updating " + metadata.repository);
|
|
9163
|
-
await this.execute("git", ["pull", "--ff-only", "origin", "main"], root);
|
|
9164
|
-
}
|
|
9165
|
-
const manifest = JSON.parse((0, import_node_fs2.readFileSync)(this.local(root, import_node_path2.default.posix.join(metadata.directory, metadata.manifest)), "utf8"));
|
|
9166
|
-
if (manifest.version !== metadata.version) throw Error("Repository component version does not match the published package version");
|
|
9167
|
-
for (const sibling of Object.values(records).filter((p) => p.root === root && p.id !== metadata.id)) {
|
|
9168
|
-
const manifest2 = JSON.parse((0, import_node_fs2.readFileSync)(this.local(root, import_node_path2.default.posix.join(sibling.directory, sibling.manifest)), "utf8"));
|
|
9169
|
-
if (manifest2.version !== sibling.version) throw Error("Shared repository also requires an update to " + sibling.id);
|
|
9170
|
-
}
|
|
9171
|
-
for (const step of metadata.prepare) {
|
|
9172
|
-
this.say("Preparing " + metadata.id + ": " + step.command);
|
|
9173
|
-
await this.step(root, step);
|
|
9174
|
-
}
|
|
9175
|
-
const bins = {};
|
|
9176
|
-
for (const [name, file] of Object.entries(metadata.bins)) {
|
|
9177
|
-
const target = this.local(root, file);
|
|
9178
|
-
if (!(0, import_node_fs2.lstatSync)(target).isFile()) throw Error("Package bin is not a regular file");
|
|
9179
|
-
(0, import_node_fs2.chmodSync)(target, 493);
|
|
9180
|
-
bins[name] = target;
|
|
9181
|
-
}
|
|
9182
|
-
const commit = await this.execute("git", ["rev-parse", "HEAD"], root);
|
|
9183
|
-
if (wasRunning && metadata.lifecycle) {
|
|
9184
|
-
await this.step(root, metadata.lifecycle.start);
|
|
9185
|
-
await this.healthy(root, metadata.lifecycle.health);
|
|
9186
|
-
}
|
|
9187
|
-
for (const [name, target] of Object.entries(bins)) {
|
|
9188
|
-
const link = import_node_path2.default.join(this.binDirectory, name), temporary = link + ".install-" + process.pid;
|
|
9189
|
-
(0, import_node_fs2.symlinkSync)(target, temporary);
|
|
9190
|
-
(0, import_node_fs2.renameSync)(temporary, link);
|
|
9191
|
-
}
|
|
9192
|
-
const record = { id: metadata.id, version: metadata.version, root, directory: metadata.directory, manifest: metadata.manifest, bins, metadataRevision: metadata.revision, commit, installedAt: (/* @__PURE__ */ new Date()).toISOString(), definition: metadata };
|
|
9193
|
-
this.retireReplaced(metadata, records);
|
|
9194
|
-
records[metadata.id] = record;
|
|
9195
|
-
this.save(records);
|
|
9196
|
-
this.say("Installed " + metadata.id + " " + metadata.version);
|
|
9197
|
-
return { ...record, changed: true };
|
|
9198
|
-
} catch (error) {
|
|
9199
|
-
if (stopped && old && previousCommit) {
|
|
9200
|
-
try {
|
|
9201
|
-
if (await this.execute("git", ["status", "--porcelain", "--untracked-files=no"], root)) throw Error("Failed upgrade changed tracked files");
|
|
9202
|
-
await this.execute("git", ["checkout", "--detach", previousCommit], root);
|
|
9203
|
-
for (const step of old.definition.prepare) await this.step(root, step);
|
|
9204
|
-
await this.step(root, old.definition.lifecycle.start);
|
|
9205
|
-
await this.healthy(root, old.definition.lifecycle.health);
|
|
9206
|
-
this.say("Upgrade failed; restored the previously installed package " + old.version);
|
|
9207
|
-
} catch {
|
|
9208
|
-
this.say("Upgrade failed and automatic recovery could not complete. Previous commit: " + previousCommit + ". Credentials, data and the installation record were preserved.");
|
|
9209
|
-
}
|
|
9210
|
-
}
|
|
9211
|
-
throw error;
|
|
9212
|
-
}
|
|
9213
|
-
}
|
|
9214
|
-
};
|
|
9215
|
-
|
|
9216
|
-
// src/installCommandParser.ts
|
|
9217
|
-
function parseInstallArgs(args) {
|
|
9218
|
-
const staged = args.length === 2 && args[1] === "--staged";
|
|
9219
|
-
if (args.length !== 1 && !staged || !args[0] || args[0].startsWith("-")) throw Error("Usage: pmate install <package> [--staged]");
|
|
9220
|
-
return { type: "install" /* Install */, package: args[0], ...staged ? { staged: true } : {} };
|
|
9221
|
-
}
|
|
9222
|
-
|
|
9223
|
-
// src/managedSession.ts
|
|
9224
|
-
var import_node_fs3 = require("fs");
|
|
9225
|
-
var import_node_os3 = require("os");
|
|
9226
|
-
var import_node_crypto2 = require("crypto");
|
|
9227
|
-
var import_node_path3 = require("path");
|
|
9228
|
-
async function ensureManagedSession(env = process.env, fetcher = fetch) {
|
|
9229
|
-
if (env.PMATE_EXPECTED_ACCOUNT_ID && env.HEBO_MEMORY_TOKEN?.trim()) throw Error("Bound Agent CLI cannot use another delegated identity");
|
|
9230
|
-
if (env.HEBO_MEMORY_TOKEN?.trim()) {
|
|
9231
|
-
const token = env.HEBO_MEMORY_TOKEN.trim(), app = "pmate:factory";
|
|
9232
|
-
const base = (env.AUTH_API_BASE_URL ?? "https://auth-api-v2.pmate.chat").replace(/\/+$/, "");
|
|
9233
|
-
const headers = { authorization: `Bearer ${token}` };
|
|
9234
|
-
const r2 = await fetcher(`${base}/session?app=${encodeURIComponent(app)}`, { headers, signal: AbortSignal.timeout(15e3) });
|
|
9235
|
-
const body = await r2.json(), session = body.data;
|
|
9236
|
-
if (!r2.ok || !body.success || session?.identity?.app !== app || !session.identity.accountId || !session.issuedAt || !Number.isFinite(Date.parse(session.expiresAt)) || Date.parse(session.expiresAt) <= Date.now()) throw Error("Delegated user session is invalid or expired");
|
|
9237
|
-
const pr = await fetcher(`${base}/profiles?app=${encodeURIComponent(app)}&account=${encodeURIComponent(session.identity.accountId)}`, { headers, signal: AbortSignal.timeout(15e3) });
|
|
9238
|
-
const profiles = await pr.json(), profileId = profiles.data?.[0]?.id;
|
|
9239
|
-
if (!pr.ok || !profiles.success || !profileId) throw Error("Delegated user profile unavailable");
|
|
9240
|
-
const dir = (0, import_node_fs3.mkdtempSync)((0, import_node_path3.join)((0, import_node_os3.tmpdir)(), "pmate-delegated-")), file = (0, import_node_path3.join)(dir, "session.json");
|
|
9241
|
-
(0, import_node_fs3.writeFileSync)(file, JSON.stringify({ version: 4, activeAppRef: app, sessions: { [app]: { token, accountId: session.identity.accountId, profileId, issuedAt: session.issuedAt, expiresAt: session.expiresAt, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } } }), { mode: 384 });
|
|
9242
|
-
env.PMATE_SESSION_FILE = file;
|
|
9243
|
-
env.PCS_AGENT_SESSION_MANAGED = "0";
|
|
9244
|
-
process.once("exit", () => {
|
|
9245
|
-
(0, import_node_fs3.rmSync)(dir, { recursive: true, force: true });
|
|
9246
|
-
});
|
|
9247
|
-
return;
|
|
9248
|
-
}
|
|
9249
|
-
if (env.PCS_AGENT_SESSION_MANAGED !== "1") return;
|
|
9250
|
-
if (!env.PMATE_SESSION_FILE) throw Error("Managed session file is required");
|
|
9251
|
-
const source = env.PCS_AGENT_SESSION_SOURCE_FILE ?? env.PMATE_SESSION_FILE;
|
|
9252
|
-
const state = JSON.parse((0, import_node_fs3.readFileSync)(source, "utf8"));
|
|
9253
|
-
if (!state.refreshKey) throw Error("Managed session refresh key is missing");
|
|
9254
|
-
const port = Number(env.PMATE_SESSION_REFRESH_PORT ?? env.PCS_AGENT_CONTROL_PORT ?? 39399);
|
|
9255
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535) throw Error("Invalid controller port");
|
|
9256
|
-
const r = await fetcher(`http://127.0.0.1:${port}/${env.PMATE_SESSION_REFRESH_PORT ? "v1" : "v2"}/session/refresh`, {
|
|
9257
|
-
method: "POST",
|
|
9258
|
-
headers: { "x-agent-session-key": state.refreshKey },
|
|
9259
|
-
signal: AbortSignal.timeout(45e3)
|
|
9260
|
-
});
|
|
9261
|
-
if (!r.ok) throw Error(`Managed session renewal unavailable (${r.status}); retry later`);
|
|
9262
|
-
if (source !== env.PMATE_SESSION_FILE) {
|
|
9263
|
-
const raw = (0, import_node_fs3.readFileSync)(source, "utf8"), current = JSON.parse(raw).sessions?.[env.PMATE_SESSION_APP ?? "pmate:factory"];
|
|
9264
|
-
if (env.PMATE_EXPECTED_ACCOUNT_ID && current?.accountId !== env.PMATE_EXPECTED_ACCOUNT_ID) throw Error("Renewed Agent CLI Account mismatch");
|
|
9265
|
-
if (env.PMATE_EXPECTED_PROFILE_ID && current?.profileId !== env.PMATE_EXPECTED_PROFILE_ID) throw Error("Renewed Agent CLI Profile mismatch");
|
|
9266
|
-
const temp = `${env.PMATE_SESSION_FILE}.${(0, import_node_crypto2.randomUUID)()}.tmp`;
|
|
9267
|
-
(0, import_node_fs3.writeFileSync)(temp, raw, { mode: 384, flag: "wx" });
|
|
9268
|
-
(0, import_node_fs3.renameSync)(temp, env.PMATE_SESSION_FILE);
|
|
9269
|
-
}
|
|
9270
|
-
}
|
|
9271
|
-
|
|
9272
|
-
// src/index.ts
|
|
9273
|
-
var import_chalk4 = __toESM(require("chalk"));
|
|
9274
|
-
|
|
9275
|
-
// src/cli.ts
|
|
9276
|
-
var import_chalk = __toESM(require("chalk"));
|
|
9277
|
-
function formatValue(value) {
|
|
9278
|
-
if (typeof value === "string") {
|
|
9279
|
-
return value;
|
|
9280
|
-
}
|
|
9281
|
-
try {
|
|
9282
|
-
return JSON.stringify(value, null, 2);
|
|
9283
|
-
} catch {
|
|
9284
|
-
return String(value);
|
|
9285
|
-
}
|
|
9286
|
-
}
|
|
9287
|
-
function withData(base, data, dataStyle = import_chalk.default.dim) {
|
|
9288
|
-
if (data === void 0) {
|
|
9289
|
-
return base;
|
|
9290
|
-
}
|
|
9291
|
-
return `${base} ${dataStyle(formatValue(data))}`;
|
|
9292
|
-
}
|
|
9293
|
-
var ui = {
|
|
9294
|
-
info(message, data) {
|
|
9295
|
-
console.log(withData(import_chalk.default.cyan(message), data));
|
|
9296
|
-
},
|
|
9297
|
-
success(message, data) {
|
|
9298
|
-
console.log(withData(import_chalk.default.green(message), data));
|
|
9299
|
-
},
|
|
9300
|
-
warn(message, data) {
|
|
9301
|
-
console.log(withData(import_chalk.default.yellow(message), data));
|
|
9302
|
-
},
|
|
9303
|
-
error(message, data) {
|
|
9304
|
-
console.error(withData(import_chalk.default.red(message), data));
|
|
9305
|
-
},
|
|
9306
|
-
plain(message) {
|
|
9307
|
-
console.log(message);
|
|
9308
|
-
},
|
|
9309
|
-
title(message) {
|
|
9310
|
-
console.log(import_chalk.default.bold(message));
|
|
9311
|
-
}
|
|
9312
|
-
};
|
|
9313
|
-
|
|
9314
|
-
// src/commands/ListCommandHandler.ts
|
|
9315
|
-
var import_node_path5 = __toESM(require("path"));
|
|
9316
|
-
|
|
9317
|
-
// src/config.ts
|
|
9318
|
-
var import_node_fs4 = require("fs");
|
|
9319
|
-
var import_node_os4 = __toESM(require("os"));
|
|
9320
|
-
var import_node_path4 = __toESM(require("path"));
|
|
9321
|
-
var import_ajv = __toESM(require("ajv"));
|
|
9322
|
-
var import_yaml = __toESM(require("yaml"));
|
|
9323
|
-
var cachedConfig;
|
|
9324
|
-
var CONFIG = new Proxy({}, {
|
|
9325
|
-
get(_target, prop) {
|
|
9326
|
-
return loadPmateConfig()[prop];
|
|
9327
|
-
}
|
|
9328
|
-
});
|
|
9329
|
-
var PMATE_USER_CONFIG_SCHEMA = {
|
|
9330
|
-
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
9331
|
-
type: "object",
|
|
9332
|
-
additionalProperties: false,
|
|
9333
|
-
properties: {
|
|
9334
|
-
aliyun: {
|
|
9335
|
-
type: "object",
|
|
9336
|
-
additionalProperties: false,
|
|
9337
|
-
properties: {
|
|
9338
|
-
ak: { type: "string" },
|
|
9339
|
-
sk: { type: "string" },
|
|
9340
|
-
region: { type: "string" },
|
|
9341
|
-
domain: { type: "string" }
|
|
9342
|
-
}
|
|
9343
|
-
},
|
|
9344
|
-
notion: {
|
|
9345
|
-
type: "object",
|
|
9346
|
-
additionalProperties: false,
|
|
9347
|
-
properties: {
|
|
9348
|
-
token: { type: "string" }
|
|
9349
|
-
}
|
|
9350
|
-
},
|
|
9351
|
-
openAI: {
|
|
9352
|
-
type: "object",
|
|
9353
|
-
additionalProperties: false,
|
|
9354
|
-
properties: {
|
|
9355
|
-
apiKey: { type: "string" },
|
|
9356
|
-
baseUrl: { type: "string" }
|
|
9357
|
-
}
|
|
9358
|
-
},
|
|
9359
|
-
linear: {
|
|
9360
|
-
type: "object",
|
|
9361
|
-
additionalProperties: false,
|
|
9362
|
-
properties: {
|
|
9363
|
-
teamId: { type: "string" },
|
|
9364
|
-
teamKey: { type: "string" },
|
|
9365
|
-
accounts: {
|
|
9366
|
-
type: "array",
|
|
9367
|
-
items: {
|
|
9368
|
-
type: "object",
|
|
9369
|
-
additionalProperties: false,
|
|
9370
|
-
required: ["title", "apiKey"],
|
|
9371
|
-
properties: {
|
|
9372
|
-
title: { type: "string" },
|
|
9373
|
-
apiKey: { type: "string" },
|
|
9374
|
-
teamId: { type: "string" },
|
|
9375
|
-
teamKey: { type: "string" }
|
|
9376
|
-
}
|
|
9377
|
-
}
|
|
9378
|
-
}
|
|
9379
|
-
}
|
|
9380
|
-
},
|
|
9381
|
-
oss: {
|
|
9382
|
-
type: "object",
|
|
9383
|
-
additionalProperties: false,
|
|
9384
|
-
properties: {
|
|
9385
|
-
public: {
|
|
9386
|
-
type: "object",
|
|
9387
|
-
additionalProperties: false,
|
|
9388
|
-
properties: {
|
|
9389
|
-
region: { type: "string" },
|
|
9390
|
-
bucket: { type: "string" }
|
|
9391
|
-
}
|
|
9392
|
-
},
|
|
9393
|
-
private: {
|
|
9394
|
-
type: "object",
|
|
9395
|
-
additionalProperties: false,
|
|
9396
|
-
properties: {
|
|
9397
|
-
region: { type: "string" },
|
|
9398
|
-
bucket: { type: "string" }
|
|
9399
|
-
}
|
|
9400
|
-
}
|
|
9401
|
-
}
|
|
9402
|
-
},
|
|
9403
|
-
ssl: {
|
|
9404
|
-
type: "object",
|
|
9405
|
-
additionalProperties: false,
|
|
9406
|
-
properties: {
|
|
9407
|
-
pub: { type: "string" },
|
|
9408
|
-
prv: { type: "string" }
|
|
9409
|
-
}
|
|
9410
|
-
},
|
|
9411
|
-
github: {
|
|
9412
|
-
type: "object",
|
|
9413
|
-
additionalProperties: false,
|
|
9414
|
-
properties: {
|
|
9415
|
-
token: { type: "string" }
|
|
9416
|
-
}
|
|
9417
|
-
},
|
|
9418
|
-
test: {
|
|
9419
|
-
type: "object",
|
|
9420
|
-
additionalProperties: false,
|
|
9421
|
-
properties: {
|
|
9422
|
-
authKey: { type: "string" }
|
|
9423
|
-
}
|
|
9424
|
-
},
|
|
9425
|
-
vercel: {
|
|
9426
|
-
type: "object",
|
|
9427
|
-
additionalProperties: false,
|
|
9428
|
-
properties: {
|
|
9429
|
-
scope: { type: "string" }
|
|
9430
|
-
}
|
|
9431
|
-
}
|
|
9432
|
-
}
|
|
9433
|
-
};
|
|
9434
|
-
var ajv = new import_ajv.default({
|
|
9435
|
-
allErrors: true,
|
|
9436
|
-
allowUnionTypes: true,
|
|
9437
|
-
validateSchema: false
|
|
9438
|
-
});
|
|
9439
|
-
var validatePmateUserConfig = ajv.compile(PMATE_USER_CONFIG_SCHEMA);
|
|
9440
|
-
function loadPmateConfig() {
|
|
9441
|
-
if (cachedConfig !== void 0) {
|
|
9442
|
-
return cachedConfig ?? {};
|
|
9443
|
-
}
|
|
9444
|
-
const pmateDir = import_node_path4.default.join(import_node_os4.default.homedir(), ".pmate");
|
|
9445
|
-
const yamlPath = import_node_path4.default.join(pmateDir, "config.yaml");
|
|
9446
|
-
let yamlConfig = null;
|
|
9447
|
-
if ((0, import_node_fs4.existsSync)(yamlPath)) {
|
|
9448
|
-
const raw = (0, import_node_fs4.readFileSync)(yamlPath, "utf8");
|
|
9449
|
-
try {
|
|
9450
|
-
const parsed = import_yaml.default.parse(raw);
|
|
9451
|
-
yamlConfig = validateParsedConfig(parsed, yamlPath);
|
|
9452
|
-
} catch (error) {
|
|
9453
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
9454
|
-
throw new Error(`Invalid YAML in ${yamlPath}: ${message}`);
|
|
9455
|
-
}
|
|
9456
|
-
}
|
|
9457
|
-
cachedConfig = yamlConfig ?? {};
|
|
9458
|
-
return cachedConfig;
|
|
9459
|
-
}
|
|
9460
|
-
function loadRepoConfig(cwd) {
|
|
9461
|
-
const paths = findRepoConfigPaths(cwd, true);
|
|
9462
|
-
if (!paths) {
|
|
9463
|
-
return null;
|
|
9464
|
-
}
|
|
9465
|
-
const parsed = loadMergedRepoYaml(paths);
|
|
9466
|
-
return normalizeRepoConfig(parsed, paths.configPath);
|
|
9467
|
-
}
|
|
9468
|
-
function loadAppConfig(cwd, options = {}) {
|
|
9469
|
-
const searchParents = options.searchParents ?? true;
|
|
9470
|
-
const paths = findRepoConfigPaths(cwd, searchParents);
|
|
9471
|
-
if (!paths) {
|
|
9472
|
-
const suffix = searchParents ? " or parent directories" : "";
|
|
9473
|
-
throw new Error(`No .pmate.yaml found in ${cwd}${suffix}`);
|
|
9474
|
-
}
|
|
9475
|
-
const parsed = loadMergedRepoYaml(paths);
|
|
9476
|
-
const appConfig = parsed;
|
|
9477
|
-
if (!appConfig || typeof appConfig !== "object" || !Array.isArray(appConfig.apps)) {
|
|
9478
|
-
throw new Error(
|
|
9479
|
-
`Invalid .pmate.yaml: missing apps array (${describeRepoConfigPaths(paths)})`
|
|
9480
|
-
);
|
|
9481
|
-
}
|
|
9482
|
-
return {
|
|
9483
|
-
config: appConfig,
|
|
9484
|
-
rootDir: paths.rootDir,
|
|
9485
|
-
configPath: paths.configPath,
|
|
9486
|
-
localConfigPath: paths.localConfigPath
|
|
9487
|
-
};
|
|
9488
|
-
}
|
|
9489
|
-
function describeLoadedAppConfig({
|
|
9490
|
-
configPath,
|
|
9491
|
-
localConfigPath
|
|
9492
|
-
}) {
|
|
9493
|
-
return localConfigPath ? `${configPath} + ${localConfigPath}` : configPath;
|
|
9494
|
-
}
|
|
9495
|
-
function validateParsedConfig(value, configPath) {
|
|
9496
|
-
if (value === null || value === void 0) {
|
|
9497
|
-
return {};
|
|
9498
|
-
}
|
|
9499
|
-
if (!validatePmateUserConfig(value)) {
|
|
9500
|
-
const detail = ajv.errorsText(validatePmateUserConfig.errors, {
|
|
9501
|
-
separator: "\n",
|
|
9502
|
-
dataVar: "config"
|
|
9503
|
-
});
|
|
9504
|
-
throw new Error(`Invalid config schema in ${configPath}:
|
|
9505
|
-
${detail}`);
|
|
9506
|
-
}
|
|
9507
|
-
return value;
|
|
9508
|
-
}
|
|
9509
|
-
function findRepoConfigPaths(cwd, searchParents) {
|
|
9510
|
-
let current = import_node_path4.default.resolve(cwd);
|
|
9511
|
-
while (true) {
|
|
9512
|
-
const candidate = import_node_path4.default.join(current, ".pmate.yaml");
|
|
9513
|
-
if ((0, import_node_fs4.existsSync)(candidate)) {
|
|
9514
|
-
const localCandidate = import_node_path4.default.join(current, ".pmate.local.yaml");
|
|
9515
|
-
return {
|
|
9516
|
-
rootDir: current,
|
|
9517
|
-
configPath: candidate,
|
|
9518
|
-
localConfigPath: (0, import_node_fs4.existsSync)(localCandidate) ? localCandidate : void 0
|
|
9519
|
-
};
|
|
9520
|
-
}
|
|
9521
|
-
if (!searchParents) {
|
|
9522
|
-
return null;
|
|
9523
|
-
}
|
|
9524
|
-
const parent = import_node_path4.default.dirname(current);
|
|
9525
|
-
if (parent === current) {
|
|
9526
|
-
return null;
|
|
9527
|
-
}
|
|
9528
|
-
current = parent;
|
|
9529
|
-
}
|
|
9530
|
-
}
|
|
9531
|
-
function loadMergedRepoYaml(paths) {
|
|
9532
|
-
const base = parseYamlFile(paths.configPath);
|
|
9533
|
-
if (!paths.localConfigPath) {
|
|
9534
|
-
return base;
|
|
9535
|
-
}
|
|
9536
|
-
const local = parseYamlFile(paths.localConfigPath);
|
|
9537
|
-
return mergeRepoConfigValues(base, local);
|
|
9538
|
-
}
|
|
9539
|
-
function parseYamlFile(configPath) {
|
|
9540
|
-
const raw = (0, import_node_fs4.readFileSync)(configPath, "utf8");
|
|
9541
|
-
try {
|
|
9542
|
-
return import_yaml.default.parse(raw);
|
|
9543
|
-
} catch (error) {
|
|
9544
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
9545
|
-
throw new Error(`Invalid YAML in ${configPath}: ${message}`);
|
|
9546
|
-
}
|
|
9547
|
-
}
|
|
9548
|
-
function describeRepoConfigPaths(paths) {
|
|
9549
|
-
return paths.localConfigPath ? `${paths.configPath} + ${paths.localConfigPath}` : paths.configPath;
|
|
9550
|
-
}
|
|
9551
|
-
function mergeRepoConfigValues(base, local) {
|
|
9552
|
-
if (Array.isArray(base) && Array.isArray(local)) {
|
|
9553
|
-
return [...base, ...local];
|
|
9554
|
-
}
|
|
9555
|
-
if (isPlainObject(base) && isPlainObject(local)) {
|
|
9556
|
-
const merged = { ...base };
|
|
9557
|
-
for (const [key, localValue] of Object.entries(local)) {
|
|
9558
|
-
const baseValue = merged[key];
|
|
9559
|
-
if (key === "apps" && Array.isArray(baseValue) && Array.isArray(localValue)) {
|
|
9560
|
-
merged[key] = mergeApps(baseValue, localValue);
|
|
9561
|
-
continue;
|
|
9630
|
+
stopped = true;
|
|
9631
|
+
}
|
|
9632
|
+
} catch (error) {
|
|
9633
|
+
if (drained && !stopped) await this.step(root, lifecycle.start);
|
|
9634
|
+
throw error;
|
|
9562
9635
|
}
|
|
9563
|
-
merged[key] = mergeRepoConfigValues(baseValue, localValue);
|
|
9564
|
-
}
|
|
9565
|
-
return merged;
|
|
9566
|
-
}
|
|
9567
|
-
return local === void 0 ? base : local;
|
|
9568
|
-
}
|
|
9569
|
-
function mergeApps(baseApps, localApps) {
|
|
9570
|
-
const merged = [...baseApps];
|
|
9571
|
-
for (const localApp of localApps) {
|
|
9572
|
-
const localName = getAppName(localApp);
|
|
9573
|
-
if (!localName) {
|
|
9574
|
-
merged.push(localApp);
|
|
9575
|
-
continue;
|
|
9576
9636
|
}
|
|
9577
|
-
const
|
|
9578
|
-
|
|
9579
|
-
|
|
9580
|
-
|
|
9637
|
+
const previousCommit = (0, import_node_fs3.existsSync)(root) ? await this.execute("git", ["rev-parse", "HEAD"], root) : void 0;
|
|
9638
|
+
try {
|
|
9639
|
+
if (!(0, import_node_fs3.existsSync)(root)) {
|
|
9640
|
+
this.say("Cloning " + metadata.repository);
|
|
9641
|
+
await this.execute("git", ["clone", "--branch", "main", "https://github.com/" + metadata.repository + ".git", root], import_node_path3.default.dirname(root));
|
|
9642
|
+
} else {
|
|
9643
|
+
this.say("Updating " + metadata.repository);
|
|
9644
|
+
await this.execute("git", ["pull", "--ff-only", "origin", "main"], root);
|
|
9645
|
+
}
|
|
9646
|
+
const manifest = JSON.parse((0, import_node_fs3.readFileSync)(this.local(root, import_node_path3.default.posix.join(metadata.directory, metadata.manifest)), "utf8"));
|
|
9647
|
+
if (manifest.version !== metadata.version) throw Error("Repository component version does not match the published package version");
|
|
9648
|
+
for (const sibling of Object.values(records).filter((p) => p.root === root && p.id !== metadata.id)) {
|
|
9649
|
+
const manifest2 = JSON.parse((0, import_node_fs3.readFileSync)(this.local(root, import_node_path3.default.posix.join(sibling.directory, sibling.manifest)), "utf8"));
|
|
9650
|
+
if (manifest2.version !== sibling.version) throw Error("Shared repository also requires an update to " + sibling.id);
|
|
9651
|
+
}
|
|
9652
|
+
for (const step of metadata.prepare) {
|
|
9653
|
+
this.say("Preparing " + metadata.id + ": " + step.command);
|
|
9654
|
+
await this.step(root, step);
|
|
9655
|
+
}
|
|
9656
|
+
const bins = {};
|
|
9657
|
+
for (const [name, file] of Object.entries(metadata.bins)) {
|
|
9658
|
+
const target = this.local(root, file);
|
|
9659
|
+
if (!(0, import_node_fs3.lstatSync)(target).isFile()) throw Error("Package bin is not a regular file");
|
|
9660
|
+
(0, import_node_fs3.chmodSync)(target, 493);
|
|
9661
|
+
bins[name] = target;
|
|
9662
|
+
}
|
|
9663
|
+
const commit = await this.execute("git", ["rev-parse", "HEAD"], root);
|
|
9664
|
+
if (wasRunning && metadata.lifecycle) {
|
|
9665
|
+
await this.step(root, metadata.lifecycle.start);
|
|
9666
|
+
await this.healthy(root, metadata.lifecycle.health);
|
|
9667
|
+
}
|
|
9668
|
+
for (const [name, target] of Object.entries(bins)) {
|
|
9669
|
+
const link = import_node_path3.default.join(this.binDirectory, name), temporary = link + ".install-" + process.pid;
|
|
9670
|
+
(0, import_node_fs3.symlinkSync)(target, temporary);
|
|
9671
|
+
(0, import_node_fs3.renameSync)(temporary, link);
|
|
9672
|
+
}
|
|
9673
|
+
const record = { id: metadata.id, version: metadata.version, root, directory: metadata.directory, manifest: metadata.manifest, bins, metadataRevision: metadata.revision, commit, installedAt: (/* @__PURE__ */ new Date()).toISOString(), definition: metadata };
|
|
9674
|
+
this.retireReplaced(metadata, records);
|
|
9675
|
+
records[metadata.id] = record;
|
|
9676
|
+
this.save(records);
|
|
9677
|
+
this.say("Installed " + metadata.id + " " + metadata.version);
|
|
9678
|
+
return { ...record, changed: true };
|
|
9679
|
+
} catch (error) {
|
|
9680
|
+
if (stopped && old && previousCommit) {
|
|
9681
|
+
try {
|
|
9682
|
+
if (await this.execute("git", ["status", "--porcelain", "--untracked-files=no"], root)) throw Error("Failed upgrade changed tracked files");
|
|
9683
|
+
await this.execute("git", ["checkout", "--detach", previousCommit], root);
|
|
9684
|
+
for (const step of old.definition.prepare) await this.step(root, step);
|
|
9685
|
+
await this.step(root, old.definition.lifecycle.start);
|
|
9686
|
+
await this.healthy(root, old.definition.lifecycle.health);
|
|
9687
|
+
this.say("Upgrade failed; restored the previously installed package " + old.version);
|
|
9688
|
+
} catch {
|
|
9689
|
+
this.say("Upgrade failed and automatic recovery could not complete. Previous commit: " + previousCommit + ". Credentials, data and the installation record were preserved.");
|
|
9690
|
+
}
|
|
9691
|
+
}
|
|
9692
|
+
throw error;
|
|
9581
9693
|
}
|
|
9582
|
-
merged[baseIndex] = mergeRepoConfigValues(merged[baseIndex], localApp);
|
|
9583
|
-
}
|
|
9584
|
-
return merged;
|
|
9585
|
-
}
|
|
9586
|
-
function getAppName(value) {
|
|
9587
|
-
if (!isPlainObject(value) || typeof value.app !== "string") {
|
|
9588
|
-
return void 0;
|
|
9589
9694
|
}
|
|
9590
|
-
|
|
9591
|
-
|
|
9592
|
-
|
|
9593
|
-
function
|
|
9594
|
-
|
|
9695
|
+
};
|
|
9696
|
+
|
|
9697
|
+
// src/installCommandParser.ts
|
|
9698
|
+
function parseInstallArgs(args) {
|
|
9699
|
+
if (args.length !== 1 || !args[0] || args[0].startsWith("-")) throw Error("Usage: pmate install <package>");
|
|
9700
|
+
return { type: "install" /* Install */, package: args[0] };
|
|
9595
9701
|
}
|
|
9596
|
-
|
|
9597
|
-
|
|
9598
|
-
|
|
9599
|
-
|
|
9600
|
-
|
|
9601
|
-
|
|
9602
|
-
|
|
9603
|
-
|
|
9604
|
-
|
|
9605
|
-
|
|
9606
|
-
|
|
9607
|
-
|
|
9608
|
-
|
|
9609
|
-
|
|
9610
|
-
|
|
9611
|
-
}
|
|
9612
|
-
|
|
9702
|
+
|
|
9703
|
+
// src/managedSession.ts
|
|
9704
|
+
var import_node_fs4 = require("fs");
|
|
9705
|
+
var import_node_os4 = require("os");
|
|
9706
|
+
var import_node_crypto2 = require("crypto");
|
|
9707
|
+
var import_node_path4 = require("path");
|
|
9708
|
+
async function ensureManagedSession(env = process.env, fetcher = fetch) {
|
|
9709
|
+
if (env.PMATE_EXPECTED_ACCOUNT_ID && env.HEBO_MEMORY_TOKEN?.trim()) throw Error("Bound Agent CLI cannot use another delegated identity");
|
|
9710
|
+
if (env.HEBO_MEMORY_TOKEN?.trim()) {
|
|
9711
|
+
const token = env.HEBO_MEMORY_TOKEN.trim(), app = "pmate:factory";
|
|
9712
|
+
const base = (env.AUTH_API_BASE_URL ?? "https://auth-api-v2.pmate.chat").replace(/\/+$/, "");
|
|
9713
|
+
const headers = { authorization: `Bearer ${token}` };
|
|
9714
|
+
const r2 = await fetcher(`${base}/session?app=${encodeURIComponent(app)}`, { headers, signal: AbortSignal.timeout(15e3) });
|
|
9715
|
+
const body = await r2.json(), session = body.data;
|
|
9716
|
+
if (!r2.ok || !body.success || session?.identity?.app !== app || !session.identity.accountId || !session.issuedAt || !Number.isFinite(Date.parse(session.expiresAt)) || Date.parse(session.expiresAt) <= Date.now()) throw Error("Delegated user session is invalid or expired");
|
|
9717
|
+
const pr = await fetcher(`${base}/profiles?app=${encodeURIComponent(app)}&account=${encodeURIComponent(session.identity.accountId)}`, { headers, signal: AbortSignal.timeout(15e3) });
|
|
9718
|
+
const profiles = await pr.json(), profileId = profiles.data?.[0]?.id;
|
|
9719
|
+
if (!pr.ok || !profiles.success || !profileId) throw Error("Delegated user profile unavailable");
|
|
9720
|
+
const dir = (0, import_node_fs4.mkdtempSync)((0, import_node_path4.join)((0, import_node_os4.tmpdir)(), "pmate-delegated-")), file = (0, import_node_path4.join)(dir, "session.json");
|
|
9721
|
+
(0, import_node_fs4.writeFileSync)(file, JSON.stringify({ version: 4, activeAppRef: app, sessions: { [app]: { token, accountId: session.identity.accountId, profileId, issuedAt: session.issuedAt, expiresAt: session.expiresAt, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } } }), { mode: 384 });
|
|
9722
|
+
env.PMATE_SESSION_FILE = file;
|
|
9723
|
+
env.PCS_AGENT_SESSION_MANAGED = "0";
|
|
9724
|
+
process.once("exit", () => {
|
|
9725
|
+
(0, import_node_fs4.rmSync)(dir, { recursive: true, force: true });
|
|
9726
|
+
});
|
|
9727
|
+
return;
|
|
9613
9728
|
}
|
|
9614
|
-
if (
|
|
9615
|
-
|
|
9616
|
-
|
|
9617
|
-
|
|
9618
|
-
|
|
9619
|
-
|
|
9620
|
-
|
|
9621
|
-
|
|
9622
|
-
|
|
9623
|
-
|
|
9624
|
-
|
|
9625
|
-
|
|
9626
|
-
|
|
9627
|
-
|
|
9628
|
-
|
|
9629
|
-
|
|
9630
|
-
|
|
9631
|
-
|
|
9632
|
-
|
|
9633
|
-
|
|
9634
|
-
};
|
|
9729
|
+
if (env.PCS_AGENT_SESSION_MANAGED !== "1") return;
|
|
9730
|
+
if (!env.PMATE_SESSION_FILE) throw Error("Managed session file is required");
|
|
9731
|
+
const source = env.PCS_AGENT_SESSION_SOURCE_FILE ?? env.PMATE_SESSION_FILE;
|
|
9732
|
+
const state = JSON.parse((0, import_node_fs4.readFileSync)(source, "utf8"));
|
|
9733
|
+
if (!state.refreshKey) throw Error("Managed session refresh key is missing");
|
|
9734
|
+
const port = Number(env.PMATE_SESSION_REFRESH_PORT ?? env.PCS_AGENT_CONTROL_PORT ?? 39399);
|
|
9735
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw Error("Invalid controller port");
|
|
9736
|
+
const r = await fetcher(`http://127.0.0.1:${port}/${env.PMATE_SESSION_REFRESH_PORT ? "v1" : "v2"}/session/refresh`, {
|
|
9737
|
+
method: "POST",
|
|
9738
|
+
headers: { "x-agent-session-key": state.refreshKey },
|
|
9739
|
+
signal: AbortSignal.timeout(45e3)
|
|
9740
|
+
});
|
|
9741
|
+
if (!r.ok) throw Error(`Managed session renewal unavailable (${r.status}); retry later`);
|
|
9742
|
+
if (source !== env.PMATE_SESSION_FILE) {
|
|
9743
|
+
const raw = (0, import_node_fs4.readFileSync)(source, "utf8"), current = JSON.parse(raw).sessions?.[env.PMATE_SESSION_APP ?? "pmate:factory"];
|
|
9744
|
+
if (env.PMATE_EXPECTED_ACCOUNT_ID && current?.accountId !== env.PMATE_EXPECTED_ACCOUNT_ID) throw Error("Renewed Agent CLI Account mismatch");
|
|
9745
|
+
if (env.PMATE_EXPECTED_PROFILE_ID && current?.profileId !== env.PMATE_EXPECTED_PROFILE_ID) throw Error("Renewed Agent CLI Profile mismatch");
|
|
9746
|
+
const temp = `${env.PMATE_SESSION_FILE}.${(0, import_node_crypto2.randomUUID)()}.tmp`;
|
|
9747
|
+
(0, import_node_fs4.writeFileSync)(temp, raw, { mode: 384, flag: "wx" });
|
|
9748
|
+
(0, import_node_fs4.renameSync)(temp, env.PMATE_SESSION_FILE);
|
|
9635
9749
|
}
|
|
9636
|
-
return repoConfig;
|
|
9637
9750
|
}
|
|
9638
|
-
|
|
9639
|
-
|
|
9640
|
-
|
|
9641
|
-
|
|
9642
|
-
|
|
9643
|
-
|
|
9644
|
-
|
|
9645
|
-
|
|
9646
|
-
|
|
9647
|
-
throw new Error(
|
|
9648
|
-
`Invalid repo config in ${configPath}: linear account must be an object.`
|
|
9649
|
-
);
|
|
9751
|
+
|
|
9752
|
+
// src/index.ts
|
|
9753
|
+
var import_chalk4 = __toESM(require("chalk"));
|
|
9754
|
+
|
|
9755
|
+
// src/cli.ts
|
|
9756
|
+
var import_chalk = __toESM(require("chalk"));
|
|
9757
|
+
function formatValue(value) {
|
|
9758
|
+
if (typeof value === "string") {
|
|
9759
|
+
return value;
|
|
9650
9760
|
}
|
|
9651
|
-
|
|
9652
|
-
|
|
9653
|
-
|
|
9654
|
-
|
|
9655
|
-
`Invalid repo config in ${configPath}: linear account apiKey is required.`
|
|
9656
|
-
);
|
|
9761
|
+
try {
|
|
9762
|
+
return JSON.stringify(value, null, 2);
|
|
9763
|
+
} catch {
|
|
9764
|
+
return String(value);
|
|
9657
9765
|
}
|
|
9658
|
-
const title = getOptionalString(raw.title) || "Repo";
|
|
9659
|
-
const teamId = getOptionalString(raw.teamId);
|
|
9660
|
-
const teamKey = getOptionalString(raw.teamKey);
|
|
9661
|
-
return {
|
|
9662
|
-
title,
|
|
9663
|
-
apiKey,
|
|
9664
|
-
teamId,
|
|
9665
|
-
teamKey
|
|
9666
|
-
};
|
|
9667
9766
|
}
|
|
9668
|
-
function
|
|
9669
|
-
if (
|
|
9670
|
-
return
|
|
9767
|
+
function withData(base, data, dataStyle = import_chalk.default.dim) {
|
|
9768
|
+
if (data === void 0) {
|
|
9769
|
+
return base;
|
|
9671
9770
|
}
|
|
9672
|
-
|
|
9673
|
-
return trimmed ? trimmed : void 0;
|
|
9771
|
+
return `${base} ${dataStyle(formatValue(data))}`;
|
|
9674
9772
|
}
|
|
9773
|
+
var ui = {
|
|
9774
|
+
info(message, data) {
|
|
9775
|
+
console.log(withData(import_chalk.default.cyan(message), data));
|
|
9776
|
+
},
|
|
9777
|
+
success(message, data) {
|
|
9778
|
+
console.log(withData(import_chalk.default.green(message), data));
|
|
9779
|
+
},
|
|
9780
|
+
warn(message, data) {
|
|
9781
|
+
console.log(withData(import_chalk.default.yellow(message), data));
|
|
9782
|
+
},
|
|
9783
|
+
error(message, data) {
|
|
9784
|
+
console.error(withData(import_chalk.default.red(message), data));
|
|
9785
|
+
},
|
|
9786
|
+
plain(message) {
|
|
9787
|
+
console.log(message);
|
|
9788
|
+
},
|
|
9789
|
+
title(message) {
|
|
9790
|
+
console.log(import_chalk.default.bold(message));
|
|
9791
|
+
}
|
|
9792
|
+
};
|
|
9675
9793
|
|
|
9676
9794
|
// src/commands/ListCommandHandler.ts
|
|
9795
|
+
var import_node_path5 = __toESM(require("path"));
|
|
9677
9796
|
var ListCommandHandler = class {
|
|
9678
9797
|
handle(args) {
|
|
9679
9798
|
const targetDir = this.resolveTargetDir(args.targetPath);
|
|
@@ -17311,6 +17430,20 @@ async function loadChatSdk() {
|
|
|
17311
17430
|
return await dynamicImport("@pmate/chat-sdk/runtime");
|
|
17312
17431
|
}
|
|
17313
17432
|
|
|
17433
|
+
// src/receiptText.ts
|
|
17434
|
+
function receiptText(value, indent = "") {
|
|
17435
|
+
if (value === void 0 || value === null) return "\u2014";
|
|
17436
|
+
if (typeof value !== "object") return String(value);
|
|
17437
|
+
if (Array.isArray(value)) {
|
|
17438
|
+
return value.length ? value.map(
|
|
17439
|
+
(item) => indent + "\u2022 " + receiptText(item, indent + " ").trimStart()
|
|
17440
|
+
).join("\n") : "\uFF08\u65E0\u8BB0\u5F55\uFF09";
|
|
17441
|
+
}
|
|
17442
|
+
return Object.entries(value).map(
|
|
17443
|
+
([name, item]) => item !== null && typeof item === "object" ? indent + name + "\n" + receiptText(item, indent + " ") : indent + name + " " + receiptText(item)
|
|
17444
|
+
).join("\n") || "\uFF08\u65E0\u5B57\u6BB5\uFF09";
|
|
17445
|
+
}
|
|
17446
|
+
|
|
17314
17447
|
// src/commands/RegistryResourceCommandHandler.ts
|
|
17315
17448
|
var import_node_fs16 = require("fs");
|
|
17316
17449
|
var import_node_path21 = __toESM(require("path"));
|
|
@@ -17332,7 +17465,7 @@ var RegistryResourceCommandHandler = class {
|
|
|
17332
17465
|
const resourcePath = `/v1/${resource}/${encodeURIComponent(args.namespace)}${args.action === "create" ? "" : `/${encodeURIComponent(args.name)}`}`;
|
|
17333
17466
|
if (args.action === "get" || args.action === "view") {
|
|
17334
17467
|
const record2 = await request(baseUrl2, token, "GET", resourcePath);
|
|
17335
|
-
ui.plain(JSON.stringify(record2, null, 2));
|
|
17468
|
+
ui.plain(args.json ? JSON.stringify(record2, null, 2) : receiptText(record2));
|
|
17336
17469
|
return;
|
|
17337
17470
|
}
|
|
17338
17471
|
if (args.action === "create" || args.action === "update") {
|
|
@@ -17801,7 +17934,7 @@ var NsCommandHandler = class {
|
|
|
17801
17934
|
}
|
|
17802
17935
|
return;
|
|
17803
17936
|
}
|
|
17804
|
-
ui.plain(
|
|
17937
|
+
ui.plain(receiptText(value));
|
|
17805
17938
|
}
|
|
17806
17939
|
};
|
|
17807
17940
|
|
|
@@ -18012,7 +18145,7 @@ var StoreCommandHandler = class {
|
|
|
18012
18145
|
ui.warn("Not found.");
|
|
18013
18146
|
return;
|
|
18014
18147
|
}
|
|
18015
|
-
ui.plain(
|
|
18148
|
+
ui.plain(receiptText(value));
|
|
18016
18149
|
}
|
|
18017
18150
|
};
|
|
18018
18151
|
|
|
@@ -18724,6 +18857,15 @@ function readNamespaceFromPayload(payload) {
|
|
|
18724
18857
|
return namespace.trim().toLowerCase();
|
|
18725
18858
|
}
|
|
18726
18859
|
|
|
18860
|
+
// src/services/ManualInfraRouting.ts
|
|
18861
|
+
function usesManualInfraRouting(service) {
|
|
18862
|
+
const id = service.id ?? `${service.namespace}:${service.name}`;
|
|
18863
|
+
if (/^pmate:(?:blockchain|(?:[a-z0-9-]+-)?indexer|qablk01|qaidx)$/.test(id)) return true;
|
|
18864
|
+
const repository = service.repository ?? service.network?.repository;
|
|
18865
|
+
return repository === "parrot-mate/pmate-rust" && /(?:^|\/)infra\/scripts\/(?:blockchain|(?:[a-z0-9-]+-)?indexer)(?:\.[a-z0-9-]+)?$/.test(service.runner?.script ?? "");
|
|
18866
|
+
}
|
|
18867
|
+
var MANUAL_INFRA_ROUTING_MESSAGE = "Indexer/Blockchain ingress is managed manually. Edit the versioned Nginx configuration, run nginx -t, then reload Nginx separately.";
|
|
18868
|
+
|
|
18727
18869
|
// src/commands/ServiceCommandHandler.ts
|
|
18728
18870
|
var import_node_fs29 = require("fs");
|
|
18729
18871
|
var import_promises5 = require("fs/promises");
|
|
@@ -21226,6 +21368,10 @@ var ServiceRuntimeRunner = class {
|
|
|
21226
21368
|
return site;
|
|
21227
21369
|
}
|
|
21228
21370
|
async initServerApp(service, options, config) {
|
|
21371
|
+
if (usesManualInfraRouting(service)) {
|
|
21372
|
+
ui.info(MANUAL_INFRA_ROUTING_MESSAGE);
|
|
21373
|
+
return;
|
|
21374
|
+
}
|
|
21229
21375
|
const network = this.getNetwork(service);
|
|
21230
21376
|
const subdomain = "subdomain" in network ? network.subdomain?.trim() : void 0;
|
|
21231
21377
|
const port = network.port;
|
|
@@ -22463,6 +22609,9 @@ var ServiceCommandHandler = class {
|
|
|
22463
22609
|
return;
|
|
22464
22610
|
}
|
|
22465
22611
|
case "service-env-expose": {
|
|
22612
|
+
const [namespace, name] = args.appId.split(":");
|
|
22613
|
+
const service = await registry.getService(namespace, name);
|
|
22614
|
+
if (usesManualInfraRouting(service)) throw new Error(MANUAL_INFRA_ROUTING_MESSAGE);
|
|
22466
22615
|
const payload = await this.request(
|
|
22467
22616
|
baseUrl2,
|
|
22468
22617
|
"POST",
|
|
@@ -23009,6 +23158,10 @@ var ServiceCommandHandler = class {
|
|
|
23009
23158
|
}
|
|
23010
23159
|
}
|
|
23011
23160
|
async deleteServerResources(service) {
|
|
23161
|
+
if (usesManualInfraRouting(service)) {
|
|
23162
|
+
ui.info(MANUAL_INFRA_ROUTING_MESSAGE);
|
|
23163
|
+
return;
|
|
23164
|
+
}
|
|
23012
23165
|
const network = this.getNetworkConfig(service);
|
|
23013
23166
|
const subdomain = network.subdomain?.trim();
|
|
23014
23167
|
if (!subdomain) {
|
|
@@ -23246,8 +23399,8 @@ function sleep3(ms) {
|
|
|
23246
23399
|
|
|
23247
23400
|
// src/commands/VercelCommandHandler.ts
|
|
23248
23401
|
var import_node_child_process17 = require("child_process");
|
|
23249
|
-
var
|
|
23250
|
-
var
|
|
23402
|
+
var import_node_util3 = require("util");
|
|
23403
|
+
var execFileAsync2 = (0, import_node_util3.promisify)(import_node_child_process17.execFile);
|
|
23251
23404
|
function parseDeploymentTimestamp(deployment) {
|
|
23252
23405
|
const candidates = [deployment.createdAt, deployment.readyAt, deployment.ready];
|
|
23253
23406
|
for (const value of candidates) {
|
|
@@ -23275,7 +23428,7 @@ function filterDeploymentsFromLastWeek(deployments, now = Date.now()) {
|
|
|
23275
23428
|
});
|
|
23276
23429
|
}
|
|
23277
23430
|
var VercelCommandHandler = class {
|
|
23278
|
-
constructor(runCommand2 = async (command, args) =>
|
|
23431
|
+
constructor(runCommand2 = async (command, args) => execFileAsync2(command, args, { maxBuffer: 1024 * 1024 * 20 }), getConfig = loadPmateConfig) {
|
|
23279
23432
|
this.runCommand = runCommand2;
|
|
23280
23433
|
this.getConfig = getConfig;
|
|
23281
23434
|
}
|
|
@@ -23387,7 +23540,7 @@ var RoleCommandHandler = class {
|
|
|
23387
23540
|
else if (Array.isArray(result)) {
|
|
23388
23541
|
if (result.length) console.table(result);
|
|
23389
23542
|
else ui.warn("No authorization records found.");
|
|
23390
|
-
} else ui.plain(
|
|
23543
|
+
} else ui.plain(receiptText(result));
|
|
23391
23544
|
}
|
|
23392
23545
|
};
|
|
23393
23546
|
|
|
@@ -27680,7 +27833,9 @@ var HELP_TEXT = [
|
|
|
27680
27833
|
` ${import_chalk4.default.green("-v, --version")} Print the current version`,
|
|
27681
27834
|
` ${import_chalk4.default.green("-n, --name <n>")} Print a greeting for <n>`,
|
|
27682
27835
|
"",
|
|
27836
|
+
"\u6309\u547D\u4EE4\u67E5\u770B\u53C2\u6570\u548C\u540E\u7EED\u64CD\u4F5C\uFF1Apmate <\u547D\u4EE4> --help",
|
|
27683
27837
|
import_chalk4.default.bold("Commands:"),
|
|
27838
|
+
" session status|read|logout --app <\u5E94\u7528>",
|
|
27684
27839
|
` ${import_chalk4.default.cyan("list [path]")} List apps from .pmate.yaml in the target folder`,
|
|
27685
27840
|
` ${import_chalk4.default.cyan("login")} Log in and store a session in ~/.pmate/session.json`,
|
|
27686
27841
|
` ${import_chalk4.default.gray(" [--app <app-id>] [--profile-id <id>] [--select-profile] [--token-stdin]")}`,
|
|
@@ -27835,7 +27990,7 @@ var HELP_TEXT = [
|
|
|
27835
27990
|
` ${import_chalk4.default.gray(" icon add <ns:objectId> <name> (--size <w>x<h>|--<w>x<h>) [--format <png|webp|svg|ico>] [--output <path>] [--force] [--base-url <url>] [--json]")}`,
|
|
27836
27991
|
` ${import_chalk4.default.gray(" delete <ns:objectId> [--base-url <url>] [--json]")}`,
|
|
27837
27992
|
` ${import_chalk4.default.gray(" default base URL: https://apps-api.pmate.chat")}`,
|
|
27838
|
-
` ${import_chalk4.default.cyan("install")} Install or update a managed source package: pmate install <package
|
|
27993
|
+
` ${import_chalk4.default.cyan("install")} Install or update a managed source package: pmate install <package>`,
|
|
27839
27994
|
` ${import_chalk4.default.cyan("service")} Manage service registry entries via apps-api`,
|
|
27840
27995
|
" deploy <ns:service> [--env prod] [--cwd <repo>] [--dry-run] [--local] [--unsigned] [--platform mac|win] [--arch arm64|x64] [--config <file>] [--out-dir <dir>] [--scheme <name>] [--team <id>] [--variant release|debug] [--bump patch|minor|major]",
|
|
27841
27996
|
` ${import_chalk4.default.gray(" list [--namespace <ns>] [--repo <owner/name|https://github.com/owner/name>] [--server <name>] [--base-url <url>] [--json]")}`,
|
|
@@ -29592,18 +29747,17 @@ function parseNonNegativeInt(value, optionName) {
|
|
|
29592
29747
|
}
|
|
29593
29748
|
return parsed;
|
|
29594
29749
|
}
|
|
29595
|
-
async function
|
|
29750
|
+
async function dispatch() {
|
|
29596
29751
|
try {
|
|
29597
|
-
|
|
29598
|
-
process.stdout.write(JSON.stringify(sessionCommand(process.argv.slice(3))) + "\n");
|
|
29599
|
-
return;
|
|
29600
|
-
}
|
|
29601
|
-
const help = serviceHelp(process.argv.slice(2));
|
|
29752
|
+
const help = commandGuide(process.argv.slice(2), HELP_TEXT);
|
|
29602
29753
|
if (help) {
|
|
29603
29754
|
ui.plain(help);
|
|
29604
29755
|
return;
|
|
29605
29756
|
}
|
|
29606
|
-
|
|
29757
|
+
if (process.argv[2] === "session") {
|
|
29758
|
+
process.stdout.write(JSON.stringify(sessionCommand(process.argv.slice(3))) + "\n");
|
|
29759
|
+
return;
|
|
29760
|
+
}
|
|
29607
29761
|
const args = parseArgs(process.argv.slice(2));
|
|
29608
29762
|
if (args.help) {
|
|
29609
29763
|
showHelp();
|
|
@@ -29617,10 +29771,11 @@ async function main() {
|
|
|
29617
29771
|
greet(args.name);
|
|
29618
29772
|
return;
|
|
29619
29773
|
}
|
|
29774
|
+
await ensureManagedSession();
|
|
29620
29775
|
const commandArgs = parseCommandArgs(args);
|
|
29621
29776
|
switch (commandArgs.type) {
|
|
29622
29777
|
case "install" /* Install */: {
|
|
29623
|
-
await new PackageInstaller({ progress: (message) => ui.plain(message) }).install(commandArgs.package
|
|
29778
|
+
await new PackageInstaller({ progress: (message) => ui.plain(message) }).install(commandArgs.package);
|
|
29624
29779
|
return;
|
|
29625
29780
|
}
|
|
29626
29781
|
case "list" /* List */: {
|
|
@@ -29790,9 +29945,18 @@ async function main() {
|
|
|
29790
29945
|
} catch (error) {
|
|
29791
29946
|
const message = error instanceof Error ? error.message : String(error);
|
|
29792
29947
|
ui.error(`Error: ${message}`);
|
|
29948
|
+
const group = groupName(process.argv.slice(2));
|
|
29949
|
+
if (group && !process.argv.includes("--json")) console.error(`\u67E5\u770B\u53C2\u6570\u548C\u9002\u7528\u8303\u56F4\uFF1Apmate ${group} --help`);
|
|
29793
29950
|
process.exitCode = 1;
|
|
29794
29951
|
}
|
|
29795
29952
|
}
|
|
29953
|
+
async function main() {
|
|
29954
|
+
await dispatch();
|
|
29955
|
+
if (!process.exitCode) {
|
|
29956
|
+
const hint = nextRead(process.argv.slice(2));
|
|
29957
|
+
if (hint) console.error(hint);
|
|
29958
|
+
}
|
|
29959
|
+
}
|
|
29796
29960
|
if (require.main === module) {
|
|
29797
29961
|
void main();
|
|
29798
29962
|
}
|