Created
December 3, 2019 14:48
-
-
Save intergalactic-overlords/4a85afadd839ae8e0a04ee6d15d9fe53 to your computer and use it in GitHub Desktop.
replace all package.json byindex.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
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