@cashscript/utils 0.10.2 → 0.10.4

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.
@@ -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,43 @@ 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 = JSON.stringify(artifact, null, 2);
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
+ // We remove any undefined values to make the artifact serializable using stringifyAsTs
12
+ const normalisedArtifact = JSON.parse(JSON.stringify(artifact));
13
+ return `export default ${stringifyAsTs(normalisedArtifact)} as const;\n`;
14
+ }
15
+ return JSON.stringify(artifact, null, 2);
16
+ }
17
+ const indent = (level) => ' '.repeat(level);
18
+ function stringifyAsTs(obj, indentationLevel = 1) {
19
+ // For strings, we use JSON.stringify, but we convert double quotes to single quotes
20
+ if (typeof obj === 'string') {
21
+ return JSON.stringify(obj).replace(/'/g, "\\'").replace(/"/g, "'");
22
+ }
23
+ // Numbers and booleans are just converted to strings
24
+ if (typeof obj === 'number' || typeof obj === 'boolean') {
25
+ return JSON.stringify(obj);
26
+ }
27
+ // Arrays are recursively formatted with indentation
28
+ if (Array.isArray(obj)) {
29
+ if (obj.length === 0)
30
+ return '[]';
31
+ const formattedItems = obj.map((item) => `${indent(indentationLevel)}${stringifyAsTs(item, indentationLevel + 1)}`);
32
+ return `[\n${formattedItems.join(',\n')},\n${indent(indentationLevel - 1)}]`;
33
+ }
34
+ // Objects are recursively formatted with indentation
35
+ if (typeof obj === 'object') {
36
+ const entries = Object.entries(obj);
37
+ if (entries.length === 0)
38
+ return '{}';
39
+ const formattedEntries = entries.map(([key, value]) => (`${indent(indentationLevel)}${key}: ${stringifyAsTs(value, indentationLevel + 1)}`));
40
+ return `{\n${formattedEntries.join(',\n')},\n${indent(indentationLevel - 1)}}`;
41
+ }
42
+ throw new Error(`Unsupported type: ${typeof obj}`);
43
+ }
9
44
  //# sourceMappingURL=artifact.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashscript/utils",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
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": "a7c4f652462ddfccd54fc929f06cb16022769df9"
51
+ "gitHead": "d8993655ca9b0074d6d22c1680fcd01b23a62054"
52
52
  }