/
build-api.mjs
executable file
·80 lines (69 loc) · 1.94 KB
/
build-api.mjs
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
#!/usr/bin/env node
//@ts-check
import {glob} from 'glob';
import minimist from 'minimist';
import {$, chalk, echo, fs} from 'zx';
/**
* @template {string} Keys
* @typedef {{[K in Keys]: string | string[]} & { _: never[]; }} ParseArgs<Keys>
*/
const args = /** @type {ParseArgs<'tsc'| 'aec'>} */ (
minimist(process.argv.slice(2), {
default: {
tsc: './tsconfig.json',
aec: './api-extractor.json',
},
})
);
const tscPaths = await glob(args.tsc);
const aecPaths = await glob(args.aec);
echo(
chalk.cyan(
`TS Configuration${tscPaths.length > 1 ? 's' : ''}: ${chalk.greenBright(
tscPaths.join(', '),
)}`,
),
);
echo(
chalk.cyan(
`API Extractor configuration${
aecPaths.length > 1 ? 's' : ''
}: ${chalk.greenBright(aecPaths.join(chalk.gray(', ')))}`,
),
);
echo('');
echo(chalk.cyan(`Running tsc (${chalk.greenBright(tscPaths.join(', '))})`));
await Promise.all(tscPaths.map((tscPath) => $`npx tsc -b ${tscPath}`));
echo(
chalk.cyan(
`Running API Extractor (${chalk.greenBright(aecPaths.join(', '))})`,
),
);
await Promise.all(
aecPaths.map(async (aecPath) => {
await $`npx api-extractor run --local --verbose -c ${aecPath}`;
const configFile = await fs.readJson(aecPath);
if (configFile.dtsRollup?.untrimmedFilePath) {
const path = configFile.dtsRollup.untrimmedFilePath.replace(
'<projectFolder>',
configFile.projectFolder ?? '.',
);
echo(
chalk.cyan(
`Replacing "React_2" for "React" from ${chalk.greenBright(path)}`,
),
);
const dtsFile = await fs.readFile(path, 'utf-8');
await fs.writeFile(path, dtsFile.replace(/React_2/g, 'React'));
const mdtsPath = path.replace(/\.d\.ts$/, '.d.mts');
echo(
chalk.cyan(
`Copying ${chalk.greenBright(path)} into ${chalk.greenBright(
mdtsPath,
)}`,
),
);
await fs.copyFile(path, mdtsPath);
}
}),
);