@crawl82/cli 0.0.0-stage → 0.6.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/AGENTS.md +259 -0
- package/README.md +120 -2
- package/dist/api-types.generated.js +12 -0
- package/dist/api-types.generated.js.map +1 -0
- package/dist/commands/api.js +111 -0
- package/dist/commands/api.js.map +1 -0
- package/dist/commands/audit.js +200 -0
- package/dist/commands/audit.js.map +1 -0
- package/dist/commands/auth.js +284 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/completion.js +120 -0
- package/dist/commands/completion.js.map +1 -0
- package/dist/commands/doctor.js +141 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/feedback.js +39 -0
- package/dist/commands/feedback.js.map +1 -0
- package/dist/commands/help-json.js +145 -0
- package/dist/commands/help-json.js.map +1 -0
- package/dist/commands/mcp.js +133 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/read.js +107 -0
- package/dist/commands/read.js.map +1 -0
- package/dist/commands/sites.js +621 -0
- package/dist/commands/sites.js.map +1 -0
- package/dist/commands/workspaces.js +66 -0
- package/dist/commands/workspaces.js.map +1 -0
- package/dist/credentials.js +151 -0
- package/dist/credentials.js.map +1 -0
- package/dist/errors.js +150 -0
- package/dist/errors.js.map +1 -0
- package/dist/exit-codes.js +82 -0
- package/dist/exit-codes.js.map +1 -0
- package/dist/http.js +291 -0
- package/dist/http.js.map +1 -0
- package/dist/index.js +164 -0
- package/dist/index.js.map +1 -0
- package/dist/keychain.js +185 -0
- package/dist/keychain.js.map +1 -0
- package/dist/oauth/browser.js +33 -0
- package/dist/oauth/browser.js.map +1 -0
- package/dist/oauth/loopback.js +13 -0
- package/dist/oauth/loopback.js.map +1 -0
- package/dist/oauth/pkce.js +119 -0
- package/dist/oauth/pkce.js.map +1 -0
- package/dist/oauth/server.js +281 -0
- package/dist/oauth/server.js.map +1 -0
- package/dist/operations.generated.js +6712 -0
- package/dist/operations.generated.js.map +1 -0
- package/dist/output.js +161 -0
- package/dist/output.js.map +1 -0
- package/dist/refresh-lock.js +84 -0
- package/dist/refresh-lock.js.map +1 -0
- package/dist/run.js +188 -0
- package/dist/run.js.map +1 -0
- package/dist/session.js +181 -0
- package/dist/session.js.map +1 -0
- package/dist/version.js +34 -0
- package/dist/version.js.map +1 -0
- package/package.json +47 -4
package/dist/run.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single error boundary.
|
|
3
|
+
*
|
|
4
|
+
* Every command body runs inside `run()`, so there is exactly one place that
|
|
5
|
+
* decides how a failure is printed and which exit code it produces. A command
|
|
6
|
+
* that wants to fail simply throws.
|
|
7
|
+
*/
|
|
8
|
+
import readline from "node:readline";
|
|
9
|
+
import { CliError, forceRequired, toErrorReport, yesRequired } from "./errors.js";
|
|
10
|
+
import { EXIT } from "./exit-codes.js";
|
|
11
|
+
import { createPrinter, resolveOutput } from "./output.js";
|
|
12
|
+
import { createSession } from "./session.js";
|
|
13
|
+
/**
|
|
14
|
+
* Commands whose `--output` means the display mode. A download command owns a
|
|
15
|
+
* `--output <path>` of its own (that is the flag people expect for "write it
|
|
16
|
+
* here"), so its value must never be read as a display mode.
|
|
17
|
+
*/
|
|
18
|
+
export const OWNS_GLOBAL_OUTPUT_FLAG = new WeakSet();
|
|
19
|
+
/**
|
|
20
|
+
* Global flags are declared on the root *and* on every leaf, so both
|
|
21
|
+
* `crawl82 --json sites list` and `crawl82 sites list --json` work. This walks
|
|
22
|
+
* the chain root-to-leaf and merges them.
|
|
23
|
+
*
|
|
24
|
+
* `--no-retry` is merged specially: commander gives a negated option a default
|
|
25
|
+
* of `true`, so a leaf's untouched default would otherwise silently cancel a
|
|
26
|
+
* `--no-retry` typed before the subcommand. Any `false` anywhere wins.
|
|
27
|
+
*/
|
|
28
|
+
export function collectGlobalOptions(command) {
|
|
29
|
+
const chain = [];
|
|
30
|
+
for (let node = command; node; node = node.parent)
|
|
31
|
+
chain.push(node);
|
|
32
|
+
chain.reverse();
|
|
33
|
+
const merged = {};
|
|
34
|
+
for (const node of chain) {
|
|
35
|
+
const options = node.opts();
|
|
36
|
+
if (options.output !== undefined && OWNS_GLOBAL_OUTPUT_FLAG.has(node)) {
|
|
37
|
+
merged.output = options.output;
|
|
38
|
+
}
|
|
39
|
+
if (options.json !== undefined)
|
|
40
|
+
merged.json = options.json;
|
|
41
|
+
if (options.profile !== undefined)
|
|
42
|
+
merged.profile = options.profile;
|
|
43
|
+
if (options.workspace !== undefined)
|
|
44
|
+
merged.workspace = options.workspace;
|
|
45
|
+
if (options.retry === false)
|
|
46
|
+
merged.retry = false;
|
|
47
|
+
if (options.timeout !== undefined)
|
|
48
|
+
merged.timeout = options.timeout;
|
|
49
|
+
}
|
|
50
|
+
return merged;
|
|
51
|
+
}
|
|
52
|
+
export async function run(command, handler) {
|
|
53
|
+
let session;
|
|
54
|
+
try {
|
|
55
|
+
session = createSession(collectGlobalOptions(command));
|
|
56
|
+
await handler(session);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error instanceof SilentAbort) {
|
|
60
|
+
// The person answered "no" at a confirmation prompt. That is not a failure.
|
|
61
|
+
process.exitCode = EXIT.OK;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const { envelope, exitCode } = toErrorReport(error);
|
|
65
|
+
const printer = session?.printer ??
|
|
66
|
+
createPrinter(resolveOutput({}, { env: process.env, stdoutIsTTY: Boolean(process.stdout.isTTY) }));
|
|
67
|
+
printer.error(envelope);
|
|
68
|
+
process.exitCode = exitCode;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** `{ data, request_id }` → `data`. Tolerates a bare payload for robustness. */
|
|
72
|
+
export function unwrap(body) {
|
|
73
|
+
if (body && typeof body === "object" && "data" in body) {
|
|
74
|
+
return body.data;
|
|
75
|
+
}
|
|
76
|
+
return body;
|
|
77
|
+
}
|
|
78
|
+
export function paginationOf(body) {
|
|
79
|
+
const record = (body ?? {});
|
|
80
|
+
return {
|
|
81
|
+
has_more: record.has_more === true,
|
|
82
|
+
next_cursor: typeof record.next_cursor === "string" ? record.next_cursor : null,
|
|
83
|
+
limit: typeof record.limit === "number" ? record.limit : null,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Follows `next_cursor` until a list is exhausted, for `--all`.
|
|
88
|
+
*
|
|
89
|
+
* `ndjson` mode wants every row printed as it arrives rather than held in
|
|
90
|
+
* memory until the last page lands — pass `onPage` for that. Table and JSON
|
|
91
|
+
* mode want the whole set at once, which is the return value.
|
|
92
|
+
*
|
|
93
|
+
* Capped at `maxPages` (200, ~20,000 rows at the max page size) as a backstop
|
|
94
|
+
* against a cursor that never terminates; a real account is expected to hit
|
|
95
|
+
* `has_more: false` long before that.
|
|
96
|
+
*/
|
|
97
|
+
export async function collectPages(fetchPage, options = {}) {
|
|
98
|
+
const maxPages = options.maxPages ?? 200;
|
|
99
|
+
const all = [];
|
|
100
|
+
let cursor = options.startCursor;
|
|
101
|
+
for (let page = 0; page < maxPages; page++) {
|
|
102
|
+
const result = await fetchPage(cursor);
|
|
103
|
+
all.push(...result.rows);
|
|
104
|
+
options.onPage?.(result.rows, page);
|
|
105
|
+
if (!result.has_more || !result.next_cursor)
|
|
106
|
+
return { rows: all, pages: page + 1 };
|
|
107
|
+
cursor = result.next_cursor;
|
|
108
|
+
}
|
|
109
|
+
throw new CliError(`Stopped after fetching ${maxPages} pages with --all, that is an unusually large result set. Narrow the filters, or page manually with --cursor.`, { exitCode: EXIT.API_ERROR, code: "internal_error", type: "api_error" });
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Destructive commands ask before acting. When there is no terminal to ask,
|
|
113
|
+
* they fail with an error naming `--force` rather than hanging on a prompt
|
|
114
|
+
* nobody will ever answer — that hang is the classic way an agent burns a
|
|
115
|
+
* timeout budget.
|
|
116
|
+
*/
|
|
117
|
+
export async function confirmDestructive(session, question, options) {
|
|
118
|
+
if (options.force)
|
|
119
|
+
return;
|
|
120
|
+
if (!session.canPrompt || !session.printer.resolution.interactive) {
|
|
121
|
+
throw forceRequired(options.command);
|
|
122
|
+
}
|
|
123
|
+
const answer = await ask(`${question} [y/N] `);
|
|
124
|
+
if (!/^y(es)?$/i.test(answer.trim())) {
|
|
125
|
+
session.printer.note("Cancelled.");
|
|
126
|
+
process.exitCode = EXIT.OK;
|
|
127
|
+
throw new SilentAbort();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** The command as it was typed, without the program name, quoted so it can be run again as printed. */
|
|
131
|
+
export function typedCommand(command, drop = []) {
|
|
132
|
+
let root = command;
|
|
133
|
+
while (root.parent)
|
|
134
|
+
root = root.parent;
|
|
135
|
+
return root.rawArgs
|
|
136
|
+
.slice(2)
|
|
137
|
+
.filter((a) => !drop.includes(a))
|
|
138
|
+
.map((a) => (/^[\w@%+=:,./-]+$/.test(a) ? a : `'${a.replace(/'/g, "'\\''")}'`))
|
|
139
|
+
.join(" ");
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The same gate for a command that SPENDS rather than deletes (`sites add`, `crawls start`, a paid
|
|
143
|
+
* `recipes run`). Its own flag, `--yes`, because "force" says "I know this destroys something", and
|
|
144
|
+
* starting a crawl destroys nothing: it commits pages and credits. With no terminal and no `--yes`
|
|
145
|
+
* it fails naming the flag and the command to run again, never hanging.
|
|
146
|
+
*/
|
|
147
|
+
export async function confirmSpend(session, question, options) {
|
|
148
|
+
if (options.yes)
|
|
149
|
+
return;
|
|
150
|
+
if (!session.canPrompt || !session.printer.resolution.interactive) {
|
|
151
|
+
throw yesRequired(options.command, options.reviewCommand, options.spends);
|
|
152
|
+
}
|
|
153
|
+
const answer = await ask(`${question} [y/N] `);
|
|
154
|
+
if (!/^y(es)?$/i.test(answer.trim())) {
|
|
155
|
+
session.printer.note("Cancelled. Nothing was charged.");
|
|
156
|
+
process.exitCode = EXIT.OK;
|
|
157
|
+
throw new SilentAbort();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Thrown after a user-cancelled confirmation; produces no output and exit 0. */
|
|
161
|
+
export class SilentAbort extends Error {
|
|
162
|
+
exitCode = EXIT.OK;
|
|
163
|
+
constructor() {
|
|
164
|
+
super("cancelled");
|
|
165
|
+
this.name = "SilentAbort";
|
|
166
|
+
}
|
|
167
|
+
toEnvelope() {
|
|
168
|
+
return {
|
|
169
|
+
error: {
|
|
170
|
+
type: "invalid_request_error",
|
|
171
|
+
code: "invalid_request",
|
|
172
|
+
message: "Cancelled.",
|
|
173
|
+
docs_url: "https://crawl82.com/docs/api/errors#invalid_request",
|
|
174
|
+
},
|
|
175
|
+
request_id: null,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function ask(prompt) {
|
|
180
|
+
return new Promise((resolve) => {
|
|
181
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
|
|
182
|
+
rl.question(prompt, (answer) => {
|
|
183
|
+
rl.close();
|
|
184
|
+
resolve(answer);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=run.js.map
|
package/dist/run.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAoC,MAAM,cAAc,CAAC;AAE/E;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,OAAO,EAAW,CAAC;AAE9D;;;;;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AAC;AACL,CAAC"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-invocation context: which profile, which credential, which output
|
|
3
|
+
* mode, and a ready-to-use API client.
|
|
4
|
+
*
|
|
5
|
+
* Every command receives one of these, so no command reads `process.env` or
|
|
6
|
+
* decides on its own how to print a failure.
|
|
7
|
+
*/
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { clearCredential, configDir, loadCredential, resolveProfile, saveCredential, } from "./credentials.js";
|
|
10
|
+
import { CliError, notAuthenticated } from "./errors.js";
|
|
11
|
+
import { ApiClient } from "./http.js";
|
|
12
|
+
import { createPrinter, resolveOutput } from "./output.js";
|
|
13
|
+
import { AuthServer, defaultEndpoints } from "./oauth/server.js";
|
|
14
|
+
import { withRefreshLock } from "./refresh-lock.js";
|
|
15
|
+
import { apiBaseUrl, apiOrigin, authBaseUrl, mcpBaseUrl } from "./version.js";
|
|
16
|
+
/**
|
|
17
|
+
* A hung request is the worst outcome for an unattended caller: it produces no
|
|
18
|
+
* output at all and burns the whole budget of whatever is waiting on it. The
|
|
19
|
+
* ceiling exists so `--timeout 0` or a typo cannot switch that protection off.
|
|
20
|
+
*/
|
|
21
|
+
export function resolveTimeoutMs(value) {
|
|
22
|
+
if (value === undefined)
|
|
23
|
+
return undefined;
|
|
24
|
+
const seconds = Number(value);
|
|
25
|
+
if (!Number.isFinite(seconds) || seconds <= 0) {
|
|
26
|
+
throw new CliError(`--timeout expects a positive number of seconds, got '${value}'.`, { code: "invalid_parameter", param: "timeout" });
|
|
27
|
+
}
|
|
28
|
+
return Math.round(seconds * 1000);
|
|
29
|
+
}
|
|
30
|
+
/** An access token is used as long as it has at least this much life left. */
|
|
31
|
+
const EXPIRY_MARGIN_MS = 60_000;
|
|
32
|
+
const isFresh = (record) => !record.expires_at || record.expires_at - Date.now() > EXPIRY_MARGIN_MS;
|
|
33
|
+
export function createSession(options, env = process.env, deps = {}) {
|
|
34
|
+
const resolution = resolveOutput(options, {
|
|
35
|
+
env,
|
|
36
|
+
stdoutIsTTY: Boolean(process.stdout.isTTY),
|
|
37
|
+
});
|
|
38
|
+
const printer = createPrinter(resolution);
|
|
39
|
+
const profile = resolveProfile(options.profile, env);
|
|
40
|
+
let cachedCredential;
|
|
41
|
+
const credentialDeps = { ...deps.credentials, env };
|
|
42
|
+
const peekCredential = async () => {
|
|
43
|
+
if (cachedCredential === undefined) {
|
|
44
|
+
cachedCredential = await loadCredential(profile, credentialDeps);
|
|
45
|
+
}
|
|
46
|
+
return cachedCredential;
|
|
47
|
+
};
|
|
48
|
+
const authServer = deps.authServer ?? new AuthServer(authBaseUrl(env));
|
|
49
|
+
const requireToken = async () => {
|
|
50
|
+
const resolved = await peekCredential();
|
|
51
|
+
if (!resolved)
|
|
52
|
+
throw notAuthenticated();
|
|
53
|
+
if (resolved.record.kind === "api_key") {
|
|
54
|
+
return { token: resolved.record.api_key, source: resolved.source, kind: "api_key" };
|
|
55
|
+
}
|
|
56
|
+
if (isFresh(resolved.record)) {
|
|
57
|
+
return { token: resolved.record.access_token, source: resolved.source, kind: "oauth" };
|
|
58
|
+
}
|
|
59
|
+
if (!resolved.record.refresh_token)
|
|
60
|
+
throw notAuthenticated();
|
|
61
|
+
// Refreshing rotates the refresh token, so two processes refreshing at the
|
|
62
|
+
// same time cannot both win. One at a time, and whoever comes second finds
|
|
63
|
+
// the credential the first already saved.
|
|
64
|
+
const lockPath = path.join(configDir(env), `refresh-${profile.replace(/[^A-Za-z0-9._-]/g, "_")}.lock`);
|
|
65
|
+
return withRefreshLock(lockPath, () => refreshStored(resolved), deps.lock);
|
|
66
|
+
};
|
|
67
|
+
const refreshLockPath = () => path.join(configDir(env), `refresh-${profile.replace(/[^A-Za-z0-9._-]/g, "_")}.lock`);
|
|
68
|
+
/** Called with the refresh lock held (or after it timed out). */
|
|
69
|
+
const refreshStored = async (seen) => {
|
|
70
|
+
let current = seen;
|
|
71
|
+
// At most one retry: the second pass exists only for "another process
|
|
72
|
+
// rotated the token while this request was in flight".
|
|
73
|
+
for (let attempt = 0;; attempt++) {
|
|
74
|
+
// Re-read: the process holding the lock before us may have refreshed.
|
|
75
|
+
const latest = await loadCredential(profile, credentialDeps);
|
|
76
|
+
if (latest && latest.record.kind === "oauth") {
|
|
77
|
+
current = latest;
|
|
78
|
+
cachedCredential = latest;
|
|
79
|
+
}
|
|
80
|
+
const record = current.record;
|
|
81
|
+
if (record.kind !== "oauth")
|
|
82
|
+
throw notAuthenticated();
|
|
83
|
+
if (isFresh(record)) {
|
|
84
|
+
return { token: record.access_token, source: current.source, kind: "oauth" };
|
|
85
|
+
}
|
|
86
|
+
if (!record.refresh_token)
|
|
87
|
+
throw notAuthenticated();
|
|
88
|
+
const tokenEndpoint = record.token_endpoint ?? defaultEndpoints(authBaseUrl(env)).token_endpoint;
|
|
89
|
+
let refreshed;
|
|
90
|
+
try {
|
|
91
|
+
refreshed = await authServer.refresh(tokenEndpoint, record.refresh_token);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (error instanceof CliError && error.code === "api_key_revoked") {
|
|
95
|
+
// `invalid_grant` is either a real revocation or the loser of a race:
|
|
96
|
+
// another process rotated the token between our read and our request.
|
|
97
|
+
// The store tells them apart, because the winner saved a new refresh
|
|
98
|
+
// token before releasing the lock.
|
|
99
|
+
const after = await loadCredential(profile, credentialDeps);
|
|
100
|
+
const rotatedByAnother = after?.record.kind === "oauth" &&
|
|
101
|
+
after.record.refresh_token !== undefined &&
|
|
102
|
+
after.record.refresh_token !== record.refresh_token;
|
|
103
|
+
if (rotatedByAnother && attempt === 0)
|
|
104
|
+
continue;
|
|
105
|
+
// A revoked or expired grant can never be refreshed again, so keeping
|
|
106
|
+
// it on disk leaves every later command retrying the same doomed
|
|
107
|
+
// request and reporting a confusing failure. Dropping it makes `auth
|
|
108
|
+
// status` tell the truth and turns the next command into a plain
|
|
109
|
+
// "not logged in".
|
|
110
|
+
await clearCredential(profile, credentialDeps);
|
|
111
|
+
cachedCredential = null;
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
const updated = {
|
|
116
|
+
...record,
|
|
117
|
+
access_token: refreshed.access_token,
|
|
118
|
+
refresh_token: refreshed.refresh_token ?? record.refresh_token,
|
|
119
|
+
expires_at: refreshed.expires_in ? Date.now() + refreshed.expires_in * 1000 : undefined,
|
|
120
|
+
};
|
|
121
|
+
await saveCredential(profile, updated, credentialDeps);
|
|
122
|
+
cachedCredential = { ...current, record: updated };
|
|
123
|
+
return { token: refreshed.access_token, source: current.source, kind: "oauth" };
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const resolveWorkspace = async () => {
|
|
127
|
+
const resolved = await peekCredential();
|
|
128
|
+
// An explicit choice is always sent, even with an API key: the API refuses a
|
|
129
|
+
// key's request for another workspace (403), where dropping the header would
|
|
130
|
+
// run a write in the key's own workspace instead of the one asked for.
|
|
131
|
+
const explicit = options.workspace?.trim() || env.CRAWL82_WORKSPACE?.trim();
|
|
132
|
+
if (explicit)
|
|
133
|
+
return explicit;
|
|
134
|
+
if (resolved?.record.kind === "oauth" && resolved.record.workspace_id) {
|
|
135
|
+
return resolved.record.workspace_id;
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
};
|
|
139
|
+
const setDefaultWorkspace = async (workspaceId) => {
|
|
140
|
+
await withRefreshLock(refreshLockPath(), async () => {
|
|
141
|
+
// Re-read inside the refresh lock so we never overwrite a freshly rotated refresh token.
|
|
142
|
+
const latest = (await loadCredential(profile, credentialDeps)) ?? (await peekCredential());
|
|
143
|
+
if (!latest)
|
|
144
|
+
throw notAuthenticated();
|
|
145
|
+
if (latest.record.kind !== "oauth") {
|
|
146
|
+
throw new CliError("API keys are bound to one workspace. Sign in with 'crawl82 auth login' to switch workspaces.", { code: "feature_not_available" });
|
|
147
|
+
}
|
|
148
|
+
const updated = {
|
|
149
|
+
...latest.record,
|
|
150
|
+
workspace_id: workspaceId ?? undefined,
|
|
151
|
+
};
|
|
152
|
+
await saveCredential(profile, updated, credentialDeps);
|
|
153
|
+
cachedCredential = { ...latest, record: updated };
|
|
154
|
+
}, deps.lock);
|
|
155
|
+
};
|
|
156
|
+
const client = new ApiClient({
|
|
157
|
+
baseUrl: apiBaseUrl(env),
|
|
158
|
+
getToken: async () => (await requireToken()).token,
|
|
159
|
+
getWorkspace: resolveWorkspace,
|
|
160
|
+
retry: options.retry !== false,
|
|
161
|
+
timeoutMs: resolveTimeoutMs(options.timeout),
|
|
162
|
+
onNotice: (message) => printer.warn(message),
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
profile,
|
|
166
|
+
printer,
|
|
167
|
+
apiUrl: apiBaseUrl(env),
|
|
168
|
+
apiOrigin: apiOrigin(env),
|
|
169
|
+
authUrl: authBaseUrl(env),
|
|
170
|
+
mcpUrl: mcpBaseUrl(env),
|
|
171
|
+
env,
|
|
172
|
+
requireToken,
|
|
173
|
+
peekCredential,
|
|
174
|
+
resolveWorkspace,
|
|
175
|
+
setDefaultWorkspace,
|
|
176
|
+
client,
|
|
177
|
+
authServer,
|
|
178
|
+
canPrompt: Boolean(process.stdin.isTTY),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,eAAe,EACf,SAAS,EACT,cAAc,EACd,cAAc,EACd,cAAc,GAMf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAgB,MAAM,aAAa,CAAC;AACzE,OAAO,EAAE,UAAU,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AAE,CAClC,CAAC;YACJ,CAAC;YACD,MAAM,OAAO,GAAgB;gBAC3B,GAAG,MAAM,CAAC,MAAM;gBAChB,YAAY,EAAE,WAAW,IAAI,SAAS;aACvC,CAAC;YACF,MAAM,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC;YACvD,gBAAgB,GAAG,EAAE,GAAG,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;QACpD,CAAC,EACD,IAAI,CAAC,IAAI,CACV,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;QAC3B,OAAO,EAAE,UAAU,CAAC,GAAG,CAAC;QACxB,QAAQ,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,YAAY,EAAE,CAAC,CAAC,KAAK;QAClD,YAAY,EAAE,gBAAgB;QAC9B,KAAK,EAAE,OAAO,CAAC,KAAK,KAAK,KAAK;QAC9B,SAAS,EAAE,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC;QAC5C,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;KAC7C,CAAC,CAAC;IAEH,OAAO;QACL,OAAO;QACP,OAAO;QACP,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC;QACvB,SAAS,EAAE,SAAS,CAAC,GAAG,CAAC;QACzB,OAAO,EAAE,WAAW,CAAC,GAAG,CAAC;QACzB,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC;QACvB,GAAG;QACH,YAAY;QACZ,cAAc;QACd,gBAAgB;QAChB,mBAAmB;QACnB,MAAM;QACN,UAAU;QACV,SAAS,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;KACxC,CAAC;AACJ,CAAC"}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
const pkg = require("../package.json");
|
|
4
|
+
export const PACKAGE_NAME = pkg.name;
|
|
5
|
+
export const VERSION = pkg.version;
|
|
6
|
+
/** Sent on every request, so the API can distinguish CLI traffic and flag old versions. */
|
|
7
|
+
export const USER_AGENT = `crawl82/${VERSION}`;
|
|
8
|
+
export const DEFAULT_API_URL = "https://api.crawl82.com/v1";
|
|
9
|
+
export const DEFAULT_MCP_URL = "https://mcp.crawl82.com";
|
|
10
|
+
export function apiBaseUrl(env = process.env) {
|
|
11
|
+
return (env.CRAWL82_API_URL || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
12
|
+
}
|
|
13
|
+
export function mcpBaseUrl(env = process.env) {
|
|
14
|
+
return (env.CRAWL82_MCP_URL || DEFAULT_MCP_URL).replace(/\/+$/, "");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The authorization server sits one level above the versioned API base:
|
|
18
|
+
* `https://api.crawl82.com/v1` → `https://api.crawl82.com`. `CRAWL82_AUTH_URL`
|
|
19
|
+
* overrides it for anyone running the two on different hosts.
|
|
20
|
+
*/
|
|
21
|
+
export function authBaseUrl(env = process.env) {
|
|
22
|
+
if (env.CRAWL82_AUTH_URL)
|
|
23
|
+
return env.CRAWL82_AUTH_URL.replace(/\/+$/, "");
|
|
24
|
+
const api = new URL(apiBaseUrl(env));
|
|
25
|
+
const segments = api.pathname.split("/").filter(Boolean);
|
|
26
|
+
segments.pop();
|
|
27
|
+
api.pathname = segments.length ? `/${segments.join("/")}` : "/";
|
|
28
|
+
return api.toString().replace(/\/+$/, "");
|
|
29
|
+
}
|
|
30
|
+
/** The origin of the API, where the unauthenticated meta routes live. */
|
|
31
|
+
export function apiOrigin(env = process.env) {
|
|
32
|
+
return new URL(apiBaseUrl(env)).origin;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],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AG,EAAE,CAAC;IACf,GAAG,CAAC,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;IAChE,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC5C,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,SAAS,CAAC,MAAyB,OAAO,CAAC,GAAG;IAC5D,OAAO,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACzC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawl82/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "The official Crawl82 CLI: crawl a site, read its findings and Search Console figures, get the fixes worth doing first and run SEO and AI-search recipes, from a terminal, a CI job or an AI agent.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"crawl82": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"AGENTS.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20.0.0"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.build.json && node -e \"require('fs').chmodSync('dist/index.js', 0o755)\"",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"generate:types": "node scripts/generate-api-types.mjs",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run test:package",
|
|
25
|
+
"test:package": "npm run build && node scripts/smoke-package.mjs"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"crawl82",
|
|
29
|
+
"cli",
|
|
30
|
+
"api",
|
|
31
|
+
"mcp",
|
|
32
|
+
"agent",
|
|
33
|
+
"automation"
|
|
34
|
+
],
|
|
35
|
+
"author": "REPLACE_ME_AUTHOR",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"homepage": "https://crawl82.com",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"commander": "^14.0.3"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^22.10.0",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
44
|
+
"vitest": "^4.1.11"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
}
|
|
49
|
+
}
|