1import { resolve, basename, dirname, join } from 'path';
2import { writeFileSync } from 'fs';
3import { compiler, beautify } from 'flowgen';
4
5import glob from 'glob';
6
7function flowTypings() {
8 return {
9 name: 'flow-typings',
10 async writeBundle() {
11 const cwd = process.cwd();
12 for (const file of glob.sync('dist/types/**/*.d.ts')) {
13 const fullpath = resolve(cwd, file);
14 const flowdef = beautify(compiler.compileDefinitionFile(fullpath));
15 const name = basename(fullpath, '.d.ts');
16 const filepath = dirname(fullpath);
17 const newpath = join(filepath, name + '.js.flow');
18 const definition = flowdef.replace(/import/g, 'import type');
19 writeFileSync(newpath, '// @flow\n\n' + definition);
20 }
21 },
22 };
23}
24
25export default flowTypings;