@superintern/sites 0.2.0 → 0.3.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/CHANGELOG.md +20 -0
- package/README.md +44 -4
- package/bin/sites.mjs +89 -24
- package/package.json +6 -2
- package/src/cli/api.mjs +125 -54
- package/src/cli/commands.mjs +391 -15
- package/src/cli/mcp-tools.json +1368 -0
- package/src/cli/mcp.mjs +146 -0
- package/src/cli/media.mjs +233 -0
- package/src/cli/package-info.mjs +1 -1
- package/src/cli/tools.mjs +513 -0
package/src/cli/commands.mjs
CHANGED
|
@@ -2,14 +2,17 @@
|
|
|
2
2
|
// store, git, the screen, the clock) is handed in, so the tests run the real
|
|
3
3
|
// commands against a stand-in.
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { stat } from "node:fs/promises";
|
|
6
|
+
import { basename, resolve } from "node:path";
|
|
6
7
|
|
|
7
8
|
import { ApiError, pathWord, printable, printableLines } from "./api.mjs";
|
|
8
9
|
import { environmentOfGitUrl, gitUrl, slugOfGitUrl } from "./environments.mjs";
|
|
9
10
|
import { answerCredentialRequest, cloneSite, DATA_HELPER_SETTING, dataHelperCommand, ensureIdentity, GitError, helperCommand, linkClone, STARTER_REMOTE } from "./git.mjs";
|
|
10
11
|
import { checkedKey } from "./keys.mjs";
|
|
12
|
+
import { fewAtATime, MEDIA_EXTENSIONS, MEDIA_PREFIX, mediaName, putFile, sizeText, typedMediaName, UPLOADS_AT_ONCE, uploadAddress } from "./media.mjs";
|
|
11
13
|
import { nameProblem } from "./names.mjs";
|
|
12
14
|
import { NotInteractive } from "./prompt.mjs";
|
|
15
|
+
import { argumentsFrom, ArgumentError, commandOf, completeArguments, findTool, groupHelp, knownTools, render, toolHelp } from "./tools.mjs";
|
|
13
16
|
|
|
14
17
|
/** The starter's name at the git address: readable with any working key, never writable. */
|
|
15
18
|
const STARTER_SLUG = "site-starter";
|
|
@@ -222,7 +225,7 @@ export async function status(ctx, slugArg, options = {}) {
|
|
|
222
225
|
|
|
223
226
|
if (options.wait) return waitForBuild(ctx, api, site, options);
|
|
224
227
|
|
|
225
|
-
const
|
|
228
|
+
const { versions, builds } = await api.readSite(site.id).catch((error) => {
|
|
226
229
|
throw friendly(error, slug);
|
|
227
230
|
});
|
|
228
231
|
if (options.json) {
|
|
@@ -279,21 +282,22 @@ export async function publish(ctx, slugArg, options = {}) {
|
|
|
279
282
|
/** @type {import("./api.mjs").Version[]} */
|
|
280
283
|
let versions;
|
|
281
284
|
try {
|
|
282
|
-
|
|
285
|
+
// The latest few: an older version is looked up by the service.
|
|
286
|
+
({ versions } = await api.readSite(site.id));
|
|
283
287
|
} catch (error) {
|
|
284
288
|
throw friendly(error, slug);
|
|
285
289
|
}
|
|
286
290
|
|
|
287
|
-
/** @type {
|
|
288
|
-
let
|
|
291
|
+
/** @type {number | undefined} */
|
|
292
|
+
let versionNumber;
|
|
289
293
|
if (options.version !== undefined) {
|
|
290
294
|
const number = Number(options.version);
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (chosen.state !== "ready") {
|
|
295
|
+
if (!Number.isInteger(number) || number < 1) throw new CliError(`${slug} has no version ${options.version}. See them with: sites status ${slug}`);
|
|
296
|
+
const chosen = versions.find((version) => version.number === number);
|
|
297
|
+
if (chosen && chosen.state !== "ready") {
|
|
294
298
|
throw new CliError([`Version ${chosen.number} cannot be published: it is ${chosen.state}.`, ...problems(chosen).map((line) => ` ${line}`)].join("\n"));
|
|
295
299
|
}
|
|
296
|
-
|
|
300
|
+
versionNumber = number;
|
|
297
301
|
} else if (!versions.some((version) => version.state === "ready")) {
|
|
298
302
|
throw new CliError(
|
|
299
303
|
versions.length === 0
|
|
@@ -305,18 +309,21 @@ export async function publish(ctx, slugArg, options = {}) {
|
|
|
305
309
|
/** @type {import("./api.mjs").Site} */
|
|
306
310
|
let live;
|
|
307
311
|
try {
|
|
308
|
-
live = await api.publish(site.id,
|
|
312
|
+
live = await api.publish(site.id, versionNumber);
|
|
309
313
|
} catch (error) {
|
|
314
|
+
if (versionNumber !== undefined && error instanceof ApiError && error.code === "not_found") {
|
|
315
|
+
throw new CliError(`${slug} has no version ${versionNumber}. See them with: sites status ${slug}`);
|
|
316
|
+
}
|
|
310
317
|
throw (await keyMayNot(api, error, "put a site online")) ?? friendly(error, slug);
|
|
311
318
|
}
|
|
312
319
|
|
|
313
|
-
const served = live.live_version ?? versions.find((version) => version.
|
|
320
|
+
const served = live.live_version ?? versions.find((version) => version.number === versionNumber) ?? versions.find((version) => version.state === "ready");
|
|
314
321
|
ctx.out(`Published${served ? ` version ${served.number} (commit ${short(served.commit_sha)})` : ""} at ${addressOf(live) ?? addressOf(site) ?? slug}`);
|
|
315
322
|
ctx.out("It can take up to a minute to show everywhere.");
|
|
316
323
|
|
|
317
324
|
const newest = versions[0];
|
|
318
325
|
// A higher number that is ready belongs to an older push built side by side: nothing to say.
|
|
319
|
-
if (served && newest && newest.number > served.number && newest.state !== "ready" &&
|
|
326
|
+
if (served && newest && newest.number > served.number && newest.state !== "ready" && versionNumber === undefined) {
|
|
320
327
|
ctx.note(`Version ${newest.number} is newer but is ${newest.state}, so it was not published. See why: sites status ${slug}`);
|
|
321
328
|
}
|
|
322
329
|
const head = await localHead(ctx, slug);
|
|
@@ -562,6 +569,9 @@ export async function accounts(ctx, slugArg, options = {}) {
|
|
|
562
569
|
try {
|
|
563
570
|
state = Object.keys(changes).length ? await api.switchSignIn(site.id, changes) : await api.signInSwitches(site.id);
|
|
564
571
|
} catch (error) {
|
|
572
|
+
if (error instanceof ApiError && error.code === "no_customer_accounts") {
|
|
573
|
+
throw new CliError("This service doesn't show customer accounts to a key yet. See them in SuperIntern: the website's Settings tab, Customer accounts.", 3);
|
|
574
|
+
}
|
|
565
575
|
if (error instanceof ApiError && error.status === 403 && error.code === "read_only") {
|
|
566
576
|
throw new CliError("Your key can only look at websites. Make one that can change websites (Website, Keys), then: sites login", 3);
|
|
567
577
|
}
|
|
@@ -645,6 +655,267 @@ export async function link(ctx) {
|
|
|
645
655
|
return 0;
|
|
646
656
|
}
|
|
647
657
|
|
|
658
|
+
// ---------------------------------------------------------------- media files
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* `sites media add|list|remove`: the site's large pictures, video, audio and
|
|
662
|
+
* PDFs, kept outside its versions and served at /_media/<name>.
|
|
663
|
+
* @param {Context} ctx @param {string | undefined} action @param {string[]} args
|
|
664
|
+
* @param {{ site?: string, json?: boolean }} [options]
|
|
665
|
+
*/
|
|
666
|
+
export async function media(ctx, action, args, options = {}) {
|
|
667
|
+
switch (action) {
|
|
668
|
+
case "add":
|
|
669
|
+
return mediaAdd(ctx, args, options);
|
|
670
|
+
case "list":
|
|
671
|
+
if (args.length > 0) throw new CliError("`sites media list` takes no names. Name the site with --site <slug>.", 2);
|
|
672
|
+
return mediaList(ctx, options);
|
|
673
|
+
case "remove":
|
|
674
|
+
return mediaRemove(ctx, args, options);
|
|
675
|
+
default:
|
|
676
|
+
throw new CliError(
|
|
677
|
+
`${action ? `"${printable(action)}" is not something sites media does.` : "What should sites media do?"} Use: sites media add <file...>, sites media list, sites media remove <name>`,
|
|
678
|
+
2,
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** What stops every file in a batch, not just one: said once, with the exit code to end on. */
|
|
684
|
+
class BatchStopped extends Error {
|
|
685
|
+
/** @param {string} message @param {number} exitCode */
|
|
686
|
+
constructor(message, exitCode) {
|
|
687
|
+
super(message);
|
|
688
|
+
this.exitCode = exitCode;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* When a refusal from the service is about the key or the site, not about
|
|
694
|
+
* one file, the rest of the batch would be refused the same way.
|
|
695
|
+
* @param {import("./api.mjs").Api} api @param {unknown} error @param {string} slug @param {string} action "add files"
|
|
696
|
+
* @returns {Promise<BatchStopped | null>}
|
|
697
|
+
*/
|
|
698
|
+
async function stopsTheBatch(api, error, slug, action) {
|
|
699
|
+
if (!(error instanceof ApiError)) return null;
|
|
700
|
+
// A website key that may only look (`read_only`), or a sign-in not given `sites.write`.
|
|
701
|
+
if (error.status === 403 && (error.code === "read_only" || error.code === "insufficient_scope")) {
|
|
702
|
+
return new BatchStopped("Your key can only look at websites. Make one that can change websites (Website, Keys), then: sites login", 3);
|
|
703
|
+
}
|
|
704
|
+
if (error.code === "media_unavailable") return new BatchStopped(`${error.message} Nothing more was sent.`, 1);
|
|
705
|
+
if (error.code === "rate_limited") return new BatchStopped(`Too many files in a short time. ${error.message}`, 1);
|
|
706
|
+
if (error.code === "site_suspended") return new BatchStopped(error.message, 1);
|
|
707
|
+
if (error.status === 401) {
|
|
708
|
+
const keyProblem = await keyMayNot(api, error, action);
|
|
709
|
+
return new BatchStopped((keyProblem ?? friendly(error, slug)).message, 3);
|
|
710
|
+
}
|
|
711
|
+
return null;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Put files in the site's media: for each, ask for an address to send it
|
|
716
|
+
* to, then send it there, a few at a time.
|
|
717
|
+
* @param {Context} ctx @param {string[]} paths @param {{ site?: string }} options
|
|
718
|
+
*/
|
|
719
|
+
async function mediaAdd(ctx, paths, options) {
|
|
720
|
+
if (paths.length === 0) throw new CliError("Which files? Use: sites media add <file...> [--site <slug>]", 2);
|
|
721
|
+
|
|
722
|
+
/** @type {Array<{ shown: string, path: string, size: number, name: string }>} */
|
|
723
|
+
const files = [];
|
|
724
|
+
/** @type {Array<{ shown: string, message: string }>} */
|
|
725
|
+
const refused = [];
|
|
726
|
+
/** @type {Map<string, string>} */
|
|
727
|
+
const byName = new Map();
|
|
728
|
+
const seen = new Set();
|
|
729
|
+
for (const given of paths) {
|
|
730
|
+
const path = resolve(ctx.cwd, given);
|
|
731
|
+
if (seen.has(path)) continue;
|
|
732
|
+
seen.add(path);
|
|
733
|
+
const label = printable(given);
|
|
734
|
+
/** @type {import("node:fs").Stats} */
|
|
735
|
+
let found;
|
|
736
|
+
try {
|
|
737
|
+
found = await stat(path);
|
|
738
|
+
} catch {
|
|
739
|
+
refused.push({ shown: label, message: "There is no such file." });
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
if (found.isDirectory()) {
|
|
743
|
+
refused.push({ shown: label, message: `It is a folder. Name the files in it, for example: sites media add ${shellWord(given.replace(/\/+$/, ""))}/*.jpg` });
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
if (!found.isFile()) {
|
|
747
|
+
refused.push({ shown: label, message: "It is not a file." });
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (found.size === 0) {
|
|
751
|
+
refused.push({ shown: label, message: "The file is empty." });
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
const name = mediaName(basename(path));
|
|
755
|
+
if (name === null) {
|
|
756
|
+
refused.push({ shown: label, message: `A file like this can't be added. Large files can be ${MEDIA_EXTENSIONS.map((extension) => `.${extension}`).join(", ")}.` });
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
const twin = byName.get(name);
|
|
760
|
+
if (twin !== undefined) {
|
|
761
|
+
refused.push({ shown: label, message: `It would be kept as ${name}, the same name as ${twin}, and replace it. Rename one of them.` });
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
byName.set(name, label);
|
|
765
|
+
files.push({ shown: label, path, size: found.size, name: basename(path) });
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
for (const { shown: label, message } of refused) ctx.note(`Not added: ${label}: ${message}`);
|
|
769
|
+
if (files.length === 0) {
|
|
770
|
+
throw new CliError(paths.length === 1 ? "Nothing was added." : "None of these files could be added.");
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const slug = await siteSlug(ctx, [options.site], "sites media add <file...> --site <slug>");
|
|
774
|
+
const api = await signedIn(ctx);
|
|
775
|
+
const site = await findSite(ctx, api, slug);
|
|
776
|
+
|
|
777
|
+
/** What stopped the batch, once something has. @type {{ by: BatchStopped | null }} */
|
|
778
|
+
const halt = { by: null };
|
|
779
|
+
/** @type {Array<{ shown: string, name: string, path: string, size: number }>} */
|
|
780
|
+
const added = [];
|
|
781
|
+
let failed = refused.length;
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* One try: an address, then the file.
|
|
785
|
+
* @param {(typeof files)[number]} file
|
|
786
|
+
* @returns {Promise<{ ok: true, name: string, path: string } | { ok: false, message: string, again: boolean }>}
|
|
787
|
+
*/
|
|
788
|
+
const sendOnce = async (file) => {
|
|
789
|
+
/** @type {import("./api.mjs").MediaUpload} */
|
|
790
|
+
let started;
|
|
791
|
+
try {
|
|
792
|
+
started = await api.startMediaUpload(site.id, { name: file.name, size: file.size });
|
|
793
|
+
} catch (error) {
|
|
794
|
+
const stop = await stopsTheBatch(api, error, slug, "add files to a website");
|
|
795
|
+
if (stop) throw stop;
|
|
796
|
+
if (error instanceof ApiError) return { ok: false, message: error.message, again: error.code === "unreachable" || error.status >= 500 };
|
|
797
|
+
throw error;
|
|
798
|
+
}
|
|
799
|
+
const url = uploadAddress(ctx.environment, slug, started?.upload?.url);
|
|
800
|
+
if (url === null) {
|
|
801
|
+
return { ok: false, message: `The service named an address that is not ${slug}'s own, so the file was not sent.`, again: false };
|
|
802
|
+
}
|
|
803
|
+
const contentType = started.upload?.headers?.["Content-Type"] ?? started.media?.content_type;
|
|
804
|
+
const sent = await putFile(url, { path: file.path, size: file.size, contentType });
|
|
805
|
+
if (!sent.ok) return { ok: false, message: sent.message, again: sent.again };
|
|
806
|
+
const name = typedMediaName(sent.path ?? "") ?? typedMediaName(sent.name ?? "") ?? typedMediaName(started.media?.name ?? "") ?? file.name;
|
|
807
|
+
return { ok: true, name, path: `${MEDIA_PREFIX}${name}` };
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
await fewAtATime(files, UPLOADS_AT_ONCE, async (file) => {
|
|
811
|
+
if (halt.by) return;
|
|
812
|
+
try {
|
|
813
|
+
let result = await sendOnce(file);
|
|
814
|
+
if (!result.ok && result.again && !halt.by) {
|
|
815
|
+
await ctx.sleep(2000);
|
|
816
|
+
result = await sendOnce(file);
|
|
817
|
+
}
|
|
818
|
+
if (result.ok) {
|
|
819
|
+
added.push({ shown: file.shown, name: result.name, path: result.path, size: file.size });
|
|
820
|
+
ctx.out(`Added ${file.shown} (${sizeText(file.size)}): ${result.path}`);
|
|
821
|
+
} else {
|
|
822
|
+
failed += 1;
|
|
823
|
+
ctx.note(`Not added: ${file.shown}: ${result.message}`);
|
|
824
|
+
}
|
|
825
|
+
} catch (error) {
|
|
826
|
+
if (!(error instanceof BatchStopped)) throw error;
|
|
827
|
+
halt.by ??= error;
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
const stopped = halt.by;
|
|
832
|
+
if (stopped) {
|
|
833
|
+
const left = files.length - added.length;
|
|
834
|
+
throw new CliError(
|
|
835
|
+
[stopped.message, added.length ? `${added.length} file${added.length === 1 ? " was" : "s were"} added before that; ${left} ${left === 1 ? "was" : "were"} not.` : "No file was added."].join("\n"),
|
|
836
|
+
stopped.exitCode,
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
if (added.length > 0) {
|
|
840
|
+
const example = added.find((file) => /\.(?:png|jpe?g|gif|webp|avif)$/.test(file.name)) ?? added[0];
|
|
841
|
+
ctx.note(`In a page, use the address after the colon, for example ${/\.(?:png|jpe?g|gif|webp|avif)$/.test(example?.name ?? "") ? `<img src="${example?.path}" alt="…">` : `<a href="${example?.path}">…</a>`}.`);
|
|
842
|
+
ctx.note(`Media files are not part of the site's versions: they show on the site straight away, and pushing or publishing does not change them.`);
|
|
843
|
+
}
|
|
844
|
+
if (failed > 0) {
|
|
845
|
+
ctx.note(`${added.length} of ${added.length + failed} added.`);
|
|
846
|
+
return 1;
|
|
847
|
+
}
|
|
848
|
+
return 0;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/** @param {Context} ctx @param {{ site?: string, json?: boolean }} options */
|
|
852
|
+
async function mediaList(ctx, options) {
|
|
853
|
+
const slug = await siteSlug(ctx, [options.site], "sites media list --site <slug>");
|
|
854
|
+
const api = await signedIn(ctx);
|
|
855
|
+
const site = await findSite(ctx, api, slug);
|
|
856
|
+
/** @type {import("./api.mjs").MediaList} */
|
|
857
|
+
let found;
|
|
858
|
+
try {
|
|
859
|
+
found = await api.mediaFiles(site.id);
|
|
860
|
+
} catch (error) {
|
|
861
|
+
throw (await keyMayNot(api, error, "see a website's media files")) ?? friendly(error, slug);
|
|
862
|
+
}
|
|
863
|
+
if (options.json) {
|
|
864
|
+
ctx.out(jsonText({ site: slug, ...found }, 2));
|
|
865
|
+
return 0;
|
|
866
|
+
}
|
|
867
|
+
if (!found.available) {
|
|
868
|
+
ctx.out(`Large files can't be added to websites on ${ctx.environment.name} yet.`);
|
|
869
|
+
return 0;
|
|
870
|
+
}
|
|
871
|
+
if (found.files.length === 0) {
|
|
872
|
+
ctx.out(`${slug} has no media files yet. Add large pictures, video, audio and PDFs with: sites media add <file...>`);
|
|
873
|
+
} else {
|
|
874
|
+
const rows = found.files.map((file) => [String(file.path ?? `${MEDIA_PREFIX}${file.name}`), sizeText(Number(file.size))]);
|
|
875
|
+
for (const line of table([["ADDRESS", "SIZE"], ...rows])) ctx.out(line);
|
|
876
|
+
}
|
|
877
|
+
const usage = found.usage ?? {};
|
|
878
|
+
if (typeof usage.bytes === "number" && typeof usage.max_bytes === "number") {
|
|
879
|
+
const files = Number(usage.files ?? found.files.length);
|
|
880
|
+
const most = typeof usage.max_file_bytes === "number" ? `; at most ${sizeText(usage.max_file_bytes)} a file` : "";
|
|
881
|
+
ctx.note(`${files} file${files === 1 ? "" : "s"}, ${sizeText(usage.bytes)} of ${sizeText(usage.max_bytes)}${typeof usage.max_files === "number" ? ` and ${usage.max_files} files` : ""}${most}.`);
|
|
882
|
+
}
|
|
883
|
+
return 0;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/** @param {Context} ctx @param {string[]} typed @param {{ site?: string }} options */
|
|
887
|
+
async function mediaRemove(ctx, typed, options) {
|
|
888
|
+
if (typed.length === 0) throw new CliError("Which file? Use: sites media remove <name> [--site <slug>]. See them with: sites media list", 2);
|
|
889
|
+
/** @type {string[]} */
|
|
890
|
+
const names = [];
|
|
891
|
+
for (const each of typed) {
|
|
892
|
+
const name = typedMediaName(each);
|
|
893
|
+
if (name === null) throw new CliError(`"${printable(each)}" is not a media file's name, such as summer-menu.pdf or /_media/summer-menu.pdf. Nothing was deleted.`, 2);
|
|
894
|
+
if (!names.includes(name)) names.push(name);
|
|
895
|
+
}
|
|
896
|
+
const slug = await siteSlug(ctx, [options.site], "sites media remove <name> --site <slug>");
|
|
897
|
+
const api = await signedIn(ctx);
|
|
898
|
+
const site = await findSite(ctx, api, slug);
|
|
899
|
+
|
|
900
|
+
let failed = 0;
|
|
901
|
+
for (const name of names) {
|
|
902
|
+
try {
|
|
903
|
+
await api.deleteMedia(site.id, name);
|
|
904
|
+
} catch (error) {
|
|
905
|
+
const stop = await stopsTheBatch(api, error, slug, "delete a website's files");
|
|
906
|
+
if (stop) throw new CliError(stop.message, stop.exitCode);
|
|
907
|
+
if (error instanceof ApiError && error.status === 404) {
|
|
908
|
+
failed += 1;
|
|
909
|
+
ctx.note(`${name} is not one of ${slug}'s media files. See them with: sites media list`);
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
throw friendly(error, slug);
|
|
913
|
+
}
|
|
914
|
+
ctx.out(`Deleted ${MEDIA_PREFIX}${name}. A page that still uses it shows nothing there.`);
|
|
915
|
+
}
|
|
916
|
+
return failed > 0 ? 1 : 0;
|
|
917
|
+
}
|
|
918
|
+
|
|
648
919
|
/**
|
|
649
920
|
* The site a command is about: the one named (after the command, or with
|
|
650
921
|
* `--site`; both is fine when they agree), or the one whose folder this is.
|
|
@@ -917,6 +1188,104 @@ function jsonText(value, indent) {
|
|
|
917
1188
|
return JSON.stringify(value, null, indent).replace(/[\u007f-\u009f\u2028\u2029]/g, (character) => `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
918
1189
|
}
|
|
919
1190
|
|
|
1191
|
+
// ---------------------------------------------------------------- every tool of the MCP server
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* `sites <group> <command> [options]`: one tool of SuperIntern's MCP server,
|
|
1195
|
+
* the same one Claude is given, read from the server's own list (tools.mjs).
|
|
1196
|
+
* With no command, or `--help`, what the group (or the command) takes.
|
|
1197
|
+
* @param {Context} ctx @param {string} group "crm" or "site"
|
|
1198
|
+
* @param {string[]} argv everything typed after the group, but --env, --json and --help
|
|
1199
|
+
* @param {{ json?: boolean, help?: boolean, stdin?: () => string }} [options]
|
|
1200
|
+
*/
|
|
1201
|
+
export async function tool(ctx, group, argv, options = {}) {
|
|
1202
|
+
const key = await ctx.keys.get(ctx.environment.name).catch(() => null);
|
|
1203
|
+
const api = key ? tidied(ctx.api(key)) : null;
|
|
1204
|
+
const signIn = `sites login${ctx.environment.name === "test" ? "" : ` --env ${ctx.environment.name}`}`;
|
|
1205
|
+
|
|
1206
|
+
/** @type {import("./mcp.mjs").McpTool[] | null} */
|
|
1207
|
+
let tools = null;
|
|
1208
|
+
let where = "";
|
|
1209
|
+
if (api) {
|
|
1210
|
+
try {
|
|
1211
|
+
tools = await api.mcp.tools();
|
|
1212
|
+
} catch (error) {
|
|
1213
|
+
if (!options.help && !(argv.length === 0)) throw friendly(error);
|
|
1214
|
+
where = `This list was not read from ${ctx.environment.name} (${friendly(error).message}), so it may not be what your key can use.`;
|
|
1215
|
+
}
|
|
1216
|
+
} else {
|
|
1217
|
+
where = `This is every command there is. Which ones your key may use shows once you sign in: ${signIn}`;
|
|
1218
|
+
}
|
|
1219
|
+
tools ??= knownTools();
|
|
1220
|
+
|
|
1221
|
+
/** @type {string[]} */
|
|
1222
|
+
const words = [];
|
|
1223
|
+
for (const word of argv) {
|
|
1224
|
+
if (word.startsWith("-")) break;
|
|
1225
|
+
words.push(word);
|
|
1226
|
+
}
|
|
1227
|
+
const found = words.length ? findTool(tools, group, words) : null;
|
|
1228
|
+
if (!found) {
|
|
1229
|
+
if (words.length === 0) {
|
|
1230
|
+
ctx.out(groupHelp(group, tools, where));
|
|
1231
|
+
return options.help ? 0 : 2;
|
|
1232
|
+
}
|
|
1233
|
+
const typed = words.join(" ");
|
|
1234
|
+
const elsewhere = knownTools().find((each) => commandOf(each.name).group === group && findTool([each], group, words));
|
|
1235
|
+
if (elsewhere && api) {
|
|
1236
|
+
throw new CliError(`Your key can't use \`sites ${group} ${commandOf(elsewhere.name).command}\`. Ask a team admin for a key that may (Website, Keys), then: ${signIn}`, 3);
|
|
1237
|
+
}
|
|
1238
|
+
throw new CliError(`\`sites ${group} ${typed}\` is not a command. See them all with: sites ${group} --help`, 2);
|
|
1239
|
+
}
|
|
1240
|
+
const chosen = found.tool;
|
|
1241
|
+
if (options.help) {
|
|
1242
|
+
ctx.out(toolHelp(chosen));
|
|
1243
|
+
return 0;
|
|
1244
|
+
}
|
|
1245
|
+
if (!api) throw new CliError(`No key is saved for ${ctx.environment.name}. Run: ${signIn}`);
|
|
1246
|
+
|
|
1247
|
+
/** @type {Record<string, unknown>} */
|
|
1248
|
+
let args;
|
|
1249
|
+
try {
|
|
1250
|
+
const parsed = argumentsFrom(chosen, argv.slice(found.used), { stdin: options.stdin });
|
|
1251
|
+
const here = "site" in (chosen.inputSchema?.properties ?? {}) ? await siteHere(ctx) : null;
|
|
1252
|
+
args = completeArguments(chosen, parsed.args, parsed.positionals, { site: here?.slug ?? null });
|
|
1253
|
+
} catch (error) {
|
|
1254
|
+
if (error instanceof ArgumentError) throw new CliError(error.message, 2);
|
|
1255
|
+
throw error;
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
/** @type {import("./mcp.mjs").McpAnswer} */
|
|
1259
|
+
let answer;
|
|
1260
|
+
try {
|
|
1261
|
+
answer = await api.mcp.call(chosen.name, args);
|
|
1262
|
+
} catch (error) {
|
|
1263
|
+
if (error instanceof ApiError && error.code === "insufficient_scope") {
|
|
1264
|
+
throw new CliError(`Your key may not do that. Ask a team admin for a key that may (Website, Keys), then: ${signIn}`, 3);
|
|
1265
|
+
}
|
|
1266
|
+
if (error instanceof ApiError && error.code === "not_authenticated") throw friendly(error);
|
|
1267
|
+
// The tool's own words, written for a person (and for Claude).
|
|
1268
|
+
throw new CliError(error instanceof ApiError ? error.message : String(error));
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
if (options.json) {
|
|
1272
|
+
// A tool that answers only in words: those, as a JSON list, rather than null.
|
|
1273
|
+
if (answer.data === null || answer.data === undefined) {
|
|
1274
|
+
ctx.out(jsonText(answer.text, 2));
|
|
1275
|
+
return 0;
|
|
1276
|
+
}
|
|
1277
|
+
ctx.out(jsonText(answer.data, 2));
|
|
1278
|
+
for (const text of answer.text) ctx.note(printableLines(text));
|
|
1279
|
+
return 0;
|
|
1280
|
+
}
|
|
1281
|
+
if (answer.data !== null && answer.data !== undefined) for (const line of render(answer.data)) ctx.out(line);
|
|
1282
|
+
for (const text of answer.text) {
|
|
1283
|
+
ctx.out("");
|
|
1284
|
+
ctx.out(printableLines(text));
|
|
1285
|
+
}
|
|
1286
|
+
return 0;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
920
1289
|
// ---------------------------------------------------------------- waiting for a build
|
|
921
1290
|
|
|
922
1291
|
/**
|
|
@@ -932,13 +1301,15 @@ async function waitForBuild(ctx, api, site, options) {
|
|
|
932
1301
|
let commit = options.commit ?? head?.commit;
|
|
933
1302
|
const deadline = ctx.now() + (options.timeoutSeconds ?? 900) * 1000;
|
|
934
1303
|
let said = "";
|
|
1304
|
+
let waited = false;
|
|
935
1305
|
const say = (/** @type {string} */ line) => {
|
|
936
1306
|
if (line !== said) ctx.note(line);
|
|
937
1307
|
said = line;
|
|
938
1308
|
};
|
|
939
1309
|
|
|
940
1310
|
for (;;) {
|
|
941
|
-
|
|
1311
|
+
// The first look answers at once; after that the server waits for the build.
|
|
1312
|
+
const { versions, builds } = await api.readSite(site.id, { wait: waited }).catch((error) => {
|
|
942
1313
|
throw friendly(error, slug);
|
|
943
1314
|
});
|
|
944
1315
|
|
|
@@ -947,7 +1318,9 @@ async function waitForBuild(ctx, api, site, options) {
|
|
|
947
1318
|
const wanted = commit;
|
|
948
1319
|
const matches = (/** @type {string} */ sha) => wanted !== undefined && (sha === wanted || sha.startsWith(wanted));
|
|
949
1320
|
|
|
950
|
-
const
|
|
1321
|
+
const pushed = builds?.find((candidate) => matches(candidate.commit_sha));
|
|
1322
|
+
// Its version, also when newer CRM rebuilds pushed it past the latest few by commit.
|
|
1323
|
+
const version = versions.find((candidate) => matches(candidate.commit_sha)) ?? versions.find((candidate) => pushed?.version_id && candidate.id === pushed.version_id);
|
|
951
1324
|
if (version?.state === "ready") {
|
|
952
1325
|
ctx.out(`Built: version ${version.number} (commit ${short(version.commit_sha)}) passed its checks and is ready.`);
|
|
953
1326
|
ctx.out(`Put it online with: sites publish ${slug}`);
|
|
@@ -965,7 +1338,7 @@ async function waitForBuild(ctx, api, site, options) {
|
|
|
965
1338
|
return 1;
|
|
966
1339
|
}
|
|
967
1340
|
|
|
968
|
-
const build =
|
|
1341
|
+
const build = pushed;
|
|
969
1342
|
if (build?.state === "failed") {
|
|
970
1343
|
ctx.out(`The build of commit ${short(build.commit_sha)} failed${whyFailed(build)}.`);
|
|
971
1344
|
for (const line of failureLines(build)) ctx.out(` ${line}`);
|
|
@@ -989,6 +1362,7 @@ async function waitForBuild(ctx, api, site, options) {
|
|
|
989
1362
|
: `Waiting for the build of commit ${short(wanted)} to start…`,
|
|
990
1363
|
);
|
|
991
1364
|
await ctx.sleep(5000);
|
|
1365
|
+
waited = true;
|
|
992
1366
|
}
|
|
993
1367
|
}
|
|
994
1368
|
|
|
@@ -1019,6 +1393,8 @@ function tidied(api) {
|
|
|
1019
1393
|
...Object.fromEntries(Object.entries(api).map(([name, call]) => [name, clean(/** @type {any} */ (call))])),
|
|
1020
1394
|
dataPass: api.dataPass,
|
|
1021
1395
|
data: api.data,
|
|
1396
|
+
// What a tool answers is shown by tools.mjs, which cleans what it prints.
|
|
1397
|
+
mcp: api.mcp,
|
|
1022
1398
|
});
|
|
1023
1399
|
}
|
|
1024
1400
|
|