This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
155 lines (138 loc) · 6.21 KB
/
index.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
const fs = require('fs');
const copyfiles = require('copyfiles');
const movefiles = require('glob-move');
const archiver = require('archiver');
const packFormat = require('pack-format');
type rpkrConfig = { [key: string]: any };
type booleanConfig = { [key: string]: boolean };
type mcmeta = {
pack: { pack_format: number | undefined, description: string, }
language?: {
[lang: string]: { name: string, region: string, }
}
}
const log = (msg: string): void => console.log('<resourcepacker> ' + msg);
const CONFIG: rpkrConfig = {
configver: 2,
name: process.cwd().replace(/^.+[/\\]([^/\\]+)$/, '$1'),
packver: 'v1',
mcver: '1.16.5',
mcsnap: null,
description: 'Default resource pack configuration, which creates an automatic pack.mcmeta file when packing.',
languages: null,
files: [
"pack.png",
"pack.mcmeta",
"assets/**/*.png",
"assets/**/*.mcmeta",
"assets/**/*.json",
"assets/**/lang/**/*.lang",
"assets/**/texts/**/*.txt",
"assets/**/sounds/**/*.ogg",
"assets/**/shaders/**/*.fsh",
"assets/**/shaders/**/*.bin",
],
};
function init({ force }: booleanConfig = { force: false }) {
if (fs.existsSync('.rpkr.json') && !force) {
log('Warning: This folder is already initialised with an .rpkr.json configuration file.');
log('Type `rpkr init force` to overwrite it with default settings.');
}
else fs.writeFile('.rpkr.json', JSON.stringify(CONFIG, null, '\t'), (err: Error) => {
if (err) throw 'FSWriteError: Cannot write to .rpkr.json configuration file.';
else {
if (force) log('Warning: Overwriting existing .rpkr.json configuration file.');
log(`Successfully created .rpkr.json configuration file with default settings.`);
}
});
}
function pack(input: string = '.', output: string, { zipped }: booleanConfig = { zipped: true }) {
fs.readFile('.rpkr.json', 'utf8', (err: Error, contents: string) => {
if (err) init();
let config: rpkrConfig = err ? CONFIG : JSON.parse(contents);
let { name, packver, mcver, mcsnap, description, languages, files = CONFIG.files } = config;
// Create version label
let versionLabel: string = mcver;
if (mcsnap) {
if (mcsnap.match(/\d\dw\d\d\w/)) versionLabel = mcsnap; // snapshot
if (mcsnap.match(/\w+\d+/)) versionLabel = mcver + '-' + mcsnap; // short name of pre and rc
else versionLabel = mcver + ' ' + mcsnap; // full name of pre and rc
}
output = output || `${name} ${packver} (${versionLabel})`;
// Create automatic mcmeta file
const mcmetaTemp = 'temp-pack.mcmeta';
if (description) {
const mcmetaContent: mcmeta = { pack: { pack_format: 0, description: '' } };
mcmetaContent.pack.pack_format = packFormat(versionLabel, 'resource') || packFormat(mcver, 'resource') || 0;
/// Compile description
for (const item in config) {
let escapedItem: string = item.replace(/[-\[\]{}().*+?^$\\\/|]/g, "\\$&");
description = description
.replace(/&([0-9a-fk-or])/g, '§$1') // formatting codes
.replace(RegExp(`<${escapedItem}>`, 'g'), config[item]) // custom parameters
}
mcmetaContent.pack.description = description;
/// Parse languages
if (languages) {
mcmetaContent.language = {};
for (const lang in languages) {
const matcher: RegExp = /^\s*(.*?)\s*\((.*?)\)\s*/i;
let [, name, region] = languages[lang].match(matcher);
mcmetaContent.language[lang] = { name, region };
}
}
/// Write mcmeta to disc
if (!zipped || output.includes('/')) fs.mkdirSync(output, { recursive: true });
fs.writeFileSync(
(zipped ? mcmetaTemp : output + '/pack.mcmeta'),
JSON.stringify(mcmetaContent, null, '\t'),
(err: Error) => {
if (err) log('FSWriteError: Could not create automatic pack.mcmeta file');
else log('Created automatic pack.mcmeta file');
}
);
}
// Localise paths
for (let i in files) {
files[i] = input.replace(/\/$/, '') + '/' + files[i];
}
// Write output to disk
const success = function (s: boolean): void {
const outFile: string = output.replace(/\/$|\.zip$/, '') + (zipped ? '.zip' : '/');
log(`${s ? 'S' : 'Uns'}uccessfully packaged version ${packver} of '${name}' for Minecraft ${mcver} to '${outFile}'`);
}
log(`Packaging version ${packver} of '${name}'...`);
if (zipped) {
const zipFilename: string = output + '.zip';
if (fs.existsSync(zipFilename)) log(`Warning: Overwriting existing file ${zipFilename}.`);
const outFile = fs.createWriteStream(zipFilename);
const zipFile = archiver('zip', { zlib: { level: 9 } });
outFile.on('close', function () {
fs.unlink(mcmetaTemp, (err: Error) => err ? log(`Error: ${err}`) : success(true));
});
zipFile.pipe(outFile); // Start writing to zip file
zipFile.file(mcmetaTemp, { name: 'pack.mcmeta' });
for (let glob of [...files]) zipFile.glob(glob.replace(/^.\//, ''));
zipFile.finalize(); // End writing to zip file
}
else {
copyfiles([...files, output], {}, (err: Error) => {
if (err) {
log(`Error: ${err}`);
success(false);
}
else if (input !== '.') {
const oldOutput = output + '/' + input;
movefiles(oldOutput + '/*', output)
.then(function() {
fs.rmdirSync(oldOutput);
success(true);
})
.catch((err: Error) => log(`Error: ${err}`))
}
else success(true);
});
}
});
}
export = { init, pack };