-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
94 lines (91 loc) · 2.52 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Config } from './config.ts'
import {
colors,
Command,
createRequire,
InputSettings,
Wrapper,
wrapper,
} from './deps.ts'
import * as api from './mod.ts'
const require = createRequire(import.meta.url)
const cwd = Deno.cwd()
await new Command()
.name('sol_build')
.description('Deno CLI for compiling Solidity smart contracts.')
.version('0.0.0')
.command('init')
.description(
'Initializes a basic project with a sample contract and a compiler binary.',
)
.arguments('<name:string>')
.option('-v, --version <version:string>', 'Solidity compiler version')
.action(async ({ version }, name) => {
console.log(colors.cyan('Initializing a new project'))
await api.initProject(name, version!)
})
.command('compile')
.description('Compile Solidity file')
.option(
'--optimizer [runs:number]',
'Enable optimizer (optionally with custom number of runs)',
)
.option('--config, -c <file:string>', 'Config file')
.option('--abi', 'Generate only ABIs')
.action(async ({ optimizer, abi, config }) => {
let solc: Wrapper
try {
solc = wrapper(require('./.solc.js'))
} catch {
return console.error(
colors.red(
`Error: Solidity compiler is not installed, run sol_build init first.`,
),
)
}
let count = 0
let configAbi: boolean | undefined = false
let configModule: InputSettings = {}
if (config) {
const { abi, ...cfg }: Config = await import(
`${cwd}/${config}`
).then((m) => m.config)
configAbi = abi
configModule = cfg
}
try {
count = await api.compileToFs(solc, {
...configModule,
optimizer: {
enabled: typeof optimizer === 'undefined'
? configModule.optimizer?.enabled
: !!optimizer,
runs: typeof optimizer === 'undefined'
? configModule.optimizer?.runs
: typeof optimizer === 'number'
? optimizer
: 200,
},
outputSelection: {
'*': {
// @ts-ignore types bug
'*': configAbi || abi
? ['abi']
: ['evm.bytecode', 'evm.deployedBytecode', 'abi'],
},
...configModule.outputSelection,
},
})
} catch (e) {
return console.error(
colors.red(`Error: Failed to compile\n`),
e as Error,
)
}
console.log(
colors.green(
`Compiled ${count} Solidity file${count > 1 ? 's' : ''} successfully`,
),
)
})
.parse(Deno.args)