Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
94
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 file
  8.  
  9. const contentsOfSource = fs.readFileSync(source, 'utf8');
  10.  
  11. // get lines of source into an array, remove empty lines
  12.  
  13. const linesInSource = contentsOfSource.split('\n').filter(Boolean);
  14.  
  15. // make the target dir if it doesn't exist
  16.  
  17. if (!fs.existsSync(target)) {
  18. fs.mkdirSync(target);
  19. }
  20.  
  21. //iterate over lines
  22.  
  23. linesInSource.forEach(line => {
  24. // get the content of the lines, first word is a filename, rest is content
  25. const [filename, ...contentArr] = line.split(' ');
  26. // construct the full path for the file to create
  27. const newFilePath = path.join(__dirname, target, filename);
  28.  
  29. // write the file and it's contents
  30. fs.writeFileSync(
  31. newFilePath,
  32. contentArr.join(' '),
  33. { flag: 'w+', encoding: 'utf8' }
  34. );
  35. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement