generated from DeltaFA/Delta-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-functions.ts
More file actions
424 lines (404 loc) · 12 KB
/
build-functions.ts
File metadata and controls
424 lines (404 loc) · 12 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import { readFileSync, writeFileSync, mkdirSync, createWriteStream, existsSync, symlinkSync, rmSync, cpSync, statSync, readdirSync } from 'fs';
import { homedir } from 'os';
import chalk from 'chalk';
import prompts from 'prompts'
import { execSync } from 'child_process';
import semver from 'semver'
import archiver from 'archiver'
import path from 'path';
import cliSpinners from 'cli-spinners';
import ora from 'ora';
export interface BuildParams {
dir: string,
method: string,
version: string,
name: string,
patchNotes: boolean
}
// Function for handling cancellation
function onCancel(): void {
console.log(chalk.red("Aborting"));
process.exit(128); // SIGINT
}
export function runCommand(command: string, exit = true): string {
try {
const result = execSync(command, {
encoding: "utf8",
cwd: process.cwd(),
});
return result;
} catch (error) {
if (!exit) return error.stdout;
console.error(error.message);
process.exit(1);
}
}
// Function to load JSON files with error handling
export function loadJson(filePath: string): any {
try {
const jsonText = readFileSync(filePath, "utf8");
return JSON.parse(jsonText);
} catch (error) {
console.error(chalk.red(`${path.basename(filePath)}.json is not valid JSON: ${error.message}`));
process.exit(1);
}
}
// Function to load JSON files with error handling
export function loadTxt(filePath: string) {
try {
const data = readFileSync(filePath, "utf-8");
return data
} catch (error) {
console.error(chalk.red(`${path.basename(filePath)}.txt is not valid TEXT file: ${error.message}`));
process.exit(1);
}
}
// Function for confirmation prompt with error handling
async function confirmOrExit(message: string, initial = false): Promise<void> {
try {
const { doContinue } = await prompts(
{
type: "confirm",
name: "doContinue",
message: chalk.yellow(message),
initial,
},
{ onCancel },
);
if (!doContinue) {
console.log(chalk.red("Aborting"));
process.exit(0);
}
} catch (error) {
console.error(chalk.red("Error during confirmation prompt:", error.message));
process.exit(1);
}
}
export async function getDestination(): Promise<string> {
let destPath: string | null;
({ destPath } = await prompts(
{
type: "select",
name: "destPath",
message: "Destination folder",
choices: [
{
title: `Factorio mods folder`,
value: process.platform === "win32" ? path.join(`${process.env.APPDATA}`, '/Factorio/mods') : path.join("~/.factorio/mods"),
},
{
title: `./dist`,
value: "./dist",
},
{
title: 'custom',
value: null,
}
],
},
{ onCancel }
));
if (!destPath) {
({destPath} = await prompts(
{
type: "text",
name: "destPath",
message: "Please provide a path",
initial: homedir(),
validate: value => existsSync(value) ? true : "Path not valid"
}
))
}
console.log(`Export path: ${destPath}`);
return destPath as string;
}
export async function getBuildMethod(): Promise<string> {
let buildMethod: string
({ buildMethod } = await prompts(
{
type: "select",
name: "buildMethod",
message: "Build method",
choices: [
{
title: `Zip`,
value: `zip`,
},
{
title: `Folder`,
value: `folder`,
},
{
title: `Symlink [Requires admin]`,
value: `symlink`,
}
],
},
{ onCancel },
));
return buildMethod;
}
export async function validateVersion(version: string | null, ): Promise<string> {
// Check if version exists
if (!semver.valid(version)) {
({ version } = await prompts(
{
type: "text",
name: "version",
message: version ? "Version" : "Custom Version",
initial: version ?? "1.0.0",
validate: value => semver.valid(value) ? true : `Version ${value} is not a valid semver.`
},
{ onCancel },
));
}
version = version as string
// Version Cleanup
const cleaned = semver.clean(version);
if (cleaned !== version) {
let { clean } = await prompts({
type: "confirm",
name: "clean",
message: `Convert ${version} to cleaned version ${cleaned}?`,
initial: true,
});
if (clean) version = cleaned;
}
return version as string
}
export async function getVersion(currentVersion: string): Promise<string> {
const validatedCurrentVersion = await validateVersion(currentVersion)
if (validatedCurrentVersion !== currentVersion) {
return validatedCurrentVersion
}
const nextPatch = semver.inc(currentVersion, "patch");
const nextMinor = semver.inc(currentVersion, "minor");
const nextMajor = semver.inc(currentVersion, "major");
let nextVersion: string | null;
({ nextVersion } = await prompts(
{
type: "select",
name: "nextVersion",
message: "Version",
choices: [
{
title: `Current: v${currentVersion}`,
value: currentVersion,
},
{
title: `Patch: v${nextPatch}`,
value: nextPatch,
},
{
title: `Minor: v${nextMinor}`,
value: nextMinor,
},
{
title: `Major: v${nextMajor}`,
value: nextMajor,
},
{
title: "Custom",
value: null,
},
],
},
{ onCancel }
));
nextVersion = await validateVersion(nextVersion)
if (nextVersion && semver.lte(nextVersion, currentVersion)) {
await confirmOrExit(`Version ${nextVersion} is not greater than ${currentVersion}. Continue?`, true);
}
return nextVersion
}
// Patch notes
const changelogRegex = /-{99}\n(?<content>Version: (?<version>\d+\.\d+\.\d+)\nDate: (?<date>(?<day>[0-2]?[0-9]|3[0-1])[./-] ?(?<month>1[0-2]|0?[0-9])[./-] ?(?<year>\d+))\n((?!-{99}).+\n?)+)/gm
async function editPatchNotes(version: string, currentPatchNotes?: string): Promise<string> {
const date = new Date;
const { patchNotes } = await prompts(
{
type: "text",
name: "patchNotes",
message: `Please provide the patch notes for v${version}`,
initial: currentPatchNotes ?? `---------------------------------------------------------------------------------------------------
Version: ${version}
Date: ${date.getDay}.${date.getMonth}.${date.getFullYear}
`,
validate: value => changelogRegex.exec(value) ? true : "Invalid Patch Notes"
},
{ onCancel }
);
return (patchNotes as string).trim()
}
export async function getPatchNotes(changelog: string, version: string) {
let parsedChangelog = changelogRegex.exec(changelog.replace(/\r\n/g, "\n"));
if (!parsedChangelog || !parsedChangelog.groups || !parsedChangelog.groups.version ) {
console.log(`Valid patch notes for v${version} not found in changelog.txt`)
parsedChangelog = changelogRegex.exec(await editPatchNotes(version))
}
else {
console.log(chalk.green(`Patch notes for v${version} found in changelog.txt`))
console.log('Current Patch notes:\n',parsedChangelog.groups.content)
}
const { correct } = await prompts(
{
type: "confirm",
name: "correct",
message: chalk.yellow("Are the Patch notes correct?"),
initial: true
}
)
let edit: boolean | null
if (!correct) {
({ edit } = await prompts(
{
type: "confirm",
name: "edit",
message: chalk.yellow("Would you like to edit the Patch notes?"),
initial: true,
},
{ onCancel },
));
if (edit) {
parsedChangelog = changelogRegex.exec(await editPatchNotes(parsedChangelog![0]))
}
};
return parsedChangelog as RegExpExecArray
}
export function exportPatchNotes(changelog: string, version: string) {
let parsedChangelog = changelogRegex.exec(changelog.replace(/\r\n/g, "\n"));
if (!parsedChangelog || !parsedChangelog.groups || !parsedChangelog.groups.version ) {
console.log(`Valid patch notes for v${version} not found in changelog.txt`)
}
else {
console.log(chalk.green(`\nPatch notes for v${version} found in changelog.txt`))
try {
writeFileSync('./dist/patch_notes.txt', parsedChangelog.groups.content)
} catch (error) {
console.error(chalk.red("Error while exporting patch notes:", error))
process.exit(1);
}
}
}
export function savePatchNotes(changelog: string, patchNotes: string, version: string) {
let parsedChangelog = changelogRegex.exec(changelog.replace(/\r\n/g, "\n"))
if (parsedChangelog && parsedChangelog.groups && parsedChangelog.groups.version == version){
let newChangelog = `---------------------------------------------------------------------------------------------------\n${patchNotes.trimEnd()}\n\n${changelog.trimStart()}`.trim()
try {
writeFileSync('./src/changelog.txt', newChangelog)
} catch (error) {
console.error(chalk.red("Error saving changes to the changelog:", error))
process.exit(1);
}
}
}
export async function launchFactorioPrompt(): Promise<boolean> {
const { launch } = await prompts(
{
type: "confirm",
name: "launch",
message: "Would you like to launch factorio?",
initial: true
},
{ onCancel },
);
return launch as boolean
}
export function resolveFactorioPath(): string {
return process.platform === 'win32' ? path.join(`${process.env.APPDATA}`, '/Factorio/mods') : path.join(`${process.env.HOME}`,`.factorio.mods`)
}
export async function build(params:BuildParams, changelog: string) {
const nameRegex = new RegExp(`^${params.name}(_\\d\\.\\d\\.\\d)?(\\.zip)?$`)
const spin = ora(`Building ${params.name} v${params.version}`).start()
spin.spinner = cliSpinners.dots3
const Path = params.method == "zip" ? path.join(params.dir, `${params.name}_${params.version}.zip`): path.join(params.dir, params.name)
if (params.dir == "./dist" && !existsSync("./dist")) mkdirSync("./dist")
if (params.dir == resolveFactorioPath()) {
readdirSync(params.dir).forEach((file) => {
if (nameRegex.exec(path.parse(file).base)) {
rmSync(path.resolve(params.dir, file), {recursive: true});
}});
}
switch (params.method) {
case "zip":
const output = createWriteStream(Path);
const archive = archiver('zip', { zlib: { level: 9 } });
archive.on('error', (err) => {
throw err;
});
output.on('close', () => {
console.log(`${archive.pointer()} total bytes archived.`);
});
archive.pipe(output);
archive.directory(`./src`, `${params.name}_${params.version}`);
await archive.finalize();
break;
case "folder":
cpSync("./src", Path, {recursive: true});
break;
case "symlink":
symlinkSync(path.resolve(`./src`), Path, 'dir')
break;
default:
console.error(chalk.red(`Invalid build method: ${params.method}`))
process.exit(1);
}
if (params.patchNotes) exportPatchNotes(changelog, params.version)
spin.stop()
console.log(chalk.green("Build complete!"))
return 0
}
export async function Release(params:BuildParams, version: string) {
console.log(chalk.bold("Starting release..."))
runCommand("git add ./src/info.json package.json ./src/changelog.txt");
const { message } = await prompts(
{
type: "text",
name: "message",
message: "Commit message",
initial: `Release v${version}`,
validate: (value) => {
if (!value.trim()) return "Commit message is required";
return true;
},
},
{ onCancel },
);
const existingTags = runCommand("git tag --list").split("\n").filter(Boolean);
const { tagName } = await prompts(
{
type: "text",
name: "tagName",
message: "Tag name",
initial: `v${params.version}`,
validate: (value) => {
if (!value.trim()) return "Tag name is required";
if (existingTags.includes(value)) return `Tag ${value} already exists`;
return true;
},
},
{ onCancel },
);
const hasSigningKey = Boolean(runCommand("git config --get user.signingkey", false).trim());
const commitSigningEnabled = runCommand("git config --get commit.gpgsign", false).trim() === "true";
const tagSigningEnabled = runCommand("git config --get tag.gpgsign", false).trim() === "true";
let sign = false;
if (hasSigningKey && (!commitSigningEnabled || !tagSigningEnabled)) {
({ sign } = await prompts({
type: "confirm",
name: "sign",
message: "Sign commit and tag?",
initial: true,
}));
}
// Commit changes
runCommand(`git commit${sign ? " -S" : ""} -m "${message}"`);
// Tag commit
runCommand(`git tag${sign ? " -s" : ""} -a -m "${message}" "${tagName}"`);
// Push changes
await confirmOrExit("Push changes to remote?", true);
runCommand("git push");
// And the tag
runCommand("git push --tags");
console.log(chalk.green("Release complete!"))
}