-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-functions.ts
More file actions
285 lines (270 loc) · 7.6 KB
/
build-functions.ts
File metadata and controls
285 lines (270 loc) · 7.6 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
import { readFileSync, writeFileSync, mkdirSync, createWriteStream, existsSync, unlinkSync, symlinkSync, rmSync, cpSync, statSync, PathLike } from 'fs';
import { homedir } from 'os';
import chalk from 'chalk';
import prompts from 'prompts'
import { execSync } from 'child_process';
import semver, { SemVer } from 'semver'
import archiver from 'archiver'
import path from 'path';
// 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): { version: string | null; name: string | null; } {
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: false,
},
{
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 ? 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 getNextVersion(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: `---------------------------------------------------------------------------------------------------
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);
if (!parsedChangelog) {
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 Patchnotes:\n',parsedChangelog)
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 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
}