-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (104 loc) · 3.03 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
// const prompt = require('prompt');
const { mkdir } = require('fs').promises;
var fs = require('fs');
var path = require('path');
const indexTemplate = require('./templates/indexTemplate');
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
const availableModule = {
'nginx':'nginx',
'apache':'apache',
'mongodb':'mongodb',
'mysql':'mysql',
'postgres': 'postgres'
};
var actionName = [];
(async() => {
try {
const name = await prompt("Action Name: ");
actionName = name.split(" ");
switch (actionName[0]) {
case availableModule[actionName[0]]:
createFileStructure(actionName[0]);
break;
case 'clean':
cleanFolder(path.join(__dirname, `../plugins/${actionName[1]}`));
rl.close();
break;
default:
console.log("Please Provide Appropriate input");
rl.close();
break;
}
// const lastName = await prompt(`Thanks for the test`);
} catch (e) {
console.error("Unable to prompt", e);
}
})();
rl.on('close', () => process.exit(0));
async function createFileStructure(fi){
let file = path.join(__dirname, `../plugins/${fi}`)
if (!fs.existsSync(file)){
fs.mkdirSync(file, { recursive: true });
fs.writeFile(`${file}/index.tsx`, indexTemplate.content.replace(/{file}/gi,fi[0].toUpperCase() + fi.slice(1)), function (err) {
if (err) throw err;
});
await createSubFolder(file);
rl.close();
}else{
onErr('Already Existed');
}
}
function onErr(msg){
console.log(msg);
}
var folderFile = {
doc :["constant.tsx", "index.tsx"],
schema :["schema.ts"]
// tabs: ["index.tsx"]
};
function createFilesInFolder(dirName,parent){
console.log("In the filname");
folderFile[dirName].map((file) => {
const fileContent = indexTemplate[actionName[0]+''+file.split('.')[0]];
fs.writeFileSync(`${parent}/${dirName}/${file}`, fileContent);
})
};
async function createSubFolder(parent) {
try {
const dirnames = ['doc', 'schema'];
await Promise.all(
dirnames.map(dirname => mkdir(`${parent}/${dirname}`)
.then((res) => {
if(fs.existsSync(`${parent}/${dirname}`)){
createFilesInFolder(dirname,parent);
}
})
.catch((err) => {console.error(err)}))
);
// All dirs are created here or errors reported.
} catch (err) {
console.error(err);
}
}
const cleanFolder = function(path) {
if (fs.existsSync(path)) {
const files = fs.readdirSync(path)
if (files.length > 0) {
files.forEach(function(filename) {
if (fs.statSync(path + "/" + filename).isDirectory()) {
cleanFolder(path + "/" + filename)
} else {
fs.unlinkSync(path + "/" + filename)
}
})
fs.rmdirSync(path)
} else {
fs.rmdirSync(path)
}
} else {
console.log("Integeration path not found.")
}
}