Skip to content

Instantly share code, notes, and snippets.

@intergalactic-overlords
Created December 3, 2019 14:48
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 intergalactic-overlords/4a85afadd839ae8e0a04ee6d15d9fe53 to your computer and use it in GitHub Desktop.
Save intergalactic-overlords/4a85afadd839ae8e0a04ee6d15d9fe53 to your computer and use it in GitHub Desktop.
replace all package.json byindex.js
var fs = require('fs');
var path = require('path');
const replacePackageByIndex = function(dir) {
const list = fs.readdirSync(dir);
const dirName = path.basename(dir)
list.forEach(function(file) {
if (file === "package.json") {
fs.writeFile(`${dir}/index.js`, createIndexContents(dirName), (err) => {
if (err) {
console.error(err)
}
})
fs.unlink(`${dir}/package.json`, (err) => {
if (err) {
console.error(err)
}
})
} else {
const filePath = dir + '/' + file;
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
replacePackageByIndex(filePath)
}
}
});
}
const createIndexContents = (dirName) => {
return `export { default } from './${dirName}'`
}
replacePackageByIndex('src')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment