-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
84 lines (73 loc) · 2.13 KB
/
utils.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const readline = require('readline')
const path = require('path')
const fs = require('fs')
const chalk = require('chalk')
const conf = require('conf')
const store = new conf()
const { ext } = require('./config')
// Keeps a history of all folder(s) and file(s) created. Used for undoing build
function saveCreatedItem({key, value}){
store.set(key, value)
return true;
}
// Retrieve list of files and folders created in order to perform undo operation
function retrieveCreatedItem(key){
const folderList = store.get(key);
if(!folderList){
console.log(
chalk.yellowBright.bold("Unknown folder. Navigate to folder containing generated file(s) and folder(s)")
);
process.exit(1)
}else{
return folderList;
}
}
// Clear the list of files and folders created after successful undo
function clearCreatedItem(key){
store.delete(key)
}
// Checks to ensure inputed template file exists and is of the right extension
function checkTemplate(template_file){
if(template_file.split('.').slice(-1)[0] !== `${ext}`){
console.log(
chalk.redBright("Template is not a"),
chalk.redBright.bold(`.${ext}`),
chalk.redBright("file type")
);
process.exit(1);
}
if(!fs.existsSync(template_file)){
console.log(
chalk.redBright(`You have no .${ext} Template file at: `),
chalk.cyanBright(template_file)
);
console.log(
"Use the",
chalk.blueBright.bold('-t'),
"flag for named template file(s) OR",
"run",
chalk.blueBright.bold('egusi create'),
"to create an empty template"
);
process.exit(1);
}
}
//
function userPrompt(message){
let response = false;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
rl.question(message, function(input){
if (input.match(/^y(es)?$/i)) response = true;
})
return response;
}
module.exports = {
userPrompt,
saveCreatedItem,
retrieveCreatedItem,
clearCreatedItem,
checkTemplate,
}