1
1
const shell = require ( `shelljs` )
2
2
const path = require ( `path` )
3
3
4
- const templateFile = `${ __dirname } /../templates/template-web3.js`
5
-
6
- const tempFile = `/tmp/temp999` // Copy the template to working file for us to modify
4
+ const getTempFile = type => `./temp999-${ type } ` // Copy the template to working file for us to modify
7
5
const fs = require ( `fs` )
8
6
9
7
const portisConfigString = ( { portisApiKey, network, infuraApiKey } ) => {
@@ -45,16 +43,20 @@ const contractDeclarationString = (contracts) => {
45
43
return returnString
46
44
}
47
45
48
- const contractInstantiationString = ( params , contracts ) => {
49
- let { contractOutput } = params
50
- const { web3ModuleOutput } = params
46
+ const contractInstantiationString = ( program , contracts , type ) => {
47
+ let { contractOutput } = program
48
+ const { projectDir } = program
49
+ const web3AdapterPath =
50
+ type === `client` ? program . web3ClientPath : program . web3ServerPath
51
+
51
52
if ( ! contractOutput ) {
52
- contractOutput = `/add/abi/path/to/config `
53
+ contractOutput = `${ projectDir } /build/contracts `
53
54
}
54
55
const relativePath = path . relative (
55
- path . dirname ( web3ModuleOutput ) ,
56
+ path . dirname ( web3AdapterPath ) ,
56
57
contractOutput
57
58
)
59
+
58
60
let returnString = `\n`
59
61
60
62
contracts . forEach ( ( contract ) => {
@@ -76,10 +78,9 @@ const contractInstantiationString = (params, contracts) => {
76
78
return returnString
77
79
}
78
80
79
- const exportConfig = ( program , contracts ) => {
80
- console . log ( `Copying` , templateFile , `to` , tempFile )
81
- shell . cp ( templateFile , tempFile )
82
-
81
+ const exportClientConfig = ( program , contracts ) => {
82
+ const tempFile = getTempFile ( `client` )
83
+ copyTemplate ( `client` )
83
84
shell . sed (
84
85
`-i` ,
85
86
`PORTIS_DECLARATION` ,
@@ -99,14 +100,47 @@ const exportConfig = (program, contracts) => {
99
100
contractInstantiationString ( program , contracts ) ,
100
101
tempFile
101
102
)
102
- const outPath = path . dirname ( program . web3ModuleOutput )
103
+ const outPath = path . dirname ( program . web3ClientPath )
104
+ shell . mkdir ( `-p` , outPath )
105
+ if ( ! fs . existsSync ( outPath ) ) {
106
+ return Promise . reject ( new Error ( `Cannot create web3 adapter at ${ outPath } ` ) )
107
+ }
108
+ console . log ( ` $ Moving` , tempFile , `to` , program . web3ClientPath )
109
+ shell . mv ( tempFile , program . web3ClientPath )
110
+ return Promise . resolve ( true )
111
+ }
112
+
113
+ const copyTemplate = ( type ) => {
114
+ const templateFile = `${ __dirname } /../templates/template-web3-${ type } .js`
115
+ const tempFile = getTempFile ( type )
116
+ console . log ( `Copying` , templateFile , `to` , tempFile )
117
+ shell . cp ( templateFile , tempFile )
118
+ }
119
+
120
+ const exportServerConfig = ( program , contracts ) => {
121
+ const tempFile = getTempFile ( `server` )
122
+ copyTemplate ( `server` )
123
+
124
+ shell . sed (
125
+ `-i` ,
126
+ `CONTRACT_DECLARATIONS` ,
127
+ contractDeclarationString ( contracts ) ,
128
+ tempFile
129
+ )
130
+ shell . sed (
131
+ `-i` ,
132
+ `CONTRACT_INSTANTIATION` ,
133
+ contractInstantiationString ( program , contracts ) ,
134
+ tempFile
135
+ )
136
+ const outPath = path . dirname ( program . web3ServerPath )
103
137
shell . mkdir ( `-p` , outPath )
104
138
if ( ! fs . existsSync ( outPath ) ) {
105
- return Promise . reject ( new Error ( `Cannot create web3 adaptor at ${ outPath } ` ) )
139
+ return Promise . reject ( new Error ( `Cannot create web3 adapter at ${ outPath } ` ) )
106
140
}
107
- console . log ( ` $ Moving` , tempFile , `to` , program . web3ModuleOutput )
108
- shell . mv ( tempFile , program . web3ModuleOutput )
141
+ console . log ( ` $ Moving` , tempFile , `to` , program . web3ServerPath )
142
+ shell . mv ( tempFile , program . web3ServerPath )
109
143
return Promise . resolve ( true )
110
144
}
111
145
112
- module . exports = { exportConfig }
146
+ module . exports = { exportClientConfig , exportServerConfig }
0 commit comments