-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
162 lines (131 loc) · 3.93 KB
/
utils.ts
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
import { execSync } from "child_process";
import * as fs from "fs-extra";
import * as path from "path";
import * as xml from "fast-xml-parser";
import * as admZip from "adm-zip";
const luamin: {
version: string;
minify: (luaCode: string) => string;
} = require("luamin");
const basePath = path.resolve(__dirname, "../");
const distPath = path.join(basePath, "dist/");
const luaPath = path.join(distPath, "lua/");
const configPath = path.join(basePath, "./config.json");
const assetsPath = path.join(basePath, "./assets/");
const modDescPath = path.join(assetsPath, "./modDesc.xml");
interface IProjectConfig {
"buildArchive": boolean,
"minifyScripts": boolean,
"fsUserDir": string,
"log": string,
"fsModDir": string,
"modName": string,
"fsGameDir": string,
"fsGameBin": string,
"launchOptions": Array<string>
}
export function fetchProjectConfig() {
if (fs.existsSync(configPath) === false)
throw `config.json missing`;
return require(configPath) as IProjectConfig;
}
export function allFilesInDirectory(dirPath: string, currFiles: string[] = []): string[] {
const files = fs.readdirSync(dirPath);
files.forEach(file => {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
currFiles = allFilesInDirectory(filePath, currFiles);
} else {
currFiles.push(filePath);
}
});
return currFiles;
}
export function installMod(config: IProjectConfig): boolean {
if (!config.fsModDir) {
console.error(`Could not find key "fsModDir" in config.json`);
return false;
}
fs.ensureDirSync(config.fsModDir);
if (!config.buildArchive) {
const outPath = path.join(config.fsModDir, config.modName);
fs.emptyDirSync(outPath);
fs.copySync(assetsPath, outPath);
fs.copySync(luaPath, outPath);
} else {
const zipName = `${config.modName}.zip`;
const zipPath = path.join(distPath, zipName);
const outPath = path.join(config.fsModDir, zipName);
fs.copySync(zipPath, outPath);
}
return true;
}
export function bundleMod(config: IProjectConfig): boolean {
if (!config.fsModDir) {
console.error(`Could not find key "fsModDir" in config.json`);
return false;
}
fs.ensureDir(assetsPath);
if (!fs.existsSync(modDescPath)) {
console.error(`Could not find "modDesc.xml" in assets folder.`);
return false;
} else {
const parser = new xml.XMLParser();
try {
const content = fs.readFileSync(modDescPath, {
encoding: "utf-8"
});
parser.parse(content);
}
catch (err) {
console.error(err);
}
}
// Clean the dist folder
fs.emptyDirSync(distPath);
// Transpile
console.info("Transpiling TypeScript to Lua...");
execSync('tstl -p tsconfig.json', { stdio: 'inherit' });
// Get all the lua files
const luaFiles = allFilesInDirectory(luaPath).filter(filePath => {
return path.extname(filePath) === ".lua";
});
if (config.minifyScripts) {
console.info("Minifiying scripts...");
let rawSize = 0;
let finalSize = 0;
luaFiles.forEach(filePath => {
try {
rawSize += fs.statSync(filePath).size;
let contents = fs.readFileSync(filePath, {
encoding: "utf-8"
});
contents = luamin.minify(contents.toString());
fs.writeFileSync(filePath, contents);
finalSize += fs.statSync(filePath).size;
}
catch(err) {
console.error(err);
return false;
}
});
console.info(`Saved: ${rawSize-finalSize} bytes`);
}
if (config.buildArchive) {
console.info("Building archive...");
const zipName = `${config.modName}.zip`;
const zipPath = path.join(distPath, zipName);
fs.removeSync(zipPath);
try {
const zip = new admZip();
zip.addLocalFolder(luaPath);
zip.addLocalFolder(assetsPath);
zip.addZipComment(`Built with TypeScript`);
zip.writeZip(zipPath);
}
catch (err) {
console.error(err);
}
}
return true;
}