Skip to content

Instantly share code, notes, and snippets.

@wendelnascimento
Created May 24, 2019 20:22
Show Gist options
  • Save wendelnascimento/572c0f6380ccc66a0befc18580e7e2d7 to your computer and use it in GitHub Desktop.
Save wendelnascimento/572c0f6380ccc66a0befc18580e7e2d7 to your computer and use it in GitHub Desktop.
Remove flow types from all js and jsx files
const fs = require('fs');
const glob = require('glob-fs')();
const flowRemoveTypes = require('flow-remove-types');
const firstline = require('firstline');
const files = glob.readdirSync('src/**/@(**.js|**.jsx)', {});
files.forEach(async (file) => {
console.log(`Found file ${file}, checking if it has flow annotion on first line`);
const hasFlowAnnotation = await firstline(file);
if (hasFlowAnnotation.includes('@flow')) {
console.log(`The file ${file} has a flow annotation, removing all types`);
const fileToRemoveTypes = fs.readFileSync(file, 'utf-8');
const output = flowRemoveTypes(fileToRemoveTypes);
console.log(`Writing temporary file as ${file}.$$`);
fs.writeFileSync(`${file}.$$`, output.toString());
console.log(`Renaming file back to ${file}`);
fs.rename(`${file}.$$`, file, (err) => {
if (err) {
console.log(`Error renaming file ${file}.$$`, err);
} else {
console.log(`File ${file}.$$ successfuly renamed to ${file}`);
}
})
} else {
console.log(`The file ${file} does not have a flow annotation, skipping`);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment