-
Notifications
You must be signed in to change notification settings - Fork 0
/
buggy-script.js
34 lines (27 loc) · 952 Bytes
/
buggy-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
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(' ')
// construct the full path for the file to create
const newFilePath = path.join(__dirname, target, filename)
const content = contentArr.join(' ')
// write the file and it's contents
fs.writeFileSync(
newFilePath,
content,
{ flag: 'w+', encoding: 'utf-8' }
)
})