-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateHtml.js
65 lines (48 loc) · 1.93 KB
/
CreateHtml.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
const fs = require('fs');
const HELPER = require('../utils/helper');
const error = require('../utils/error');
module.exports = async (cmd) => {
const DIRECTORY = {
current: process.cwd()
}
class CreateHtml {
constructor(options) {
this.cmd = options;
this.run = {};
}
async cloneRepo(cmd) {
const projectName = HELPER.validateName(cmd.name, true);
const projectPath = DIRECTORY.current + "/" + projectName;
if (!fs.existsSync(projectPath)) {
return HELPER.execAsync(`git clone https://xituz@bitbucket.org/xituz/html.git ${HELPER.validateName(cmd.name, true)}`)
} else {
return "exists"
}
}
async installPackage(cmd) {
return HELPER.execAsync(`cd ${DIRECTORY.current}/${HELPER.validateName(cmd.name, true)} && yarn install`)
}
async updatePackage(cmd) {
const packageJson = `${DIRECTORY.current}/${HELPER.validateName(cmd.name, true)}/package.json`;
let contentPackageJson = fs.readFileSync(packageJson, 'utf8');
let parsePackege = JSON.parse(contentPackageJson);
parsePackege["name"] = `${HELPER.validateName(cmd.name, true)}`;
return fs.writeFileSync(packageJson, JSON.stringify(parsePackege, null, 2), 'utf8');
}
async runCmd(){
const { cmd } = this;
this.run["cloneRepo"] = await this.cloneRepo(cmd);
if (this.run["cloneRepo"] != "exists"){
this.run["updatePackage"] = await this.updatePackage(cmd);
this.run["installPackage"] = await this.installPackage(cmd);
}else{
error(`Project ${HELPER.validateName(cmd.name, true)} is exist`, 1);
}
}
init() {
this.runCmd();
}
}
const html = new CreateHtml(cmd);
html.init();
}