@unbrained/pm-cli 2026.5.18 → 2026.5.24
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/CHANGELOG.md +60 -0
- package/README.md +2 -1
- package/dist/cli/commander-usage.js +16 -2
- package/dist/cli/commander-usage.js.map +1 -1
- package/dist/cli/commands/annotation-command.d.ts +49 -0
- package/dist/cli/commands/annotation-command.js +135 -0
- package/dist/cli/commands/annotation-command.js.map +1 -0
- package/dist/cli/commands/append.js +3 -7
- package/dist/cli/commands/append.js.map +1 -1
- package/dist/cli/commands/calendar.js +3 -6
- package/dist/cli/commands/calendar.js.map +1 -1
- package/dist/cli/commands/claim.js +12 -22
- package/dist/cli/commands/claim.js.map +1 -1
- package/dist/cli/commands/close.js +61 -9
- package/dist/cli/commands/close.js.map +1 -1
- package/dist/cli/commands/comments.d.ts +5 -0
- package/dist/cli/commands/comments.js +27 -117
- package/dist/cli/commands/comments.js.map +1 -1
- package/dist/cli/commands/completion.js +102 -15
- package/dist/cli/commands/completion.js.map +1 -1
- package/dist/cli/commands/context.js +4 -10
- package/dist/cli/commands/context.js.map +1 -1
- package/dist/cli/commands/contracts.js +168 -36
- package/dist/cli/commands/contracts.js.map +1 -1
- package/dist/cli/commands/create.js +49 -44
- package/dist/cli/commands/create.js.map +1 -1
- package/dist/cli/commands/dedupe-audit.js +7 -4
- package/dist/cli/commands/dedupe-audit.js.map +1 -1
- package/dist/cli/commands/delete.d.ts +3 -0
- package/dist/cli/commands/delete.js +9 -8
- package/dist/cli/commands/delete.js.map +1 -1
- package/dist/cli/commands/docs.d.ts +1 -0
- package/dist/cli/commands/docs.js +4 -8
- package/dist/cli/commands/docs.js.map +1 -1
- package/dist/cli/commands/event-validation-messages.d.ts +3 -0
- package/dist/cli/commands/event-validation-messages.js +44 -0
- package/dist/cli/commands/event-validation-messages.js.map +1 -0
- package/dist/cli/commands/extension.d.ts +1 -0
- package/dist/cli/commands/extension.js +138 -21
- package/dist/cli/commands/extension.js.map +1 -1
- package/dist/cli/commands/files.js +6 -13
- package/dist/cli/commands/files.js.map +1 -1
- package/dist/cli/commands/gc.js +17 -4
- package/dist/cli/commands/gc.js.map +1 -1
- package/dist/cli/commands/get.d.ts +3 -2
- package/dist/cli/commands/get.js +50 -8
- package/dist/cli/commands/get.js.map +1 -1
- package/dist/cli/commands/health.d.ts +10 -0
- package/dist/cli/commands/health.js +254 -75
- package/dist/cli/commands/health.js.map +1 -1
- package/dist/cli/commands/history-redact.d.ts +8 -0
- package/dist/cli/commands/history-redact.js +14 -97
- package/dist/cli/commands/history-redact.js.map +1 -1
- package/dist/cli/commands/history-repair.d.ts +33 -0
- package/dist/cli/commands/history-repair.js +166 -0
- package/dist/cli/commands/history-repair.js.map +1 -0
- package/dist/cli/commands/history.d.ts +4 -4
- package/dist/cli/commands/history.js +10 -88
- package/dist/cli/commands/history.js.map +1 -1
- package/dist/cli/commands/index.d.ts +3 -1
- package/dist/cli/commands/index.js +5 -3
- package/dist/cli/commands/index.js.map +1 -1
- package/dist/cli/commands/init.d.ts +28 -0
- package/dist/cli/commands/init.js +23 -2
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/learnings.js +20 -119
- package/dist/cli/commands/learnings.js.map +1 -1
- package/dist/cli/commands/linked-test-entry.d.ts +3 -0
- package/dist/cli/commands/linked-test-entry.js +62 -0
- package/dist/cli/commands/linked-test-entry.js.map +1 -0
- package/dist/cli/commands/list.js +32 -22
- package/dist/cli/commands/list.js.map +1 -1
- package/dist/cli/commands/notes.js +20 -119
- package/dist/cli/commands/notes.js.map +1 -1
- package/dist/cli/commands/plan.d.ts +3 -0
- package/dist/cli/commands/plan.js +184 -22
- package/dist/cli/commands/plan.js.map +1 -1
- package/dist/cli/commands/restore.js +7 -50
- package/dist/cli/commands/restore.js.map +1 -1
- package/dist/cli/commands/schema.d.ts +31 -0
- package/dist/cli/commands/schema.js +98 -0
- package/dist/cli/commands/schema.js.map +1 -0
- package/dist/cli/commands/search.js +151 -40
- package/dist/cli/commands/search.js.map +1 -1
- package/dist/cli/commands/templates.d.ts +4 -0
- package/dist/cli/commands/templates.js +89 -17
- package/dist/cli/commands/templates.js.map +1 -1
- package/dist/cli/commands/test-all.js +4 -8
- package/dist/cli/commands/test-all.js.map +1 -1
- package/dist/cli/commands/test.d.ts +1 -0
- package/dist/cli/commands/test.js +7 -10
- package/dist/cli/commands/test.js.map +1 -1
- package/dist/cli/commands/update-many.js +4 -8
- package/dist/cli/commands/update-many.js.map +1 -1
- package/dist/cli/commands/update.js +109 -51
- package/dist/cli/commands/update.js.map +1 -1
- package/dist/cli/commands/validate.d.ts +3 -1
- package/dist/cli/commands/validate.js +23 -71
- package/dist/cli/commands/validate.js.map +1 -1
- package/dist/cli/error-guidance.js +96 -6
- package/dist/cli/error-guidance.js.map +1 -1
- package/dist/cli/extension-command-help.d.ts +0 -1
- package/dist/cli/extension-command-help.js +2 -13
- package/dist/cli/extension-command-help.js.map +1 -1
- package/dist/cli/extension-command-options.d.ts +1 -0
- package/dist/cli/extension-command-options.js +106 -7
- package/dist/cli/extension-command-options.js.map +1 -1
- package/dist/cli/help-content.d.ts +0 -1
- package/dist/cli/help-content.js +13 -9
- package/dist/cli/help-content.js.map +1 -1
- package/dist/cli/help-json-payload.d.ts +1 -0
- package/dist/cli/help-json-payload.js +33 -3
- package/dist/cli/help-json-payload.js.map +1 -1
- package/dist/cli/main.js +35 -29
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/register-list-query.d.ts +1 -1
- package/dist/cli/register-list-query.js +40 -17
- package/dist/cli/register-list-query.js.map +1 -1
- package/dist/cli/register-mutation.d.ts +1 -1
- package/dist/cli/register-mutation.js +232 -64
- package/dist/cli/register-mutation.js.map +1 -1
- package/dist/cli/register-operations.js +16 -11
- package/dist/cli/register-operations.js.map +1 -1
- package/dist/cli/register-setup.js +26 -14
- package/dist/cli/register-setup.js.map +1 -1
- package/dist/cli/registration-helpers.d.ts +0 -2
- package/dist/cli/registration-helpers.js +13 -40
- package/dist/cli/registration-helpers.js.map +1 -1
- package/dist/cli.js +23 -3
- package/dist/cli.js.map +1 -1
- package/dist/core/extensions/index.d.ts +0 -1
- package/dist/core/extensions/index.js +2 -14
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.js +3 -9
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/fs/path-utils.d.ts +1 -0
- package/dist/core/fs/path-utils.js +12 -0
- package/dist/core/fs/path-utils.js.map +1 -0
- package/dist/core/history/drift-scan.d.ts +11 -0
- package/dist/core/history/drift-scan.js +67 -0
- package/dist/core/history/drift-scan.js.map +1 -0
- package/dist/core/history/replay.d.ts +82 -0
- package/dist/core/history/replay.js +249 -0
- package/dist/core/history/replay.js.map +1 -0
- package/dist/core/item/item-format.js +11 -8
- package/dist/core/item/item-format.js.map +1 -1
- package/dist/core/item/item-type-definition.d.ts +52 -0
- package/dist/core/item/item-type-definition.js +123 -0
- package/dist/core/item/item-type-definition.js.map +1 -0
- package/dist/core/item/parse.js +3 -2
- package/dist/core/item/parse.js.map +1 -1
- package/dist/core/item/priority.d.ts +23 -0
- package/dist/core/item/priority.js +55 -0
- package/dist/core/item/priority.js.map +1 -0
- package/dist/core/item/status.d.ts +14 -1
- package/dist/core/item/status.js +22 -2
- package/dist/core/item/status.js.map +1 -1
- package/dist/core/item/toon-decode.d.ts +19 -0
- package/dist/core/item/toon-decode.js +69 -0
- package/dist/core/item/toon-decode.js.map +1 -0
- package/dist/core/item/type-registry.js +13 -84
- package/dist/core/item/type-registry.js.map +1 -1
- package/dist/core/packages/manifest.js +3 -9
- package/dist/core/packages/manifest.js.map +1 -1
- package/dist/core/schema/item-types-file.d.ts +85 -0
- package/dist/core/schema/item-types-file.js +243 -0
- package/dist/core/schema/item-types-file.js.map +1 -0
- package/dist/core/schema/runtime-schema.d.ts +2 -1
- package/dist/core/schema/runtime-schema.js +11 -9
- package/dist/core/schema/runtime-schema.js.map +1 -1
- package/dist/core/search/semantic-defaults.js +3 -3
- package/dist/core/search/semantic-defaults.js.map +1 -1
- package/dist/core/shared/author.d.ts +1 -0
- package/dist/core/shared/author.js +9 -0
- package/dist/core/shared/author.js.map +1 -0
- package/dist/core/shared/lazy-module.d.ts +1 -0
- package/dist/core/shared/lazy-module.js +11 -0
- package/dist/core/shared/lazy-module.js.map +1 -0
- package/dist/core/shared/option-alias-visibility.d.ts +44 -0
- package/dist/core/shared/option-alias-visibility.js +76 -0
- package/dist/core/shared/option-alias-visibility.js.map +1 -0
- package/dist/core/shared/text-normalization.d.ts +0 -1
- package/dist/core/shared/text-normalization.js +2 -5
- package/dist/core/shared/text-normalization.js.map +1 -1
- package/dist/core/store/item-store.d.ts +2 -0
- package/dist/core/store/item-store.js +70 -39
- package/dist/core/store/item-store.js.map +1 -1
- package/dist/core/store/settings-validator.d.ts +106 -0
- package/dist/core/store/settings-validator.js +279 -0
- package/dist/core/store/settings-validator.js.map +1 -0
- package/dist/core/store/settings.js +6 -343
- package/dist/core/store/settings.js.map +1 -1
- package/dist/core/telemetry/runtime.js +5 -3
- package/dist/core/telemetry/runtime.js.map +1 -1
- package/dist/mcp/server.js +64 -13
- package/dist/mcp/server.js.map +1 -1
- package/dist/sdk/cli-contracts.d.ts +9 -2
- package/dist/sdk/cli-contracts.js +204 -13
- package/dist/sdk/cli-contracts.js.map +1 -1
- package/dist/sdk/runtime.d.ts +25 -1
- package/dist/sdk/runtime.js +46 -3
- package/dist/sdk/runtime.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.js +10 -2
- package/dist/types.js.map +1 -1
- package/docs/AGENT_GUIDE.md +7 -1
- package/docs/ARCHITECTURE.md +1 -1
- package/docs/COMMANDS.md +39 -6
- package/docs/CONFIGURATION.md +1 -1
- package/docs/RELEASING.md +11 -7
- package/docs/SDK.md +11 -2
- package/package.json +12 -11
- package/packages/pm-calendar/README.md +3 -1
- package/packages/pm-calendar/extensions/calendar/index.js +21 -2
- package/packages/pm-calendar/extensions/calendar/index.ts +21 -2
- package/packages/pm-search-advanced/README.md +8 -0
- package/packages/pm-search-advanced/extensions/search-advanced/index.js +74 -0
- package/packages/pm-search-advanced/extensions/search-advanced/index.ts +74 -0
- package/packages/pm-search-advanced/extensions/search-advanced/runtime.js +67 -9
- package/packages/pm-search-advanced/extensions/search-advanced/runtime.ts +67 -9
- package/packages/pm-templates/extensions/templates/runtime.js +11 -202
- package/packages/pm-templates/extensions/templates/runtime.ts +38 -230
- package/dist/core/output/command-aware.d.ts +0 -1
- package/dist/core/output/command-aware.js +0 -397
- package/dist/core/output/command-aware.js.map +0 -1
|
@@ -1,226 +1,35 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import { pathToFileURL } from "node:url";
|
|
4
|
-
|
|
5
3
|
const PM_PACKAGE_ROOT_ENV = "PM_CLI_PACKAGE_ROOT";
|
|
6
4
|
const sdk = await loadTemplatesSdkModule();
|
|
7
|
-
const {
|
|
8
|
-
CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS,
|
|
9
|
-
EXIT_CODE,
|
|
10
|
-
PmCliError,
|
|
11
|
-
getSettingsPath,
|
|
12
|
-
nowIso,
|
|
13
|
-
pathExists,
|
|
14
|
-
readFileIfExists,
|
|
15
|
-
resolvePmRoot,
|
|
16
|
-
writeFileAtomic,
|
|
17
|
-
} = sdk;
|
|
18
|
-
|
|
19
5
|
async function loadTemplatesSdkModule() {
|
|
20
6
|
const envRoot = process.env[PM_PACKAGE_ROOT_ENV];
|
|
21
7
|
if (typeof envRoot !== "string" || envRoot.trim().length === 0) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
`builtin-templates requires ${PM_PACKAGE_ROOT_ENV} to locate core SDK runtime exports.`,
|
|
24
|
-
);
|
|
8
|
+
throw new Error(`builtin-templates requires ${PM_PACKAGE_ROOT_ENV} to locate core SDK runtime exports.`);
|
|
25
9
|
}
|
|
26
10
|
const modulePath = path.join(path.resolve(envRoot.trim()), "dist", "sdk", "index.js");
|
|
27
11
|
try {
|
|
28
12
|
const loaded = await import(pathToFileURL(modulePath).href);
|
|
29
|
-
if (
|
|
30
|
-
typeof loaded.
|
|
31
|
-
typeof loaded.
|
|
32
|
-
typeof loaded.
|
|
33
|
-
Array.isArray(loaded.CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS)
|
|
34
|
-
) {
|
|
13
|
+
if (typeof loaded.loadCreateTemplateOptions === "function" &&
|
|
14
|
+
typeof loaded.runTemplatesList === "function" &&
|
|
15
|
+
typeof loaded.runTemplatesSave === "function" &&
|
|
16
|
+
typeof loaded.runTemplatesShow === "function") {
|
|
35
17
|
return loaded;
|
|
36
18
|
}
|
|
37
19
|
} catch {
|
|
38
20
|
// Fall through to deterministic failure message below.
|
|
39
21
|
}
|
|
40
|
-
throw new Error(
|
|
41
|
-
`builtin-templates failed to load SDK runtime exports from ${modulePath}.`,
|
|
42
|
-
);
|
|
22
|
+
throw new Error(`builtin-templates failed to load template runtime exports from ${modulePath}.`);
|
|
43
23
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const TEMPLATE_FILE_EXTENSION = ".json";
|
|
47
|
-
const TEMPLATE_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
48
|
-
const TEMPLATE_OPTION_REPEATABLE_KEY_SET = new Set(CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS.map((entry) => entry.target));
|
|
49
|
-
|
|
50
|
-
function normalizeTemplateName(rawName) {
|
|
51
|
-
const name = rawName.trim();
|
|
52
|
-
if (!TEMPLATE_NAME_PATTERN.test(name)) {
|
|
53
|
-
throw new PmCliError(
|
|
54
|
-
`Invalid template name "${rawName}". Expected 1-64 characters matching [A-Za-z0-9][A-Za-z0-9._-]*.`,
|
|
55
|
-
EXIT_CODE.USAGE,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
return name;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function templatesDirectory(pmRoot) {
|
|
62
|
-
return path.join(pmRoot, TEMPLATE_DIRECTORY_NAME);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function templatePath(pmRoot, normalizedName) {
|
|
66
|
-
return path.join(templatesDirectory(pmRoot), `${normalizedName}${TEMPLATE_FILE_EXTENSION}`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async function ensureTrackerInitialized(pmRoot) {
|
|
70
|
-
if (!(await pathExists(getSettingsPath(pmRoot)))) {
|
|
71
|
-
throw new PmCliError(`Tracker is not initialized at ${pmRoot}. Run pm init first.`, EXIT_CODE.NOT_FOUND);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function sortTemplateOptions(options) {
|
|
76
|
-
return Object.fromEntries(Object.entries(options).sort(([left], [right]) => left.localeCompare(right)));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function extractTemplateOptions(rawOptions) {
|
|
80
|
-
const next = {};
|
|
81
|
-
for (const [key, value] of Object.entries(rawOptions)) {
|
|
82
|
-
if (value === undefined) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
if (TEMPLATE_OPTION_REPEATABLE_KEY_SET.has(key)) {
|
|
86
|
-
if (typeof value === "string") {
|
|
87
|
-
next[key] = [value];
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
if (!Array.isArray(value)) {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const values = value.filter((entry) => typeof entry === "string");
|
|
94
|
-
if (values.length > 0) {
|
|
95
|
-
next[key] = values;
|
|
96
|
-
}
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
if (typeof value === "string") {
|
|
100
|
-
next[key] = value;
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
if (Array.isArray(value) && value.every((entry) => typeof entry === "string")) {
|
|
104
|
-
next[key] = [...value];
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return sortTemplateOptions(next);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function parseStoredTemplateOptions(rawOptions, templateName) {
|
|
111
|
-
if (typeof rawOptions !== "object" || rawOptions === null || Array.isArray(rawOptions)) {
|
|
112
|
-
throw new PmCliError(`Template "${templateName}" has invalid options payload.`, EXIT_CODE.GENERIC_FAILURE);
|
|
113
|
-
}
|
|
114
|
-
const normalized = {};
|
|
115
|
-
for (const [key, value] of Object.entries(rawOptions)) {
|
|
116
|
-
const normalizedKey = key.trim();
|
|
117
|
-
if (normalizedKey.length === 0) {
|
|
118
|
-
throw new PmCliError(`Template "${templateName}" contains an empty option key.`, EXIT_CODE.GENERIC_FAILURE);
|
|
119
|
-
}
|
|
120
|
-
if (typeof value === "string") {
|
|
121
|
-
normalized[normalizedKey] = value;
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
if (Array.isArray(value) && value.every((entry) => typeof entry === "string")) {
|
|
125
|
-
normalized[normalizedKey] = [...value];
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
throw new PmCliError(`Template "${templateName}" contains invalid value for option "${normalizedKey}".`, EXIT_CODE.GENERIC_FAILURE);
|
|
129
|
-
}
|
|
130
|
-
return sortTemplateOptions(normalized);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function parseStoredTemplateDocument(raw, normalizedName) {
|
|
134
|
-
let parsed;
|
|
135
|
-
try {
|
|
136
|
-
parsed = JSON.parse(raw);
|
|
137
|
-
} catch {
|
|
138
|
-
throw new PmCliError(`Template "${normalizedName}" contains invalid JSON.`, EXIT_CODE.GENERIC_FAILURE);
|
|
139
|
-
}
|
|
140
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
141
|
-
throw new PmCliError(`Template "${normalizedName}" has invalid document shape.`, EXIT_CODE.GENERIC_FAILURE);
|
|
142
|
-
}
|
|
143
|
-
const options = parseStoredTemplateOptions(parsed.options, normalizedName);
|
|
144
|
-
const now = nowIso();
|
|
145
|
-
return {
|
|
146
|
-
name: typeof parsed.name === "string" && parsed.name.trim().length > 0 ? parsed.name.trim() : normalizedName,
|
|
147
|
-
created_at: typeof parsed.created_at === "string" ? parsed.created_at : now,
|
|
148
|
-
updated_at: typeof parsed.updated_at === "string" ? parsed.updated_at : now,
|
|
149
|
-
options,
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async function readStoredTemplateDocument(pmRoot, normalizedName) {
|
|
154
|
-
const raw = await readFileIfExists(templatePath(pmRoot, normalizedName));
|
|
155
|
-
if (raw === null) {
|
|
156
|
-
throw new PmCliError(`Template "${normalizedName}" not found`, EXIT_CODE.NOT_FOUND);
|
|
157
|
-
}
|
|
158
|
-
return parseStoredTemplateDocument(raw, normalizedName);
|
|
24
|
+
export async function loadCreateTemplateOptions(pmRoot, rawTemplateName) {
|
|
25
|
+
return sdk.loadCreateTemplateOptions(pmRoot, rawTemplateName);
|
|
159
26
|
}
|
|
160
|
-
|
|
161
27
|
export async function runTemplatesSave(rawTemplateName, options, global) {
|
|
162
|
-
|
|
163
|
-
await ensureTrackerInitialized(pmRoot);
|
|
164
|
-
const normalizedName = normalizeTemplateName(rawTemplateName);
|
|
165
|
-
const nextOptions = extractTemplateOptions(options);
|
|
166
|
-
if (Object.keys(nextOptions).length === 0) {
|
|
167
|
-
throw new PmCliError("templates save requires at least one create option flag", EXIT_CODE.USAGE);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const now = nowIso();
|
|
171
|
-
const storedPath = templatePath(pmRoot, normalizedName);
|
|
172
|
-
let createdAt = now;
|
|
173
|
-
if (await pathExists(storedPath)) {
|
|
174
|
-
const existing = await readStoredTemplateDocument(pmRoot, normalizedName);
|
|
175
|
-
createdAt = existing.created_at;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const document = {
|
|
179
|
-
name: normalizedName,
|
|
180
|
-
created_at: createdAt,
|
|
181
|
-
updated_at: now,
|
|
182
|
-
options: nextOptions,
|
|
183
|
-
};
|
|
184
|
-
await fs.mkdir(templatesDirectory(pmRoot), { recursive: true });
|
|
185
|
-
await writeFileAtomic(storedPath, `${JSON.stringify(document, null, 2)}\n`);
|
|
186
|
-
return {
|
|
187
|
-
name: document.name,
|
|
188
|
-
created_at: document.created_at,
|
|
189
|
-
updated_at: document.updated_at,
|
|
190
|
-
path: storedPath,
|
|
191
|
-
options: document.options,
|
|
192
|
-
};
|
|
28
|
+
return sdk.runTemplatesSave(rawTemplateName, options, global);
|
|
193
29
|
}
|
|
194
|
-
|
|
195
30
|
export async function runTemplatesList(global) {
|
|
196
|
-
|
|
197
|
-
await ensureTrackerInitialized(pmRoot);
|
|
198
|
-
const dirPath = templatesDirectory(pmRoot);
|
|
199
|
-
if (!(await pathExists(dirPath))) {
|
|
200
|
-
return { templates: [], count: 0 };
|
|
201
|
-
}
|
|
202
|
-
const entries = await fs.readdir(dirPath);
|
|
203
|
-
const templates = entries
|
|
204
|
-
.filter((entry) => entry.toLowerCase().endsWith(TEMPLATE_FILE_EXTENSION))
|
|
205
|
-
.map((entry) => entry.slice(0, -TEMPLATE_FILE_EXTENSION.length))
|
|
206
|
-
.filter((entry) => TEMPLATE_NAME_PATTERN.test(entry))
|
|
207
|
-
.sort((left, right) => left.localeCompare(right));
|
|
208
|
-
return {
|
|
209
|
-
templates,
|
|
210
|
-
count: templates.length,
|
|
211
|
-
};
|
|
31
|
+
return sdk.runTemplatesList(global);
|
|
212
32
|
}
|
|
213
|
-
|
|
214
33
|
export async function runTemplatesShow(rawTemplateName, global) {
|
|
215
|
-
|
|
216
|
-
await ensureTrackerInitialized(pmRoot);
|
|
217
|
-
const normalizedName = normalizeTemplateName(rawTemplateName);
|
|
218
|
-
const stored = await readStoredTemplateDocument(pmRoot, normalizedName);
|
|
219
|
-
return {
|
|
220
|
-
name: stored.name,
|
|
221
|
-
created_at: stored.created_at,
|
|
222
|
-
updated_at: stored.updated_at,
|
|
223
|
-
path: templatePath(pmRoot, normalizedName),
|
|
224
|
-
options: stored.options,
|
|
225
|
-
};
|
|
34
|
+
return sdk.runTemplatesShow(rawTemplateName, global);
|
|
226
35
|
}
|
|
@@ -1,81 +1,12 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import { pathToFileURL } from "node:url";
|
|
4
3
|
import type { GlobalOptions } from "../../../../src/sdk/index.js";
|
|
5
4
|
|
|
6
5
|
const PM_PACKAGE_ROOT_ENV = "PM_CLI_PACKAGE_ROOT";
|
|
7
6
|
|
|
8
|
-
interface TemplatesSdkModule {
|
|
9
|
-
CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS: Array<{ target: string }>;
|
|
10
|
-
EXIT_CODE: {
|
|
11
|
-
USAGE: number;
|
|
12
|
-
NOT_FOUND: number;
|
|
13
|
-
GENERIC_FAILURE: number;
|
|
14
|
-
};
|
|
15
|
-
PmCliError: new (message: string, exitCode?: number) => Error;
|
|
16
|
-
getSettingsPath: (pmRoot: string) => string;
|
|
17
|
-
nowIso: () => string;
|
|
18
|
-
pathExists: (targetPath: string) => Promise<boolean>;
|
|
19
|
-
readFileIfExists: (targetPath: string) => Promise<string | null>;
|
|
20
|
-
resolvePmRoot: (cwd: string, overridePath?: string) => string;
|
|
21
|
-
writeFileAtomic: (targetPath: string, contents: string) => Promise<void>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const sdk = await loadTemplatesSdkModule();
|
|
25
|
-
const {
|
|
26
|
-
CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS,
|
|
27
|
-
EXIT_CODE,
|
|
28
|
-
PmCliError,
|
|
29
|
-
getSettingsPath,
|
|
30
|
-
nowIso,
|
|
31
|
-
pathExists,
|
|
32
|
-
readFileIfExists,
|
|
33
|
-
resolvePmRoot,
|
|
34
|
-
writeFileAtomic,
|
|
35
|
-
} = sdk;
|
|
36
|
-
|
|
37
|
-
async function loadTemplatesSdkModule(): Promise<TemplatesSdkModule> {
|
|
38
|
-
const envRoot = process.env[PM_PACKAGE_ROOT_ENV];
|
|
39
|
-
if (typeof envRoot !== "string" || envRoot.trim().length === 0) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
`builtin-templates requires ${PM_PACKAGE_ROOT_ENV} to locate core SDK runtime exports.`,
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
const modulePath = path.join(path.resolve(envRoot.trim()), "dist", "sdk", "index.js");
|
|
45
|
-
try {
|
|
46
|
-
const loaded = (await import(pathToFileURL(modulePath).href)) as Partial<TemplatesSdkModule>;
|
|
47
|
-
if (
|
|
48
|
-
typeof loaded.resolvePmRoot === "function" &&
|
|
49
|
-
typeof loaded.pathExists === "function" &&
|
|
50
|
-
typeof loaded.PmCliError === "function" &&
|
|
51
|
-
Array.isArray(loaded.CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS)
|
|
52
|
-
) {
|
|
53
|
-
return loaded as TemplatesSdkModule;
|
|
54
|
-
}
|
|
55
|
-
} catch {
|
|
56
|
-
// Fall through to deterministic failure message below.
|
|
57
|
-
}
|
|
58
|
-
throw new Error(
|
|
59
|
-
`builtin-templates failed to load SDK runtime exports from ${modulePath}.`,
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const TEMPLATE_DIRECTORY_NAME = "templates";
|
|
64
|
-
const TEMPLATE_FILE_EXTENSION = ".json";
|
|
65
|
-
const TEMPLATE_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
66
|
-
const TEMPLATE_OPTION_REPEATABLE_KEYS = CREATE_COMMANDER_REPEATABLE_OPTION_CONTRACTS.map((entry) => entry.target);
|
|
67
|
-
const TEMPLATE_OPTION_REPEATABLE_KEY_SET = new Set<string>(TEMPLATE_OPTION_REPEATABLE_KEYS);
|
|
68
|
-
|
|
69
7
|
type TemplateOptionValue = string | string[];
|
|
70
8
|
export type CreateTemplateOptions = Record<string, TemplateOptionValue>;
|
|
71
9
|
|
|
72
|
-
interface StoredCreateTemplateDocument {
|
|
73
|
-
name: string;
|
|
74
|
-
created_at: string;
|
|
75
|
-
updated_at: string;
|
|
76
|
-
options: CreateTemplateOptions;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
10
|
export interface TemplatesSaveResult {
|
|
80
11
|
name: string;
|
|
81
12
|
created_at: string;
|
|
@@ -87,128 +18,60 @@ export interface TemplatesSaveResult {
|
|
|
87
18
|
export interface TemplatesListResult {
|
|
88
19
|
templates: string[];
|
|
89
20
|
count: number;
|
|
21
|
+
builtin_templates: string[];
|
|
22
|
+
user_templates: string[];
|
|
90
23
|
}
|
|
91
24
|
|
|
92
25
|
export interface TemplatesShowResult {
|
|
93
26
|
name: string;
|
|
27
|
+
source: "builtin" | "user";
|
|
94
28
|
created_at: string;
|
|
95
29
|
updated_at: string;
|
|
96
30
|
path: string;
|
|
97
31
|
options: CreateTemplateOptions;
|
|
98
32
|
}
|
|
99
33
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function templatesDirectory(pmRoot: string): string {
|
|
112
|
-
return path.join(pmRoot, TEMPLATE_DIRECTORY_NAME);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function templatePath(pmRoot: string, normalizedName: string): string {
|
|
116
|
-
return path.join(templatesDirectory(pmRoot), `${normalizedName}${TEMPLATE_FILE_EXTENSION}`);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function ensureTrackerInitialized(pmRoot: string): Promise<void> {
|
|
120
|
-
if (!(await pathExists(getSettingsPath(pmRoot)))) {
|
|
121
|
-
throw new PmCliError(`Tracker is not initialized at ${pmRoot}. Run pm init first.`, EXIT_CODE.NOT_FOUND);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function sortTemplateOptions(options: CreateTemplateOptions): CreateTemplateOptions {
|
|
126
|
-
return Object.fromEntries(
|
|
127
|
-
Object.entries(options).sort(([left], [right]) => left.localeCompare(right)),
|
|
128
|
-
) as CreateTemplateOptions;
|
|
34
|
+
interface TemplatesSdkModule {
|
|
35
|
+
loadCreateTemplateOptions: (pmRoot: string, rawTemplateName: string) => Promise<CreateTemplateOptions>;
|
|
36
|
+
runTemplatesList: (global: GlobalOptions) => Promise<TemplatesListResult>;
|
|
37
|
+
runTemplatesSave: (
|
|
38
|
+
rawTemplateName: string,
|
|
39
|
+
options: Record<string, unknown>,
|
|
40
|
+
global: GlobalOptions,
|
|
41
|
+
) => Promise<TemplatesSaveResult>;
|
|
42
|
+
runTemplatesShow: (rawTemplateName: string, global: GlobalOptions) => Promise<TemplatesShowResult>;
|
|
129
43
|
}
|
|
130
44
|
|
|
131
|
-
|
|
132
|
-
const next: CreateTemplateOptions = {};
|
|
133
|
-
for (const [key, value] of Object.entries(rawOptions)) {
|
|
134
|
-
if (value === undefined) {
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
if (TEMPLATE_OPTION_REPEATABLE_KEY_SET.has(key)) {
|
|
138
|
-
if (typeof value === "string") {
|
|
139
|
-
next[key] = [value];
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
if (!Array.isArray(value)) {
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
const values = value.filter((entry): entry is string => typeof entry === "string");
|
|
146
|
-
if (values.length > 0) {
|
|
147
|
-
next[key] = values;
|
|
148
|
-
}
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (typeof value === "string") {
|
|
152
|
-
next[key] = value;
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
if (Array.isArray(value) && value.every((entry) => typeof entry === "string")) {
|
|
156
|
-
next[key] = [...value];
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return sortTemplateOptions(next);
|
|
160
|
-
}
|
|
45
|
+
const sdk = await loadTemplatesSdkModule();
|
|
161
46
|
|
|
162
|
-
function
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const normalizedKey = key.trim();
|
|
169
|
-
if (normalizedKey.length === 0) {
|
|
170
|
-
throw new PmCliError(`Template "${templateName}" contains an empty option key.`, EXIT_CODE.GENERIC_FAILURE);
|
|
171
|
-
}
|
|
172
|
-
if (typeof value === "string") {
|
|
173
|
-
normalized[normalizedKey] = value;
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
if (Array.isArray(value) && value.every((entry) => typeof entry === "string")) {
|
|
177
|
-
normalized[normalizedKey] = [...value];
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
throw new PmCliError(`Template "${templateName}" contains invalid value for option "${normalizedKey}".`, EXIT_CODE.GENERIC_FAILURE);
|
|
47
|
+
async function loadTemplatesSdkModule(): Promise<TemplatesSdkModule> {
|
|
48
|
+
const envRoot = process.env[PM_PACKAGE_ROOT_ENV];
|
|
49
|
+
if (typeof envRoot !== "string" || envRoot.trim().length === 0) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`builtin-templates requires ${PM_PACKAGE_ROOT_ENV} to locate core SDK runtime exports.`,
|
|
52
|
+
);
|
|
181
53
|
}
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function parseStoredTemplateDocument(raw: string, normalizedName: string): StoredCreateTemplateDocument {
|
|
186
|
-
let parsed: unknown;
|
|
54
|
+
const modulePath = path.join(path.resolve(envRoot.trim()), "dist", "sdk", "index.js");
|
|
187
55
|
try {
|
|
188
|
-
|
|
56
|
+
const loaded = (await import(pathToFileURL(modulePath).href)) as Partial<TemplatesSdkModule>;
|
|
57
|
+
if (
|
|
58
|
+
typeof loaded.loadCreateTemplateOptions === "function" &&
|
|
59
|
+
typeof loaded.runTemplatesList === "function" &&
|
|
60
|
+
typeof loaded.runTemplatesSave === "function" &&
|
|
61
|
+
typeof loaded.runTemplatesShow === "function"
|
|
62
|
+
) {
|
|
63
|
+
return loaded as TemplatesSdkModule;
|
|
64
|
+
}
|
|
189
65
|
} catch {
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
193
|
-
throw new PmCliError(`Template "${normalizedName}" has invalid document shape.`, EXIT_CODE.GENERIC_FAILURE);
|
|
66
|
+
// Fall through to deterministic failure message below.
|
|
194
67
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return {
|
|
199
|
-
name: typeof record.name === "string" && record.name.trim().length > 0 ? record.name.trim() : normalizedName,
|
|
200
|
-
created_at: typeof record.created_at === "string" ? record.created_at : now,
|
|
201
|
-
updated_at: typeof record.updated_at === "string" ? record.updated_at : now,
|
|
202
|
-
options,
|
|
203
|
-
};
|
|
68
|
+
throw new Error(
|
|
69
|
+
`builtin-templates failed to load template runtime exports from ${modulePath}.`,
|
|
70
|
+
);
|
|
204
71
|
}
|
|
205
72
|
|
|
206
|
-
async function
|
|
207
|
-
|
|
208
|
-
if (raw === null) {
|
|
209
|
-
throw new PmCliError(`Template "${normalizedName}" not found`, EXIT_CODE.NOT_FOUND);
|
|
210
|
-
}
|
|
211
|
-
return parseStoredTemplateDocument(raw, normalizedName);
|
|
73
|
+
export async function loadCreateTemplateOptions(pmRoot: string, rawTemplateName: string): Promise<CreateTemplateOptions> {
|
|
74
|
+
return sdk.loadCreateTemplateOptions(pmRoot, rawTemplateName);
|
|
212
75
|
}
|
|
213
76
|
|
|
214
77
|
export async function runTemplatesSave(
|
|
@@ -216,68 +79,13 @@ export async function runTemplatesSave(
|
|
|
216
79
|
options: Record<string, unknown>,
|
|
217
80
|
global: GlobalOptions,
|
|
218
81
|
): Promise<TemplatesSaveResult> {
|
|
219
|
-
|
|
220
|
-
await ensureTrackerInitialized(pmRoot);
|
|
221
|
-
const normalizedName = normalizeTemplateName(rawTemplateName);
|
|
222
|
-
const nextOptions = extractTemplateOptions(options);
|
|
223
|
-
if (Object.keys(nextOptions).length === 0) {
|
|
224
|
-
throw new PmCliError("templates save requires at least one create option flag", EXIT_CODE.USAGE);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const now = nowIso();
|
|
228
|
-
const storedPath = templatePath(pmRoot, normalizedName);
|
|
229
|
-
let createdAt = now;
|
|
230
|
-
if (await pathExists(storedPath)) {
|
|
231
|
-
const existing = await readStoredTemplateDocument(pmRoot, normalizedName);
|
|
232
|
-
createdAt = existing.created_at;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const document: StoredCreateTemplateDocument = {
|
|
236
|
-
name: normalizedName,
|
|
237
|
-
created_at: createdAt,
|
|
238
|
-
updated_at: now,
|
|
239
|
-
options: nextOptions,
|
|
240
|
-
};
|
|
241
|
-
await fs.mkdir(templatesDirectory(pmRoot), { recursive: true });
|
|
242
|
-
await writeFileAtomic(storedPath, `${JSON.stringify(document, null, 2)}\n`);
|
|
243
|
-
return {
|
|
244
|
-
name: document.name,
|
|
245
|
-
created_at: document.created_at,
|
|
246
|
-
updated_at: document.updated_at,
|
|
247
|
-
path: storedPath,
|
|
248
|
-
options: document.options,
|
|
249
|
-
};
|
|
82
|
+
return sdk.runTemplatesSave(rawTemplateName, options, global);
|
|
250
83
|
}
|
|
251
84
|
|
|
252
85
|
export async function runTemplatesList(global: GlobalOptions): Promise<TemplatesListResult> {
|
|
253
|
-
|
|
254
|
-
await ensureTrackerInitialized(pmRoot);
|
|
255
|
-
const dirPath = templatesDirectory(pmRoot);
|
|
256
|
-
if (!(await pathExists(dirPath))) {
|
|
257
|
-
return { templates: [], count: 0 };
|
|
258
|
-
}
|
|
259
|
-
const entries = await fs.readdir(dirPath);
|
|
260
|
-
const templates = entries
|
|
261
|
-
.filter((entry) => entry.toLowerCase().endsWith(TEMPLATE_FILE_EXTENSION))
|
|
262
|
-
.map((entry) => entry.slice(0, -TEMPLATE_FILE_EXTENSION.length))
|
|
263
|
-
.filter((entry) => TEMPLATE_NAME_PATTERN.test(entry))
|
|
264
|
-
.sort((left, right) => left.localeCompare(right));
|
|
265
|
-
return {
|
|
266
|
-
templates,
|
|
267
|
-
count: templates.length,
|
|
268
|
-
};
|
|
86
|
+
return sdk.runTemplatesList(global);
|
|
269
87
|
}
|
|
270
88
|
|
|
271
89
|
export async function runTemplatesShow(rawTemplateName: string, global: GlobalOptions): Promise<TemplatesShowResult> {
|
|
272
|
-
|
|
273
|
-
await ensureTrackerInitialized(pmRoot);
|
|
274
|
-
const normalizedName = normalizeTemplateName(rawTemplateName);
|
|
275
|
-
const stored = await readStoredTemplateDocument(pmRoot, normalizedName);
|
|
276
|
-
return {
|
|
277
|
-
name: stored.name,
|
|
278
|
-
created_at: stored.created_at,
|
|
279
|
-
updated_at: stored.updated_at,
|
|
280
|
-
path: templatePath(pmRoot, normalizedName),
|
|
281
|
-
options: stored.options,
|
|
282
|
-
};
|
|
90
|
+
return sdk.runTemplatesShow(rawTemplateName, global);
|
|
283
91
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function buildCommandAwareEnvelope(command: string, result: unknown): Record<string, unknown>;
|