@clarigen/cli 4.0.1 → 4.0.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm-CO92uOgU.mjs +917 -0
- package/dist/esm-CO92uOgU.mjs.map +1 -0
- package/dist/esm-CRbGFHSR.cjs +1226 -0
- package/dist/esm-CRbGFHSR.cjs.map +1 -0
- package/dist/index.cjs +47 -1365
- package/dist/index.d.cts +173 -243
- package/dist/index.d.mts +252 -0
- package/dist/index.mjs +3 -0
- package/dist/run-cli.cjs +259 -1528
- package/dist/run-cli.cjs.map +1 -1
- package/dist/run-cli.d.cts +1 -1
- package/dist/run-cli.d.mts +1 -0
- package/dist/run-cli.mjs +321 -0
- package/dist/run-cli.mjs.map +1 -0
- package/package.json +18 -7
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -322
- package/dist/index.js +0 -1291
- package/dist/index.js.map +0 -1
- package/dist/run-cli.d.ts +0 -1
- package/dist/run-cli.js +0 -1585
- package/dist/run-cli.js.map +0 -1
package/dist/index.js
DELETED
|
@@ -1,1291 +0,0 @@
|
|
|
1
|
-
// src/docs/markdown.ts
|
|
2
|
-
import { getContractName as getContractName2, getTypeString } from "@clarigen/core";
|
|
3
|
-
|
|
4
|
-
// src/logger.ts
|
|
5
|
-
import { pino } from "pino";
|
|
6
|
-
import pinoPretty from "pino-pretty";
|
|
7
|
-
var colorizedClarigen = `\x1B[33m[Clarigen]\x1B[0m`;
|
|
8
|
-
var logger = pino(
|
|
9
|
-
pinoPretty({
|
|
10
|
-
colorize: true,
|
|
11
|
-
ignore: "pid,hostname,time",
|
|
12
|
-
messageFormat: `${colorizedClarigen} {msg}`,
|
|
13
|
-
minimumLevel: "debug"
|
|
14
|
-
})
|
|
15
|
-
);
|
|
16
|
-
logger.level = "info";
|
|
17
|
-
var log = logger;
|
|
18
|
-
|
|
19
|
-
// src/docs/index.ts
|
|
20
|
-
import { spawn } from "child_process";
|
|
21
|
-
var FN_TYPES = ["read-only", "public", "private"];
|
|
22
|
-
var VAR_TYPES = ["map", "data-var", "constant"];
|
|
23
|
-
function createContractDocInfo({
|
|
24
|
-
contractSrc,
|
|
25
|
-
abi
|
|
26
|
-
}) {
|
|
27
|
-
const lines = contractSrc.split("\n");
|
|
28
|
-
let comments = [];
|
|
29
|
-
let parensCount = 0;
|
|
30
|
-
let currentFn;
|
|
31
|
-
const contract = {
|
|
32
|
-
comments: [],
|
|
33
|
-
functions: [],
|
|
34
|
-
variables: [],
|
|
35
|
-
maps: []
|
|
36
|
-
};
|
|
37
|
-
lines.forEach((line, lineNumber) => {
|
|
38
|
-
if (currentFn) {
|
|
39
|
-
currentFn.source.push(line);
|
|
40
|
-
parensCount = traceParens(line, parensCount);
|
|
41
|
-
if (parensCount === 0) {
|
|
42
|
-
pushItem(contract, currentFn);
|
|
43
|
-
currentFn = void 0;
|
|
44
|
-
}
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (isComment(line)) {
|
|
48
|
-
const comment = line.replace(/^\s*;;\s*/g, "");
|
|
49
|
-
if (contract.comments.length === lineNumber) {
|
|
50
|
-
contract.comments.push(comment);
|
|
51
|
-
} else {
|
|
52
|
-
comments.push(comment);
|
|
53
|
-
}
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const name = findItemNameFromLine(line);
|
|
57
|
-
if (typeof name === "undefined") {
|
|
58
|
-
comments = [];
|
|
59
|
-
} else {
|
|
60
|
-
const abiFn = findAbiItemByName(abi, name);
|
|
61
|
-
if (!abiFn) {
|
|
62
|
-
console.debug(`[claridoc]: Unable to find ABI for function \`${name}\`. Probably a bug.`);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
parensCount = traceParens(line, 0);
|
|
66
|
-
const metaComments = parseComments(comments, abiFn);
|
|
67
|
-
currentFn = {
|
|
68
|
-
abi: abiFn,
|
|
69
|
-
comments: metaComments,
|
|
70
|
-
startLine: lineNumber,
|
|
71
|
-
source: [line]
|
|
72
|
-
};
|
|
73
|
-
if (parensCount === 0) {
|
|
74
|
-
pushItem(contract, currentFn);
|
|
75
|
-
currentFn = void 0;
|
|
76
|
-
}
|
|
77
|
-
comments = [];
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
return contract;
|
|
81
|
-
}
|
|
82
|
-
function pushItem(contract, item) {
|
|
83
|
-
if ("args" in item.abi) {
|
|
84
|
-
contract.functions.push(item);
|
|
85
|
-
} else if ("key" in item.abi) {
|
|
86
|
-
contract.maps.push(item);
|
|
87
|
-
} else if ("access" in item.abi) {
|
|
88
|
-
contract.variables.push(item);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
function clarityNameMatcher(line) {
|
|
92
|
-
return /[\w|\-|\?|\!]+/.exec(line);
|
|
93
|
-
}
|
|
94
|
-
function findItemNameFromLine(line) {
|
|
95
|
-
const fnType = FN_TYPES.find((type3) => {
|
|
96
|
-
return line.startsWith(`(define-${type3}`);
|
|
97
|
-
});
|
|
98
|
-
if (fnType) {
|
|
99
|
-
const prefix = `(define-${fnType} (`;
|
|
100
|
-
const startString = line.slice(prefix.length);
|
|
101
|
-
const match = clarityNameMatcher(startString);
|
|
102
|
-
if (!match) {
|
|
103
|
-
console.debug(`[claridocs]: Unable to determine function name from line:
|
|
104
|
-
\`${line}\``);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
return match[0];
|
|
108
|
-
}
|
|
109
|
-
for (const type3 of VAR_TYPES) {
|
|
110
|
-
const prefix = `(define-${type3} `;
|
|
111
|
-
if (!line.startsWith(prefix)) continue;
|
|
112
|
-
const startString = line.slice(prefix.length);
|
|
113
|
-
const match = clarityNameMatcher(startString);
|
|
114
|
-
if (!match) {
|
|
115
|
-
console.debug(`[claridocs]: Unable to determine ${type3} name from line:
|
|
116
|
-
\`${line}\``);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
return match[0];
|
|
120
|
-
}
|
|
121
|
-
return void 0;
|
|
122
|
-
}
|
|
123
|
-
function findAbiItemByName(abi, name) {
|
|
124
|
-
const fn = abi.functions.find((fn2) => {
|
|
125
|
-
return fn2.name === name;
|
|
126
|
-
});
|
|
127
|
-
if (fn) return fn;
|
|
128
|
-
const map = abi.maps.find((m) => m.name === name);
|
|
129
|
-
if (map) return map;
|
|
130
|
-
const v = abi.variables.find((v2) => v2.name === name);
|
|
131
|
-
return v;
|
|
132
|
-
}
|
|
133
|
-
function isComment(line) {
|
|
134
|
-
return line.startsWith(";;");
|
|
135
|
-
}
|
|
136
|
-
function getFnName(line) {
|
|
137
|
-
const fnType = FN_TYPES.find((type3) => {
|
|
138
|
-
return line.startsWith(`(define-${type3}`);
|
|
139
|
-
});
|
|
140
|
-
if (typeof fnType === "undefined") return;
|
|
141
|
-
const prefix = `(define-${fnType} (`;
|
|
142
|
-
const startString = line.slice(prefix.length);
|
|
143
|
-
const match = clarityNameMatcher(startString);
|
|
144
|
-
if (!match) {
|
|
145
|
-
console.debug(`[claridocs]: Unable to determine function name from line:
|
|
146
|
-
\`${line}\``);
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
return match[0];
|
|
150
|
-
}
|
|
151
|
-
function traceParens(line, count) {
|
|
152
|
-
let newCount = count;
|
|
153
|
-
line.split("").forEach((char) => {
|
|
154
|
-
if (char === "(") newCount++;
|
|
155
|
-
if (char === ")") newCount--;
|
|
156
|
-
});
|
|
157
|
-
return newCount;
|
|
158
|
-
}
|
|
159
|
-
function parseComments(comments, abi) {
|
|
160
|
-
let curParam;
|
|
161
|
-
const parsed = {
|
|
162
|
-
text: [],
|
|
163
|
-
params: {}
|
|
164
|
-
};
|
|
165
|
-
comments.forEach((line) => {
|
|
166
|
-
const paramMatches = /\s*@param\s([\w|\-]+)([;|-|\s]*)(.*)/.exec(line);
|
|
167
|
-
if (paramMatches === null) {
|
|
168
|
-
if (!curParam || line.trim() === "") {
|
|
169
|
-
curParam = void 0;
|
|
170
|
-
parsed.text.push(line);
|
|
171
|
-
} else {
|
|
172
|
-
parsed.params[curParam].comments.push(line);
|
|
173
|
-
}
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (!("args" in abi)) return;
|
|
177
|
-
const [_full, name, _separator, rest] = paramMatches;
|
|
178
|
-
const arg = abi.args.find((arg2) => arg2.name === name);
|
|
179
|
-
if (!arg) {
|
|
180
|
-
console.debug(`[claridocs]: Unable to find ABI for @param ${name}`);
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
curParam = name;
|
|
184
|
-
parsed.params[curParam] = {
|
|
185
|
-
abi: arg,
|
|
186
|
-
comments: [rest]
|
|
187
|
-
};
|
|
188
|
-
});
|
|
189
|
-
if ("args" in abi) {
|
|
190
|
-
abi.args.forEach((arg) => {
|
|
191
|
-
if (!parsed.params[arg.name]) {
|
|
192
|
-
parsed.params[arg.name] = {
|
|
193
|
-
abi: arg,
|
|
194
|
-
comments: []
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
return parsed;
|
|
200
|
-
}
|
|
201
|
-
async function afterDocs(config) {
|
|
202
|
-
var _a;
|
|
203
|
-
const command = (_a = config.docs) == null ? void 0 : _a.after;
|
|
204
|
-
if (!command) return;
|
|
205
|
-
logger.debug(`Running after docs command: ${command}`);
|
|
206
|
-
const parts = command.split(" ");
|
|
207
|
-
const [cmd, ...args] = parts;
|
|
208
|
-
return new Promise((resolve3, reject) => {
|
|
209
|
-
const child = spawn(cmd, args, {
|
|
210
|
-
cwd: config.cwd,
|
|
211
|
-
stdio: "inherit"
|
|
212
|
-
});
|
|
213
|
-
child.on("error", reject);
|
|
214
|
-
child.on("exit", (code) => {
|
|
215
|
-
if (code === 0) {
|
|
216
|
-
resolve3();
|
|
217
|
-
} else {
|
|
218
|
-
reject(new Error(`Command failed with code ${code ?? "unknown"}`));
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// src/docs/markdown.ts
|
|
225
|
-
import { basename } from "path";
|
|
226
|
-
|
|
227
|
-
// src/utils.ts
|
|
228
|
-
import { getContractName, toCamelCase } from "@clarigen/core";
|
|
229
|
-
import { stat, mkdir, writeFile as fsWriteFile } from "fs/promises";
|
|
230
|
-
import { dirname, relative } from "path";
|
|
231
|
-
function encodeVariableName(name) {
|
|
232
|
-
if (/^[A-Z\-_]*$/.test(name)) return name.replaceAll("-", "_");
|
|
233
|
-
return toCamelCase(name);
|
|
234
|
-
}
|
|
235
|
-
async function fileExists(filename) {
|
|
236
|
-
try {
|
|
237
|
-
await stat(filename);
|
|
238
|
-
return true;
|
|
239
|
-
} catch (error) {
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
async function writeFile(path, contents) {
|
|
244
|
-
const dir = dirname(path);
|
|
245
|
-
await mkdir(dir, { recursive: true });
|
|
246
|
-
await fsWriteFile(path, contents, "utf-8");
|
|
247
|
-
return path;
|
|
248
|
-
}
|
|
249
|
-
function cwdRelative(path) {
|
|
250
|
-
return relative(process.cwd(), path);
|
|
251
|
-
}
|
|
252
|
-
function sortContracts(contracts) {
|
|
253
|
-
const nameSorted = [...contracts].sort((a, b) => {
|
|
254
|
-
if (getContractName(a.contract_id, false) < getContractName(b.contract_id, false)) {
|
|
255
|
-
return -1;
|
|
256
|
-
}
|
|
257
|
-
return 1;
|
|
258
|
-
});
|
|
259
|
-
return nameSorted;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// src/docs/markdown.ts
|
|
263
|
-
function generateMarkdown({
|
|
264
|
-
contract,
|
|
265
|
-
contractFile,
|
|
266
|
-
withToc = true
|
|
267
|
-
}) {
|
|
268
|
-
const contractName = getContractName2(contract.contract_id, false);
|
|
269
|
-
const doc = createContractDocInfo({
|
|
270
|
-
contractSrc: contract.source,
|
|
271
|
-
abi: contract.contract_interface
|
|
272
|
-
});
|
|
273
|
-
const functions = doc.functions.map((fn) => markdownFunction(fn, contractFile));
|
|
274
|
-
const maps = doc.maps.map((map) => markdownMap(map, contractFile));
|
|
275
|
-
const vars = doc.variables.filter((v) => v.abi.access === "variable").map((v) => markdownVar(v, contractFile));
|
|
276
|
-
const constants = doc.variables.filter((v) => v.abi.access === "constant").map((v) => markdownVar(v, contractFile));
|
|
277
|
-
let fileLine = "";
|
|
278
|
-
if (contractFile) {
|
|
279
|
-
const fileName = basename(contractFile);
|
|
280
|
-
fileLine = `
|
|
281
|
-
[\`${fileName}\`](${contractFile})`;
|
|
282
|
-
}
|
|
283
|
-
return `
|
|
284
|
-
# ${contractName}
|
|
285
|
-
${fileLine}
|
|
286
|
-
|
|
287
|
-
${doc.comments.join("\n\n")}
|
|
288
|
-
|
|
289
|
-
${withToc ? markdownTOC(doc) : ""}
|
|
290
|
-
|
|
291
|
-
## Functions
|
|
292
|
-
|
|
293
|
-
${functions.join("\n\n")}
|
|
294
|
-
|
|
295
|
-
## Maps
|
|
296
|
-
|
|
297
|
-
${maps.join("\n\n")}
|
|
298
|
-
|
|
299
|
-
## Variables
|
|
300
|
-
|
|
301
|
-
${vars.join("\n\n")}
|
|
302
|
-
|
|
303
|
-
## Constants
|
|
304
|
-
|
|
305
|
-
${constants.join("\n\n")}
|
|
306
|
-
`;
|
|
307
|
-
}
|
|
308
|
-
function markdownFunction(fn, contractFile) {
|
|
309
|
-
const params = mdParams(fn);
|
|
310
|
-
const returnType = getTypeString(fn.abi.outputs.type);
|
|
311
|
-
const paramSigs = fn.abi.args.map((arg) => {
|
|
312
|
-
return `(${arg.name} ${getTypeString(arg.type)})`;
|
|
313
|
-
});
|
|
314
|
-
const startLine = fn.startLine + 1;
|
|
315
|
-
let link = "";
|
|
316
|
-
if (contractFile) {
|
|
317
|
-
link = `[View in file](${contractFile}#L${startLine})`;
|
|
318
|
-
}
|
|
319
|
-
const source = `<details>
|
|
320
|
-
<summary>Source code:</summary>
|
|
321
|
-
|
|
322
|
-
\`\`\`clarity
|
|
323
|
-
${fn.source.join("\n")}
|
|
324
|
-
\`\`\`
|
|
325
|
-
</details>
|
|
326
|
-
`;
|
|
327
|
-
const sig = `(define-${fn.abi.access.replace("_", "-")} (${fn.abi.name} (${paramSigs.join(
|
|
328
|
-
" "
|
|
329
|
-
)}) ${returnType})`;
|
|
330
|
-
return `### ${fn.abi.name}
|
|
331
|
-
|
|
332
|
-
${link}
|
|
333
|
-
|
|
334
|
-
\`${sig}\`
|
|
335
|
-
|
|
336
|
-
${fn.comments.text.join("\n")}
|
|
337
|
-
|
|
338
|
-
${source}
|
|
339
|
-
|
|
340
|
-
${params}`;
|
|
341
|
-
}
|
|
342
|
-
function mdParams(fn) {
|
|
343
|
-
if (fn.abi.args.length === 0) return "";
|
|
344
|
-
const hasDescription = Object.values(fn.comments.params).some((p) => p.comments.length > 0);
|
|
345
|
-
const params = Object.values(fn.comments.params).map((p) => markdownParam(p, hasDescription));
|
|
346
|
-
return `**Parameters:**
|
|
347
|
-
|
|
348
|
-
| Name | Type | ${hasDescription ? "Description |" : ""}
|
|
349
|
-
| --- | --- | ${hasDescription ? "--- |" : ""}
|
|
350
|
-
${params.join("\n")}`;
|
|
351
|
-
}
|
|
352
|
-
function markdownParam(param, withDescription) {
|
|
353
|
-
const typeString = getTypeString(param.abi.type);
|
|
354
|
-
const base = `| ${param.abi.name} | ${typeString} |`;
|
|
355
|
-
if (!withDescription) return base;
|
|
356
|
-
return `${base} ${param.comments.join(" ")} |`;
|
|
357
|
-
}
|
|
358
|
-
function markdownMap(map, contractFile) {
|
|
359
|
-
const startLine = map.startLine + 1;
|
|
360
|
-
let link = "";
|
|
361
|
-
if (contractFile) {
|
|
362
|
-
link = `[View in file](${contractFile}#L${startLine})`;
|
|
363
|
-
}
|
|
364
|
-
return `### ${map.abi.name}
|
|
365
|
-
|
|
366
|
-
${map.comments.text.join("\n")}
|
|
367
|
-
|
|
368
|
-
\`\`\`clarity
|
|
369
|
-
${map.source.join("\n")}
|
|
370
|
-
\`\`\`
|
|
371
|
-
|
|
372
|
-
${link}`;
|
|
373
|
-
}
|
|
374
|
-
function markdownVar(variable, contractFile) {
|
|
375
|
-
const startLine = variable.startLine + 1;
|
|
376
|
-
let link = "";
|
|
377
|
-
if (contractFile) {
|
|
378
|
-
link = `[View in file](${contractFile}#L${startLine})`;
|
|
379
|
-
}
|
|
380
|
-
const sig = variable.abi.access === "variable" ? getTypeString(variable.abi.type) : "";
|
|
381
|
-
return `### ${variable.abi.name}
|
|
382
|
-
|
|
383
|
-
${sig}
|
|
384
|
-
|
|
385
|
-
${variable.comments.text.join("\n")}
|
|
386
|
-
|
|
387
|
-
\`\`\`clarity
|
|
388
|
-
${variable.source.join("\n")}
|
|
389
|
-
\`\`\`
|
|
390
|
-
|
|
391
|
-
${link}`;
|
|
392
|
-
}
|
|
393
|
-
function markdownTOC(contract) {
|
|
394
|
-
const publics = contract.functions.filter((fn) => fn.abi.access === "public");
|
|
395
|
-
const readOnly = contract.functions.filter((fn) => fn.abi.access === "read_only");
|
|
396
|
-
const privates = contract.functions.filter((fn) => fn.abi.access === "private");
|
|
397
|
-
const maps = contract.maps;
|
|
398
|
-
const constants = contract.variables.filter((v) => v.abi.access === "constant");
|
|
399
|
-
const vars = contract.variables.filter((v) => v.abi.access === "variable");
|
|
400
|
-
function tocLine(fn) {
|
|
401
|
-
const name = fn.abi.name;
|
|
402
|
-
return `- [\`${name}\`](#${name.toLowerCase().replaceAll("?", "")})`;
|
|
403
|
-
}
|
|
404
|
-
return `**Public functions:**
|
|
405
|
-
|
|
406
|
-
${publics.map(tocLine).join("\n")}
|
|
407
|
-
|
|
408
|
-
**Read-only functions:**
|
|
409
|
-
|
|
410
|
-
${readOnly.map(tocLine).join("\n")}
|
|
411
|
-
|
|
412
|
-
**Private functions:**
|
|
413
|
-
|
|
414
|
-
${privates.map(tocLine).join("\n")}
|
|
415
|
-
|
|
416
|
-
**Maps**
|
|
417
|
-
|
|
418
|
-
${maps.map(tocLine).join("\n")}
|
|
419
|
-
|
|
420
|
-
**Variables**
|
|
421
|
-
|
|
422
|
-
${vars.map(tocLine).join("\n")}
|
|
423
|
-
|
|
424
|
-
**Constants**
|
|
425
|
-
|
|
426
|
-
${constants.map(tocLine).join("\n")}
|
|
427
|
-
`;
|
|
428
|
-
}
|
|
429
|
-
function generateReadme(session, excluded) {
|
|
430
|
-
const contractLines = [];
|
|
431
|
-
sortContracts(session.contracts).forEach((contract) => {
|
|
432
|
-
const name = getContractName2(contract.contract_id, false);
|
|
433
|
-
if (excluded[name]) return;
|
|
434
|
-
const fileName = `${name}.md`;
|
|
435
|
-
const line = `- [\`${name}\`](${fileName})`;
|
|
436
|
-
contractLines.push(line);
|
|
437
|
-
});
|
|
438
|
-
const fileContents = `# Contracts
|
|
439
|
-
|
|
440
|
-
${contractLines.join("\n")}
|
|
441
|
-
`;
|
|
442
|
-
return fileContents;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// src/config.ts
|
|
446
|
-
import { type as type2 } from "arktype";
|
|
447
|
-
|
|
448
|
-
// src/clarinet-config.ts
|
|
449
|
-
import { type } from "arktype";
|
|
450
|
-
import { readFile } from "fs/promises";
|
|
451
|
-
import { parse } from "@iarna/toml";
|
|
452
|
-
var ClarinetConfig = type({
|
|
453
|
-
project: type({
|
|
454
|
-
requirements: type({
|
|
455
|
-
contract_id: type("string").describe("Contract ID")
|
|
456
|
-
}).array().describe("Project requirements").optional(),
|
|
457
|
-
cache_location: type({
|
|
458
|
-
path: type("string").describe("Cache location path")
|
|
459
|
-
}).optional()
|
|
460
|
-
}),
|
|
461
|
-
contracts: type({
|
|
462
|
-
"[string]": type({
|
|
463
|
-
path: type("string").describe("Contract path")
|
|
464
|
-
})
|
|
465
|
-
}).optional()
|
|
466
|
-
});
|
|
467
|
-
async function getClarinetConfig(path) {
|
|
468
|
-
const file = await readFile(path, "utf-8");
|
|
469
|
-
const config = ClarinetConfig.assert(parse(file));
|
|
470
|
-
return config;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// src/config.ts
|
|
474
|
-
import { dirname as dirname2, join, relative as relative2, resolve as resolve2 } from "path";
|
|
475
|
-
import { stringify, parse as parse2 } from "@iarna/toml";
|
|
476
|
-
import { readFile as readFile2 } from "fs/promises";
|
|
477
|
-
var CONFIG_FILE = "Clarigen.toml";
|
|
478
|
-
var OutputType = /* @__PURE__ */ ((OutputType2) => {
|
|
479
|
-
OutputType2["ESM"] = "types";
|
|
480
|
-
OutputType2["ESM_OLD"] = "esm";
|
|
481
|
-
OutputType2["Docs"] = "docs";
|
|
482
|
-
return OutputType2;
|
|
483
|
-
})(OutputType || {});
|
|
484
|
-
var typesSchema = type2({
|
|
485
|
-
"output?": type2("string").describe("Path to the output file"),
|
|
486
|
-
"outputs?": type2("string[]").describe("Paths to the output files"),
|
|
487
|
-
"include_accounts?": type2("boolean").describe("Include accounts in the output"),
|
|
488
|
-
"after?": type2("string").describe("Script to run after the output is generated"),
|
|
489
|
-
"include_boot_contracts?": type2("boolean").describe("Include boot contracts in the output"),
|
|
490
|
-
"watch_folders?": type2("string[]").describe("Folders to watch for changes")
|
|
491
|
-
}).optional();
|
|
492
|
-
var ConfigFile = type2({
|
|
493
|
-
clarinet: type2("string").describe("Path to the Clarinet config file"),
|
|
494
|
-
["types" /* ESM */]: typesSchema,
|
|
495
|
-
["esm" /* ESM_OLD */]: typesSchema,
|
|
496
|
-
["docs" /* Docs */]: type2({
|
|
497
|
-
"output?": type2("string").describe("Path to docs output folder. Defaults to ./docs"),
|
|
498
|
-
"outputs?": type2("string[]").describe("Paths to docs output folders"),
|
|
499
|
-
"exclude?": type2("string[]").describe("Contracts to exclude from docs generation"),
|
|
500
|
-
"after?": type2("string").describe("Script to run after docs are generated")
|
|
501
|
-
}).optional()
|
|
502
|
-
});
|
|
503
|
-
var defaultConfigFile = {
|
|
504
|
-
clarinet: "./Clarinet.toml"
|
|
505
|
-
};
|
|
506
|
-
var Config = class {
|
|
507
|
-
configFile;
|
|
508
|
-
clarinet;
|
|
509
|
-
cwd;
|
|
510
|
-
constructor(config, clarinet, cwd) {
|
|
511
|
-
this.configFile = config;
|
|
512
|
-
this.clarinet = clarinet;
|
|
513
|
-
this.cwd = cwd ?? process.cwd();
|
|
514
|
-
}
|
|
515
|
-
static async load(cwd) {
|
|
516
|
-
const config = await getConfig(cwd);
|
|
517
|
-
if (config["esm" /* ESM_OLD */]) {
|
|
518
|
-
config["types" /* ESM */] = config["esm" /* ESM_OLD */];
|
|
519
|
-
delete config["esm" /* ESM_OLD */];
|
|
520
|
-
}
|
|
521
|
-
const clarinet = await getClarinetConfig(resolve2(cwd ?? "", config.clarinet));
|
|
522
|
-
return new this(config, clarinet, cwd);
|
|
523
|
-
}
|
|
524
|
-
getOutputs(type3) {
|
|
525
|
-
var _a, _b;
|
|
526
|
-
const singlePath = (_a = this.configFile[type3]) == null ? void 0 : _a.output;
|
|
527
|
-
const multiPath = ((_b = this.configFile[type3]) == null ? void 0 : _b.outputs) || [];
|
|
528
|
-
if (singlePath !== void 0) return [singlePath];
|
|
529
|
-
return multiPath;
|
|
530
|
-
}
|
|
531
|
-
outputResolve(type3, filePath) {
|
|
532
|
-
const outputs = this.getOutputs(type3);
|
|
533
|
-
if (!this.supports(type3)) return null;
|
|
534
|
-
return outputs.map((path) => {
|
|
535
|
-
return resolve2(this.cwd, path, filePath || "");
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
async writeOutput(type3, contents, filePath) {
|
|
539
|
-
const paths = this.outputResolve(type3, filePath);
|
|
540
|
-
if (paths === null) return null;
|
|
541
|
-
await Promise.all(
|
|
542
|
-
paths.map(async (path) => {
|
|
543
|
-
await writeFile(path, contents);
|
|
544
|
-
log.debug(`Generated ${type3} file at ${relative2(this.cwd, path)}`);
|
|
545
|
-
})
|
|
546
|
-
);
|
|
547
|
-
return paths;
|
|
548
|
-
}
|
|
549
|
-
supports(type3) {
|
|
550
|
-
return this.getOutputs(type3).length > 0;
|
|
551
|
-
}
|
|
552
|
-
type(type3) {
|
|
553
|
-
return this.configFile[type3];
|
|
554
|
-
}
|
|
555
|
-
get esm() {
|
|
556
|
-
return this.configFile["types" /* ESM */];
|
|
557
|
-
}
|
|
558
|
-
get docs() {
|
|
559
|
-
return this.configFile["docs" /* Docs */];
|
|
560
|
-
}
|
|
561
|
-
clarinetFile() {
|
|
562
|
-
return resolve2(this.cwd, this.configFile.clarinet);
|
|
563
|
-
}
|
|
564
|
-
joinFromClarinet(filePath) {
|
|
565
|
-
const baseDir = dirname2(this.clarinetFile());
|
|
566
|
-
return join(baseDir, filePath);
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
function configFilePath(cwd) {
|
|
570
|
-
return resolve2(cwd ?? process.cwd(), CONFIG_FILE);
|
|
571
|
-
}
|
|
572
|
-
async function saveConfig(config) {
|
|
573
|
-
const configToml = stringify({ ...config });
|
|
574
|
-
await writeFile(configFilePath(), configToml);
|
|
575
|
-
}
|
|
576
|
-
var sessionConfig;
|
|
577
|
-
async function getConfig(cwd) {
|
|
578
|
-
if (typeof sessionConfig !== "undefined") return sessionConfig;
|
|
579
|
-
const path = configFilePath(cwd);
|
|
580
|
-
if (await fileExists(path)) {
|
|
581
|
-
const toml = await readFile2(path, "utf-8");
|
|
582
|
-
const parsedToml = parse2(toml);
|
|
583
|
-
const parsed = ConfigFile(parsedToml);
|
|
584
|
-
if (parsed instanceof type2.errors) {
|
|
585
|
-
logger.error(`Error parsing Clarigen config: ${parsed.summary}`);
|
|
586
|
-
throw new Error(`Error parsing Clarigen config: ${parsed.summary}`);
|
|
587
|
-
}
|
|
588
|
-
sessionConfig = parsed;
|
|
589
|
-
} else {
|
|
590
|
-
sessionConfig = defaultConfigFile;
|
|
591
|
-
}
|
|
592
|
-
return sessionConfig;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
// src/files/docs.ts
|
|
596
|
-
import { getContractName as getContractName3 } from "@clarigen/core";
|
|
597
|
-
import { relative as relative3, extname } from "path";
|
|
598
|
-
async function generateDocs({
|
|
599
|
-
session,
|
|
600
|
-
config
|
|
601
|
-
}) {
|
|
602
|
-
const docs = config.configFile["docs" /* Docs */];
|
|
603
|
-
const docsBase = docs == null ? void 0 : docs.output;
|
|
604
|
-
if (!docsBase) {
|
|
605
|
-
warnNoDocs();
|
|
606
|
-
return;
|
|
607
|
-
}
|
|
608
|
-
const docsPathExt = extname(docsBase);
|
|
609
|
-
if (docsPathExt) {
|
|
610
|
-
log.warn(`Docs output path ('${docsBase}') looks like a file - it needs to be a directory.`);
|
|
611
|
-
}
|
|
612
|
-
const excluded = Object.fromEntries(
|
|
613
|
-
(docs.exclude || []).map((e) => {
|
|
614
|
-
return [e, true];
|
|
615
|
-
})
|
|
616
|
-
);
|
|
617
|
-
log.debug(`Generating docs at path \`${docsBase}\``);
|
|
618
|
-
const docsBaseFolder = config.outputResolve("docs" /* Docs */, "./")[0];
|
|
619
|
-
const paths = await Promise.all(
|
|
620
|
-
session.contracts.map(async (contract) => {
|
|
621
|
-
var _a, _b;
|
|
622
|
-
const name = getContractName3(contract.contract_id, false);
|
|
623
|
-
if (excluded[name]) return null;
|
|
624
|
-
const docFile = `${name}.md`;
|
|
625
|
-
const contractPathDef = (_b = (_a = config.clarinet.contracts) == null ? void 0 : _a[name]) == null ? void 0 : _b.path;
|
|
626
|
-
let contractFile;
|
|
627
|
-
if (contractPathDef) {
|
|
628
|
-
const contractPathFull = config.joinFromClarinet(contractPathDef);
|
|
629
|
-
contractFile = relative3(docsBaseFolder, contractPathFull);
|
|
630
|
-
} else {
|
|
631
|
-
log.debug(`Couldn't find contract file from Clarinet.toml for contract ${name}`);
|
|
632
|
-
}
|
|
633
|
-
const md = generateMarkdown({ contract, contractFile });
|
|
634
|
-
const path = await config.writeOutput("docs" /* Docs */, md, docFile);
|
|
635
|
-
return path[0];
|
|
636
|
-
})
|
|
637
|
-
);
|
|
638
|
-
const readme = generateReadme(session, excluded);
|
|
639
|
-
paths.push((await config.writeOutput("docs" /* Docs */, readme, "README.md"))[0]);
|
|
640
|
-
await afterDocs(config);
|
|
641
|
-
}
|
|
642
|
-
function warnNoDocs() {
|
|
643
|
-
log.warn(
|
|
644
|
-
`
|
|
645
|
-
Clarigen config file doesn't include an output directory for docs.
|
|
646
|
-
|
|
647
|
-
To generate docs, specify 'docs.output' in your config file:
|
|
648
|
-
|
|
649
|
-
[docs]
|
|
650
|
-
output = "docs/"
|
|
651
|
-
`.trimEnd()
|
|
652
|
-
);
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
// src/files/variables.ts
|
|
656
|
-
import {
|
|
657
|
-
cvToValue,
|
|
658
|
-
getContractName as getContractName5
|
|
659
|
-
} from "@clarigen/core";
|
|
660
|
-
|
|
661
|
-
// src/declaration.ts
|
|
662
|
-
import {
|
|
663
|
-
isClarityAbiBuffer,
|
|
664
|
-
isClarityAbiList,
|
|
665
|
-
isClarityAbiOptional,
|
|
666
|
-
isClarityAbiPrimitive,
|
|
667
|
-
isClarityAbiResponse,
|
|
668
|
-
isClarityAbiStringAscii,
|
|
669
|
-
isClarityAbiStringUtf8,
|
|
670
|
-
isClarityAbiTuple,
|
|
671
|
-
isClarityAbiTraitReference
|
|
672
|
-
} from "@clarigen/core";
|
|
673
|
-
import { toCamelCase as toCamelCase2 } from "@clarigen/core";
|
|
674
|
-
var jsTypeFromAbiType = (val, isArgument = false) => {
|
|
675
|
-
if (isClarityAbiPrimitive(val)) {
|
|
676
|
-
if (val === "uint128") {
|
|
677
|
-
if (isArgument) return "number | bigint";
|
|
678
|
-
return "bigint";
|
|
679
|
-
} else if (val === "int128") {
|
|
680
|
-
if (isArgument) return "number | bigint";
|
|
681
|
-
return "bigint";
|
|
682
|
-
} else if (val === "bool") {
|
|
683
|
-
return "boolean";
|
|
684
|
-
} else if (val === "principal") {
|
|
685
|
-
return "string";
|
|
686
|
-
} else if (val === "none") {
|
|
687
|
-
return "null";
|
|
688
|
-
} else if (val === "trait_reference") {
|
|
689
|
-
return "string";
|
|
690
|
-
} else {
|
|
691
|
-
throw new Error(`Unexpected Clarity ABI type primitive: ${JSON.stringify(val)}`);
|
|
692
|
-
}
|
|
693
|
-
} else if (isClarityAbiBuffer(val)) {
|
|
694
|
-
return "Uint8Array";
|
|
695
|
-
} else if (isClarityAbiResponse(val)) {
|
|
696
|
-
const ok = jsTypeFromAbiType(val.response.ok, isArgument);
|
|
697
|
-
const err = jsTypeFromAbiType(val.response.error, isArgument);
|
|
698
|
-
return `Response<${ok}, ${err}>`;
|
|
699
|
-
} else if (isClarityAbiOptional(val)) {
|
|
700
|
-
const innerType = jsTypeFromAbiType(val.optional, isArgument);
|
|
701
|
-
return `${innerType} | null`;
|
|
702
|
-
} else if (isClarityAbiTuple(val)) {
|
|
703
|
-
const tupleDefs = [];
|
|
704
|
-
val.tuple.forEach(({ name, type: type3 }) => {
|
|
705
|
-
const camelName = toCamelCase2(name);
|
|
706
|
-
const innerType = jsTypeFromAbiType(type3, isArgument);
|
|
707
|
-
tupleDefs.push(`"${camelName}": ${innerType};`);
|
|
708
|
-
});
|
|
709
|
-
return `{
|
|
710
|
-
${tupleDefs.join("\n ")}
|
|
711
|
-
}`;
|
|
712
|
-
} else if (isClarityAbiList(val)) {
|
|
713
|
-
const innerType = jsTypeFromAbiType(val.list.type, isArgument);
|
|
714
|
-
return `${innerType}[]`;
|
|
715
|
-
} else if (isClarityAbiStringAscii(val)) {
|
|
716
|
-
return "string";
|
|
717
|
-
} else if (isClarityAbiStringUtf8(val)) {
|
|
718
|
-
return "string";
|
|
719
|
-
} else if (isClarityAbiTraitReference(val)) {
|
|
720
|
-
return "string";
|
|
721
|
-
} else {
|
|
722
|
-
throw new Error(`Unexpected Clarity ABI type: ${JSON.stringify(val)}`);
|
|
723
|
-
}
|
|
724
|
-
};
|
|
725
|
-
function abiArgType(arg) {
|
|
726
|
-
const nativeType = jsTypeFromAbiType(arg.type, true);
|
|
727
|
-
const argName = getArgName(arg.name);
|
|
728
|
-
return `${argName}: TypedAbiArg<${nativeType}, "${argName}">`;
|
|
729
|
-
}
|
|
730
|
-
function abiFunctionType(abiFunction) {
|
|
731
|
-
const args = abiFunction.args.map(abiArgType);
|
|
732
|
-
const retType = jsTypeFromAbiType(abiFunction.outputs.type);
|
|
733
|
-
const argsTuple = `[${args.join(", ")}]`;
|
|
734
|
-
return `TypedAbiFunction<${argsTuple}, ${retType}>`;
|
|
735
|
-
}
|
|
736
|
-
function getArgName(name) {
|
|
737
|
-
const camel = toCamelCase2(name);
|
|
738
|
-
const prefix = RESERVED[camel] ? "_" : "";
|
|
739
|
-
return `${prefix}${camel}`;
|
|
740
|
-
}
|
|
741
|
-
function _hash(...words) {
|
|
742
|
-
const h = {};
|
|
743
|
-
for (const word of words) {
|
|
744
|
-
h[word] = true;
|
|
745
|
-
}
|
|
746
|
-
return h;
|
|
747
|
-
}
|
|
748
|
-
var RESERVED = _hash(
|
|
749
|
-
// Keywords, ES6 11.6.2.1, http://www.ecma-international.org/ecma-262/6.0/index.html#sec-keywords
|
|
750
|
-
"break",
|
|
751
|
-
"do",
|
|
752
|
-
"in",
|
|
753
|
-
"typeof",
|
|
754
|
-
"case",
|
|
755
|
-
"else",
|
|
756
|
-
"instanceof",
|
|
757
|
-
"var",
|
|
758
|
-
"catch",
|
|
759
|
-
"export",
|
|
760
|
-
"new",
|
|
761
|
-
"void",
|
|
762
|
-
"class",
|
|
763
|
-
"extends",
|
|
764
|
-
"return",
|
|
765
|
-
"while",
|
|
766
|
-
"const",
|
|
767
|
-
"finally",
|
|
768
|
-
"super",
|
|
769
|
-
"with",
|
|
770
|
-
"continue",
|
|
771
|
-
"for",
|
|
772
|
-
"switch",
|
|
773
|
-
"yield",
|
|
774
|
-
"debugger",
|
|
775
|
-
"function",
|
|
776
|
-
"this",
|
|
777
|
-
"default",
|
|
778
|
-
"if",
|
|
779
|
-
"throw",
|
|
780
|
-
"delete",
|
|
781
|
-
"import",
|
|
782
|
-
"try",
|
|
783
|
-
// Future Reserved Words, ES6 11.6.2.2
|
|
784
|
-
// http://www.ecma-international.org/ecma-262/6.0/index.html#sec-future-reserved-words
|
|
785
|
-
"enum",
|
|
786
|
-
"await",
|
|
787
|
-
// NullLiteral & BooleanLiteral
|
|
788
|
-
"null",
|
|
789
|
-
"true",
|
|
790
|
-
"false"
|
|
791
|
-
);
|
|
792
|
-
|
|
793
|
-
// src/files/base.ts
|
|
794
|
-
import { toCamelCase as toCamelCase3 } from "@clarigen/core";
|
|
795
|
-
|
|
796
|
-
// src/files/accounts.ts
|
|
797
|
-
function generateAccountsCode(accounts) {
|
|
798
|
-
const sortedAccounts = sortAccounts(accounts);
|
|
799
|
-
const namedAccounts = Object.fromEntries(
|
|
800
|
-
sortedAccounts.map((a) => {
|
|
801
|
-
const { name, ...rest } = a;
|
|
802
|
-
return [name, rest];
|
|
803
|
-
})
|
|
804
|
-
);
|
|
805
|
-
return JSON.stringify(namedAccounts);
|
|
806
|
-
}
|
|
807
|
-
function sortAccounts(accounts) {
|
|
808
|
-
const nameSorted = [...accounts].sort((a, b) => {
|
|
809
|
-
if (a.name < b.name) {
|
|
810
|
-
return -1;
|
|
811
|
-
}
|
|
812
|
-
return 1;
|
|
813
|
-
});
|
|
814
|
-
return nameSorted;
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
// src/files/identifiers.ts
|
|
818
|
-
import { getContractName as getContractName4 } from "@clarigen/core";
|
|
819
|
-
function generateIdentifiers(session) {
|
|
820
|
-
const identifiers = Object.fromEntries(
|
|
821
|
-
sortContracts(session.contracts).map((c) => {
|
|
822
|
-
const contractName = getContractName4(c.contract_id);
|
|
823
|
-
return [contractName, c.contract_id];
|
|
824
|
-
})
|
|
825
|
-
);
|
|
826
|
-
return identifiers;
|
|
827
|
-
}
|
|
828
|
-
function generateIdentifiersCode(session) {
|
|
829
|
-
const identifiers = generateIdentifiers(session);
|
|
830
|
-
return `export const identifiers = ${JSON.stringify(identifiers)} as const`;
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
// src/files/base.ts
|
|
834
|
-
import { inspect } from "util";
|
|
835
|
-
function generateContractMeta(contract, constants) {
|
|
836
|
-
const abi = contract.contract_interface;
|
|
837
|
-
const functionLines = [];
|
|
838
|
-
const { functions, maps, variables, non_fungible_tokens, ...rest } = abi;
|
|
839
|
-
functions.forEach((func) => {
|
|
840
|
-
let functionLine = `${toCamelCase3(func.name)}: `;
|
|
841
|
-
const funcDef = JSON.stringify(func);
|
|
842
|
-
functionLine += funcDef;
|
|
843
|
-
const functionType = abiFunctionType(func);
|
|
844
|
-
functionLine += ` as ${functionType}`;
|
|
845
|
-
functionLines.push(functionLine);
|
|
846
|
-
});
|
|
847
|
-
const mapLines = maps.map((map) => {
|
|
848
|
-
let mapLine = `${toCamelCase3(map.name)}: `;
|
|
849
|
-
const keyType = jsTypeFromAbiType(map.key, true);
|
|
850
|
-
const valType = jsTypeFromAbiType(map.value);
|
|
851
|
-
mapLine += JSON.stringify(map);
|
|
852
|
-
mapLine += ` as TypedAbiMap<${keyType}, ${valType}>`;
|
|
853
|
-
return mapLine;
|
|
854
|
-
});
|
|
855
|
-
const otherAbi = JSON.stringify(rest);
|
|
856
|
-
const contractName = contract.contract_id.split(".")[1];
|
|
857
|
-
const variableLines = encodeVariables(variables);
|
|
858
|
-
const nftLines = non_fungible_tokens.map((nft) => {
|
|
859
|
-
return JSON.stringify(nft);
|
|
860
|
-
});
|
|
861
|
-
return `{
|
|
862
|
-
${serializeLines("functions", functionLines)}
|
|
863
|
-
${serializeLines("maps", mapLines)}
|
|
864
|
-
${serializeLines("variables", variableLines)}
|
|
865
|
-
constants: ${constants},
|
|
866
|
-
${serializeArray("non_fungible_tokens", nftLines)}
|
|
867
|
-
${otherAbi.slice(1, -1)},
|
|
868
|
-
contractName: '${contractName}',
|
|
869
|
-
}`;
|
|
870
|
-
}
|
|
871
|
-
var TYPE_IMPORTS = `import type { TypedAbiArg, TypedAbiFunction, TypedAbiMap, TypedAbiVariable, Response } from '@clarigen/core';`;
|
|
872
|
-
function generateBaseFile(session) {
|
|
873
|
-
const combined = session.contracts.map((c, i) => ({
|
|
874
|
-
...c,
|
|
875
|
-
constants: session.variables[i]
|
|
876
|
-
}));
|
|
877
|
-
const contractDefs = sortContracts(combined).map((contract) => {
|
|
878
|
-
const meta = generateContractMeta(contract, contract.constants);
|
|
879
|
-
const id = contract.contract_id.split(".")[1];
|
|
880
|
-
const keyName = toCamelCase3(id);
|
|
881
|
-
return `${keyName}: ${meta}`;
|
|
882
|
-
});
|
|
883
|
-
const file = `
|
|
884
|
-
${TYPE_IMPORTS}
|
|
885
|
-
|
|
886
|
-
export const contracts = {
|
|
887
|
-
${contractDefs.join(",\n")}
|
|
888
|
-
} as const;
|
|
889
|
-
|
|
890
|
-
export const accounts = ${generateAccountsCode(session.accounts)} as const;
|
|
891
|
-
|
|
892
|
-
${generateIdentifiersCode(session)}
|
|
893
|
-
|
|
894
|
-
export const simnet = {
|
|
895
|
-
accounts,
|
|
896
|
-
contracts,
|
|
897
|
-
identifiers,
|
|
898
|
-
} as const;
|
|
899
|
-
|
|
900
|
-
`;
|
|
901
|
-
return file;
|
|
902
|
-
}
|
|
903
|
-
function encodeVariables(variables) {
|
|
904
|
-
return variables.map((v) => {
|
|
905
|
-
let varLine = `${encodeVariableName(v.name)}: `;
|
|
906
|
-
const type3 = jsTypeFromAbiType(v.type);
|
|
907
|
-
const varJSON = serialize(v);
|
|
908
|
-
varLine += `${varJSON} as TypedAbiVariable<${type3}>`;
|
|
909
|
-
return varLine;
|
|
910
|
-
});
|
|
911
|
-
}
|
|
912
|
-
Uint8Array.prototype[inspect.custom] = function(depth, options) {
|
|
913
|
-
return `Uint8Array.from([${this.join(",")}])`;
|
|
914
|
-
};
|
|
915
|
-
function serialize(obj) {
|
|
916
|
-
return inspect(obj, {
|
|
917
|
-
// showHidden: false,
|
|
918
|
-
// depth: 100,
|
|
919
|
-
// colors: false,
|
|
920
|
-
showHidden: false,
|
|
921
|
-
// iterableLimit: 100000,
|
|
922
|
-
compact: false,
|
|
923
|
-
// trailingComma: true,
|
|
924
|
-
depth: 100,
|
|
925
|
-
colors: false,
|
|
926
|
-
maxArrayLength: Infinity,
|
|
927
|
-
maxStringLength: Infinity,
|
|
928
|
-
breakLength: Infinity,
|
|
929
|
-
numericSeparator: true
|
|
930
|
-
// strAbbreviateSize: 100000,
|
|
931
|
-
});
|
|
932
|
-
}
|
|
933
|
-
function serializeLines(key, lines) {
|
|
934
|
-
return `"${key}": {
|
|
935
|
-
${lines.join(",\n ")}
|
|
936
|
-
},`;
|
|
937
|
-
}
|
|
938
|
-
function serializeArray(key, lines) {
|
|
939
|
-
return `"${key}": [
|
|
940
|
-
${lines.join(",\n ")}
|
|
941
|
-
],`;
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
// src/files/variables.ts
|
|
945
|
-
function clarityVersionForContract(contract) {
|
|
946
|
-
switch (contract.contract_interface.clarity_version) {
|
|
947
|
-
case "Clarity1":
|
|
948
|
-
return 1;
|
|
949
|
-
case "Clarity2":
|
|
950
|
-
return 2;
|
|
951
|
-
case "Clarity3":
|
|
952
|
-
return 3;
|
|
953
|
-
case "Clarity4":
|
|
954
|
-
return 4;
|
|
955
|
-
default:
|
|
956
|
-
return 3;
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
function getVariablesV2(contract, simnet, verbose) {
|
|
960
|
-
const [deployer] = contract.contract_id.split(".");
|
|
961
|
-
const fakeId = `${getContractName5(contract.contract_id)}-vars`;
|
|
962
|
-
logger.debug(`Deploying ${contract.contract_id} for variables.`);
|
|
963
|
-
if (!contract.source) {
|
|
964
|
-
logger.debug(
|
|
965
|
-
`Contract ${getContractName5(contract.contract_id)} has no source. Skipping variables.`
|
|
966
|
-
);
|
|
967
|
-
return {};
|
|
968
|
-
}
|
|
969
|
-
if (contract.contract_interface.variables.length === 0) {
|
|
970
|
-
logger.info(`Contract ${getContractName5(contract.contract_id, false)} has no variables`);
|
|
971
|
-
return {};
|
|
972
|
-
}
|
|
973
|
-
let varFn = `{
|
|
974
|
-
`;
|
|
975
|
-
const varLines = contract.contract_interface.variables.map((variable) => {
|
|
976
|
-
let varLine = `${variable.name}: `;
|
|
977
|
-
if (variable.access === "constant") {
|
|
978
|
-
varLine += `${variable.name}`;
|
|
979
|
-
} else {
|
|
980
|
-
varLine += `(var-get ${variable.name})`;
|
|
981
|
-
}
|
|
982
|
-
return varLine;
|
|
983
|
-
});
|
|
984
|
-
varFn += varLines.map((l) => ` ${l},`).join("\n");
|
|
985
|
-
varFn += "\n}";
|
|
986
|
-
const fullSrc = contract.source + `
|
|
987
|
-
|
|
988
|
-
${varFn}`;
|
|
989
|
-
try {
|
|
990
|
-
const receipt = simnet.deployContract(
|
|
991
|
-
fakeId,
|
|
992
|
-
fullSrc,
|
|
993
|
-
{
|
|
994
|
-
clarityVersion: clarityVersionForContract(contract)
|
|
995
|
-
},
|
|
996
|
-
deployer
|
|
997
|
-
);
|
|
998
|
-
const result = receipt.result;
|
|
999
|
-
const varsAbi = {
|
|
1000
|
-
tuple: []
|
|
1001
|
-
};
|
|
1002
|
-
contract.contract_interface.variables.forEach((v) => {
|
|
1003
|
-
const _v = v;
|
|
1004
|
-
varsAbi.tuple.push({
|
|
1005
|
-
type: _v.type,
|
|
1006
|
-
name: _v.name
|
|
1007
|
-
});
|
|
1008
|
-
});
|
|
1009
|
-
if (verbose) {
|
|
1010
|
-
logger.info(cvToValue(result, true));
|
|
1011
|
-
}
|
|
1012
|
-
return cvToValue(result, true);
|
|
1013
|
-
} catch (error) {
|
|
1014
|
-
logger.warn(
|
|
1015
|
-
{ err: error },
|
|
1016
|
-
`Error getting variables for ${getContractName5(contract.contract_id, false)}`
|
|
1017
|
-
);
|
|
1018
|
-
return {};
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
function mapVariables(session, simnet) {
|
|
1022
|
-
return session.contracts.map((contract) => {
|
|
1023
|
-
const vars = getVariablesV2(contract, simnet);
|
|
1024
|
-
return serialize(vars);
|
|
1025
|
-
});
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
// src/files/esm.ts
|
|
1029
|
-
import { readFile as readFile3 } from "fs/promises";
|
|
1030
|
-
import { join as join2, dirname as dirname3, relative as relative4 } from "path";
|
|
1031
|
-
import { parse as parse3 } from "yaml";
|
|
1032
|
-
|
|
1033
|
-
// src/deployments.ts
|
|
1034
|
-
function flatBatch(batches) {
|
|
1035
|
-
const txs = [];
|
|
1036
|
-
batches.forEach((batch) => txs.push(...batch.transactions));
|
|
1037
|
-
return txs;
|
|
1038
|
-
}
|
|
1039
|
-
function getContractTxs(batches) {
|
|
1040
|
-
const txs = flatBatch(batches);
|
|
1041
|
-
return txs.filter(isContractTx);
|
|
1042
|
-
}
|
|
1043
|
-
function getDeploymentContract(contractName, deployment) {
|
|
1044
|
-
const txs = flatBatch(deployment.plan.batches);
|
|
1045
|
-
for (const tx of txs) {
|
|
1046
|
-
if (!isContractTx(tx)) continue;
|
|
1047
|
-
if ("requirement-publish" in tx) {
|
|
1048
|
-
const [_, name] = tx["requirement-publish"]["contract-id"].split(".");
|
|
1049
|
-
if (name === contractName) {
|
|
1050
|
-
return tx;
|
|
1051
|
-
}
|
|
1052
|
-
} else if ("emulated-contract-publish" in tx) {
|
|
1053
|
-
if (tx["emulated-contract-publish"]["contract-name"] === contractName) {
|
|
1054
|
-
return tx;
|
|
1055
|
-
}
|
|
1056
|
-
} else if ("contract-publish" in tx) {
|
|
1057
|
-
const contract = tx["contract-publish"];
|
|
1058
|
-
if (contract["contract-name"] === contractName) {
|
|
1059
|
-
return tx;
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
throw new Error(`Unable to find deployment tx for contract '${contractName}'`);
|
|
1064
|
-
}
|
|
1065
|
-
function getDeploymentTxPath(tx) {
|
|
1066
|
-
if (!isContractTx(tx)) {
|
|
1067
|
-
throw new Error("Unable to get path for tx type.");
|
|
1068
|
-
}
|
|
1069
|
-
if ("requirement-publish" in tx) {
|
|
1070
|
-
return tx["requirement-publish"].path;
|
|
1071
|
-
} else if ("emulated-contract-publish" in tx) {
|
|
1072
|
-
const contract = tx["emulated-contract-publish"];
|
|
1073
|
-
return contract.path;
|
|
1074
|
-
} else if ("contract-publish" in tx) {
|
|
1075
|
-
const contract = tx["contract-publish"];
|
|
1076
|
-
return contract.path;
|
|
1077
|
-
}
|
|
1078
|
-
throw new Error("Couldnt get path for deployment tx.");
|
|
1079
|
-
}
|
|
1080
|
-
function getIdentifier(tx) {
|
|
1081
|
-
if (!isContractTx(tx)) {
|
|
1082
|
-
throw new Error("Unable to get ID for tx type.");
|
|
1083
|
-
}
|
|
1084
|
-
if ("requirement-publish" in tx) {
|
|
1085
|
-
const spec = tx["requirement-publish"];
|
|
1086
|
-
const [_, name] = spec["contract-id"].split(".");
|
|
1087
|
-
return `${spec["remap-sender"]}.${name}`;
|
|
1088
|
-
} else if ("emulated-contract-publish" in tx) {
|
|
1089
|
-
const contract = tx["emulated-contract-publish"];
|
|
1090
|
-
return `${contract["emulated-sender"]}.${contract["contract-name"]}`;
|
|
1091
|
-
} else if ("contract-publish" in tx) {
|
|
1092
|
-
const contract = tx["contract-publish"];
|
|
1093
|
-
return `${contract["expected-sender"]}.${contract["contract-name"]}`;
|
|
1094
|
-
}
|
|
1095
|
-
throw new Error(`Unable to find ID for contract.`);
|
|
1096
|
-
}
|
|
1097
|
-
function isContractTx(tx) {
|
|
1098
|
-
if ("contract-call" in tx) return false;
|
|
1099
|
-
if ("btc-transfer" in tx) return false;
|
|
1100
|
-
if ("emulated-contract-call" in tx) return false;
|
|
1101
|
-
return true;
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
// src/files/esm.ts
|
|
1105
|
-
import { getContractName as getContractName6 } from "@clarigen/core";
|
|
1106
|
-
import { spawn as spawn2 } from "child_process";
|
|
1107
|
-
async function parseDeployment(path) {
|
|
1108
|
-
const contents = await readFile3(path, "utf-8");
|
|
1109
|
-
const parsed = parse3(contents);
|
|
1110
|
-
return parsed;
|
|
1111
|
-
}
|
|
1112
|
-
var DEPLOYMENT_NETWORKS = ["devnet", "simnet", "testnet", "mainnet"];
|
|
1113
|
-
async function getDeployments(config) {
|
|
1114
|
-
const entries = await Promise.all(
|
|
1115
|
-
DEPLOYMENT_NETWORKS.map(async (network) => {
|
|
1116
|
-
const file = `default.${network}-plan.yaml`;
|
|
1117
|
-
const path = join2(dirname3(config.clarinetFile()), "deployments", file);
|
|
1118
|
-
let plan;
|
|
1119
|
-
try {
|
|
1120
|
-
plan = await parseDeployment(path);
|
|
1121
|
-
} catch (_) {
|
|
1122
|
-
}
|
|
1123
|
-
return [network, plan];
|
|
1124
|
-
})
|
|
1125
|
-
);
|
|
1126
|
-
return Object.fromEntries(entries);
|
|
1127
|
-
}
|
|
1128
|
-
async function generateESMFile({
|
|
1129
|
-
baseFile,
|
|
1130
|
-
session,
|
|
1131
|
-
config
|
|
1132
|
-
}) {
|
|
1133
|
-
const deployments = await getDeployments(config);
|
|
1134
|
-
const contractDeployments = collectContractDeployments(session, deployments, config);
|
|
1135
|
-
const simnet = generateSimnetCode(config, deployments, session);
|
|
1136
|
-
return `${baseFile}
|
|
1137
|
-
export const deployments = ${JSON.stringify(contractDeployments)} as const;
|
|
1138
|
-
${simnet}
|
|
1139
|
-
export const project = {
|
|
1140
|
-
contracts,
|
|
1141
|
-
deployments,
|
|
1142
|
-
} as const;
|
|
1143
|
-
`;
|
|
1144
|
-
}
|
|
1145
|
-
function insertNetworkId(deployments, identifier, network) {
|
|
1146
|
-
const name = getContractName6(identifier);
|
|
1147
|
-
if (!deployments[name]) {
|
|
1148
|
-
return;
|
|
1149
|
-
}
|
|
1150
|
-
if (deployments[name][network] === null) {
|
|
1151
|
-
deployments[name][network] = identifier;
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
function collectContractDeployments(session, deployments, config) {
|
|
1155
|
-
var _a;
|
|
1156
|
-
const full = Object.fromEntries(
|
|
1157
|
-
sortContracts(session.contracts).map((contract) => {
|
|
1158
|
-
const contractName = getContractName6(contract.contract_id);
|
|
1159
|
-
const contractDeployments = Object.fromEntries(
|
|
1160
|
-
DEPLOYMENT_NETWORKS.map((network) => {
|
|
1161
|
-
const deployment = deployments[network];
|
|
1162
|
-
if (typeof deployment === "undefined") {
|
|
1163
|
-
return [network, null];
|
|
1164
|
-
}
|
|
1165
|
-
try {
|
|
1166
|
-
const contractName2 = contract.contract_id.split(".")[1];
|
|
1167
|
-
const tx = getDeploymentContract(contractName2, deployment);
|
|
1168
|
-
const id = getIdentifier(tx);
|
|
1169
|
-
return [network, id];
|
|
1170
|
-
} catch (_) {
|
|
1171
|
-
return [network, null];
|
|
1172
|
-
}
|
|
1173
|
-
})
|
|
1174
|
-
);
|
|
1175
|
-
return [contractName, contractDeployments];
|
|
1176
|
-
})
|
|
1177
|
-
);
|
|
1178
|
-
const deployer = session.accounts.find((a) => a.name === "deployer");
|
|
1179
|
-
(_a = config.clarinet.project.requirements) == null ? void 0 : _a.forEach(({ contract_id }) => {
|
|
1180
|
-
insertNetworkId(full, contract_id, "mainnet");
|
|
1181
|
-
const contractName = contract_id.split(".")[1];
|
|
1182
|
-
if (deployer) {
|
|
1183
|
-
const devnetId = `${deployer.address}.${contractName}`;
|
|
1184
|
-
insertNetworkId(full, devnetId, "devnet");
|
|
1185
|
-
}
|
|
1186
|
-
});
|
|
1187
|
-
session.contracts.forEach((contract) => {
|
|
1188
|
-
insertNetworkId(full, contract.contract_id, "devnet");
|
|
1189
|
-
insertNetworkId(full, contract.contract_id, "simnet");
|
|
1190
|
-
});
|
|
1191
|
-
return full;
|
|
1192
|
-
}
|
|
1193
|
-
function collectDeploymentFiles(deployments, clarinetFolder, cwd) {
|
|
1194
|
-
if (!deployments.simnet) return [];
|
|
1195
|
-
const simnet = deployments.simnet;
|
|
1196
|
-
const txs = getContractTxs(simnet.plan.batches);
|
|
1197
|
-
const entries = txs.map((tx) => {
|
|
1198
|
-
const id = getIdentifier(tx);
|
|
1199
|
-
const contractFile = getDeploymentTxPath(tx);
|
|
1200
|
-
return {
|
|
1201
|
-
identifier: id,
|
|
1202
|
-
file: relative4(cwd, join2(clarinetFolder, contractFile))
|
|
1203
|
-
};
|
|
1204
|
-
});
|
|
1205
|
-
return entries;
|
|
1206
|
-
}
|
|
1207
|
-
function generateSimnetCode(config, deployments, _session) {
|
|
1208
|
-
var _a;
|
|
1209
|
-
if (!((_a = config.esm) == null ? void 0 : _a.include_accounts)) return "";
|
|
1210
|
-
const clarinetFolder = dirname3(config.clarinetFile());
|
|
1211
|
-
const files = collectDeploymentFiles(deployments, clarinetFolder, config.cwd);
|
|
1212
|
-
return `
|
|
1213
|
-
export const simnetDeployment = ${JSON.stringify(files)};
|
|
1214
|
-
`;
|
|
1215
|
-
}
|
|
1216
|
-
async function afterESM(config) {
|
|
1217
|
-
var _a;
|
|
1218
|
-
const command = (_a = config.esm) == null ? void 0 : _a.after;
|
|
1219
|
-
if (!command) return;
|
|
1220
|
-
logger.debug(`Running after ESM command: ${command}`);
|
|
1221
|
-
const parts = command.split(" ");
|
|
1222
|
-
const [cmd, ...args] = parts;
|
|
1223
|
-
return new Promise((resolve3, reject) => {
|
|
1224
|
-
const child = spawn2(cmd, args, {
|
|
1225
|
-
cwd: config.cwd,
|
|
1226
|
-
stdio: "inherit"
|
|
1227
|
-
});
|
|
1228
|
-
child.on("error", reject);
|
|
1229
|
-
child.on("exit", (code) => {
|
|
1230
|
-
if (code === 0) {
|
|
1231
|
-
resolve3();
|
|
1232
|
-
} else {
|
|
1233
|
-
reject(new Error(`Command failed with code ${code ?? "unknown"}`));
|
|
1234
|
-
}
|
|
1235
|
-
});
|
|
1236
|
-
});
|
|
1237
|
-
}
|
|
1238
|
-
export {
|
|
1239
|
-
CONFIG_FILE,
|
|
1240
|
-
ClarinetConfig,
|
|
1241
|
-
Config,
|
|
1242
|
-
ConfigFile,
|
|
1243
|
-
DEPLOYMENT_NETWORKS,
|
|
1244
|
-
FN_TYPES,
|
|
1245
|
-
OutputType,
|
|
1246
|
-
TYPE_IMPORTS,
|
|
1247
|
-
VAR_TYPES,
|
|
1248
|
-
abiArgType,
|
|
1249
|
-
abiFunctionType,
|
|
1250
|
-
afterDocs,
|
|
1251
|
-
afterESM,
|
|
1252
|
-
collectContractDeployments,
|
|
1253
|
-
collectDeploymentFiles,
|
|
1254
|
-
configFilePath,
|
|
1255
|
-
createContractDocInfo,
|
|
1256
|
-
cwdRelative,
|
|
1257
|
-
defaultConfigFile,
|
|
1258
|
-
encodeVariableName,
|
|
1259
|
-
encodeVariables,
|
|
1260
|
-
fileExists,
|
|
1261
|
-
flatBatch,
|
|
1262
|
-
generateBaseFile,
|
|
1263
|
-
generateContractMeta,
|
|
1264
|
-
generateDocs,
|
|
1265
|
-
generateESMFile,
|
|
1266
|
-
generateMarkdown,
|
|
1267
|
-
generateReadme,
|
|
1268
|
-
getArgName,
|
|
1269
|
-
getClarinetConfig,
|
|
1270
|
-
getConfig,
|
|
1271
|
-
getContractTxs,
|
|
1272
|
-
getDeploymentContract,
|
|
1273
|
-
getDeploymentTxPath,
|
|
1274
|
-
getDeployments,
|
|
1275
|
-
getFnName,
|
|
1276
|
-
getIdentifier,
|
|
1277
|
-
getVariablesV2,
|
|
1278
|
-
isComment,
|
|
1279
|
-
isContractTx,
|
|
1280
|
-
jsTypeFromAbiType,
|
|
1281
|
-
mapVariables,
|
|
1282
|
-
markdownFunction,
|
|
1283
|
-
parseComments,
|
|
1284
|
-
parseDeployment,
|
|
1285
|
-
saveConfig,
|
|
1286
|
-
serialize,
|
|
1287
|
-
sortContracts,
|
|
1288
|
-
traceParens,
|
|
1289
|
-
writeFile
|
|
1290
|
-
};
|
|
1291
|
-
//# sourceMappingURL=index.js.map
|