-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (58 loc) · 2.26 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
#! /usr/bin/env node
import i from 'inquirer';
import fs from 'fs-extra';
import { exec } from 'child_process';
import s from 'nanospinner';
import * as ts from 'chalk-template';
const t = ts.template;
const genQ = (ry, n, m, e = {}) => Object.assign({}, { type: ry, name: n, message: m }, e);
const rs = (e, s, [msg, misc], rv) => {
s[e ? 'error' : 'success']({
text: t(
e
? `{red ${msg} failed}\n\nError: ${misc.name}\n${misc.message}`
: `{green ${msg} Successfully!}`
)
})
return rv
}
const { d, cid, tk, p } = await i.prompt([
genQ('input', 'd', "What would you like to name your bot directory?"),
genQ('input', 'cid', "What is your bot's client ID?"),
genQ('password', 'tk', "What is your bot's token?", { mask: '*' }),
genQ('input', 'p', "What would you like your bot's prefix to be?")
]);
//* Creating Bot Directory
const cDS = s.createSpinner(t(`{yellow Creating bot directory...}`)).start();
await fs
.copy(`${process.argv[1].replace(/\\*/gm, '/').replace('/index.js', '')}/base/`, `${process.cwd()}/${d}/`)
.then(
(s) => rs(false, cDS, [`Created bot d`], s),
(f) => rs(true, cDS, ['Directory Creation', f], f)
);
//* Dependency Installation
const deps = s.createSpinner(t(`{yellow Installing dependencies...}`)).start();
await new Promise((resolve, reject) => exec(`cd ${d} && npm install`, (err, stdout, stderr) => err ? reject(stderr) : resolve(stdout)))
.then((v, err) => rs(Boolean(err), deps, err ? ['Dependency Installation', err] : ['Dependencies Installed'], err || v));
//* Environment Setup
const env = s.createSpinner(t(`{yellow Setting up .env...}`)).start();
await fs
.writeFile(
`./${d}/.env`,
fs
.readFileSync(`./${d}/.env`, 'utf8')
.replace(/(\$(TOKEN|CLIENT_ID|PREFIX))/gm, (f) => {
switch (f) {
case '$TOKEN': return tk;
case '$CLIENT_ID': return cid;
case '$PREFIX': return p;
}
}),
{ encoding: 'utf8' }
)
.then(
(v) => rs(false, env, ['Environment Setup'], v),
(f) => rs(true, env, ['Environment Setup', f], f)
);
//* Finished
console.log(t(`{green Bot setup complete!}`))