Skip to content

Instantly share code, notes, and snippets.

@ninhxuanluu
Created April 3, 2019 10:18
Show Gist options
  • Save ninhxuanluu/bf98c1509c923b788996fa2fb6d4760d to your computer and use it in GitHub Desktop.
Save ninhxuanluu/bf98c1509c923b788996fa2fb6d4760d to your computer and use it in GitHub Desktop.
Read all names and version in node_modules, generate package.json
/**
* Created by ninh on 6/17/16.
*/
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment