File size: 1,814 Bytes
e4a10af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const iconv_corefoundation_1 = require("iconv-corefoundation");
const Context_1 = require("./Context");
const util_1 = require("./util");
const format_verror_1 = require("./util/format-verror");
var BodySpec;
(function (BodySpec) {
    async function prepare(spec, lang, contextOrOptions = {}) {
        const context = Context_1.default.from(contextOrOptions);
        const fpath = spec.file && context.resolvePath(spec.file);
        function encodeBodyText(text) {
            try {
                if (typeof text === "string")
                    return lang.charset.encode(text);
                else
                    return iconv_corefoundation_1.transcode(text, spec.charset || "UTF-8", lang.charset);
            }
            catch (e) {
                throw new format_verror_1.PrettyVError(e, "Cannot encode %s license text", lang.englishName);
            }
        }
        let data;
        if (fpath) {
            let ftext;
            try {
                ftext = await util_1.readFileP(fpath);
            }
            catch (e) {
                throw new format_verror_1.PrettyVError(e, "Cannot read %s license text from “%s”", lang.englishName, fpath);
            }
            data = encodeBodyText(ftext);
        }
        else
            data = encodeBodyText(spec.text);
        let type;
        if (spec.type === "rtf" || (fpath && fpath.endsWith(".rtf")))
            type = "RTF ";
        else
            type = "TEXT";
        return { data, type };
    }
    BodySpec.prepare = prepare;
})(BodySpec = exports.BodySpec || (exports.BodySpec = {}));
Object.defineProperty(BodySpec, Symbol.toStringTag, { value: "BodySpec" });
exports.default = BodySpec;
//# sourceMappingURL=BodySpec.js.map