@diagonal-hq/cli 0.38.2 → 0.38.3
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/README.md +1 -1
- package/dist/check-data.json +1 -0
- package/dist/cli-assets-cli-VQBXYEIZ.mjs +386 -0
- package/dist/cli-check-data-GHZW7MSI.mjs +17893 -0
- package/dist/cli-chunk-A7Q2M4ZU.mjs +1052 -0
- package/dist/cli-chunk-F2J44UIS.mjs +39865 -0
- package/dist/cli-chunk-HWHRAOWD.mjs +24035 -0
- package/dist/cli-chunk-IJTP2OMH.mjs +212 -0
- package/dist/cli-chunk-LRS5IEI3.mjs +48 -0
- package/dist/cli-chunk-T6442V5V.mjs +7463 -0
- package/dist/cli-chunk-WPC3DBHX.mjs +52 -0
- package/dist/cli-chunk-XMVYLKEB.mjs +359 -0
- package/dist/cli-chunk-YIPLFIKL.mjs +63 -0
- package/dist/cli-files.generated-WAU3U62D.mjs +61 -0
- package/dist/cli-sdk-files.generated-HAH7DJPZ.mjs +1429 -0
- package/dist/cli-skill-file-TEJUXCJB.mjs +262 -0
- package/dist/cli-workflows-E2QBBI3W.mjs +462 -0
- package/dist/cli.mjs +388 -99396
- package/dist/script-sdk.json +1 -0
- package/package.json +7 -4
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { createRequire as __diagonalCreateRequire } from 'node:module';
|
|
2
|
+
const require = __diagonalCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
ASSETS_LIST_USAGE,
|
|
5
|
+
ASSETS_UPLOAD_USAGE,
|
|
6
|
+
credentials,
|
|
7
|
+
errorText,
|
|
8
|
+
formatDiagnostic,
|
|
9
|
+
parseFlags,
|
|
10
|
+
request
|
|
11
|
+
} from "./cli-chunk-XMVYLKEB.mjs";
|
|
12
|
+
import {
|
|
13
|
+
assetKeyMessage,
|
|
14
|
+
assetLibraryMessage,
|
|
15
|
+
buildAssets,
|
|
16
|
+
isAssetKey
|
|
17
|
+
} from "./cli-chunk-YIPLFIKL.mjs";
|
|
18
|
+
import {
|
|
19
|
+
SourceError,
|
|
20
|
+
defaultExportOf,
|
|
21
|
+
diagnosticFromError,
|
|
22
|
+
expectDefineCall,
|
|
23
|
+
parseSource,
|
|
24
|
+
staticObject,
|
|
25
|
+
traverse
|
|
26
|
+
} from "./cli-chunk-F2J44UIS.mjs";
|
|
27
|
+
import {
|
|
28
|
+
diagnostic
|
|
29
|
+
} from "./cli-chunk-A7Q2M4ZU.mjs";
|
|
30
|
+
import "./cli-chunk-LRS5IEI3.mjs";
|
|
31
|
+
|
|
32
|
+
// scripts/lib/runtime-build/assets-cli.mjs
|
|
33
|
+
import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
34
|
+
import { basename, join, resolve } from "node:path";
|
|
35
|
+
|
|
36
|
+
// scripts/lib/runtime-build/assets-file.mjs
|
|
37
|
+
var traverse2 = traverse.default ?? traverse;
|
|
38
|
+
function readAssetEntries(source) {
|
|
39
|
+
return source === null ? [] : buildAssets(parseSource(source, { jsx: false }), null);
|
|
40
|
+
}
|
|
41
|
+
function applyEdits(source, edits) {
|
|
42
|
+
for (const { start, end = start, text } of edits.sort((a, b) => b.start - a.start)) {
|
|
43
|
+
source = source.slice(0, start) + text + source.slice(end);
|
|
44
|
+
}
|
|
45
|
+
return source;
|
|
46
|
+
}
|
|
47
|
+
function addAssetImport(ast, source, edits) {
|
|
48
|
+
const imports = ast.program.body.filter(
|
|
49
|
+
(node) => node.type === "ImportDeclaration" && node.source.value === "@diagonal-hq/cli"
|
|
50
|
+
);
|
|
51
|
+
const hasAsset = imports.some(
|
|
52
|
+
(node) => node.importKind !== "type" && node.specifiers.some(
|
|
53
|
+
(specifier) => specifier.type === "ImportSpecifier" && specifier.importKind !== "type" && specifier.imported.name === "asset" && specifier.local.name === "asset"
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
if (hasAsset) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const namedImport = imports.find(
|
|
60
|
+
(node) => node.importKind !== "type" && node.specifiers.some((specifier) => specifier.type === "ImportSpecifier")
|
|
61
|
+
);
|
|
62
|
+
if (namedImport) {
|
|
63
|
+
const brace = source.indexOf("{", namedImport.start);
|
|
64
|
+
edits.push({ start: brace + 1, text: " asset," });
|
|
65
|
+
} else {
|
|
66
|
+
edits.push({
|
|
67
|
+
start: ast.program.interpreter?.end ?? 0,
|
|
68
|
+
text: "\nimport { asset } from '@diagonal-hq/cli';\n"
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function writeAssetEntry(source, key, reference) {
|
|
73
|
+
if (!isAssetKey(key)) {
|
|
74
|
+
throw new SourceError(assetKeyMessage(key));
|
|
75
|
+
}
|
|
76
|
+
const entries = readAssetEntries(source);
|
|
77
|
+
let output;
|
|
78
|
+
if (source === null) {
|
|
79
|
+
output = `import { defineAssets, asset } from '@diagonal-hq/cli';
|
|
80
|
+
|
|
81
|
+
export default defineAssets({
|
|
82
|
+
${key}: asset.version('${reference}')
|
|
83
|
+
});
|
|
84
|
+
`;
|
|
85
|
+
} else {
|
|
86
|
+
const ast = parseSource(source, { jsx: false });
|
|
87
|
+
const { declaration } = defaultExportOf(ast);
|
|
88
|
+
const [argument] = expectDefineCall(declaration, "defineAssets", 1);
|
|
89
|
+
const definition = ["TSAsExpression", "TSSatisfiesExpression"].includes(argument.type) ? argument.expression : argument;
|
|
90
|
+
const { value } = staticObject(definition, { assetVersions: true });
|
|
91
|
+
const edits = [];
|
|
92
|
+
const existing = entries.find((entry) => entry.key === key);
|
|
93
|
+
if (existing) {
|
|
94
|
+
const node = value[key].node;
|
|
95
|
+
const quote = source[node.start];
|
|
96
|
+
edits.push({ start: node.start, end: node.end, text: `${quote}${reference}${quote}` });
|
|
97
|
+
} else {
|
|
98
|
+
const last = definition.properties.at(-1);
|
|
99
|
+
const property = `${key}: asset.version('${reference}')`;
|
|
100
|
+
const newline = source.includes("\r\n") ? "\r\n" : "\n";
|
|
101
|
+
if (!last) {
|
|
102
|
+
edits.push({ start: definition.start + 1, text: `${newline} ${property}${newline}` });
|
|
103
|
+
} else {
|
|
104
|
+
let between = source.slice(last.end, definition.end - 1);
|
|
105
|
+
for (const comment of ast.comments) {
|
|
106
|
+
if (comment.start >= last.end && comment.end < definition.end) {
|
|
107
|
+
const start = comment.start - last.end;
|
|
108
|
+
between = between.slice(0, start) + " ".repeat(comment.end - comment.start) + between.slice(comment.end - last.end);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const comma = between.indexOf(",");
|
|
112
|
+
const trailingComma = comma !== -1;
|
|
113
|
+
let lineEnd = source.indexOf("\n", last.end);
|
|
114
|
+
for (const comment of ast.comments) {
|
|
115
|
+
if (comment.start >= last.end && comment.start < lineEnd && comment.end > lineEnd) {
|
|
116
|
+
lineEnd = source.indexOf("\n", comment.end);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const multiline = last.loc.end.line < definition.loc.end.line;
|
|
120
|
+
if (multiline) {
|
|
121
|
+
const lineStart = source.lastIndexOf("\n", last.start - 1) + 1;
|
|
122
|
+
const indent = source.slice(lineStart, last.start).match(/^[\t ]*/)[0];
|
|
123
|
+
if (!trailingComma) {
|
|
124
|
+
edits.push({ start: last.end, text: "," });
|
|
125
|
+
}
|
|
126
|
+
const insertBeforeBrace = lineEnd === -1 || lineEnd >= definition.end;
|
|
127
|
+
edits.push({
|
|
128
|
+
start: insertBeforeBrace ? definition.end - 1 : lineEnd + 1,
|
|
129
|
+
text: `${insertBeforeBrace ? newline : ""}${indent}${property}${trailingComma ? "," : ""}${newline}`
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
edits.push({
|
|
133
|
+
start: trailingComma ? last.end + comma + 1 : last.end,
|
|
134
|
+
text: `${trailingComma ? "" : ","} ${property}${trailingComma ? "," : ""}`
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
addAssetImport(ast, source, edits);
|
|
140
|
+
output = applyEdits(source, edits);
|
|
141
|
+
}
|
|
142
|
+
const result = readAssetEntries(output);
|
|
143
|
+
const expected = new Map(entries.map((entry) => [entry.key, entry.reference]));
|
|
144
|
+
expected.set(key, reference);
|
|
145
|
+
if (result.length !== expected.size || result.some((entry) => expected.get(entry.key) !== entry.reference)) {
|
|
146
|
+
throw new SourceError("The assets.ts edit did not preserve the asset entries.");
|
|
147
|
+
}
|
|
148
|
+
return output;
|
|
149
|
+
}
|
|
150
|
+
var REFERENCE_MEMBERS = Object.freeze({ url: ".url", asset: "", key: ".key" });
|
|
151
|
+
var CSS_FORM = "css";
|
|
152
|
+
var ASSET_REFERENCE_FORMS = Object.freeze([...Object.keys(REFERENCE_MEMBERS), CSS_FORM]);
|
|
153
|
+
|
|
154
|
+
// scripts/lib/runtime-build/assets-cli.mjs
|
|
155
|
+
var NIL_REFERENCE = "00000000-0000-0000-0000-000000000000";
|
|
156
|
+
var MISSING_VERSION = "This version is not in the Application Asset library.";
|
|
157
|
+
function readManifest(path) {
|
|
158
|
+
try {
|
|
159
|
+
return readFileSync(path, "utf8");
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error.code === "ENOENT") {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function printFileError(error, stderr) {
|
|
168
|
+
if (error instanceof SourceError || error instanceof SyntaxError) {
|
|
169
|
+
stderr.write(`${formatDiagnostic(diagnosticFromError("assets.ts", error))}
|
|
170
|
+
`);
|
|
171
|
+
} else if (error.code) {
|
|
172
|
+
stderr.write(`Could not access assets.ts: ${error.message}
|
|
173
|
+
`);
|
|
174
|
+
} else {
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async function uploadAsset(args, { cwd, env, stdout, stderr }) {
|
|
179
|
+
const parsed = parseFlags(args, ["--key"], ["--no-write", "--json"]);
|
|
180
|
+
const key = parsed.values?.["--key"];
|
|
181
|
+
const noWrite = parsed.booleans?.has("--no-write");
|
|
182
|
+
const usageError = parsed.error ?? (parsed.positional.length !== 1 ? "Give one file to upload" : key !== void 0 && !isAssetKey(key) ? assetKeyMessage(key) : !noWrite && key === void 0 ? "--key is required unless --no-write is given" : null);
|
|
183
|
+
if (usageError !== null) {
|
|
184
|
+
stderr.write(`${usageError}
|
|
185
|
+
${ASSETS_UPLOAD_USAGE}
|
|
186
|
+
`);
|
|
187
|
+
return 2;
|
|
188
|
+
}
|
|
189
|
+
const file = resolve(cwd, parsed.positional[0]);
|
|
190
|
+
let bytes;
|
|
191
|
+
try {
|
|
192
|
+
if (!statSync(file, { throwIfNoEntry: false })?.isFile()) {
|
|
193
|
+
stderr.write(`Not a regular file: ${file}
|
|
194
|
+
`);
|
|
195
|
+
return 2;
|
|
196
|
+
}
|
|
197
|
+
bytes = readFileSync(file);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (!error.code) throw error;
|
|
200
|
+
stderr.write(`Could not read ${file}: ${error.message}
|
|
201
|
+
`);
|
|
202
|
+
return 2;
|
|
203
|
+
}
|
|
204
|
+
const manifest = join(cwd, "assets.ts");
|
|
205
|
+
let source = null;
|
|
206
|
+
let current;
|
|
207
|
+
if (!noWrite && !statSync(join(cwd, "diagonal.config.ts"), { throwIfNoEntry: false })?.isFile()) {
|
|
208
|
+
stderr.write(
|
|
209
|
+
"Run diagonal assets upload in the workspace root, the folder with diagonal.config.ts, or pass --no-write.\n"
|
|
210
|
+
);
|
|
211
|
+
return 2;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
source = readManifest(manifest);
|
|
215
|
+
const entries = readAssetEntries(source);
|
|
216
|
+
current = entries.find((entry) => entry.key === key);
|
|
217
|
+
if (!noWrite) writeAssetEntry(source, key, NIL_REFERENCE);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
printFileError(error, stderr);
|
|
220
|
+
return 1;
|
|
221
|
+
}
|
|
222
|
+
const target = credentials(env, stderr);
|
|
223
|
+
if (target === null) return 2;
|
|
224
|
+
if (await request(target, "GET", "/api/application-assets?per_page=1", void 0, stderr) === null)
|
|
225
|
+
return 1;
|
|
226
|
+
let assetId;
|
|
227
|
+
if (current) {
|
|
228
|
+
const lookup = await request(
|
|
229
|
+
target,
|
|
230
|
+
"GET",
|
|
231
|
+
`/api/application-asset-versions/${current.reference}`,
|
|
232
|
+
void 0,
|
|
233
|
+
stderr,
|
|
234
|
+
{ responseDetails: true }
|
|
235
|
+
);
|
|
236
|
+
if (lookup === null) return 1;
|
|
237
|
+
if (!lookup.ok) {
|
|
238
|
+
if (lookup.status === 404 && lookup.body.message === MISSING_VERSION) {
|
|
239
|
+
stderr.write(
|
|
240
|
+
`${formatDiagnostic(diagnostic("assets.ts", current.line, current.column, assetLibraryMessage(current.reference)))}
|
|
241
|
+
`
|
|
242
|
+
);
|
|
243
|
+
} else {
|
|
244
|
+
stderr.write(errorText(lookup.status, lookup.body));
|
|
245
|
+
}
|
|
246
|
+
return 1;
|
|
247
|
+
}
|
|
248
|
+
assetId = lookup.body.asset.id;
|
|
249
|
+
}
|
|
250
|
+
const form = new FormData();
|
|
251
|
+
form.append("file", new Blob([bytes]), basename(file));
|
|
252
|
+
const uploaded = await request(target, "POST", "/api/files/uploads", form, stderr);
|
|
253
|
+
if (uploaded === null) return 1;
|
|
254
|
+
const result = await request(
|
|
255
|
+
target,
|
|
256
|
+
"POST",
|
|
257
|
+
assetId ? `/api/application-assets/${assetId}/versions` : "/api/application-assets",
|
|
258
|
+
{ upload_key: uploaded.key },
|
|
259
|
+
stderr,
|
|
260
|
+
{ responseDetails: true }
|
|
261
|
+
);
|
|
262
|
+
if (result === null) return 1;
|
|
263
|
+
if (!result.ok) {
|
|
264
|
+
stderr.write(errorText(result.status, result.body));
|
|
265
|
+
return 1;
|
|
266
|
+
}
|
|
267
|
+
const { asset, version } = result.body;
|
|
268
|
+
let written = false;
|
|
269
|
+
let action;
|
|
270
|
+
if (!noWrite) {
|
|
271
|
+
try {
|
|
272
|
+
const latest = readManifest(manifest);
|
|
273
|
+
const existing = readAssetEntries(latest).find((entry) => entry.key === key);
|
|
274
|
+
const output = writeAssetEntry(latest, key, version.reference);
|
|
275
|
+
if (output !== latest) writeFileSync(manifest, output);
|
|
276
|
+
written = true;
|
|
277
|
+
action = existing?.reference === version.reference ? `${key} already uses this version` : existing ? `Updated ${key} in assets.ts` : `Added ${key} to assets.ts`;
|
|
278
|
+
} catch (error) {
|
|
279
|
+
printFileError(error, stderr);
|
|
280
|
+
stderr.write(
|
|
281
|
+
`${key}: asset.version('${version.reference}')
|
|
282
|
+
Do not run the upload again: a new key creates another asset.
|
|
283
|
+
`
|
|
284
|
+
);
|
|
285
|
+
return 1;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (parsed.booleans.has("--json")) {
|
|
289
|
+
stdout.write(`${JSON.stringify({ key: key ?? null, written, asset, version }, null, 2)}
|
|
290
|
+
`);
|
|
291
|
+
} else if (noWrite) {
|
|
292
|
+
stdout.write(`${version.reference}
|
|
293
|
+
`);
|
|
294
|
+
} else {
|
|
295
|
+
if (result.status === 200)
|
|
296
|
+
stdout.write(`The bytes match version ${version.number}; no new version was created.
|
|
297
|
+
`);
|
|
298
|
+
stdout.write(
|
|
299
|
+
`${key} ${version.reference} ${asset.display_name} v${version.number}
|
|
300
|
+
${action}
|
|
301
|
+
`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
return 0;
|
|
305
|
+
}
|
|
306
|
+
async function listAssets(args, { env, stdout, stderr }) {
|
|
307
|
+
const parsed = parseFlags(args, ["--search"], ["--versions", "--json"]);
|
|
308
|
+
if (parsed.error !== void 0 || parsed.positional.length > 0) {
|
|
309
|
+
stderr.write(
|
|
310
|
+
`${parsed.error ?? "diagonal assets list takes no arguments"}
|
|
311
|
+
${ASSETS_LIST_USAGE}
|
|
312
|
+
`
|
|
313
|
+
);
|
|
314
|
+
return 2;
|
|
315
|
+
}
|
|
316
|
+
const target = credentials(env, stderr);
|
|
317
|
+
if (target === null) return 2;
|
|
318
|
+
const assets = [];
|
|
319
|
+
let page = 1;
|
|
320
|
+
while (true) {
|
|
321
|
+
const query = new URLSearchParams({ per_page: "100", page: String(page) });
|
|
322
|
+
if (parsed.values["--search"] !== void 0) query.set("search", parsed.values["--search"]);
|
|
323
|
+
const body = await request(
|
|
324
|
+
target,
|
|
325
|
+
"GET",
|
|
326
|
+
`/api/application-assets?${query}`,
|
|
327
|
+
void 0,
|
|
328
|
+
stderr
|
|
329
|
+
);
|
|
330
|
+
if (body === null) return 1;
|
|
331
|
+
assets.push(...body.data);
|
|
332
|
+
if (body.pagination.current_page >= body.pagination.last_page) break;
|
|
333
|
+
page++;
|
|
334
|
+
}
|
|
335
|
+
if (parsed.booleans.has("--versions")) {
|
|
336
|
+
for (const asset of assets) {
|
|
337
|
+
const body = await request(
|
|
338
|
+
target,
|
|
339
|
+
"GET",
|
|
340
|
+
`/api/application-assets/${asset.id}`,
|
|
341
|
+
void 0,
|
|
342
|
+
stderr
|
|
343
|
+
);
|
|
344
|
+
if (body === null) return 1;
|
|
345
|
+
asset.versions = body.versions;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (parsed.booleans.has("--json")) {
|
|
349
|
+
stdout.write(`${JSON.stringify({ data: assets }, null, 2)}
|
|
350
|
+
`);
|
|
351
|
+
return 0;
|
|
352
|
+
}
|
|
353
|
+
const rows = assets.map((asset) => [
|
|
354
|
+
asset.latest_version?.reference ?? "-",
|
|
355
|
+
asset.latest_version ? `v${asset.latest_version.number} of ${asset.versions_count}` : `- of ${asset.versions_count}`,
|
|
356
|
+
asset.latest_version?.mime_type ?? "-",
|
|
357
|
+
asset.display_name
|
|
358
|
+
]);
|
|
359
|
+
const widths = [0, 1, 2].map((column) => Math.max(0, ...rows.map((row) => row[column].length)));
|
|
360
|
+
for (const [index, row] of rows.entries()) {
|
|
361
|
+
stdout.write(
|
|
362
|
+
`${row.map((value, column) => column < 3 ? value.padEnd(widths[column]) : value).join(" ")}
|
|
363
|
+
`
|
|
364
|
+
);
|
|
365
|
+
const asset = assets[index];
|
|
366
|
+
for (const version of asset.versions ?? []) {
|
|
367
|
+
stdout.write(
|
|
368
|
+
` v${version.number} ${version.reference} ${version.original_filename} ${version.bytes} bytes
|
|
369
|
+
`
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
if (asset.versions && asset.versions.length < asset.versions_count) {
|
|
373
|
+
stdout.write(` showing the newest ${asset.versions.length} of ${asset.versions_count}
|
|
374
|
+
`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
stdout.write(`${assets.length} asset${assets.length === 1 ? "" : "s"}
|
|
378
|
+
`);
|
|
379
|
+
return 0;
|
|
380
|
+
}
|
|
381
|
+
export {
|
|
382
|
+
ASSETS_LIST_USAGE,
|
|
383
|
+
ASSETS_UPLOAD_USAGE,
|
|
384
|
+
listAssets,
|
|
385
|
+
uploadAsset
|
|
386
|
+
};
|