11import * as fs from 'fs'
22import path from 'path'
3- import { fileURLToPath } from 'url' ;
4- import { exec , execSync } from 'node:child_process' ;
5-
6- const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
73
4+ import { argvs , exchangeArgv , execSync , cp , capitalize , regexAll } from './utils' ;
85
9- // ##################### helpers ##################### //
10-
11- function cp ( source : string , destination : string ) : void {
12- // check if source is file or dir
13- if ( ! fs . existsSync ( source ) ) {
14- throw new Error ( `Source file/directory does not exist: ${ source } ` ) ;
15- }
16- const stats = fs . statSync ( source ) ;
17- if ( stats . isFile ( ) ) {
18- // get parent directory
19- const parentDir = path . dirname ( destination ) ;
20- // check if parent directory exists
21- if ( ! fs . existsSync ( parentDir ) ) {
22- fs . mkdirSync ( parentDir , { recursive : true } ) ;
23- }
24- fs . copyFileSync ( source , destination ) ;
25- return ;
26- }
27- if ( ! fs . existsSync ( destination ) ) {
28- fs . mkdirSync ( destination , { recursive : true } ) ;
29- }
30- const files = fs . readdirSync ( source ) ;
31- for ( const file of files ) {
32- const srcPath = path . join ( source , file ) ;
33- const destPath = path . join ( destination , file ) ;
34- cp ( srcPath , destPath ) ;
35- }
36- }
37-
38- function capitalize ( str ) {
39- return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
40- }
6+ import { fileURLToPath } from 'url' ;
7+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
418
42- // ################################################### //
439
4410
4511
@@ -60,7 +26,7 @@ class build {
6026 this . init ( exchange ) ;
6127 }
6228
63- async downloadRepo ( ) {
29+ async downloadCcxtRepo ( ) {
6430 try {
6531 execSync ( 'rm -rf ccxt/' , { stdio : 'ignore' } ) ;
6632 } catch ( ex ) {
@@ -69,7 +35,7 @@ class build {
6935 execSync ( 'git clone --depth 1 https://github.com/ccxt/ccxt.git' ) ;
7036 }
7137
72- copyFiles ( exchange :string ) : void {
38+ copyCcxtFiles ( exchange :string ) : void {
7339 const sourceDir = this . sourceFolder + '/python/ccxt/' ;
7440 const copyList = [
7541 // exchange files
@@ -101,21 +67,11 @@ class build {
10167 }
10268 }
10369
104- regexAll ( text : string , array : any [ ] ) {
105- for ( const i in array ) {
106- const regexValue = array [ i ] [ 0 ]
107- const flags = ( typeof regexValue === 'string' ) ? 'g' : undefined
108- const regex = new RegExp ( regexValue , flags )
109- text = text . replace ( regex , array [ i ] [ 1 ] )
110- }
111- return text
112- }
113-
11470 async cleanInitFile ( filePath : string , async = false ) {
11571 let fileContent = fs . readFileSync ( filePath , 'utf8' ) ;
11672 for ( const id of this . allExchangesList ) {
11773 if ( id !== this . exchange ) {
118- fileContent = this . regexAll ( fileContent , [
74+ fileContent = regexAll ( fileContent , [
11975 [ new RegExp ( `from ccxt\.${ id } import ${ id } .+\n` ) , '' ] ,
12076 [ new RegExp ( `from ccxt\.async_support\.${ id } import ${ id } .+\n` ) , '' ] ,
12177 [ new RegExp ( `from ccxt\.pro\.${ id } import ${ id } .+\n` ) , '' ] ,
@@ -183,19 +139,22 @@ class build {
183139 fs . writeFileSync ( __dirname + '/../meta.json' , stringified ) ;
184140 }
185141
186- capitalize ( str : string ) {
187- return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
188- }
189-
190142 replaceGlobalRegexes ( text : string , array : any [ ] ) {
191143 let newText = text ;
192- newText = this . regexAll ( newText , [
144+ newText = regexAll ( newText , [
193145 [ '__exchangeName__' , this . exchange ] ,
194- [ '__ExchangeName__' , this . capitalize ( this . exchange ) ] ,
146+ [ '__ExchangeName__' , capitalize ( this . exchange ) ] ,
195147 ] ) ;
148+ const otherStrings = {
149+ '__LINK_TO_OFFICIAL_EXCHANGE_DOCS__' : 'https://ccxt.com' ,
150+ '__PYTHON_PACKAGE_NAME__' : undefined ,
151+ '__TEST_SYMBOL__' : 'BTC/USDC' ,
152+ } ;
196153 const exchangeConfig = this . globalConfigs [ 'exchanges' ] [ this . exchange ] ;
197- for ( const key in exchangeConfig ) {
198- newText = newText . replace ( new RegExp ( `${ key } ` , 'g' ) , exchangeConfig [ key ] ) ;
154+ for ( const key in otherStrings ) {
155+ const defaultValue = otherStrings [ key ] ;
156+ let value = exchangeConfig [ key ] || defaultValue ; // at first, read from config, if not, use default
157+ newText = newText . replace ( new RegExp ( `${ key } ` , 'g' ) , value ) ;
199158 }
200159 return newText ;
201160 }
@@ -223,9 +182,9 @@ class build {
223182
224183 async init ( exchange :string ) {
225184 if ( this . downloadAndDelete ) {
226- await this . downloadRepo ( ) ;
185+ await this . downloadCcxtRepo ( ) ;
227186 }
228- this . copyFiles ( exchange ) ;
187+ this . copyCcxtFiles ( exchange ) ;
229188 await this . setAllExchangesList ( ) ;
230189 await this . creataPackageInitFile ( ) ;
231190
@@ -250,18 +209,5 @@ class build {
250209
251210
252211
253-
254- const argvs = process . argv . slice ( 2 ) ;
255- let exchange = argvs [ 0 ] ;
256- if ( ! exchange || exchange . includes ( '--' ) ) {
257- const nameFile = __dirname + '/../exchange_name' ;
258- if ( fs . existsSync ( nameFile ) ) {
259- exchange = fs . readFileSync ( nameFile , 'utf8' ) . trim ( ) ;
260- }
261- }
262- if ( ! exchange ) {
263- console . error ( 'Please pass exchange name to build script or set it in a "exchange_name" file in the root of the project' ) ;
264- process . exit ( 1 ) ;
265- }
266212const donwloadAndDelete = ! argvs . includes ( '--nodownload' ) ;
267- new build ( exchange , donwloadAndDelete ) ;
213+ new build ( exchangeArgv , donwloadAndDelete ) ;
0 commit comments