UNPKG

6.62 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.addLicenseToDmg = addLicenseToDmg;
7
8function _builderUtil() {
9 const data = require("builder-util");
10
11 _builderUtil = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _license() {
19 const data = require("app-builder-lib/out/util/license");
20
21 _license = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _fsExtra() {
29 const data = require("fs-extra");
30
31 _fsExtra = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _dmgUtil() {
39 const data = require("./dmgUtil");
40
41 _dmgUtil = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _licenseButtons() {
49 const data = require("./licenseButtons");
50
51 _licenseButtons = function () {
52 return data;
53 };
54
55 return data;
56}
57
58// DropDMG/dmgbuild a in any case (even if no english, but only ru/de) set to 0 (en_US), well, without docs, just believe that's correct
59const DEFAULT_REGION_CODE = 0;
60
61async function addLicenseToDmg(packager, dmgPath) {
62 // http://www.owsiak.org/?p=700
63 const licenseFiles = await (0, _license().getLicenseFiles)(packager);
64
65 if (licenseFiles.length === 0) {
66 return null;
67 }
68
69 const licenseButtonFiles = await (0, _licenseButtons().getLicenseButtonsFile)(packager);
70 packager.debugLogger.add("dmg.licenseFiles", licenseFiles);
71 packager.debugLogger.add("dmg.licenseButtons", licenseButtonFiles);
72 const style = [];
73 const rtfs = [];
74 const defaultButtons = [];
75 let counter = 5000;
76 const addedRegionCodes = [];
77
78 for (const item of licenseFiles) {
79 _builderUtil().log.info({
80 lang: item.langName
81 }, "adding license"); // value from DropDMG, data the same for any language
82 // noinspection SpellCheckingInspection
83
84
85 style.push(`data 'styl' (${counter}, "${item.langName}") {
86 $"0001 0000 0000 000E 0011 0015 0000 000C"
87 $"0000 0000 0000"
88};`);
89 let data = `data 'RTF ' (${counter}, "${item.langName}") {\n`;
90 const fileData = await (0, _fsExtra().readFile)(item.file, "utf-8");
91 const isRtf = item.file.endsWith(".rtf") || item.file.endsWith(".RTF");
92 data += isRtf ? (0, _dmgUtil().serializeString)(Buffer.from(fileData).toString("hex")) : wrapInRtf((await (0, _fsExtra().readFile)(item.file, "utf-8")));
93 data += "\n};";
94 rtfs.push(data);
95 defaultButtons.push((await (0, _licenseButtons().getLicenseButtons)(licenseButtonFiles, item.langWithRegion, counter, item.langName)));
96 addedRegionCodes.push(getRegionCode(item.langWithRegion));
97 counter++;
98 }
99
100 const buffer = Buffer.allocUnsafe((2 + 3 * addedRegionCodes.length) * 2);
101 let offset = 0;
102 buffer.writeUInt16BE(DEFAULT_REGION_CODE, offset);
103 offset += 2;
104 buffer.writeUInt16BE(addedRegionCodes.length, offset);
105 offset += 2;
106
107 for (let i = 0; i < addedRegionCodes.length; i++) {
108 const regionCode = addedRegionCodes[i];
109 buffer.writeUInt16BE(regionCode, offset);
110 offset += 2;
111 buffer.writeUInt16BE(i, offset);
112 offset += 2;
113 buffer.writeUInt16BE(
114 /* is two byte */
115 [14, 51, 52, 53].includes(regionCode) ? 1 : 0, offset);
116 offset += 2;
117 }
118
119 const lPic = `data 'LPic' (5000) {\n${(0, _dmgUtil().serializeString)(buffer.toString("hex"))}\n};`;
120 const data = style.concat(rtfs).concat(lPic).concat(defaultButtons).join("\n\n");
121 packager.debugLogger.add("dmg.licenseResource", data);
122 const tempFile = await packager.getTempFile(".r");
123 await (0, _fsExtra().outputFile)(tempFile, data);
124 await (0, _builderUtil().exec)("hdiutil", ["unflatten", dmgPath]);
125 await (0, _builderUtil().exec)("Rez", ["-a", tempFile, "-o", dmgPath]);
126 await (0, _builderUtil().exec)("hdiutil", ["flatten", dmgPath]);
127 return data;
128}
129
130function getRtfUnicodeEscapedString(text) {
131 let result = "";
132
133 for (let i = 0; i < text.length; i++) {
134 if (text[i] === "\\" || text[i] === "{" || text[i] === "}" || text[i] === "\n") {
135 result += `\\${text[i]}`;
136 } else if (text[i] === "\r") {// ignore
137 } else if (text.charCodeAt(i) <= 0x7f) {
138 result += text[i];
139 } else {
140 result += `\\u${text.codePointAt(i)}?`;
141 }
142 }
143
144 return result;
145}
146
147function wrapInRtf(text) {
148 return ` $"7B5C 7274 6631 5C61 6E73 695C 616E 7369"
149 $"6370 6731 3235 325C 636F 636F 6172 7466"
150 $"3135 3034 5C63 6F63 6F61 7375 6272 7466"
151 $"3833 300A 7B5C 666F 6E74 7462 6C5C 6630"
152 $"5C66 7377 6973 735C 6663 6861 7273 6574"
153 $"3020 4865 6C76 6574 6963 613B 7D0A 7B5C"
154 $"636F 6C6F 7274 626C 3B5C 7265 6432 3535"
155 $"5C67 7265 656E 3235 355C 626C 7565 3235"
156 $"353B 7D0A 7B5C 2A5C 6578 7061 6E64 6564"
157 $"636F 6C6F 7274 626C 3B3B 7D0A 5C70 6172"
158 $"645C 7478 3536 305C 7478 3131 3230 5C74"
159 $"7831 3638 305C 7478 3232 3430 5C74 7832"
160 $"3830 305C 7478 3333 3630 5C74 7833 3932"
161 $"305C 7478 3434 3830 5C74 7835 3034 305C"
162 $"7478 3536 3030 5C74 7836 3136 305C 7478"
163 $"3637 3230 5C70 6172 6469 726E 6174 7572"
164 $"616C 5C70 6172 7469 6768 7465 6E66 6163"
165 $"746F 7230 0A0A 5C66 305C 6673 3234 205C"
166${(0, _dmgUtil().serializeString)("63663020" + Buffer.from(getRtfUnicodeEscapedString(text)).toString("hex").toUpperCase() + "7D")}`; // ^ to produce correctly splitted output, this two leading chunks from default wrapper appended here
167}
168
169function getRegionCode(langWithRegion) {
170 const result = regionCodes[langWithRegion];
171
172 if (result == null) {
173 throw new Error(`Cannot determine region code for ${langWithRegion}`);
174 }
175
176 return result;
177} // noinspection SpellCheckingInspection
178
179
180const regionCodes = {
181 en_US: 0,
182 fr_FR: 1,
183 en_GB: 2,
184 de_DE: 3,
185 it_IT: 4,
186 nl_NL: 5,
187 nl_BE: 6,
188 sv_SE: 7,
189 es_ES: 8,
190 da_DK: 9,
191 pt_PT: 10,
192 fr_CA: 11,
193 nb_NO: 12,
194 he_IL: 13,
195 ja_JP: 14,
196 en_AU: 15,
197 ar: 16,
198 fi_FI: 17,
199 fr_CH: 18,
200 de_CH: 19,
201 el_GR: 20,
202 is_IS: 21,
203 mt_MT: 22,
204 el_CY: 23,
205 tr_TR: 24,
206 hi_IN: 33,
207 ur_PK: 34,
208 it_CH: 36,
209 ro_RO: 39,
210 grc: 40,
211 lt_LT: 41,
212 pl_PL: 42,
213 hu_HU: 43,
214 et_EE: 44,
215 lv_LV: 45,
216 se: 46,
217 fo_FO: 47,
218 fa_IR: 48,
219 ru_RU: 49,
220 ga_IE: 50,
221 ko_KR: 51,
222 zh_CN: 52,
223 zh_TW: 53,
224 th_TH: 54,
225 cs_CZ: 56,
226 sk_SK: 57,
227 bn: 60,
228 be_BY: 61,
229 uk_UA: 62,
230 sr_RS: 65,
231 sl_SI: 66,
232 mk_MK: 67,
233 hr_HR: 68,
234 pt_BR: 71,
235 bg_BG: 72,
236 ca_ES: 73,
237 gd: 75,
238 gv: 76,
239 br: 77,
240 iu_CA: 78,
241 cy: 79,
242 "ga-Latg_IE": 81,
243 en_CA: 82,
244 dz_BT: 83,
245 hy_AM: 84,
246 ka_GE: 85,
247 es_419: 86,
248 to_TO: 88,
249 fr_001: 91,
250 de_AT: 92,
251 gu_IN: 94,
252 pa: 95,
253 ur_IN: 96,
254 vi_VN: 97,
255 fr_BE: 98,
256 uz_UZ: 99,
257 en_SG: 100,
258 nn_NO: 101,
259 af_ZA: 102,
260 eo: 103,
261 mr_IN: 104,
262 bo: 105,
263 ne_NP: 106,
264 kl: 107,
265 en_IE: 108
266};
267// __ts-babel@6.0.4
268//# sourceMappingURL=dmgLicense.js.map
\No newline at end of file