Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. const fs = require('fs');
  2. const path = require('path');
  3.  
  4. const source = process.argv[2];
  5. const target = process.argv[3];
  6.  
  7. // read contents of source
  8. const contentsOfSource = fs.readFileSync(source, 'utf-8');
  9.  
  10. // get lines of source into an array, remove empty lines
  11. const linesInSource = contentsOfSource.split('\n').filter(Boolean);
  12.  
  13. // make the target dir if it doesn't exist
  14. if (!fs.existsSync(target)) {
  15. fs.mkdirSync(target);
  16. }
  17.  
  18. // iterare over the lines
  19. linesInSource.forEach(line => {
  20. // get the content of the lines, first word is a filename, rest is content
  21. const [ filename, ...contentArr ] = line.split(' ');
  22. // construct the full path for the file to create
  23. const newFilePath = path.join(__dirname, target, filename);
  24.  
  25. // write the file and it's contents
  26. fs.writeFileSync(
  27. newFilePath,
  28. contentArr.join(' '),
  29. { flag: 'w+', encoding: 'utf-8' }
  30. );
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement