@jtff/miztemplate-lib 3.0.0-rc2 → 3.0.0-rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/mizlib.js +265 -285
  2. package/package.json +1 -1
package/lib/mizlib.js CHANGED
@@ -7,331 +7,311 @@ const {promisify} = require("util");
7
7
  const fs = require("fs");
8
8
  const lstat = promisify(fs.lstat);
9
9
 
10
- function injectLuaScriptsInMissionObject(tObject, trObject, mrObject, strTitle, scriptFilesArray, timingInSeconds, hexColor) {
11
- let nextIndex = Object.keys(trObject).length + 1;
12
- if (nextIndex === 1) {
13
- tObject['actions'] = {};
14
- tObject['func'] = {};
15
- tObject['conditions'] = {};
16
- tObject['flag'] = {};
17
- trObject = {};
18
- }
19
- let actionSentence = "";
20
- let actionsObject = {};
21
- for (const [index, scriptFile] of scriptFilesArray.entries()) {
22
- actionSentence += "a_do_script_file(getValueResourceByKey(\"" + scriptFile + "\")); "
23
- actionsObject[index + 1] = {
24
- file: scriptFile,
25
- predicate: 'a_do_script_file',
10
+ class mizlib {
11
+ injectLuaScriptsInMissionObject(tObject, trObject, mrObject, strTitle, scriptFilesArray, timingInSeconds, hexColor) {
12
+ let nextIndex = Object.keys(trObject).length + 1;
13
+ if (nextIndex === 1) {
14
+ tObject['actions'] = {};
15
+ tObject['func'] = {};
16
+ tObject['conditions'] = {};
17
+ tObject['flag'] = {};
18
+ trObject = {};
19
+ }
20
+ let actionSentence = "";
21
+ let actionsObject = {};
22
+ for (const [index, scriptFile] of scriptFilesArray.entries()) {
23
+ actionSentence += "a_do_script_file(getValueResourceByKey(\"" + scriptFile + "\")); "
24
+ actionsObject[index + 1] = {
25
+ file: scriptFile,
26
+ predicate: 'a_do_script_file',
27
+ };
28
+ mrObject[scriptFile] = scriptFile;
29
+ }
30
+ actionSentence += "mission.trig.func[" + nextIndex + "]=nil;"
31
+ tObject['actions'][nextIndex] = actionSentence;
32
+ tObject['func'][nextIndex] = "if mission.trig.conditions[" + nextIndex + "]() then mission.trig.actions[" + nextIndex + "]() end";
33
+ tObject['conditions'][nextIndex] = "return(c_time_after(" + timingInSeconds + ") )";
34
+ tObject['flag'][nextIndex] = true;
35
+ trObject[nextIndex] = {
36
+ rules: {
37
+ 1: {
38
+ coalitionlist: 'red',
39
+ seconds: timingInSeconds,
40
+ predicate: 'c_time_after',
41
+ zone: ''
42
+ }
43
+ },
44
+ eventlist: '',
45
+ comment: strTitle,
46
+ actions: actionsObject,
47
+ predicate: 'triggerOnce',
48
+ colorItem: hexColor
26
49
  };
27
- mrObject[scriptFile] = scriptFile;
50
+ return {tObject: tObject, trObject: trObject, mrObject: mrObject};
28
51
  }
29
- actionSentence += "mission.trig.func[" + nextIndex + "]=nil;"
30
- tObject['actions'][nextIndex] = actionSentence;
31
- tObject['func'][nextIndex] = "if mission.trig.conditions[" + nextIndex + "]() then mission.trig.actions[" + nextIndex + "]() end";
32
- tObject['conditions'][nextIndex] = "return(c_time_after(" + timingInSeconds + ") )";
33
- tObject['flag'][nextIndex] = true;
34
- trObject[nextIndex] = {
35
- rules: {
36
- 1: {
37
- coalitionlist: 'red',
38
- seconds: timingInSeconds,
39
- predicate: 'c_time_after',
40
- zone: ''
41
- }
42
- },
43
- eventlist: '',
44
- comment: strTitle,
45
- actions: actionsObject,
46
- predicate: 'triggerOnce',
47
- colorItem: hexColor
48
- };
49
- return { tObject: tObject, trObject: trObject, mrObject: mrObject };
50
- }
51
-
52
- async function getZipObjectFromMizPath(mizPath) {
53
- var MizFile = new jszip();
54
- const mizData = fs.readFileSync(mizPath);
55
- return MizFile.loadAsync(mizData);
56
- }
57
52
 
58
- async function mizUpdate(mizPath, copyPath, strTheatreSettings) {
59
- const zip = await getZipObjectFromMizPath(mizPath);
60
- injectLuaFilesFromFolderIntoZipObject(zip,'src');
61
- injectLuaFilesFromFolderIntoZipObject(zip,'lib');
62
- if (strTheatreSettings === null) {
63
- // TODO: Inject fake data
64
- console.log("NO THEATRE SPECIFIED. RADIO PRESETS WILL NOT BE AVAILABLE !");
65
- } else {
66
- await mizUpdateRadioPresets(zip, 'resources/radios/' + strTheatreSettings);
53
+ async getZipObjectFromMizPath(mizPath) {
54
+ var MizFile = new jszip();
55
+ const mizData = fs.readFileSync(mizPath);
56
+ return MizFile.loadAsync(mizData);
67
57
  }
68
- mizUpdateSettingsLuaFiles(zip, strTheatreSettings);
69
- await mizUpdateSoundFolders(zip);
70
- const inputZip = await zip.generateAsync({
71
- type: 'nodebuffer',
72
- streamFiles: true,
73
- compression: "DEFLATE",
74
- compressionOptions: {
75
- level: 9
76
- }
77
- });
78
- fs.writeFileSync(copyPath ? copyPath : mizPath, inputZip);
79
- }
80
-
81
- async function mizInjectMissionDataFile(mizPath, missionObject) {
82
- const zip = await getZipObjectFromMizPath(mizPath);
83
- mizUpdateMissionDataFile(zip, missionObject);
84
- const inputZip = await zip.generateAsync({
85
- type: 'nodebuffer',
86
- streamFiles: true,
87
- compression: "DEFLATE",
88
- compressionOptions: {
89
- level: 9
90
- }
91
- });
92
- fs.writeFileSync(mizPath, inputZip);
93
- }
94
58
 
95
- async function injectLuaSettingsFromFolderPathToMizPath(mizPath, settingsFolder) {
96
- const zip = await getZipObjectFromMizPath(mizPath);
97
- await injectLuaSettingsFromFolderPathToZipObject(zip,settingsFolder);
98
- const outputZip = await zip.generateAsync({
99
- type: 'nodebuffer',
100
- streamFiles: true,
101
- compression: "DEFLATE",
102
- compressionOptions: {
103
- level: 9
59
+ async mizUpdate(mizPath, copyPath, strTheatreSettings) {
60
+ const zip = await this.getZipObjectFromMizPath(mizPath);
61
+ this.injectLuaFilesFromFolderIntoZipObject(zip, 'src');
62
+ this.injectLuaFilesFromFolderIntoZipObject(zip, 'lib');
63
+ if (strTheatreSettings === null) {
64
+ // TODO: Inject fake data
65
+ console.log("NO THEATRE SPECIFIED. RADIO PRESETS WILL NOT BE AVAILABLE !");
66
+ } else {
67
+ await this.mizUpdateRadioPresets(zip, 'resources/radios/' + strTheatreSettings);
104
68
  }
105
- });
106
- fs.writeFileSync(mizPath, outputZip);
107
- }
69
+ this.mizUpdateSettingsLuaFiles(zip, strTheatreSettings);
70
+ await this.mizUpdateSoundFolders(zip);
71
+ const inputZip = await zip.generateAsync({
72
+ type: 'nodebuffer',
73
+ streamFiles: true,
74
+ compression: "DEFLATE",
75
+ compressionOptions: {
76
+ level: 9
77
+ }
78
+ });
79
+ fs.writeFileSync(copyPath ? copyPath : mizPath, inputZip);
80
+ }
108
81
 
109
- async function injectLuaSettingsFromFolderPathToZipObject(zip, settingsFolder) {
110
- for (let file of fs.readdirSync(settingsFolder).filter(file => file.endsWith(".lua"))) {
111
- injectFileIntoZipObject(zip, [
112
- settingsFolder,
113
- "/",
114
- file].join(""));
82
+ async mizInjectMissionDataFile(mizPath, missionObject) {
83
+ const zip = await this.getZipObjectFromMizPath(mizPath);
84
+ this.mizUpdateMissionDataFile(zip, missionObject);
85
+ const inputZip = await zip.generateAsync({
86
+ type: 'nodebuffer',
87
+ streamFiles: true,
88
+ compression: "DEFLATE",
89
+ compressionOptions: {
90
+ level: 9
91
+ }
92
+ });
93
+ fs.writeFileSync(mizPath, inputZip);
115
94
  }
116
- }
117
95
 
96
+ async injectLuaSettingsFromFolderPathToMizPath(mizPath, settingsFolder) {
97
+ const zip = await this.getZipObjectFromMizPath(mizPath);
98
+ await this.injectLuaSettingsFromFolderPathToZipObject(zip, settingsFolder);
99
+ const outputZip = await zip.generateAsync({
100
+ type: 'nodebuffer',
101
+ streamFiles: true,
102
+ compression: "DEFLATE",
103
+ compressionOptions: {
104
+ level: 9
105
+ }
106
+ });
107
+ fs.writeFileSync(mizPath, outputZip);
108
+ }
118
109
 
119
- async function mizInjectMapResourceFile(mizPath, mapResourceObject) {
120
- const zip = await getZipObjectFromMizPath(mizPath);
121
- mizUpdateMapResourceFile(zip, mapResourceObject);
122
- const inputZip = await zip.generateAsync({
123
- type: 'nodebuffer',
124
- streamFiles: true,
125
- compression: "DEFLATE",
126
- compressionOptions: {
127
- level: 9
110
+ async injectLuaSettingsFromFolderPathToZipObject(zip, settingsFolder) {
111
+ for (let file of fs.readdirSync(settingsFolder).filter(file => file.endsWith(".lua"))) {
112
+ this.injectFileIntoZipObject(zip, [
113
+ settingsFolder,
114
+ "/",
115
+ file].join(""));
128
116
  }
129
- });
130
- fs.writeFileSync(mizPath, inputZip);
131
- }
117
+ }
132
118
 
133
- function injectFileIntoZipObject(zip, filePath) {
134
- zip.remove("l10n/DEFAULT/" + path.basename(filePath));
135
- var stream = fs.createReadStream(filePath);
136
- zip.file("l10n/DEFAULT/" + path.basename(filePath), stream);
137
- }
138
119
 
139
- function mizUpdateMissionDataFile(zip, missionObject) {
140
- zip.remove("mission");
141
- let missionLuaT = format(missionObject, { singleQuote: false })
142
- missionLuaT = missionLuaT
143
- .split('\n')
144
- .slice(1, -1)
145
- .join('\n')
146
- .slice(0, -1)
147
- .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
148
- zip.file("mission", missionLuaT);
149
- }
120
+ async mizInjectMapResourceFile(mizPath, mapResourceObject) {
121
+ const zip = await this.getZipObjectFromMizPath(mizPath);
122
+ this.mizUpdateMapResourceFile(zip, mapResourceObject);
123
+ const inputZip = await zip.generateAsync({
124
+ type: 'nodebuffer',
125
+ streamFiles: true,
126
+ compression: "DEFLATE",
127
+ compressionOptions: {
128
+ level: 9
129
+ }
130
+ });
131
+ fs.writeFileSync(mizPath, inputZip);
132
+ }
150
133
 
151
- function mizUpdateMapResourceFile(zip, mapResourceObject) {
152
- zip.remove("l10n/DEFAULT/mapResource");
153
- let mapResourceLuaT = format(mapResourceObject, { singleQuote: false })
154
- mapResourceLuaT = mapResourceLuaT
155
- .split('\n')
156
- .slice(1, -1)
157
- .join('\n')
158
- .slice(0, -1)
159
- .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
160
- zip.file("l10n/DEFAULT/mapResource", mapResourceLuaT);
161
- }
134
+ injectFileIntoZipObject(zip, filePath) {
135
+ zip.remove("l10n/DEFAULT/" + path.basename(filePath));
136
+ var stream = fs.createReadStream(filePath);
137
+ zip.file("l10n/DEFAULT/" + path.basename(filePath), stream);
138
+ }
162
139
 
163
- function getMissionLuaStringFromZipObject(zipStream) {
164
- return zipStream.file("mission").async("string");
165
- }
140
+ mizUpdateMissionDataFile(zip, missionObject) {
141
+ zip.remove("mission");
142
+ let missionLuaT = format(missionObject, {singleQuote: false})
143
+ missionLuaT = missionLuaT
144
+ .split('\n')
145
+ .slice(1, -1)
146
+ .join('\n')
147
+ .slice(0, -1)
148
+ .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
149
+ zip.file("mission", missionLuaT);
150
+ }
166
151
 
167
- function getMapResourceLuaStringFromZipObject(zipStream) {
168
- return zipStream.file("l10n/DEFAULT/mapResource").async("string");
169
- }
152
+ mizUpdateMapResourceFile(zip, mapResourceObject) {
153
+ zip.remove("l10n/DEFAULT/mapResource");
154
+ let mapResourceLuaT = format(mapResourceObject, {singleQuote: false})
155
+ mapResourceLuaT = mapResourceLuaT
156
+ .split('\n')
157
+ .slice(1, -1)
158
+ .join('\n')
159
+ .slice(0, -1)
160
+ .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
161
+ zip.file("l10n/DEFAULT/mapResource", mapResourceLuaT);
162
+ }
170
163
 
171
- function injectLuaFilesFromFolderIntoZipObject(zip, folderPath) {
172
- for (let file of fs.readdirSync(folderPath).filter(file => file.endsWith(".lua"))) {
173
- console.log('injecting up2date ' + folderPath + '/' + file + ' file in archive');
174
- injectFileIntoZipObject(zip, folderPath + "/" + file);
175
- };
176
- }
164
+ getMissionLuaStringFromZipObject(zipStream) {
165
+ return zipStream.file("mission").async("string");
166
+ }
167
+
168
+ getMapResourceLuaStringFromZipObject(zipStream) {
169
+ return zipStream.file("l10n/DEFAULT/mapResource").async("string");
170
+ }
177
171
 
172
+ injectLuaFilesFromFolderIntoZipObject(zip, folderPath) {
173
+ for (let file of fs.readdirSync(folderPath).filter(file => file.endsWith(".lua"))) {
174
+ console.log('injecting up2date ' + folderPath + '/' + file + ' file in archive');
175
+ this.injectFileIntoZipObject(zip, folderPath + "/" + file);
176
+ }
177
+ ;
178
+ }
178
179
 
179
- async function mizUpdateRadioPresets(zip, preset_folder) {
180
- // Create folder Avionics to make sure it exists then delete it to remove any old preset in the template
181
- // Allows adding presets for A-10C
182
- zip.folder("Avionics");
183
- zip.remove("Avionics");
184
180
 
185
- const mission_object = await getMissionObjectFromZipObject(zip);
186
- for (let file of fs.readdirSync(preset_folder).filter(file => file.endsWith(".lua"))) {
187
- const file_data = fs.readFileSync(preset_folder + '/' + file).toString();
188
- const lua_string = file_data.substring(0, file_data.indexOf("radio_descriptor_table =") - 1);
189
- const radio_descriptor_table = parse("return {" + lua_string + "}").descriptor;
181
+ async mizUpdateRadioPresets(zip, preset_folder) {
182
+ // Create folder Avionics to make sure it exists then delete it to remove any old preset in the template
183
+ // Allows adding presets for A-10C
184
+ zip.folder("Avionics");
185
+ zip.remove("Avionics");
190
186
 
191
- console.log('updating radio presets (aircraft: ' + radio_descriptor_table["aircraft"] + ', group_name: ' + radio_descriptor_table["group_name"] + ') with preset in ' + preset_folder + ' folder');
192
- const dcs_radio_presets = file_data.substring(file_data.indexOf("radio_descriptor_table =") + 24);
187
+ const mission_object = await this.getMissionObjectFromZipObject(zip);
188
+ for (let file of fs.readdirSync(preset_folder).filter(file => file.endsWith(".lua"))) {
189
+ const file_data = fs.readFileSync(preset_folder + '/' + file).toString();
190
+ const lua_string = file_data.substring(0, file_data.indexOf("radio_descriptor_table =") - 1);
191
+ const radio_descriptor_table = parse("return {" + lua_string + "}").descriptor;
193
192
 
194
- for (const coalition_key in mission_object.coalition) {
195
- const coalition = mission_object.coalition[coalition_key];
196
- for (const country_list_key in coalition) {
197
- if (country_list_key != "country") continue;
198
- const country_list = coalition[country_list_key];
199
- for (const country_key in country_list) {
200
- const country = country_list[country_key];
201
- for (const plane_key in country["plane"]) {
202
- const plane = country["plane"][plane_key];
203
- for (const group_key in plane) {
204
- const group = plane[group_key];
205
- if (group["name"].match(radio_descriptor_table["group_name"]) == null) continue;
206
- for (const unit_key in group) {
207
- if (unit_key != "units") continue;
208
- const unit = group[unit_key];
209
- for (const sub_unit_key in unit) {
210
- const sub_unit = unit[sub_unit_key];
211
- if (sub_unit["skill"] != "Client") continue;
212
- // Aircraft is an A10CII, use A10C mode by creating files in the root with the unit id
213
- // if (radio_descriptor_table["aircraft"] == 'A-10C_2') {
214
- // const unit_id = sub_unit["unitId"];
215
- // ["UHF_RADIO", "VHF_AM_RADIO", "VHF_FM_RADIO"].forEach(folder => {
216
- // var zip_folder = zip.folder("Avionics/A-10C_2/" + unit_id + "/" + folder);
217
- // var file = parse("return " + dcs_radio_presets)[folder];
218
- // file = format(file, { singleQuote: false });
219
- // file = file
220
- // .split('\n')
221
- // .slice(1, -1)
222
- // .join('\n')
223
- // .slice(0, -1)
224
- // .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
225
- // zip_folder.file("SETTINGS.lua", file);
226
- // });
227
- // continue;
228
- // }
229
- if (sub_unit["type"] != radio_descriptor_table["aircraft"]) continue;
230
- // GROUP FOUND, SET RADIOS
231
- sub_unit["Radio"] = parse("return " + dcs_radio_presets)
193
+ console.log('updating radio presets (aircraft: ' + radio_descriptor_table["aircraft"] + ', group_name: ' + radio_descriptor_table["group_name"] + ') with preset in ' + preset_folder + ' folder');
194
+ const dcs_radio_presets = file_data.substring(file_data.indexOf("radio_descriptor_table =") + 24);
195
+
196
+ for (const coalition_key in mission_object.coalition) {
197
+ const coalition = mission_object.coalition[coalition_key];
198
+ for (const country_list_key in coalition) {
199
+ if (country_list_key != "country") continue;
200
+ const country_list = coalition[country_list_key];
201
+ for (const country_key in country_list) {
202
+ const country = country_list[country_key];
203
+ for (const plane_key in country["plane"]) {
204
+ const plane = country["plane"][plane_key];
205
+ for (const group_key in plane) {
206
+ const group = plane[group_key];
207
+ if (group["name"].match(radio_descriptor_table["group_name"]) == null) continue;
208
+ for (const unit_key in group) {
209
+ if (unit_key != "units") continue;
210
+ const unit = group[unit_key];
211
+ for (const sub_unit_key in unit) {
212
+ const sub_unit = unit[sub_unit_key];
213
+ if (sub_unit["skill"] != "Client") continue;
214
+ // Aircraft is an A10CII, use A10C mode by creating files in the root with the unit id
215
+ // if (radio_descriptor_table["aircraft"] == 'A-10C_2') {
216
+ // const unit_id = sub_unit["unitId"];
217
+ // ["UHF_RADIO", "VHF_AM_RADIO", "VHF_FM_RADIO"].forEach(folder => {
218
+ // var zip_folder = zip.folder("Avionics/A-10C_2/" + unit_id + "/" + folder);
219
+ // var file = parse("return " + dcs_radio_presets)[folder];
220
+ // file = format(file, { singleQuote: false });
221
+ // file = file
222
+ // .split('\n')
223
+ // .slice(1, -1)
224
+ // .join('\n')
225
+ // .slice(0, -1)
226
+ // .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
227
+ // zip_folder.file("SETTINGS.lua", file);
228
+ // });
229
+ // continue;
230
+ // }
231
+ if (sub_unit["type"] != radio_descriptor_table["aircraft"]) continue;
232
+ // GROUP FOUND, SET RADIOS
233
+ sub_unit["Radio"] = parse("return " + dcs_radio_presets)
234
+ }
232
235
  }
233
236
  }
234
237
  }
235
238
  }
236
239
  }
237
240
  }
238
- }
239
- };
240
- mizUpdateMissionDataFile(zip, {mission: mission_object});
241
- }
241
+ };
242
+ this.mizUpdateMissionDataFile(zip, {mission: mission_object});
243
+ }
242
244
 
243
- function mizUpdateSettingsLuaFiles(zip, strTheatre) {
244
- for (let file of fs.readdirSync('settings/' + strTheatre).filter(file => file.endsWith(".lua"))) {
245
- console.log('updating settings/' + strTheatre + '/' + file + ' file in miz file');
246
- injectFileIntoZipObject(zip, 'settings/' + strTheatre + '/' + file);
247
- };
248
- }
245
+ mizUpdateSettingsLuaFiles(zip, strTheatre) {
246
+ for (let file of fs.readdirSync('settings/' + strTheatre).filter(file => file.endsWith(".lua"))) {
247
+ console.log('updating settings/' + strTheatre + '/' + file + ' file in miz file');
248
+ this.injectFileIntoZipObject(zip, 'settings/' + strTheatre + '/' + file);
249
+ }
250
+ ;
251
+ }
249
252
 
250
- async function mizUpdateSoundFolders(zip) {
251
- if (fs.existsSync('resources/sounds') && fs.lstatSync('resources/sounds').isDirectory()) {
252
- const folderArray = fs.readdirSync('resources/sounds');
253
- for (const folder of folderArray) {
254
- await mizUpdateSingleSoundFolder(zip, folder);
253
+ async mizUpdateSoundFolders(zip) {
254
+ if (fs.existsSync('resources/sounds') && fs.lstatSync('resources/sounds').isDirectory()) {
255
+ const folderArray = fs.readdirSync('resources/sounds');
256
+ for (const folder of folderArray) {
257
+ await this.mizUpdateSingleSoundFolder(zip, folder);
258
+ }
255
259
  }
256
260
  }
257
- }
258
261
 
259
- async function mizUpdateSingleSoundFolder(zip, folder) {
260
- if (fs.existsSync('resources/sounds/' + folder) && fs.lstatSync('resources/sounds/' + folder).isDirectory()) {
261
- console.log('adding sound files from resources/sounds/' + folder + ' folder...');
262
- zip = zip.remove(folder).folder(folder);
263
- await addFilesToZip(zip, 'resources/sounds/' + folder, fs.readdirSync('resources/sounds/' + folder));
262
+ async mizUpdateSingleSoundFolder(zip, folder) {
263
+ if (fs.existsSync('resources/sounds/' + folder) && fs.lstatSync('resources/sounds/' + folder).isDirectory()) {
264
+ console.log('adding sound files from resources/sounds/' + folder + ' folder...');
265
+ zip = zip.remove(folder).folder(folder);
266
+ await this.addFilesToZip(zip, 'resources/sounds/' + folder, fs.readdirSync('resources/sounds/' + folder));
267
+ }
264
268
  }
265
- }
266
269
 
267
- async function addFilesToZip(zip, directoryPath, filesToInclude) {
268
- const promiseArr = await filesToInclude.map(async file => {
269
- const filePath = path.join(directoryPath, file)
270
- try {
271
- const fileStats = await lstat(filePath)
272
- const isDirectory = fileStats.isDirectory()
273
- if (isDirectory) {
274
- const directory = zip.remove(file).folder(file)
275
- const subFiles = fs.readdirSync(filePath)
276
- return addFilesToZip(directory, filePath, subFiles)
277
- } else {
278
- // console.log('added file : '+file);
279
- return zip.file(file, fs.createReadStream(filePath))
270
+ async addFilesToZip(zip, directoryPath, filesToInclude) {
271
+ const promiseArr = await filesToInclude.map(async file => {
272
+ const filePath = path.join(directoryPath, file)
273
+ try {
274
+ const fileStats = await lstat(filePath)
275
+ const isDirectory = fileStats.isDirectory()
276
+ if (isDirectory) {
277
+ const directory = zip.remove(file).folder(file)
278
+ const subFiles = fs.readdirSync(filePath)
279
+ return this.addFilesToZip(directory, filePath, subFiles)
280
+ } else {
281
+ // console.log('added file : '+file);
282
+ return zip.file(file, fs.createReadStream(filePath))
283
+ }
284
+ } catch (err) {
285
+ console.log(err)
286
+ return Promise.resolve()
280
287
  }
281
- } catch (err) {
282
- console.log(err)
283
- return Promise.resolve()
284
- }
285
- })
286
- return Promise.all(promiseArr)
287
- }
288
+ })
289
+ return Promise.all(promiseArr)
290
+ }
288
291
 
289
- async function copyMiz(srcMizPath, dstMizPath) {
290
- await fs.createReadStream(srcMizPath).pipe(fs.createWriteStream(dstMizPath));
291
- }
292
+ async copyMiz(srcMizPath, dstMizPath) {
293
+ await fs.createReadStream(srcMizPath).pipe(fs.createWriteStream(dstMizPath));
294
+ }
292
295
 
293
- async function getMissionObjectFromMizPath(MizPath) {
294
- let luaTable = 'return { \n' + await getMissionLuaStringFromZipObject(await getZipObjectFromMizPath(MizPath)) + ' }';
295
- return parse(luaTable).mission;
296
- }
296
+ async getMissionObjectFromMizPath(MizPath) {
297
+ let luaTable = 'return { \n' + await this.getMissionLuaStringFromZipObject(await this.getZipObjectFromMizPath(MizPath)) + ' }';
298
+ return parse(luaTable).mission;
299
+ }
297
300
 
298
- async function getMissionObjectFromZipObject(zip) {
299
- let luaTable = 'return { \n' + await getMissionLuaStringFromZipObject(zip) + ' }';
300
- return parse(luaTable).mission;
301
- }
301
+ async getMissionObjectFromZipObject(zip) {
302
+ let luaTable = 'return { \n' + await this.getMissionLuaStringFromZipObject(zip) + ' }';
303
+ return parse(luaTable).mission;
304
+ }
302
305
 
303
- async function getMapResourceObjectFromMizPath(MizPath) {
304
- let luaTable = 'return { \n' + await getMapResourceLuaStringFromZipObject(await getZipObjectFromMizPath(MizPath)) + ' }';
305
- return parse(luaTable).mapResource;
306
- }
306
+ async getMapResourceObjectFromMizPath(MizPath) {
307
+ let luaTable = 'return { \n' + await this.getMapResourceLuaStringFromZipObject(await this.getZipObjectFromMizPath(MizPath)) + ' }';
308
+ return parse(luaTable).mapResource;
309
+ }
307
310
 
308
- async function getMapResourceObjectFromZipObject(zip) {
309
- let luaTable = 'return { \n' + await getMapResourceLuaStringFromZipObject(zip) + ' }';
310
- return parse(luaTable).mapResource;
311
+ async getMapResourceObjectFromZipObject(zip) {
312
+ let luaTable = 'return { \n' + await this.getMapResourceLuaStringFromZipObject(zip) + ' }';
313
+ return parse(luaTable).mapResource;
314
+ }
311
315
  }
312
316
 
313
- module.exports = {
314
- injectLuaScriptsInMissionObject: injectLuaScriptsInMissionObject,
315
- getZipObjectFromMizPath: getZipObjectFromMizPath,
316
- mizUpdate: mizUpdate,
317
- mizInjectMissionDataFile: mizInjectMissionDataFile,
318
- injectLuaSettingsFromFolderPathToMizPath: injectLuaSettingsFromFolderPathToMizPath,
319
- injectLuaSettingsFromFolderPathToZipObject: injectLuaSettingsFromFolderPathToZipObject,
320
- mizInjectMapResourceFile: mizInjectMapResourceFile,
321
- injectFileIntoZipObject: injectFileIntoZipObject,
322
- mizUpdateMissionDataFile: mizUpdateMissionDataFile,
323
- mizUpdateMapResourceFile: mizUpdateMapResourceFile,
324
- getMissionLuaStringFromZipObject: getMissionLuaStringFromZipObject,
325
- getMapResourceLuaStringFromZipObject: getMapResourceLuaStringFromZipObject,
326
- mizUpdateRadioPresets: mizUpdateRadioPresets,
327
- mizUpdateSettingsLuaFiles: mizUpdateSettingsLuaFiles,
328
- mizUpdateSoundFolders: mizUpdateSoundFolders,
329
- mizUpdateSingleSoundFolder: mizUpdateSingleSoundFolder,
330
- addFilesToZip: addFilesToZip,
331
- copyMiz: copyMiz,
332
- getMissionObjectFromMizPath: getMissionObjectFromMizPath,
333
- getMissionObjectFromZipObject: getMissionObjectFromZipObject,
334
- getMapResourceObjectFromMizPath: getMapResourceObjectFromMizPath,
335
- getMapResourceObjectFromZipObject: getMapResourceObjectFromZipObject,
336
- injectLuaFilesFromFolderIntoZipObject: injectLuaFilesFromFolderIntoZipObject
337
- };
317
+ module.exports = mizlib;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jtff/miztemplate-lib",
3
- "version": "3.0.0-rc2",
3
+ "version": "3.0.0-rc3",
4
4
  "description": "JTFF mission template library",
5
5
  "main": "index.js",
6
6
  "files": [