1#!/usr/bin/env node
2
3// This CLI generates .js.flow definitions from .d.ts
4// TS definitions.
5// It accepts files to generate definitions for via
6// argv;
7
8const path = require('path');
9const fs = require('fs');
10const globby = require('globby');
11
12const { promisify } = require('util');
13const { compiler, beautify } = require('flowgen');
14
15const writeFile = promisify(fs.writeFile);
16const preamble = '// @flow\n\n';
17
18const gen = async () => {
19 const cwd = process.cwd();
20
21 const input = await globby([
22 'src/*.d.ts',
23 'src/**/*.d.ts'
24 ], {
25 gitignore: true
26 });
27
28 if (input.length === 0) {
29 throw new Error('No input files passed as arguments.');
30 }
31
32 console.log(`Compiling ${input.length} TS definitions to Flow...`);
33
34 const defs = input.map(filename => {
35 const fullpath = path.resolve(cwd, filename);
36 const flowdef = beautify(compiler.compileDefinitionFile(fullpath));
37 return { fullpath, flowdef };
38 });
39
40 const write = defs.map(({ fullpath, flowdef }) => {
41 const basename = path.basename(fullpath, '.d.ts');
42 const filepath = path.dirname(fullpath);
43 const newpath = path.join(filepath, basename + '.js.flow');
44
45 return writeFile(newpath, preamble + flowdef, {
46 encoding: 'utf8'
47 });
48 });
49
50 return Promise.all(write);
51};
52
53gen().then(() => {
54 process.exit(0);
55}).catch(err => {
56 console.error(err.message);
57 process.exit(1);
58});