-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (142 loc) · 4.5 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
import * as p from '@clack/prompts'
import { setTimeout } from 'node:timers/promises'
import pc from 'picocolors'
import { exec, spawn } from 'node:child_process'
import * as mysql from 'mysql2'
import { exit } from 'node:process'
function wpDownload(folderName) {
console.log('Downloading...')
return new Promise(resolve => {
exec(`wp core download --path=${folderName}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`${stdout}`);
return resolve()
});
});
}
function wpConfig(dbName, folderName) {
console.log('Config...')
return new Promise(resolve => {
exec(`cd ${folderName} && wp config create --dbname=${dbName} --dbuser=root --dbhost=127.0.0.1 --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`${stdout}`);
return resolve()
})
});
}
function wpInstall(folderName, siteTitle) {
console.log('Installing...')
return new Promise(resolve => {
exec(`cd ${folderName} && wp core install --url=https://${folderName}.test --title="${siteTitle}" --admin_user=admin --admin_password=admin [email protected]`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
return resolve()
})
});
}
function createDB(dbName) {
return new Promise(resolve => {
const con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: ""
});
con.connect(err => {
if (err) throw err;
con.query(`CREATE DATABASE IF NOT EXISTS ${dbName}`, function (err, result) {
if (err) throw err;
});
})
return resolve()
});
}
function folderToTitle(folderName) {
const words = folderName.replace(/-/g, ' ').replace(/_/g, ' ').split(' ');
return words.map((word) => {
return word[0].toUpperCase() + word.substring(1);
}).join(" ");
}
function clearPlugins(folderName) {
return new Promise(resolve => {
exec(`cd ${folderName} && wp plugin delete --all`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`${stdout}`);
return resolve()
})
})
}
function secureSite(folderName) {
return new Promise(resolve => {
exec(`cd ${folderName} && herd secure`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`${stdout}`);
return resolve()
})
})
}
async function main() {
console.clear();
p.intro(`${pc.bgMagenta(pc.black(' WP Cli - Site Installer '))}`)
const folderName = (await p.text({
message: "Folder name:",
placeholder: 'My Blog Name',
validate(value) {
if (value.length === 0) return `Value is required!`;
},
})).toLowerCase().replace(/ /g, '-')
const siteTitle = await p.text({
message: "Site title:",
initialValue: folderToTitle(folderName)
})
const dbName = await p.text({
message: "Database name:",
initialValue: folderName.replace(/-/g, '_')
})
await createDB(dbName)
await wpDownload(folderName)
await wpConfig(dbName, folderName)
await wpInstall(folderName, siteTitle)
await clearPlugins(folderName)
await secureSite(folderName)
console.log('Done')
exit()
}
main().catch(console.error);