Last active
August 9, 2017 08:53
-
-
Save MatthieuLemoine/ff823daffd4ff9d36044869ea19905a2 to your computer and use it in GitHub Desktop.
Refactor from dirname/filename(.test).js to dirname/filename/index(.test).js
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
/* | |
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