Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. .gitignore node_modules
  2. README.md Welcome to my project!
  3. .env NODE_ENV=development
  4. index.js console.log('Hello, world!');
  5. And here is the script file!
  6.  
  7. const fs = require('fs');
  8. const path = require('path');
  9.  
  10. const source = process.argv[2];
  11. const target = process.argv[3];
  12.  
  13. // read contents of source
  14. const contentsOfSource = fs.readFileSync(source, 'utf-8');
  15.  
  16. // get lines of source into an array, remove empty lines
  17. const linesInSource = contentsOfSource.split('\n').filter(Boolean);
  18.  
  19. // make the target dir if it doesn't exist
  20. if (!fs.existsSync(target)) {
  21. fs.mkdirSync(target);
  22. }
  23.  
  24. // iterare over the lines
  25. linesInSource.forEach(line => {
  26. // get the content of the lines, first word is a filename, rest is content
  27. const [ filename, ...contentArr ] = line.split(' ')
  28. // construct the full path for the file to create
  29. const newFilePath = path.join(__dirname, target, filename)
  30.  
  31. // write the file and it's contents
  32. fs.writeFileSync(
  33. newFilePath,
  34. contentArr.join(' '), <-- THIS WAS THE DIFFERENCE. WAS PREVIOUSLY PRINTING WITH COMMAS BECAUSE IT WAS PRINTING THE SPECIFIC INDECIES IN THE ARRAY SEPARATED BY COMMAS RATHER THAN AS ONE STRING
  35. { flag: 'w+', encoding: 'utf-8' }
  36. )
  37. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement