-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
49 lines (40 loc) · 949 Bytes
/
cli.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
let [, , ...files ] = process.argv;
files.forEach(fileName => {
checkIfFileExists(fileName);
writeFile(fileName);
return fileName;
})
function resolvePath(file){
let rootPath = path.resolve();
let filePath = rootPath+`/${file}`;
return filePath;
}
function createError(message){
let error = new Error();
error.message = message;
throw error;
}
function checkIfFileExists(file) {
const filePath = resolvePath(file);
fs.exists(filePath, (exists) => {
if(exists){
createError(`${file} already exists in path`);
}
})
}
function writeFile(file){
const filePath = resolvePath(file);
fs.writeFile(filePath, "", (err) => {
if(err){
createError('problem_creating_file');
}
return file;
});
}
module.exports = {
checkIfFileExists,
writeFile
}