Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- const source = process.argv[2];
- const target = process.argv[3];
- // read contents of source file
- const contentsOfSource = fs.readFileSync(source, 'utf8');
- // get lines of source into an array, remove empty lines
- const linesInSource = contentsOfSource.split('\n').filter(Boolean);
- // make the target dir if it doesn't exist
- if (!fs.existsSync(target)) {
- fs.mkdirSync(target);
- }
- //iterate over lines
- linesInSource.forEach(line => {
- // get the content of the lines, first word is a filename, rest is content
- const [filename, ...contentArr] = line.split(' ');
- // construct the full path for the file to create
- const newFilePath = path.join(__dirname, target, filename);
- // write the file and it's contents
- fs.writeFileSync(
- newFilePath,
- contentArr.join(' '),
- { flag: 'w+', encoding: 'utf8' }
- );
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement