@@ -4,26 +4,41 @@ import crypto from 'crypto';
4
4
5
5
import { generateERC20Options } from './erc20' ;
6
6
import { generateERC721Options } from './erc721' ;
7
+ import { generateERC1155Options } from './erc1155' ;
7
8
import { generateCustomOptions } from './custom' ;
8
- import { buildGeneric , GenericOptions } from '../build-generic' ;
9
+ import { buildGeneric , GenericOptions , KindedOptions } from '../build-generic' ;
9
10
import { printContract } from '../print' ;
10
11
import { OptionsError } from '../error' ;
11
12
import { findCover } from '../utils/find-cover' ;
12
13
import type { Contract } from '../contract' ;
13
14
14
15
type Subset = 'all' | 'minimal-cover' ;
15
16
16
- export function * generateOptions ( ) : Generator < GenericOptions > {
17
- for ( const kindOpts of generateERC20Options ( ) ) {
18
- yield { kind : 'ERC20' , ...kindOpts } ;
17
+ type Kind = keyof KindedOptions ;
18
+
19
+ export function * generateOptions ( kind ?: Kind ) : Generator < GenericOptions > {
20
+ if ( ! kind || kind === 'ERC20' ) {
21
+ for ( const kindOpts of generateERC20Options ( ) ) {
22
+ yield { kind : 'ERC20' , ...kindOpts } ;
23
+ }
19
24
}
20
25
21
- for ( const kindOpts of generateERC721Options ( ) ) {
22
- yield { kind : 'ERC721' , ...kindOpts } ;
26
+ if ( ! kind || kind === 'ERC721' ) {
27
+ for ( const kindOpts of generateERC721Options ( ) ) {
28
+ yield { kind : 'ERC721' , ...kindOpts } ;
29
+ }
23
30
}
24
31
25
- for ( const kindOpts of generateCustomOptions ( ) ) {
26
- yield { kind : 'Custom' , ...kindOpts } ;
32
+ if ( ! kind || kind === 'ERC1155' ) {
33
+ for ( const kindOpts of generateERC1155Options ( ) ) {
34
+ yield { kind : 'ERC1155' , ...kindOpts } ;
35
+ }
36
+ }
37
+
38
+ if ( ! kind || kind === 'Custom' ) {
39
+ for ( const kindOpts of generateCustomOptions ( ) ) {
40
+ yield { kind : 'Custom' , ...kindOpts } ;
41
+ }
27
42
}
28
43
}
29
44
@@ -37,10 +52,10 @@ interface GeneratedSource extends GeneratedContract {
37
52
source : string ;
38
53
}
39
54
40
- function generateContractSubset ( subset : Subset ) : GeneratedContract [ ] {
55
+ function generateContractSubset ( subset : Subset , kind ?: Kind ) : GeneratedContract [ ] {
41
56
const contracts = [ ] ;
42
57
43
- for ( const options of generateOptions ( ) ) {
58
+ for ( const options of generateOptions ( kind ) ) {
44
59
const id = crypto
45
60
. createHash ( 'sha1' )
46
61
. update ( JSON . stringify ( options ) )
@@ -70,17 +85,26 @@ function generateContractSubset(subset: Subset): GeneratedContract[] {
70
85
}
71
86
}
72
87
73
- export function * generateSources ( subset : Subset ) : Generator < GeneratedSource > {
74
- for ( const c of generateContractSubset ( subset ) ) {
88
+ export function * generateSources ( subset : Subset , uniqueName ?: boolean , kind ?: Kind ) : Generator < GeneratedSource > {
89
+ let counter = 1 ;
90
+ for ( const c of generateContractSubset ( subset , kind ) ) {
91
+ if ( uniqueName ) {
92
+ c . contract . name = `Contract${ counter ++ } ` ;
93
+ }
75
94
const source = printContract ( c . contract ) ;
76
95
yield { ...c , source } ;
77
96
}
78
97
}
79
98
80
- export async function writeGeneratedSources ( dir : string , subset : Subset ) : Promise < void > {
99
+ export async function writeGeneratedSources ( dir : string , subset : Subset , uniqueName ?: boolean , kind ?: Kind ) : Promise < string [ ] > {
81
100
await fs . mkdir ( dir , { recursive : true } ) ;
101
+ let contractNames = [ ] ;
82
102
83
- for ( const { id, source } of generateSources ( subset ) ) {
84
- await fs . writeFile ( path . format ( { dir, name : id , ext : '.cairo' } ) , source ) ;
103
+ for ( const { id, contract, source } of generateSources ( subset , uniqueName , kind ) ) {
104
+ const name = uniqueName ? contract . name : id ;
105
+ await fs . writeFile ( path . format ( { dir, name, ext : '.cairo' } ) , source ) ;
106
+ contractNames . push ( name ) ;
85
107
}
108
+
109
+ return contractNames ;
86
110
}
0 commit comments