@vpxa/aikit 0.1.148 → 0.1.149
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.
package/package.json
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const e=`#!/usr/bin/env node
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
|
|
5
|
+
function die(message) {
|
|
6
|
+
process.stderr.write('Error: ' + message + '
|
|
7
|
+
');
|
|
8
|
+
process.exit(1);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function parseArgs(argv) {
|
|
12
|
+
const args = { template: null, data: null, out: null, id: null };
|
|
13
|
+
|
|
14
|
+
for (let index = 2; index < argv.length; index += 1) {
|
|
15
|
+
const arg = argv[index];
|
|
16
|
+
|
|
17
|
+
if (arg === '--template' || arg === '--data' || arg === '--out' || arg === '--id') {
|
|
18
|
+
const value = argv[index + 1];
|
|
19
|
+
|
|
20
|
+
if (!value || value.startsWith('--')) {
|
|
21
|
+
die(arg + ' requires a value');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
args[arg.slice(2)] = value;
|
|
25
|
+
index += 1;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
die('Unknown argument: ' + arg + '. Usage: node scripts/inject-viewer.mjs --template <html> --data <json> --out <output.html> [--id <placeholder-id>]');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!args.template) {
|
|
33
|
+
die('--template is required');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!args.data) {
|
|
37
|
+
die('--data is required');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!args.out) {
|
|
41
|
+
die('--out is required');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return args;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function escapeRegExp(value) {
|
|
48
|
+
return value.replace(/[|\\{}()[]^$+*?.]/g, '\\$&');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function buildScriptPattern(id) {
|
|
52
|
+
const typeLookahead = "(?=[^>]*\\btype=['"]application\\/json['"] )".replace(' )', ')');
|
|
53
|
+
const idLookahead = id
|
|
54
|
+
? ("(?=[^>]*\\bid=['"]" + escapeRegExp(id) + "['"] )").replace(' )', ')')
|
|
55
|
+
: "(?=[^>]*\\bid=['"][^'"]+['"] )".replace(' )', ')');
|
|
56
|
+
|
|
57
|
+
return new RegExp(
|
|
58
|
+
'(<script\\b' + typeLookahead + idLookahead + '[^>]*>)[\\s\\S]*?(<\/script>)',
|
|
59
|
+
'i',
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const args = parseArgs(process.argv);
|
|
64
|
+
|
|
65
|
+
let template;
|
|
66
|
+
try {
|
|
67
|
+
template = readFileSync(args.template, 'utf8');
|
|
68
|
+
} catch (error) {
|
|
69
|
+
die('Cannot read template: ' + args.template + ' - ' + error.message);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let data;
|
|
73
|
+
try {
|
|
74
|
+
data = readFileSync(args.data, 'utf8');
|
|
75
|
+
} catch (error) {
|
|
76
|
+
die('Cannot read data: ' + args.data + ' - ' + error.message);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
JSON.parse(data);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
die('Invalid JSON in ' + args.data + ': ' + error.message);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const pattern = buildScriptPattern(args.id);
|
|
86
|
+
if (!pattern.test(template)) {
|
|
87
|
+
if (args.id) {
|
|
88
|
+
die('No <script type="application/json" id="' + args.id + '"> found in template');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
die('No <script type="application/json" id="..."> found in template');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const safeData = data.replace(/<\\/script/gi, '<\\/script');
|
|
95
|
+
const output = template.replace(pattern, '$1
|
|
96
|
+
' + safeData + '
|
|
97
|
+
$2');
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
mkdirSync(dirname(args.out), { recursive: true });
|
|
101
|
+
writeFileSync(args.out, output, 'utf8');
|
|
102
|
+
} catch (error) {
|
|
103
|
+
die('Cannot write output: ' + args.out + ' - ' + error.message);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
process.stdout.write('Injected ' + args.data + ' -> ' + args.out + '
|
|
107
|
+
');
|
|
108
|
+
`;export{e as INJECT_VIEWER_SCRIPT};
|