-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
223 lines (193 loc) · 5.15 KB
/
Gulpfile.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import fetch from "axios"
import gulp from "gulp"
import esbuild from "gulp-esbuild"
import filter from "gulp-filter"
import vinyl from "vinyl-paths"
import rename from "gulp-rename"
import del from "del"
import log from "fancy-log"
import chalk from "chalk"
import git from "git-commit-info"
import cp from "child_process"
import path from "path"
import fs from "fs"
import { dirname } from "dirname-filename-esm"
const __dirname = dirname(import.meta)
function _gitLog(cb) {
const newVersion = git({ cwd: path.join(__dirname, "temp") })
log(
[
`Updated '${chalk.cyan("bot.ts")}'`,
`[${chalk.blueBright(newVersion.shortCommit)}]`,
`${newVersion.date} -`,
`${chalk.grey(newVersion.message)}`,
].join(" ")
)
cb()
}
function _cleanDist() {
return del(["dist/**/*"])
}
function _cleanTemp() {
return del(["temp"], { force: true })
}
function _checkGulpfile(cb) {
fetch(
"https://raw.githubusercontent.com/CamilleAbella/bot.ts/master/Gulpfile.js"
)
.then((res) => res.data)
.then(async (remote) => {
const local = await fs.promises.readFile(
path.join(__dirname, "Gulpfile.js"),
"utf8"
)
if (remote !== local) {
await fs.promises.writeFile(
path.join(__dirname, "Gulpfile.js"),
remote,
"utf8"
)
log(
`${chalk.red("Gulpfile updated!")} Please re-run the ${chalk.cyan(
"update"
)} command.`
)
process.exit(0)
} else cb()
})
.catch(cb)
}
function _downloadTemp(cb) {
cp.exec("git clone https://github.com/CamilleAbella/bot.ts.git temp", cb)
}
function _build() {
return gulp
.src("src/**/*.ts")
.pipe(
esbuild({
sourcemap: "inline",
format: "esm",
target: "node16",
loader: {
".ts": "ts",
},
})
)
.pipe(gulp.dest("dist"))
}
function _watch(cb) {
const spawn = cp.spawn("nodemon dist/index --delay 1", { shell: true })
spawn.stdout.on("data", (data) => {
console.log(`${data}`.trim())
})
spawn.stderr.on("data", (data) => {
console.error(`${data}`.trim())
})
spawn.on("close", () => cb())
gulp.watch("src/**/*.ts", { delay: 500 }, gulp.series(_cleanDist, _build))
}
function _copyTemp() {
return gulp
.src(
[
"temp/src/app/*.ts",
"temp/**/*.native.ts",
"temp/src/index.ts",
"temp/.gitattributes",
"temp/.gitignore",
"temp/.github/workflows/**/*.native.*",
"temp/template.env",
"temp/tsconfig.json",
"temp/tests/**/*.js",
"!temp/src/app/database.ts",
],
{ base: "temp" }
)
.pipe(gulp.dest(__dirname, { overwrite: true }))
}
function _updateDependencies(cb) {
const localPackageJSON = JSON.parse(fs.readFileSync("./package.json", "utf8"))
const remotePackageJSON = JSON.parse(
fs.readFileSync("./temp/package.json", "utf8")
)
localPackageJSON.main = remotePackageJSON.main
localPackageJSON.engines = {
...localPackageJSON.engines,
...remotePackageJSON.engines,
}
localPackageJSON.scripts = {
...localPackageJSON.scripts,
...remotePackageJSON.scripts,
}
for (const baseKey of ["dependencies", "devDependencies"]) {
const dependencies = localPackageJSON[baseKey]
const newDependencies = remotePackageJSON[baseKey]
for (const key of Object.keys(newDependencies)) {
if (/^(?:sqlite3|pg|mysql2)$/.test(key)) continue
if (
!dependencies.hasOwnProperty(key) ||
dependencies[key] !== newDependencies[key]
) {
log(
`Updated '${chalk.cyan(key)}' [${
dependencies[key]
? `${chalk.blueBright(dependencies[key])} => ${chalk.blueBright(
newDependencies[key]
)}`
: chalk.blueBright(newDependencies[key])
}]`
)
dependencies[key] = newDependencies[key]
}
}
}
if (fs.existsSync("./package-lock.json")) fs.unlinkSync("./package-lock.json")
fs.writeFileSync(
"./package.json",
JSON.stringify(localPackageJSON, null, 2),
"utf8"
)
cp.exec("npm i", cb)
}
function _updateDatabaseFile() {
const packageJSON = JSON.parse(fs.readFileSync("./package.json", "utf8"))
const database = ["mysql2", "sqlite3", "pg"].find(
(name) => name in packageJSON.dependencies
)
return gulp
.src("node_modules/make-bot.ts/templates/" + database)
.pipe(rename("database.ts"))
.pipe(gulp.dest("src/app"))
}
function _removeDuplicates() {
return gulp
.src([
"src/**/*.native.ts",
"!src/app.native.ts",
"temp/.github/workflows/**/*.native.*",
])
.pipe(
filter((file) =>
fs.existsSync(
path.join(
file.dirname,
file.basename.replace(".native" + file.extname, file.extname)
)
)
)
)
.pipe(vinyl(del))
}
export const build = gulp.series(_cleanDist, _build)
export const watch = gulp.series(_cleanDist, _build, _watch)
export const update = gulp.series(
_checkGulpfile,
_cleanTemp,
_downloadTemp,
_copyTemp,
_removeDuplicates,
_updateDependencies,
_updateDatabaseFile,
_gitLog,
_cleanTemp
)