Skip to content

Instantly share code, notes, and snippets.

@MatthieuLemoine
Last active August 9, 2017 08:53
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 MatthieuLemoine/ff823daffd4ff9d36044869ea19905a2 to your computer and use it in GitHub Desktop.
Save MatthieuLemoine/ff823daffd4ff9d36044869ea19905a2 to your computer and use it in GitHub Desktop.
Refactor from dirname/filename(.test).js to dirname/filename/index(.test).js
/*
node refactor.js --dirs src/actions src/components
Before :
- src
- actions
messenger.js
messenger.test.js
login.js
login.test.js
index.js
index.test.js
- components
messenger.js
messenger.test.js
login.js
login.test.js
After :
- src
- actions
- messenger
index.js
index.test.js
- login
index.js
index.test.js
index.js
index.test.js
- components
- messenger
index.js
index.test.js
- login
index.js
index.test.js
*/
const fs = require('fs');
const path = require('path');
const { dirs } = require('yargs').array('dirs').argv;
dirs.forEach(dir => {
const filenames = fs.readdirSync(dir);
filenames.forEach(filename => {
if (filename.includes('index')) {
return;
}
const [dirname, ...rest] = filename.split('.');
const outputDir = path.join(dir, dirname);
const oldPath = path.join(dir, filename);
const newPath = path.join(outputDir, `index.${rest.join('.')}`);
try {
fs.accessSync(outputDir);
} catch (_) {
fs.mkdirSync(outputDir);
}
fs.renameSync(oldPath, newPath);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment