@terreno/admin-backend 0.22.2 → 0.23.0
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/adminApp.d.ts +9 -1
- package/dist/adminApp.js +73 -15
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/scriptCli.d.ts +42 -0
- package/dist/scriptCli.js +364 -0
- package/package.json +3 -3
- package/src/adminApp.test.ts +119 -0
- package/src/adminApp.ts +58 -1
- package/src/index.ts +2 -0
- package/src/scriptCli.test.ts +246 -0
- package/src/scriptCli.ts +273 -0
package/dist/adminApp.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ScriptRunner, type User } from "@terreno/api";
|
|
1
|
+
import { type PopulatePath, type ScriptArgDef, type ScriptRunner, type User } from "@terreno/api";
|
|
2
2
|
import express from "express";
|
|
3
3
|
import type { Model } from "mongoose";
|
|
4
4
|
import { type AdminActionInput, type AdminFieldsetInput, type AdminHomeInput, type AdminListFilter, type AdminModelPermissionsInput } from "./adminUiV2";
|
|
@@ -48,6 +48,8 @@ export interface AdminModelConfig {
|
|
|
48
48
|
permissions?: AdminModelPermissionsInput;
|
|
49
49
|
/** Suggested page size for the changelist */
|
|
50
50
|
pageSize?: number;
|
|
51
|
+
/** Mongoose populate paths for list/read responses (e.g. populated refs on consent responses). */
|
|
52
|
+
populatePaths?: PopulatePath[];
|
|
51
53
|
/** UI-only hint that live updates may be available */
|
|
52
54
|
realtime?: boolean;
|
|
53
55
|
/**
|
|
@@ -74,6 +76,12 @@ export interface AdminScriptConfig {
|
|
|
74
76
|
name: string;
|
|
75
77
|
/** Human-readable description shown in the admin UI */
|
|
76
78
|
description: string;
|
|
79
|
+
/**
|
|
80
|
+
* Optional declarations for the arguments this script accepts. Drives CLI help,
|
|
81
|
+
* type coercion, defaults, and validation. Scripts may still read undeclared
|
|
82
|
+
* arguments via `ctx.args`.
|
|
83
|
+
*/
|
|
84
|
+
args?: ScriptArgDef[];
|
|
77
85
|
/** The function that executes the script. Must return string[] results. */
|
|
78
86
|
runner: ScriptRunner;
|
|
79
87
|
}
|
package/dist/adminApp.js
CHANGED
|
@@ -92,6 +92,7 @@ var express_1 = __importDefault(require("express"));
|
|
|
92
92
|
var luxon_1 = require("luxon");
|
|
93
93
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
94
94
|
var adminUiV2_1 = require("./adminUiV2");
|
|
95
|
+
var scriptCli_1 = require("./scriptCli");
|
|
95
96
|
var toPlainObject = function (value) {
|
|
96
97
|
if (value &&
|
|
97
98
|
typeof value === "object" &&
|
|
@@ -445,10 +446,14 @@ var AdminApp = /** @class */ (function () {
|
|
|
445
446
|
});
|
|
446
447
|
// Build script metadata for config response
|
|
447
448
|
var scriptConfigs = (_d = this.options.scripts) !== null && _d !== void 0 ? _d : [];
|
|
448
|
-
var configScripts = scriptConfigs.map(function (script) {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
449
|
+
var configScripts = scriptConfigs.map(function (script) {
|
|
450
|
+
var _a;
|
|
451
|
+
return ({
|
|
452
|
+
args: (_a = script.args) !== null && _a !== void 0 ? _a : [],
|
|
453
|
+
description: script.description,
|
|
454
|
+
name: script.name,
|
|
455
|
+
});
|
|
456
|
+
});
|
|
452
457
|
var defaultScreens = [{ displayName: "Version Config", name: "version-config" }];
|
|
453
458
|
var mergedScreens = __spreadArray(__spreadArray([], __read(defaultScreens), false), __read(((_e = this.options.customScreens) !== null && _e !== void 0 ? _e : [])), false);
|
|
454
459
|
var configResponse = {
|
|
@@ -480,6 +485,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
480
485
|
scripts: {
|
|
481
486
|
items: {
|
|
482
487
|
properties: {
|
|
488
|
+
args: { type: "array" },
|
|
483
489
|
description: { type: "string" },
|
|
484
490
|
name: { type: "string" },
|
|
485
491
|
},
|
|
@@ -934,7 +940,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
934
940
|
}
|
|
935
941
|
: {};
|
|
936
942
|
// biome-ignore lint/suspicious/noExplicitAny: matches the Model<any> from AdminModelConfig above.
|
|
937
|
-
var routerOptions = __assign(__assign(__assign(__assign(__assign({}, (openApiMw ? { openApi: openApiMw } : {})), { defaultLimit: (_j = config.pageSize) !== null && _j !== void 0 ? _j : 100, maxLimit: 500, permissions: {
|
|
943
|
+
var routerOptions = __assign(__assign(__assign(__assign(__assign(__assign({}, (openApiMw ? { openApi: openApiMw } : {})), { defaultLimit: (_j = config.pageSize) !== null && _j !== void 0 ? _j : 100, maxLimit: 500, permissions: {
|
|
938
944
|
create: adminPermission((_k = config.permissions) === null || _k === void 0 ? void 0 : _k.create),
|
|
939
945
|
delete: adminPermission((_l = config.permissions) === null || _l === void 0 ? void 0 : _l.delete),
|
|
940
946
|
list: [api_1.Permissions.IsAdmin],
|
|
@@ -963,7 +969,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
963
969
|
? function (value, _method, _request, _options) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
964
970
|
return [2 /*return*/, removeHiddenFields(value, hiddenFieldSet)];
|
|
965
971
|
}); }); }
|
|
966
|
-
: undefined, sort: (_o = config.defaultSort) !== null && _o !== void 0 ? _o : "-created" }), auditHooks);
|
|
972
|
+
: undefined, sort: (_o = config.defaultSort) !== null && _o !== void 0 ? _o : "-created" }), (config.populatePaths ? { populatePaths: config.populatePaths } : {})), auditHooks);
|
|
967
973
|
var modelBase = express_1.default.Router();
|
|
968
974
|
modelBase.post.apply(modelBase, __spreadArray(__spreadArray(["/bulk-patch",
|
|
969
975
|
(0, api_1.authenticateMiddleware)()], __read(asMiddlewareList(bulkPatchOpenApi)), false), [(0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -1142,11 +1148,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
1142
1148
|
}); }));
|
|
1143
1149
|
// POST /admin/scripts/:name/run — Execute a script
|
|
1144
1150
|
router.post("/:name/run", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
1145
|
-
var user, script, isWetRun, now, task, err_5, detail, ctx;
|
|
1151
|
+
var user, script, isWetRun, argValues, _a, _b, _c, key, value, rawBody, bodyValues, _d, _e, _f, key, value, _g, args, argErrors, now, task, err_5, detail, ctx;
|
|
1152
|
+
var e_15, _h, e_16, _j;
|
|
1146
1153
|
var _this = this;
|
|
1147
|
-
var
|
|
1148
|
-
return __generator(this, function (
|
|
1149
|
-
switch (
|
|
1154
|
+
var _k, _l;
|
|
1155
|
+
return __generator(this, function (_m) {
|
|
1156
|
+
switch (_m.label) {
|
|
1150
1157
|
case 0:
|
|
1151
1158
|
user = req.user;
|
|
1152
1159
|
if (!(user === null || user === void 0 ? void 0 : user.admin)) {
|
|
@@ -1157,15 +1164,65 @@ var AdminApp = /** @class */ (function () {
|
|
|
1157
1164
|
throw new api_1.APIError({ status: 404, title: "Script not found: ".concat(req.params.name) });
|
|
1158
1165
|
}
|
|
1159
1166
|
isWetRun = req.query.wetRun === "true";
|
|
1167
|
+
argValues = {};
|
|
1168
|
+
try {
|
|
1169
|
+
for (_a = __values(Object.entries(req.query)), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1170
|
+
_c = __read(_b.value, 2), key = _c[0], value = _c[1];
|
|
1171
|
+
if (scriptCli_1.RESERVED_SCRIPT_FLAGS.includes(key) || value === undefined) {
|
|
1172
|
+
continue;
|
|
1173
|
+
}
|
|
1174
|
+
argValues[key] = value;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
1178
|
+
finally {
|
|
1179
|
+
try {
|
|
1180
|
+
if (_b && !_b.done && (_h = _a.return)) _h.call(_a);
|
|
1181
|
+
}
|
|
1182
|
+
finally { if (e_15) throw e_15.error; }
|
|
1183
|
+
}
|
|
1184
|
+
rawBody = req.body && typeof req.body === "object" && !Array.isArray(req.body)
|
|
1185
|
+
? req.body
|
|
1186
|
+
: {};
|
|
1187
|
+
bodyValues = rawBody.args && typeof rawBody.args === "object" && !Array.isArray(rawBody.args)
|
|
1188
|
+
? rawBody.args
|
|
1189
|
+
: rawBody;
|
|
1190
|
+
try {
|
|
1191
|
+
for (_d = __values(Object.entries(bodyValues)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
1192
|
+
_f = __read(_e.value, 2), key = _f[0], value = _f[1];
|
|
1193
|
+
if (scriptCli_1.RESERVED_SCRIPT_FLAGS.includes(key)) {
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
argValues[key] = value;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
catch (e_16_1) { e_16 = { error: e_16_1 }; }
|
|
1200
|
+
finally {
|
|
1201
|
+
try {
|
|
1202
|
+
if (_e && !_e.done && (_j = _d.return)) _j.call(_d);
|
|
1203
|
+
}
|
|
1204
|
+
finally { if (e_16) throw e_16.error; }
|
|
1205
|
+
}
|
|
1206
|
+
_g = (0, api_1.createScriptArgs)({
|
|
1207
|
+
defs: (_k = script.args) !== null && _k !== void 0 ? _k : [],
|
|
1208
|
+
values: argValues,
|
|
1209
|
+
}), args = _g.args, argErrors = _g.errors;
|
|
1210
|
+
if (argErrors.length > 0) {
|
|
1211
|
+
throw new api_1.APIError({
|
|
1212
|
+
detail: argErrors.join("; "),
|
|
1213
|
+
status: 400,
|
|
1214
|
+
title: "Invalid arguments for script: ".concat(script.name),
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1160
1217
|
now = luxon_1.DateTime.now().toJSDate();
|
|
1161
|
-
|
|
1218
|
+
_m.label = 1;
|
|
1162
1219
|
case 1:
|
|
1163
|
-
|
|
1220
|
+
_m.trys.push([1, 3, , 4]);
|
|
1164
1221
|
return [4 /*yield*/, api_1.BackgroundTask.create({
|
|
1165
1222
|
createdBy: user._id,
|
|
1166
1223
|
isDryRun: !isWetRun,
|
|
1167
1224
|
logs: [
|
|
1168
|
-
{ level: "info", message: "Script started by ".concat((
|
|
1225
|
+
{ level: "info", message: "Script started by ".concat((_l = user.name) !== null && _l !== void 0 ? _l : "admin"), timestamp: now },
|
|
1169
1226
|
],
|
|
1170
1227
|
progress: { message: "Starting...", percentage: 0, stage: "Queued" },
|
|
1171
1228
|
startedAt: now,
|
|
@@ -1173,10 +1230,10 @@ var AdminApp = /** @class */ (function () {
|
|
|
1173
1230
|
taskType: script.name,
|
|
1174
1231
|
})];
|
|
1175
1232
|
case 2:
|
|
1176
|
-
task = (
|
|
1233
|
+
task = (_m.sent());
|
|
1177
1234
|
return [3 /*break*/, 4];
|
|
1178
1235
|
case 3:
|
|
1179
|
-
err_5 =
|
|
1236
|
+
err_5 = _m.sent();
|
|
1180
1237
|
detail = err_5 instanceof Error ? err_5.message : String(err_5);
|
|
1181
1238
|
api_1.logger.error("Failed to create background task for ".concat(script.name, ": ").concat(detail));
|
|
1182
1239
|
throw new api_1.APIError({
|
|
@@ -1202,6 +1259,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
1202
1259
|
}
|
|
1203
1260
|
});
|
|
1204
1261
|
}); },
|
|
1262
|
+
args: args,
|
|
1205
1263
|
checkCancellation: function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1206
1264
|
return __generator(this, function (_a) {
|
|
1207
1265
|
switch (_a.label) {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,3 +3,5 @@ export { AdminApp } from "./adminApp";
|
|
|
3
3
|
export * from "./adminUiV2";
|
|
4
4
|
export type { DocumentFile, DocumentListResponse, DocumentStorageOptions, } from "./documentStorageApp";
|
|
5
5
|
export { DocumentStorageApp } from "./documentStorageApp";
|
|
6
|
+
export type { RunScriptCliOptions, RunScriptCliResult } from "./scriptCli";
|
|
7
|
+
export { runScriptCli } from "./scriptCli";
|
package/dist/index.js
CHANGED
|
@@ -14,9 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.DocumentStorageApp = exports.AdminApp = void 0;
|
|
17
|
+
exports.runScriptCli = exports.DocumentStorageApp = exports.AdminApp = void 0;
|
|
18
18
|
var adminApp_1 = require("./adminApp");
|
|
19
19
|
Object.defineProperty(exports, "AdminApp", { enumerable: true, get: function () { return adminApp_1.AdminApp; } });
|
|
20
20
|
__exportStar(require("./adminUiV2"), exports);
|
|
21
21
|
var documentStorageApp_1 = require("./documentStorageApp");
|
|
22
22
|
Object.defineProperty(exports, "DocumentStorageApp", { enumerable: true, get: function () { return documentStorageApp_1.DocumentStorageApp; } });
|
|
23
|
+
var scriptCli_1 = require("./scriptCli");
|
|
24
|
+
Object.defineProperty(exports, "runScriptCli", { enumerable: true, get: function () { return scriptCli_1.runScriptCli; } });
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AdminScriptConfig } from "./adminApp";
|
|
2
|
+
/**
|
|
3
|
+
* Flag names reserved by the script runner itself. They control how a script is
|
|
4
|
+
* invoked (CLI flags and the HTTP `wetRun` query) and are stripped from the
|
|
5
|
+
* arguments handed to the script runner, so script authors should avoid declaring
|
|
6
|
+
* args with these names. Exported so the admin HTTP route can strip the same set
|
|
7
|
+
* and keep `ctx.args` identical across CLI and HTTP invocations.
|
|
8
|
+
*/
|
|
9
|
+
export declare const RESERVED_SCRIPT_FLAGS: string[];
|
|
10
|
+
export interface RunScriptCliOptions {
|
|
11
|
+
/** The same script configs registered on {@link AdminApp}. */
|
|
12
|
+
scripts: AdminScriptConfig[];
|
|
13
|
+
/** Tokens to parse. Defaults to `process.argv.slice(2)`. */
|
|
14
|
+
argv?: string[];
|
|
15
|
+
/** Line writer for human-readable output. Defaults to stdout. */
|
|
16
|
+
write?: (line: string) => void;
|
|
17
|
+
/**
|
|
18
|
+
* When true (default), the process exits with the resulting exit code after the
|
|
19
|
+
* run. Pass false in tests or when embedding the runner in a larger CLI.
|
|
20
|
+
*/
|
|
21
|
+
exit?: boolean;
|
|
22
|
+
/** Program name shown in help text. Defaults to "script". */
|
|
23
|
+
programName?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface RunScriptCliResult {
|
|
26
|
+
exitCode: number;
|
|
27
|
+
script?: string;
|
|
28
|
+
wetRun: boolean;
|
|
29
|
+
success: boolean;
|
|
30
|
+
results: string[];
|
|
31
|
+
errors: string[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Runs a registered admin script from the command line. Exposes every script
|
|
35
|
+
* configured on {@link AdminApp} as a CLI so they can be invoked directly from the
|
|
36
|
+
* project (e.g. by an AI assistant) using the same definitions and argument
|
|
37
|
+
* handling as the admin HTTP routes.
|
|
38
|
+
*
|
|
39
|
+
* The caller is responsible for any environment setup the scripts need (typically
|
|
40
|
+
* connecting to MongoDB) before calling this, and for tearing it down afterwards.
|
|
41
|
+
*/
|
|
42
|
+
export declare const runScriptCli: (options: RunScriptCliOptions) => Promise<RunScriptCliResult>;
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
+
if (!m) return o;
|
|
52
|
+
var i = m.call(o), r, ar = [], e;
|
|
53
|
+
try {
|
|
54
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
+
}
|
|
56
|
+
catch (error) { e = { error: error }; }
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
+
}
|
|
61
|
+
finally { if (e) throw e.error; }
|
|
62
|
+
}
|
|
63
|
+
return ar;
|
|
64
|
+
};
|
|
65
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
+
if (ar || !(i in from)) {
|
|
68
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
+
ar[i] = from[i];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
+
};
|
|
74
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
|
+
exports.runScriptCli = exports.RESERVED_SCRIPT_FLAGS = void 0;
|
|
76
|
+
var api_1 = require("@terreno/api");
|
|
77
|
+
/**
|
|
78
|
+
* Flag names reserved by the script runner itself. They control how a script is
|
|
79
|
+
* invoked (CLI flags and the HTTP `wetRun` query) and are stripped from the
|
|
80
|
+
* arguments handed to the script runner, so script authors should avoid declaring
|
|
81
|
+
* args with these names. Exported so the admin HTTP route can strip the same set
|
|
82
|
+
* and keep `ctx.args` identical across CLI and HTTP invocations.
|
|
83
|
+
*/
|
|
84
|
+
exports.RESERVED_SCRIPT_FLAGS = ["help", "h", "list", "json", "wet", "wetRun", "dry"];
|
|
85
|
+
var formatArgUsage = function (arg) {
|
|
86
|
+
var _a;
|
|
87
|
+
var type = (_a = arg.type) !== null && _a !== void 0 ? _a : "string";
|
|
88
|
+
var base = type === "boolean" ? "--".concat(arg.name) : "--".concat(arg.name, " <").concat(type, ">");
|
|
89
|
+
return arg.required ? base : "[".concat(base, "]");
|
|
90
|
+
};
|
|
91
|
+
var describeArg = function (arg) {
|
|
92
|
+
var _a;
|
|
93
|
+
var parts = [" --".concat(arg.name)];
|
|
94
|
+
if (arg.aliases && arg.aliases.length > 0) {
|
|
95
|
+
parts.push("(-".concat(arg.aliases.join(", -"), ")"));
|
|
96
|
+
}
|
|
97
|
+
parts.push("[".concat((_a = arg.type) !== null && _a !== void 0 ? _a : "string").concat(arg.required ? ", required" : "", "]"));
|
|
98
|
+
parts.push("- ".concat(arg.description));
|
|
99
|
+
if (arg.default !== undefined) {
|
|
100
|
+
parts.push("(default: ".concat(String(arg.default), ")"));
|
|
101
|
+
}
|
|
102
|
+
if (arg.example !== undefined) {
|
|
103
|
+
parts.push("(e.g. ".concat(arg.example, ")"));
|
|
104
|
+
}
|
|
105
|
+
return parts.join(" ");
|
|
106
|
+
};
|
|
107
|
+
var printGeneralHelp = function (scripts, programName, write) {
|
|
108
|
+
var e_1, _a;
|
|
109
|
+
write("Usage: ".concat(programName, " <script> [--wet] [args...]"));
|
|
110
|
+
write("");
|
|
111
|
+
write("Runs a registered admin script. Defaults to a dry run; pass --wet to apply changes.");
|
|
112
|
+
write("");
|
|
113
|
+
write("Options:");
|
|
114
|
+
write(" --wet, --wetRun Run in wet mode (applies changes). Default is a dry run.");
|
|
115
|
+
write(" --dry Force a dry run (overrides --wet).");
|
|
116
|
+
write(" --json Emit the result as a single JSON line.");
|
|
117
|
+
write(" --list List available scripts and exit.");
|
|
118
|
+
write(" --help, -h Show help. Combine with a script name for per-script help.");
|
|
119
|
+
write("");
|
|
120
|
+
if (scripts.length === 0) {
|
|
121
|
+
write("No scripts are registered.");
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
write("Available scripts:");
|
|
125
|
+
try {
|
|
126
|
+
for (var scripts_1 = __values(scripts), scripts_1_1 = scripts_1.next(); !scripts_1_1.done; scripts_1_1 = scripts_1.next()) {
|
|
127
|
+
var script = scripts_1_1.value;
|
|
128
|
+
write(" ".concat(script.name, " - ").concat(script.description));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
132
|
+
finally {
|
|
133
|
+
try {
|
|
134
|
+
if (scripts_1_1 && !scripts_1_1.done && (_a = scripts_1.return)) _a.call(scripts_1);
|
|
135
|
+
}
|
|
136
|
+
finally { if (e_1) throw e_1.error; }
|
|
137
|
+
}
|
|
138
|
+
write("");
|
|
139
|
+
write("Run \"".concat(programName, " <script> --help\" for a script's arguments."));
|
|
140
|
+
};
|
|
141
|
+
var printScriptHelp = function (script, programName, write) {
|
|
142
|
+
var e_2, _a;
|
|
143
|
+
var _b;
|
|
144
|
+
var args = (_b = script.args) !== null && _b !== void 0 ? _b : [];
|
|
145
|
+
var usageArgs = args.map(formatArgUsage).join(" ");
|
|
146
|
+
write("Usage: ".concat(programName, " ").concat(script.name, " [--wet] ").concat(usageArgs).trimEnd());
|
|
147
|
+
write("");
|
|
148
|
+
write(script.description);
|
|
149
|
+
if (args.length > 0) {
|
|
150
|
+
write("");
|
|
151
|
+
write("Arguments:");
|
|
152
|
+
try {
|
|
153
|
+
for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
|
|
154
|
+
var arg = args_1_1.value;
|
|
155
|
+
write(describeArg(arg));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
159
|
+
finally {
|
|
160
|
+
try {
|
|
161
|
+
if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
|
|
162
|
+
}
|
|
163
|
+
finally { if (e_2) throw e_2.error; }
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Runs a registered admin script from the command line. Exposes every script
|
|
169
|
+
* configured on {@link AdminApp} as a CLI so they can be invoked directly from the
|
|
170
|
+
* project (e.g. by an AI assistant) using the same definitions and argument
|
|
171
|
+
* handling as the admin HTTP routes.
|
|
172
|
+
*
|
|
173
|
+
* The caller is responsible for any environment setup the scripts need (typically
|
|
174
|
+
* connecting to MongoDB) before calling this, and for tearing it down afterwards.
|
|
175
|
+
*/
|
|
176
|
+
var runScriptCli = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
177
|
+
var scripts, argv, write, shouldExit, programName, finish, wantsHelp, wantsList, wantsJson, scriptName, script, available, message, nameIndex, argTokens, _a, args, errors, dryRequested, wetRequested, wetRun, RESERVED_SCRIPT_FLAGS_1, RESERVED_SCRIPT_FLAGS_1_1, reserved, errors_1, errors_1_1, error, ctx, result, _b, _c, line, err_1, message;
|
|
178
|
+
var e_3, _d, e_4, _e, e_5, _f;
|
|
179
|
+
var _g, _h, _j, _k;
|
|
180
|
+
return __generator(this, function (_l) {
|
|
181
|
+
switch (_l.label) {
|
|
182
|
+
case 0:
|
|
183
|
+
scripts = options.scripts;
|
|
184
|
+
argv = (_g = options.argv) !== null && _g !== void 0 ? _g : process.argv.slice(2);
|
|
185
|
+
write = (_h = options.write) !== null && _h !== void 0 ? _h : (function (line) { return process.stdout.write("".concat(line, "\n")); });
|
|
186
|
+
shouldExit = options.exit !== false;
|
|
187
|
+
programName = (_j = options.programName) !== null && _j !== void 0 ? _j : "script";
|
|
188
|
+
finish = function (result) {
|
|
189
|
+
if (shouldExit) {
|
|
190
|
+
process.exit(result.exitCode);
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
};
|
|
194
|
+
wantsHelp = argv.includes("--help") || argv.includes("-h");
|
|
195
|
+
wantsList = argv.includes("--list");
|
|
196
|
+
wantsJson = argv.includes("--json");
|
|
197
|
+
scriptName = argv.find(function (token) { return !token.startsWith("-"); });
|
|
198
|
+
if (wantsList || (!scriptName && !wantsHelp)) {
|
|
199
|
+
printGeneralHelp(scripts, programName, write);
|
|
200
|
+
return [2 /*return*/, finish({ errors: [], exitCode: 0, results: [], success: true, wetRun: false })];
|
|
201
|
+
}
|
|
202
|
+
if (!scriptName) {
|
|
203
|
+
printGeneralHelp(scripts, programName, write);
|
|
204
|
+
return [2 /*return*/, finish({ errors: [], exitCode: 0, results: [], success: true, wetRun: false })];
|
|
205
|
+
}
|
|
206
|
+
script = scripts.find(function (s) { return s.name === scriptName; });
|
|
207
|
+
if (!script) {
|
|
208
|
+
available = scripts.map(function (s) { return s.name; }).join(", ") || "(none)";
|
|
209
|
+
message = "Unknown script: ".concat(scriptName, ". Available scripts: ").concat(available);
|
|
210
|
+
write(message);
|
|
211
|
+
return [2 /*return*/, finish({
|
|
212
|
+
errors: [message],
|
|
213
|
+
exitCode: 1,
|
|
214
|
+
results: [],
|
|
215
|
+
script: scriptName,
|
|
216
|
+
success: false,
|
|
217
|
+
wetRun: false,
|
|
218
|
+
})];
|
|
219
|
+
}
|
|
220
|
+
if (wantsHelp) {
|
|
221
|
+
printScriptHelp(script, programName, write);
|
|
222
|
+
return [2 /*return*/, finish({
|
|
223
|
+
errors: [],
|
|
224
|
+
exitCode: 0,
|
|
225
|
+
results: [],
|
|
226
|
+
script: script.name,
|
|
227
|
+
success: true,
|
|
228
|
+
wetRun: false,
|
|
229
|
+
})];
|
|
230
|
+
}
|
|
231
|
+
nameIndex = argv.indexOf(scriptName);
|
|
232
|
+
argTokens = __spreadArray(__spreadArray([], __read(argv.slice(0, nameIndex)), false), __read(argv.slice(nameIndex + 1)), false);
|
|
233
|
+
_a = (0, api_1.parseScriptArgs)(argTokens, (_k = script.args) !== null && _k !== void 0 ? _k : []), args = _a.args, errors = _a.errors;
|
|
234
|
+
dryRequested = args.getBoolean("dry");
|
|
235
|
+
wetRequested = args.getBoolean("wet") || args.getBoolean("wetRun");
|
|
236
|
+
wetRun = wetRequested && !dryRequested;
|
|
237
|
+
try {
|
|
238
|
+
for (RESERVED_SCRIPT_FLAGS_1 = __values(exports.RESERVED_SCRIPT_FLAGS), RESERVED_SCRIPT_FLAGS_1_1 = RESERVED_SCRIPT_FLAGS_1.next(); !RESERVED_SCRIPT_FLAGS_1_1.done; RESERVED_SCRIPT_FLAGS_1_1 = RESERVED_SCRIPT_FLAGS_1.next()) {
|
|
239
|
+
reserved = RESERVED_SCRIPT_FLAGS_1_1.value;
|
|
240
|
+
delete args.raw[reserved];
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
244
|
+
finally {
|
|
245
|
+
try {
|
|
246
|
+
if (RESERVED_SCRIPT_FLAGS_1_1 && !RESERVED_SCRIPT_FLAGS_1_1.done && (_d = RESERVED_SCRIPT_FLAGS_1.return)) _d.call(RESERVED_SCRIPT_FLAGS_1);
|
|
247
|
+
}
|
|
248
|
+
finally { if (e_3) throw e_3.error; }
|
|
249
|
+
}
|
|
250
|
+
if (errors.length > 0) {
|
|
251
|
+
try {
|
|
252
|
+
for (errors_1 = __values(errors), errors_1_1 = errors_1.next(); !errors_1_1.done; errors_1_1 = errors_1.next()) {
|
|
253
|
+
error = errors_1_1.value;
|
|
254
|
+
write(error);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
258
|
+
finally {
|
|
259
|
+
try {
|
|
260
|
+
if (errors_1_1 && !errors_1_1.done && (_e = errors_1.return)) _e.call(errors_1);
|
|
261
|
+
}
|
|
262
|
+
finally { if (e_4) throw e_4.error; }
|
|
263
|
+
}
|
|
264
|
+
write("");
|
|
265
|
+
printScriptHelp(script, programName, write);
|
|
266
|
+
return [2 /*return*/, finish({
|
|
267
|
+
errors: errors,
|
|
268
|
+
exitCode: 1,
|
|
269
|
+
results: [],
|
|
270
|
+
script: script.name,
|
|
271
|
+
success: false,
|
|
272
|
+
wetRun: wetRun,
|
|
273
|
+
})];
|
|
274
|
+
}
|
|
275
|
+
ctx = {
|
|
276
|
+
addLog: function (level, message) { return __awaiter(void 0, void 0, void 0, function () {
|
|
277
|
+
return __generator(this, function (_a) {
|
|
278
|
+
if (!wantsJson) {
|
|
279
|
+
write("[".concat(level, "] ").concat(message));
|
|
280
|
+
}
|
|
281
|
+
return [2 /*return*/];
|
|
282
|
+
});
|
|
283
|
+
}); },
|
|
284
|
+
args: args,
|
|
285
|
+
checkCancellation: function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
286
|
+
return __generator(this, function (_a) {
|
|
287
|
+
return [2 /*return*/];
|
|
288
|
+
});
|
|
289
|
+
}); },
|
|
290
|
+
updateProgress: function (percentage, stage, message) { return __awaiter(void 0, void 0, void 0, function () {
|
|
291
|
+
var suffix;
|
|
292
|
+
return __generator(this, function (_a) {
|
|
293
|
+
if (!wantsJson) {
|
|
294
|
+
suffix = [stage, message].filter(Boolean).join(" - ");
|
|
295
|
+
write("[progress] ".concat(percentage, "%").concat(suffix ? " ".concat(suffix) : ""));
|
|
296
|
+
}
|
|
297
|
+
return [2 /*return*/];
|
|
298
|
+
});
|
|
299
|
+
}); },
|
|
300
|
+
};
|
|
301
|
+
if (!wantsJson) {
|
|
302
|
+
write("Running \"".concat(script.name, "\" (").concat(wetRun ? "wet run" : "dry run", ")..."));
|
|
303
|
+
}
|
|
304
|
+
_l.label = 1;
|
|
305
|
+
case 1:
|
|
306
|
+
_l.trys.push([1, 3, , 4]);
|
|
307
|
+
return [4 /*yield*/, script.runner(wetRun, ctx)];
|
|
308
|
+
case 2:
|
|
309
|
+
result = _l.sent();
|
|
310
|
+
if (wantsJson) {
|
|
311
|
+
write(JSON.stringify({
|
|
312
|
+
results: result.results,
|
|
313
|
+
script: script.name,
|
|
314
|
+
success: result.success,
|
|
315
|
+
wetRun: wetRun,
|
|
316
|
+
}));
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
try {
|
|
320
|
+
for (_b = __values(result.results), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
321
|
+
line = _c.value;
|
|
322
|
+
write(line);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
326
|
+
finally {
|
|
327
|
+
try {
|
|
328
|
+
if (_c && !_c.done && (_f = _b.return)) _f.call(_b);
|
|
329
|
+
}
|
|
330
|
+
finally { if (e_5) throw e_5.error; }
|
|
331
|
+
}
|
|
332
|
+
write(result.success ? "Done." : "Script reported failure.");
|
|
333
|
+
}
|
|
334
|
+
return [2 /*return*/, finish({
|
|
335
|
+
errors: [],
|
|
336
|
+
exitCode: result.success ? 0 : 1,
|
|
337
|
+
results: result.results,
|
|
338
|
+
script: script.name,
|
|
339
|
+
success: result.success,
|
|
340
|
+
wetRun: wetRun,
|
|
341
|
+
})];
|
|
342
|
+
case 3:
|
|
343
|
+
err_1 = _l.sent();
|
|
344
|
+
message = err_1 instanceof Error ? err_1.message : String(err_1);
|
|
345
|
+
api_1.logger.error("Script ".concat(script.name, " failed: ").concat(message));
|
|
346
|
+
if (wantsJson) {
|
|
347
|
+
write(JSON.stringify({ error: message, script: script.name, success: false, wetRun: wetRun }));
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
write("Error: ".concat(message));
|
|
351
|
+
}
|
|
352
|
+
return [2 /*return*/, finish({
|
|
353
|
+
errors: [message],
|
|
354
|
+
exitCode: 1,
|
|
355
|
+
results: [],
|
|
356
|
+
script: script.name,
|
|
357
|
+
success: false,
|
|
358
|
+
wetRun: wetRun,
|
|
359
|
+
})];
|
|
360
|
+
case 4: return [2 /*return*/];
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}); };
|
|
364
|
+
exports.runScriptCli = runScriptCli;
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@google-cloud/storage": "^7.15.0",
|
|
8
|
-
"@terreno/api": "0.
|
|
8
|
+
"@terreno/api": "0.23.0",
|
|
9
9
|
"express": "^5.2.1",
|
|
10
10
|
"luxon": "^3.7.2",
|
|
11
11
|
"multer": "2.1.1"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"description": "Admin panel backend plugin for @terreno/api",
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@biomejs/biome": "^2.3.6",
|
|
16
|
-
"@terreno/test": "0.
|
|
16
|
+
"@terreno/test": "0.23.0",
|
|
17
17
|
"@types/bun": "^1.2.4",
|
|
18
18
|
"@types/express": "^5.0.6",
|
|
19
19
|
"@types/luxon": "^3.7.1",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
55
55
|
},
|
|
56
56
|
"types": "dist/index.d.ts",
|
|
57
|
-
"version": "0.
|
|
57
|
+
"version": "0.23.0"
|
|
58
58
|
}
|