Skip to content

Instantly share code, notes, and snippets.

@gnbaron
Last active January 31, 2020 12:05
Show Gist options
  • Save gnbaron/48d417e54fb9527a24422546b3ef763c to your computer and use it in GitHub Desktop.
Save gnbaron/48d417e54fb9527a24422546b3ef763c to your computer and use it in GitHub Desktop.
Convert flow typed JS files to Typescript using @khanacademy/flow-to-ts.

Usage: npx https://gist.github.com/gnbaron/48d417e54fb9527a24422546b3ef763c src/folder

#!/usr/bin/env node
const { readFile, rename, writeFileSync } = require("fs");
const { basename, dirname, join } = require("path");
const glob = require("glob");
const convert = require("@khanacademy/flow-to-ts");
const cwd = process.argv[2];
function convertFile(filePath, cb) {
readFile(filePath, "utf8", (err, src) => {
writeFileSync(filePath, convert(src), "utf8");
cb();
});
}
glob("**/*.js", { cwd, ignore: ["**/*.test.js"] }, (err, files) => {
files.forEach(file => {
const filePath = join(cwd, file);
const destinationPath = join(
dirname(filePath),
basename(filePath, ".js") + ".tsx"
);
rename(filePath, destinationPath, err => {
if (err) throw err;
convertFile(destinationPath, () => {
console.log(`Converted ${file} to Typescript`);
});
});
});
});
{
"name": "convert-flow-to-ts",
"version": "1.0.0",
"bin": "./index.js",
"dependencies": {
"@khanacademy/flow-to-ts": "^0.1.4",
"glob": "^7.1.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment