@cashscript/utils 0.10.2 → 0.10.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/dist/artifact.d.ts +5 -4
- package/dist/artifact.js +35 -2
- package/package.json +2 -2
package/dist/artifact.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface AbiInput {
|
|
|
6
6
|
export interface AbiFunction {
|
|
7
7
|
name: string;
|
|
8
8
|
covenant?: boolean;
|
|
9
|
-
inputs: AbiInput[];
|
|
9
|
+
inputs: readonly AbiInput[];
|
|
10
10
|
}
|
|
11
11
|
export interface DebugInformation {
|
|
12
12
|
bytecode: string;
|
|
@@ -32,8 +32,8 @@ export interface RequireStatement {
|
|
|
32
32
|
}
|
|
33
33
|
export interface Artifact {
|
|
34
34
|
contractName: string;
|
|
35
|
-
constructorInputs: AbiInput[];
|
|
36
|
-
abi: AbiFunction[];
|
|
35
|
+
constructorInputs: readonly AbiInput[];
|
|
36
|
+
abi: readonly AbiFunction[];
|
|
37
37
|
bytecode: string;
|
|
38
38
|
source: string;
|
|
39
39
|
debug?: DebugInformation;
|
|
@@ -44,4 +44,5 @@ export interface Artifact {
|
|
|
44
44
|
updatedAt: string;
|
|
45
45
|
}
|
|
46
46
|
export declare function importArtifact(artifactFile: PathLike): Artifact;
|
|
47
|
-
export declare function exportArtifact(artifact: Artifact, targetFile: string): void;
|
|
47
|
+
export declare function exportArtifact(artifact: Artifact, targetFile: string, format: 'json' | 'ts'): void;
|
|
48
|
+
export declare function formatArtifact(artifact: Artifact, format: 'json' | 'ts'): string;
|
package/dist/artifact.js
CHANGED
|
@@ -2,8 +2,41 @@ import fs from 'fs';
|
|
|
2
2
|
export function importArtifact(artifactFile) {
|
|
3
3
|
return JSON.parse(fs.readFileSync(artifactFile, { encoding: 'utf-8' }));
|
|
4
4
|
}
|
|
5
|
-
export function exportArtifact(artifact, targetFile) {
|
|
6
|
-
const jsonString =
|
|
5
|
+
export function exportArtifact(artifact, targetFile, format) {
|
|
6
|
+
const jsonString = formatArtifact(artifact, format);
|
|
7
7
|
fs.writeFileSync(targetFile, jsonString);
|
|
8
8
|
}
|
|
9
|
+
export function formatArtifact(artifact, format) {
|
|
10
|
+
if (format === 'ts') {
|
|
11
|
+
return `export default ${stringifyAsTs(artifact)} as const;\n`;
|
|
12
|
+
}
|
|
13
|
+
return JSON.stringify(artifact, null, 2);
|
|
14
|
+
}
|
|
15
|
+
const indent = (level) => ' '.repeat(level);
|
|
16
|
+
function stringifyAsTs(obj, indentationLevel = 1) {
|
|
17
|
+
// For strings, we use JSON.stringify, but we convert double quotes to single quotes
|
|
18
|
+
if (typeof obj === 'string') {
|
|
19
|
+
return JSON.stringify(obj).replace(/'/g, "\\'").replace(/"/g, "'");
|
|
20
|
+
}
|
|
21
|
+
// Numbers and booleans are just converted to strings
|
|
22
|
+
if (typeof obj === 'number' || typeof obj === 'boolean') {
|
|
23
|
+
return JSON.stringify(obj);
|
|
24
|
+
}
|
|
25
|
+
// Arrays are recursively formatted with indentation
|
|
26
|
+
if (Array.isArray(obj)) {
|
|
27
|
+
if (obj.length === 0)
|
|
28
|
+
return '[]';
|
|
29
|
+
const formattedItems = obj.map((item) => `${indent(indentationLevel)}${stringifyAsTs(item, indentationLevel + 1)}`);
|
|
30
|
+
return `[\n${formattedItems.join(',\n')},\n${indent(indentationLevel - 1)}]`;
|
|
31
|
+
}
|
|
32
|
+
// Objects are recursively formatted with indentation
|
|
33
|
+
if (typeof obj === 'object') {
|
|
34
|
+
const entries = Object.entries(obj);
|
|
35
|
+
if (entries.length === 0)
|
|
36
|
+
return '{}';
|
|
37
|
+
const formattedEntries = entries.map(([key, value]) => (`${indent(indentationLevel)}${key}: ${stringifyAsTs(value, indentationLevel + 1)}`));
|
|
38
|
+
return `{\n${formattedEntries.join(',\n')},\n${indent(indentationLevel - 1)}}`;
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`Unsupported type: ${typeof obj}`);
|
|
41
|
+
}
|
|
9
42
|
//# sourceMappingURL=artifact.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cashscript/utils",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.3",
|
|
4
4
|
"description": "CashScript utilities and types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bitcoin cash",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"jest": "^29.4.1",
|
|
49
49
|
"typescript": "^5.5.4"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "1aa7c6b26ee8add3ff8cf42227141d9bea8aba66"
|
|
52
52
|
}
|