Mirror: 馃帺 A tiny but capable push & pull stream library for TypeScript and Flow
at v3.0.0 2.0 kB view raw
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 cwd = process.cwd(); 16const writeFile = promisify(fs.writeFile); 17const readFile = promisify(fs.readFile); 18const preamble = '// @flow\n\n'; 19 20const genEntry = async () => { 21 let entry = await readFile(path.resolve(cwd, 'index.js.flow'), { encoding: 'utf8' }); 22 23 entry = entry.replace(/.\/src/g, '../src'); 24 25 const outputCJS = path.resolve(cwd, 'dist/wonka.js.flow'); 26 const outputES = path.resolve(cwd, 'dist/wonka.es.js.flow'); 27 28 return Promise.all([ 29 writeFile(outputCJS, entry, { encoding: 'utf8' }), 30 writeFile(outputES, entry, { encoding: 'utf8' }) 31 ]); 32}; 33 34const gen = async () => { 35 const input = await globby(['src/*.d.ts', 'src/**/*.d.ts'], { 36 gitignore: true 37 }); 38 39 if (input.length === 0) { 40 throw new Error('No input files passed as arguments.'); 41 } 42 43 console.log(`Compiling ${input.length} TS definitions to Flow...`); 44 45 const defs = input.map(filename => { 46 const fullpath = path.resolve(cwd, filename); 47 const flowdef = beautify(compiler.compileDefinitionFile(fullpath)); 48 return { fullpath, flowdef }; 49 }); 50 51 const write = defs.map(({ fullpath, flowdef }) => { 52 const basename = path.basename(fullpath, '.d.ts'); 53 const filepath = path.dirname(fullpath); 54 const newpath = path.join(filepath, basename + '.js.flow'); 55 56 // Fix incorrect imports 57 const fixedFlowdef = flowdef.replace(/^import \{/g, 'import type {'); 58 59 return writeFile(newpath, preamble + fixedFlowdef, { 60 encoding: 'utf8' 61 }); 62 }); 63 64 return Promise.all([...write, genEntry()]); 65}; 66 67gen() 68 .then(() => { 69 process.exit(0); 70 }) 71 .catch(err => { 72 console.error(err.message); 73 process.exit(1); 74 });