-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.ts
52 lines (43 loc) · 1.51 KB
/
dump.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
#!/usr/bin/env node
import * as sf from '.';
import { joinPath, mkdir, writeFile } from './FileSystem';
import { E, O, RA, RNEA, TE, flow, pipe, string } from './modules/fp';
const stringifyJSON = (val: unknown) => JSON.stringify(val, null, 2);
const getDumpPath = (subdir: string) => joinPath(['.', 'dump', subdir]);
const dumpFile = flow(getDumpPath, writeFile);
const program = (dir: string) =>
pipe(
mkdir(joinPath(['.', 'dump'])),
TE.chain(() => sf.loadRawAPI(dir)),
TE.map(stringifyJSON),
TE.tap(dumpFile('api.json')),
TE.mapError(sf.stringifyAPIError),
TE.tapError(dumpFile('error.txt'))
);
// ----------------------------------------------------------------------------
// imperative shell
// ----------------------------------------------------------------------------
/* eslint-disable functional/no-conditional-statements */
/* eslint-disable functional/no-expression-statements */
const args = process.argv.slice(2);
const dir = pipe(
args,
RA.findFirst((s) => s.startsWith('--api-path=')),
O.map(string.split('=')),
O.map(RNEA.tail),
O.map(string.intercalate('=')),
O.getOrElse(() => sf.DEFAULT_OPTIONS.directory)
);
void program(dir)().then((res) => {
if (E.isRight(res)) {
console.log('Parsing successful. Check the output in the dump/ folder.');
process.exit(0);
} else {
if (args.includes('--printErr')) {
console.error(res.left);
} else {
console.error('Error encountered. Check details in the dump/ folder.');
}
process.exit(1);
}
});