-
Notifications
You must be signed in to change notification settings - Fork 151
/
generateDeclarations.mts
47 lines (42 loc) · 1.71 KB
/
generateDeclarations.mts
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
import { $, chalk, fs, globby } from "zx";
import flowgen, { beautify } from "flowgen";
import filedirname from "filedirname";
import dedent from "dedent";
const [, __dirname] = filedirname();
export default async function generateTypeDeclarations() {
await $`ts-node --esm config/typeFiles.mts`;
console.log(chalk.greenBright.bold("Generating type declarations..."));
await $`cpy "**/*.js.flow" ../lib --cwd src --parents`;
await $`cpy "**/*.js.flow" ../es --cwd src --parents`;
await $`cpy "**/types.d.ts" ../lib --cwd src --parents`;
await $`cpy "**/types.d.ts" ../es --cwd src --parents`;
await $`del tsconfig.tsbuildinfo`; // reset potential incremental compilation information
await $`tsc --p tsconfig-build.json `;
await $`tsc --p tsconfig-build.json --rootDir src --outDir es --declaration --emitDeclarationOnly --moduleResolution node`;
console.log(chalk.greenBright.bold("Generating flow declarations..."));
const tsDeclarations = await globby("{lib,es}/*.d.ts");
await Promise.all(
tsDeclarations.map(async declaration => {
const flowDeclPath = declaration.replace(".d.ts", ".js.flow");
try {
if (await fs.pathExists(flowDeclPath)) return;
const flowDecl = beautify(
flowgen.compiler.compileDefinitionFile(declaration, {
interfaceRecords: true,
}),
);
const content = ["// @flow", flowDecl].join("\n");
await fs.writeFile(flowDeclPath, content);
} catch (err) {
if (err instanceof Error) {
err.message = dedent`
Failed to create a Flow libdef
${__dirname}/${flowDeclPath}
${err.message}
`;
throw err;
}
}
}),
);
}