Guest User

Untitled

a guest
May 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. fs = require('fs')
  2.  
  3. const needToPrint = ['name','description','version'];
  4. const needToCopy = needToPrint.concat('dependencies');
  5. const newJson = {};
  6.  
  7. fs.readFile('package.json', 'utf8', (err,data) => {
  8.  
  9. if (err) {
  10. return console.log(err);
  11. }
  12.  
  13. let json = JSON.parse(data);
  14.  
  15. for(let key in json){
  16. if (needToPrint.indexOf(key) !== -1) {
  17. console.log(key, ':' , json[key]);
  18. }
  19.  
  20. if (needToCopy.indexOf(key) !== -1) {
  21. newJson[key] = json[key];
  22. }
  23. }
  24.  
  25. fs.writeFile('data.json', JSON.stringify(newJson), err => {
  26. if(err) {
  27. return console.log(err);
  28. }
  29.  
  30. console.log("The file was saved as data.json");
  31. });
  32. });
Add Comment
Please, Sign In to add comment