Skip to content

Instantly share code, notes, and snippets.

@Thersis94
Created October 2, 2019 04:35
Show Gist options
  • Save Thersis94/37b72f233d1228788c2870d70d7dfe89 to your computer and use it in GitHub Desktop.
Save Thersis94/37b72f233d1228788c2870d70d7dfe89 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const source = process.argv[2];
const target = process.argv[3];
// read contents of source
const contentsOfSource = fs.readFileSync(source, 'utf-8');
// 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);
}
// iterare over the lines
linesInSource.forEach(line => {
// get the content of the lines, first word is a filename, rest is content
const [ filename, ...contentArr ] = line.split(' ');//You can either replacee the '.split' with a regex that only splits at the first space or....
// 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,//You can use '.join(' ')' in order to make a string with the spaces added back in.
{ flag: 'w+', encoding: 'utf-8' }
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment