Skip to content

Instantly share code, notes, and snippets.

@mariofink
Created March 5, 2021 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariofink/1a4566d8b9bc25994037f515e742148d to your computer and use it in GitHub Desktop.
Save mariofink/1a4566d8b9bc25994037f515e742148d to your computer and use it in GitHub Desktop.
const { resolve } = require('path');
const { readdir } = require('fs').promises;
const fs = require('fs');
const result = [];
async function* getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
yield* getFiles(res);
} else {
yield res;
}
}
}
(async () => {
for await (const f of getFiles('./src/components')) {
if (!f.endsWith('.js')) continue;
if (f.endsWith('stories.js') || f.endsWith('.test.js')) {
continue;
}
result.push(f);
}
result.map((filename) => {
fs.readFile(filename, function (err, data) {
if (err) throw err;
if (data.includes("import React from 'react'")) {
let a = filename.split('/');
a.splice(0, 6);
const currentName = a.join('/');
const newName = currentName.replace(/\.js$/, '.jsx');
console.log(`git mv ${currentName} ${newName}`);
}
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment