Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 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. let [ filename, ...contentArr ] = line.split(' ')
  22. contentArr = contentArr.join(' ')
  23. // construct the full path for the file to create
  24. const newFilePath = path.join(__dirname, target, filename)
  25.  
  26. // write the file and it's contents
  27. fs.writeFileSync(
  28. newFilePath,
  29. contentArr,
  30. { flag: 'w+', encoding: 'utf-8' }
  31. )
  32. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement