Skip to content

Instantly share code, notes, and snippets.

@rxgx
Created July 31, 2019 21:02
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rxgx/6587ee8d24b34b92cd248c37e49e9627 to your computer and use it in GitHub Desktop.
Save rxgx/6587ee8d24b34b92cd248c37e49e9627 to your computer and use it in GitHub Desktop.
Rename JS to JSX with ESLint JSON output
// Run eslint with the rule 'react/jsx-filename-extension' as 'error'
// eslint -o json > files.json
const fs = require('fs');
const json = require('./files.json')
console.log('run...', json.length);
json
.filter(entry => entry.messages.length)
.map(entry => {
entry.rules = entry.messages.map(item => item.ruleId);
return entry;
})
.filter(entry => entry.rules.includes('react/jsx-filename-extension'))
.forEach((entry) => {
fs.rename(entry.filePath, entry.filePath.replace('.js', '.jsx'), (err) => {
if (err) throw err;
console.log(entry.filePath);
console.log(entry.filePath.replace('.js', '.jsx'));
});
});
@vishalvisd
Copy link

How about this, work fine for general use:
find ./src -type f -name '*.js' -not -name '*.jsx' -not -name '*.ejs' -exec bash -c 'grep -l "</" $0' {} \; -exec bash -c 'mv "$0" "${0%.js}.jsx"' {} \;

@danielberndt
Copy link

Thanks @vishalvisd this worked great!
I've modified the grep part to grep -l "</\|/>" $0 such that files which only contain <Comp/> are matched as well.

@cg-tester
Copy link

@vishalvisd
I want to try your recommendation, but I have met "File not found - *.js"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment