Skip to content

Instantly share code, notes, and snippets.

@ntucker
Forked from gnbaron/README.md
Last active January 31, 2020 12:06
Show Gist options
  • Save ntucker/8c90aff06ceedccd778c84e23052b39e to your computer and use it in GitHub Desktop.
Save ntucker/8c90aff06ceedccd778c84e23052b39e 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/ntucker/8c90aff06ceedccd778c84e23052b39e 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("**/*.ts*", { cwd, ignore: ["**/*.test.js", "**/*-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