-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
159 lines (135 loc) · 4.85 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Copyright (c) 2021-present, Ullas Kunder, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'use strict';
process.on('unhandledRejection', err => {
throw err;
});
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const { colorRef: pixidust } = require('./bin/utility')
const directory = path.join(__dirname, 'templates/jhcTemplate');
const currentRoot = path.basename(path.resolve(process.cwd()));
const htmlfile = path.join(process.cwd(), 'index.html')
let canRun = false;
let projectTitle = 'My Project';
let options = `${pixidust.FgGreen}yes|${pixidust.resetBCol, pixidust.FgRed}|no${pixidust.resetBCol}`;
let setTitle = `${pixidust.FgYellow}${projectTitle}${pixidust.resetBCol}`
let projectType = ['1: Basic web', '2: git init'];
var readLine = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function asyncCreateFiles(callback) {
fs.readdir(directory, (err, files) => {
console.log('creating required files ...in a moment ♟');
if (err) {
return console.log('Unable to scan project dirctory' + err);
}
files.forEach((file) => {
var readStream = fs.createReadStream(path.posix.join(directory, file), 'utf-8');
var writeStream = fs.createWriteStream(file);
readStream.pipe(writeStream)
})
})
setTimeout(() => {
callback()
}, 1000)
}
function alterConetent() {
let style = `${pixidust.FgMagenta}style${pixidust.resetBCol}`;
let index = `${pixidust.FgMagenta}index${pixidust.resetBCol}`;
let script = `${pixidust.FgMagenta}script${pixidust.resetBCol}`;
let wish = `${pixidust.FgRed}HAPPY CODING... ( 。 ◕ ‿ ◕ 。 )${pixidust.resetBCol}`;
let currentWorkDir = `${pixidust.BgMagenta} ${currentRoot} ${pixidust.resetBCol}`
console.log('Applying regression...✨');
fs.readFile(htmlfile, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
var result = data.replace(/Document/g, `${projectTitle}`);
fs.writeFile(htmlfile, result, 'utf8', function (err) {
if (err) return console.log(err);
});
console.log(`Okkk your working directroy is: ${currentWorkDir}`);
console.log('\r\n');
console.log(`${style} file has been generated: with RESET ✔`)
console.log(`${index} file has been generated: ✔`)
console.log(`${script} file has been generated: ✔`)
console.log('\r\n');
console.log(`${wish}`);
});
}
function query(qparams) {
return new Promise(resolve => readLine.question(qparams, asn => {
resolve(asn);
}))
}
async function webProject() {
let isYes = await query(`Do you like to change the project name? (${options})`)
if (isYes === 'yes') {
canRun = true;
let answer = await query(`What should be the title ? (${setTitle}):`)
console.log(answer);
if (canRun && answer) {
projectTitle = answer;
asyncCreateFiles(alterConetent)
} else {
console.log('close...');
readLine.close();
}
readLine.close();
}
else if (isYes === 'no') {
canRun = true;
if (canRun) {
asyncCreateFiles(alterConetent)
}
readLine.close();
}
else {
console.log('what the hack');
readLine.close();
}
readLine.close()
}
function initGit() {
const { exec } = require("child_process");
exec("git init", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message} or git may be not installed`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
});
}
function init() {
readLine.question(`What project you like to create? \n${projectType[0] + "\n" + projectType[1]}\n>`, (ans) => {
switch (ans) {
case "1":
webProject()
break;
case "2":
console.log('git has been initialized :)');
initGit()
readLine.close()
default:
break;
}
})
}
init()