plac-micro-common 1.3.4 → 1.3.6
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.
|
@@ -37,54 +37,48 @@ exports.generateFormSIO = generateFormSIO;
|
|
|
37
37
|
const fs = __importStar(require("fs"));
|
|
38
38
|
const path = __importStar(require("path"));
|
|
39
39
|
const ejs = __importStar(require("ejs"));
|
|
40
|
-
/*
|
|
41
|
-
|--------------------------------------------------------------------------
|
|
42
|
-
| Cache storage
|
|
43
|
-
|--------------------------------------------------------------------------
|
|
44
|
-
*/
|
|
45
40
|
let cached_font_base64 = null;
|
|
46
41
|
const cached_templates = {};
|
|
47
|
-
/*
|
|
48
|
-
|--------------------------------------------------------------------------
|
|
49
|
-
| Resolve assets root
|
|
50
|
-
|--------------------------------------------------------------------------
|
|
51
|
-
*/
|
|
52
42
|
function getAssetsRootPath() {
|
|
53
43
|
return path.join(__dirname, "..", "..", "assets");
|
|
54
44
|
}
|
|
55
|
-
/*
|
|
56
|
-
|--------------------------------------------------------------------------
|
|
57
|
-
| Load font (cached)
|
|
58
|
-
|--------------------------------------------------------------------------
|
|
59
|
-
*/
|
|
60
45
|
function getFontBase64() {
|
|
61
46
|
if (!cached_font_base64) {
|
|
62
|
-
const
|
|
47
|
+
const assets_root = getAssetsRootPath();
|
|
48
|
+
const font_path = path.join(assets_root, "fonts", "KhmerOS.ttf");
|
|
49
|
+
if (!fs.existsSync(font_path)) {
|
|
50
|
+
throw new Error([
|
|
51
|
+
"[generateFormSIO] Font not found",
|
|
52
|
+
`__dirname: ${__dirname}`,
|
|
53
|
+
`assets_root: ${assets_root}`,
|
|
54
|
+
`font_path: ${font_path}`,
|
|
55
|
+
].join("\n"));
|
|
56
|
+
}
|
|
63
57
|
const font_buffer = fs.readFileSync(font_path);
|
|
64
58
|
cached_font_base64 = font_buffer.toString("base64");
|
|
65
59
|
}
|
|
66
60
|
return cached_font_base64;
|
|
67
61
|
}
|
|
68
|
-
/*
|
|
69
|
-
|--------------------------------------------------------------------------
|
|
70
|
-
| Load template (cached)
|
|
71
|
-
|--------------------------------------------------------------------------
|
|
72
|
-
*/
|
|
73
62
|
function getTemplate(lang) {
|
|
74
63
|
if (!cached_templates[lang]) {
|
|
64
|
+
const assets_root = getAssetsRootPath();
|
|
75
65
|
const template_path = lang === "en"
|
|
76
|
-
? path.join(
|
|
77
|
-
: path.join(
|
|
66
|
+
? path.join(assets_root, "templates", "sio-form-en.ejs")
|
|
67
|
+
: path.join(assets_root, "templates", "sio-form-kh.ejs");
|
|
68
|
+
if (!fs.existsSync(template_path)) {
|
|
69
|
+
throw new Error([
|
|
70
|
+
"[generateFormSIO] Template not found",
|
|
71
|
+
`lang: ${lang}`,
|
|
72
|
+
`__dirname: ${__dirname}`,
|
|
73
|
+
`assets_root: ${assets_root}`,
|
|
74
|
+
`template_path: ${template_path}`,
|
|
75
|
+
].join("\n"));
|
|
76
|
+
}
|
|
78
77
|
const template = fs.readFileSync(template_path, "utf8");
|
|
79
78
|
cached_templates[lang] = ejs.compile(template);
|
|
80
79
|
}
|
|
81
80
|
return cached_templates[lang];
|
|
82
81
|
}
|
|
83
|
-
/*
|
|
84
|
-
|--------------------------------------------------------------------------
|
|
85
|
-
| Main generator
|
|
86
|
-
|--------------------------------------------------------------------------
|
|
87
|
-
*/
|
|
88
82
|
async function generateFormSIO(data, lang) {
|
|
89
83
|
const template = getTemplate(lang);
|
|
90
84
|
const font_base64 = getFontBase64();
|