forked from igorek17000/alunajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrapExchange.ts
164 lines (119 loc) · 3.79 KB
/
bootstrapExchange.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import chalk from 'chalk'
import debug from 'debug'
import { existsSync } from 'fs'
import inquirer from 'inquirer'
import { merge } from 'lodash'
import { join } from 'path'
import shelljs from 'shelljs'
import { AlunaAccountEnum } from '../src/lib/enums/AlunaAccountEnum'
import { AlunaApiFeaturesEnum } from '../src/lib/enums/AlunaApiFeaturesEnum'
import { addEntryOnExchangesList } from './boostrap/addEntryOnExchangesList'
import { configureSpecs } from './boostrap/configureSpecs'
import { copySampleFiles } from './boostrap/copySampleFiles'
import { deleteLines } from './boostrap/deleteLines'
import { IBoostrapMethodParams } from './boostrap/IBoostrapMethodParams'
import { patchAlunaSpec } from './boostrap/patchingAlunaSpec'
import { removePositionFeatures } from './boostrap/removePositionFeatures'
import { renameSampleFiles } from './boostrap/renameSampleFiles'
import { replaceSampleContents } from './boostrap/replaceSampleContents'
import { skipTests } from './boostrap/skipTests'
const log = debug('alunajs:scaffold/generate')
export interface IPromptAnswers {
exchangeName: string
tradingFeatures: AlunaAccountEnum[]
apiFeatures: AlunaApiFeaturesEnum[]
}
export interface IScaffoldSettings extends IPromptAnswers {
exchangeUpper: string
exchangeLower: string
}
const ROOT = join(__dirname, '..')
const SRC = join(ROOT, 'src')
const EXCHANGES = join(SRC, 'exchanges')
const SAMPLE_EXCHANGE = join(EXCHANGES, 'sample')
export function buildSettings(params: {
answers: IPromptAnswers
}): IScaffoldSettings {
const { answers } = params
const { exchangeName } = answers
const exchangeId = exchangeName.toLowerCase()
const exchangeUpper = exchangeId.toUpperCase()
const exchangeLower = exchangeId.toLowerCase()
const settings: IScaffoldSettings = merge({}, answers, {
exchangeName,
exchangeUpper,
exchangeLower,
})
return settings
}
export async function bootstrapExchange(answers: IPromptAnswers) {
const settings = buildSettings({ answers })
const {
exchangeName,
exchangeUpper,
exchangeLower,
} = settings
log('generate', { settings })
const DESTINATION = join(EXCHANGES, exchangeLower)
/**
* Conditionally overwritting existing exchange
*/
if (existsSync(DESTINATION)) {
console.error(chalk.red('Destination path exists.'))
const question = [{
type: 'expand',
message: 'Do you want to overrite it?',
name: 'overwrite',
choices: [
{ key: 'y', name: 'Yes, please.', value: true },
{ key: 'n', name: 'No way!', value: false },
],
default: 1,
}]
const answer = await inquirer.prompt(question)
const { overwrite } = answer
if (!overwrite) {
console.error(chalk.gray('Aborting.'))
process.exit()
} else {
shelljs.rm('-rf', DESTINATION)
}
}
/**
* Copying and modifying contents
*/
const bootstrapParams: IBoostrapMethodParams = {
log,
settings,
files: [],
paths: {
ROOT,
SRC,
EXCHANGES,
SAMPLE_EXCHANGE,
DESTINATION,
},
configs: {
exchangeName,
exchangeUpper,
exchangeLower,
},
}
// initializes files array with copied sample files
bootstrapParams.files = copySampleFiles(bootstrapParams)
replaceSampleContents(bootstrapParams)
// update filepaths with renamed filenames
bootstrapParams.files = renameSampleFiles(bootstrapParams)
configureSpecs(bootstrapParams)
// update filepaths, removes position stuff
bootstrapParams.files = removePositionFeatures(bootstrapParams)
// keep on changing stuff
addEntryOnExchangesList(bootstrapParams)
patchAlunaSpec(bootstrapParams)
deleteLines(bootstrapParams)
skipTests(bootstrapParams)
/**
* Done.
*/
console.info('New exchange bootstraped at:\n\t— ', chalk.green(DESTINATION))
}