-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
runTypegen.ts
82 lines (68 loc) · 2 KB
/
runTypegen.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
import { readFileSync, writeFileSync } from 'fs';
import { globSync } from 'glob';
import mkdirp from 'mkdirp';
import { basename } from 'path';
import rimraf from 'rimraf';
import { AbiTypeGen } from './AbiTypeGen';
import type { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
import type { IFile } from './types/interfaces/IFile';
import { collectBinFilepaths } from './utils/collectBinFilePaths';
export interface IGenerateFilesParams {
cwd: string;
filepaths?: string[];
inputs?: string[];
output: string;
silent?: boolean;
programType: ProgramTypeEnum;
}
export function runTypegen(params: IGenerateFilesParams) {
const { cwd, inputs, output, silent, programType, filepaths: originalFilepaths } = params;
const cwdBasename = basename(cwd);
let { log } = console;
if (silent) {
log = () => ({});
}
/*
Assembling files array and expanding globals if needed
*/
let filepaths: string[] = [];
if (!originalFilepaths?.length && inputs?.length) {
filepaths = inputs.flatMap((i) => globSync(i, { cwd }));
} else if (originalFilepaths?.length) {
filepaths = originalFilepaths;
} else {
throw new Error('You need to inform at least one parameter: `input` or `filepaths`');
}
/*
Assembling file paths x contents
*/
const abiFiles = filepaths.map((filepath) => {
const abi: IFile = {
path: filepath,
contents: readFileSync(filepath, 'utf-8'),
};
return abi;
});
const binFiles = collectBinFilepaths({ filepaths, programType });
/*
Starting the engine
*/
const abiTypeGen = new AbiTypeGen({
outputDir: output,
abiFiles,
binFiles,
programType,
});
/*
Generating files
*/
log('Generating files..\n');
mkdirp.sync(`${output}/factories`);
abiTypeGen.files.forEach((file) => {
rimraf.sync(file.path);
writeFileSync(file.path, file.contents);
const trimPathRegex = new RegExp(`^.+${cwdBasename}/`, 'm');
log(` - ${file.path.replace(trimPathRegex, '')}`);
});
log('\nDone.⚡');
}