forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdex-integration.ts
164 lines (138 loc) · 4.46 KB
/
dex-integration.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 fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import yargs from 'yargs';
import { pascalCase, camelCase } from 'change-case';
const dexFolderPath = path.resolve('src', 'dex');
const templateFolderPath = path.resolve('dex-template');
const dexNamePlaceholder = /__DexName__/g;
const dexNameCamelPlaceholder = /__DexNameCamel__/g;
const dexNameParamPlaceholder = /__DexNameParam__/g;
const dexNameConstantPlaceholder = /__DexNameConstant__/g;
const paramCasePattern = /^[a-z0-9\-]+$/g;
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const readdirAsync = promisify(fs.readdir);
const mkdirAsync = promisify(fs.mkdir);
interface IOptions {
name: string;
}
async function createFolder(newDexPath: string) {
try {
await mkdirAsync(newDexPath);
} catch (e) {
if (e && (e as any).code === 'EEXIST') {
throw new Error(
`Dex folder ${
newDexPath.split('/').slice(-1)[0]
} already exists. Please provide original name`,
);
} else {
throw e;
}
}
}
const dotTemplatePattern = /.template$/g;
function trimTemplateExtension(name: string) {
return name.replace(dotTemplatePattern, '');
}
async function generateDexesFromTemplate(
dexNameParam: string,
newDexPath: string,
) {
const spaceSplitted = dexNameParam.replace(/-/g, ' ');
const dexNamePascal = pascalCase(spaceSplitted);
const dexNameCamel = camelCase(spaceSplitted);
const dexNameConstant = dexNameParam.replace(/-/g, '_').toUpperCase();
const fileNames = await readdirAsync(templateFolderPath);
for (const fileTemplateName of fileNames) {
let fileName = trimTemplateExtension(fileTemplateName).replace(
dexNamePlaceholder,
dexNameParam,
);
const fromPath = path.join(templateFolderPath, fileTemplateName);
const toPath = path.join(newDexPath, fileName);
const templateFileContent = await readFileAsync(fromPath, {
encoding: 'utf8',
});
const newDexFileContent = templateFileContent
.replace(dexNamePlaceholder, dexNamePascal)
.replace(dexNameCamelPlaceholder, dexNameCamel)
.replace(dexNameParamPlaceholder, dexNameParam)
.replace(dexNameConstantPlaceholder, dexNameConstant);
await writeFileAsync(toPath, newDexFileContent);
}
}
function checkArgvName(argv: IOptions) {
const { name: dexNameParam } = argv;
if (dexNameParam === undefined) {
throw new Error('You must specify dex name as the first argument');
}
if (!paramCasePattern.test(dexNameParam)) {
throw new Error(
`You should provide name in params case. Only allowed to use "a-z", "0-9" and "-" without spaces. Received: ${dexNameParam}`,
);
}
return dexNameParam;
}
async function initIntegration(argv: IOptions) {
const dexNameParam = checkArgvName(argv);
const newDexPath = path.join(dexFolderPath, dexNameParam);
console.log(`Adding new Dex Integration for ${dexNameParam}. Please wait...`);
await createFolder(newDexPath);
console.log(`Folder ${dexNameParam} is created`);
await generateDexesFromTemplate(dexNameParam, newDexPath);
console.log(`New dex ${dexNameParam} is integrated`);
}
function testIntegration(argv: IOptions) {
const dexNameParam = checkArgvName(argv);
const importLocal = require('import-local');
if (!importLocal(__filename)) {
if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}
require('../node_modules/jest-cli/build/cli').run(
`src\/dex\/${dexNameParam}\/.+\.test\.ts`,
);
}
}
yargs
.scriptName('dex-integration.ts')
.usage('$0 <cmd> [args]')
.command(
'init [name]',
'Integrate new dex by the "name" from templates',
yargsLocal => {
yargsLocal.positional('name', {
type: 'string',
describe:
'Name of the new dex. Must be in param cases using only "a-z", "0-9", and "-" without spaces',
});
},
function (argv: { name: string }) {
initIntegration(argv).catch(error => {
console.error(`${error}`);
});
},
)
.command(
'test [name]',
'Execute all tests for particular dex accessing by it\'s "name"',
yargsLocal => {
yargsLocal.positional('name', {
type: 'string',
describe: 'Name of the dex to test',
});
},
function (argv) {
try {
testIntegration(argv);
} catch (error) {
console.error(`${error}`);
}
},
)
.help()
.alias('help', 'h')
.showHelpOnFail(true)
.demandCommand().argv;