Usage: npx https://gist.github.com/ntucker/8c90aff06ceedccd778c84e23052b39e src/folder
-
-
Save ntucker/8c90aff06ceedccd778c84e23052b39e to your computer and use it in GitHub Desktop.
Convert flow typed JS files to Typescript using @khanacademy/flow-to-ts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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`); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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