json-renderers-run-instructions 1.9.4 → 1.11.2
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 +3 -3
- package/src/index.js +3 -3
- package/src/v10/index.js +218 -0
- package/src/v10/registerGlobal/index.js +16 -0
- package/src/v10/registerGlobal/meta.js +6 -0
- package/src/v10/runOptions/index.js +70 -0
- package/src/v10/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v10-2/index.js +202 -0
- package/src/v10-2/runOptions/index.js +52 -0
- package/src/v10-2/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v10-old/index.js +195 -0
- package/src/v10-old/runOptions/index.js +117 -0
- package/src/v10-old/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v11/index.js +219 -0
- package/src/v11/registerGlobal/index.js +16 -0
- package/src/v11/registerGlobal/meta.js +6 -0
- package/src/v11/runOptions/index.js +70 -0
- package/src/v11/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v8/index.js +192 -192
- package/src/v8/instructions.json +275 -275
- package/src/v9/index.js +194 -196
- package/src/v9/runOptions/index.js +12 -7
- package/src/v9/runOptions/insertSearch/index.js +28 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-renderers-run-instructions",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.2",
|
|
4
4
|
"description": "Reusable instruction execution and JSON-to-DOM mounting runtime for JSON renderers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"json-transformer-funcs-row-on-column": "^1.2.1",
|
|
23
23
|
"json-write-path": "^1.6.1",
|
|
24
24
|
"jsonpathly": "^4.0.0",
|
|
25
|
-
"spec-repo": "^1.
|
|
25
|
+
"spec-repo": "^1.19.2"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"spec-repo": "^1.
|
|
28
|
+
"spec-repo": "^1.19.2"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
|
|
3
|
-
export { default } from "./
|
|
1
|
+
export * from "./v11/index.js";
|
|
2
|
+
|
|
3
|
+
export { default } from "./v11/index.js";
|
package/src/v10/index.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import rowOnColumnFunc from "json-transformer-funcs-row-on-column";
|
|
2
|
+
import fromCollection from "json-transformer-funcs-one-dimension";
|
|
3
|
+
import applyTemplate from "json-transformer-funcs-primitive";
|
|
4
|
+
import writePath from "json-write-path";
|
|
5
|
+
import { query } from "jsonpathly";
|
|
6
|
+
import registerGlobal from "./registerGlobal/index.js";
|
|
7
|
+
|
|
8
|
+
import { spec, instructions, definitions } from "spec-repo";
|
|
9
|
+
|
|
10
|
+
import runOptions from "./runOptions/index.js";
|
|
11
|
+
|
|
12
|
+
const transformerFunctions = {
|
|
13
|
+
fromCollection,
|
|
14
|
+
applyTemplate
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const resolveSource = (sourcePath, context) => {
|
|
18
|
+
if (!sourcePath) return null;
|
|
19
|
+
|
|
20
|
+
const root = context[sourcePath.fileName];
|
|
21
|
+
if (root === undefined) {
|
|
22
|
+
throw new Error(`Context entry not found: ${sourcePath.fileName}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (sourcePath.jsonPath) {
|
|
26
|
+
const matches = query(root, sourcePath.jsonPath, { returnArray: true });
|
|
27
|
+
if (matches.length === 0) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return matches.length === 1 ? matches[0] : matches;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (Array.isArray(sourcePath.filePath) && sourcePath.filePath.length > 0) {
|
|
34
|
+
return sourcePath.filePath.reduce((curr, key) => (curr ? curr[key] : undefined), root);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return root;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const checkDestinationExists = (destinationPath, context) => {
|
|
41
|
+
if (!destinationPath) return true;
|
|
42
|
+
const root = context[destinationPath.fileName];
|
|
43
|
+
if (root === undefined) return false;
|
|
44
|
+
|
|
45
|
+
if (destinationPath.jsonPath) {
|
|
46
|
+
const matches = query(root, destinationPath.jsonPath, { returnArray: true });
|
|
47
|
+
return matches.length > 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const executeInstruction = ({ instruction, context, spec, showLog = false }) => {
|
|
54
|
+
if (!instruction) return null;
|
|
55
|
+
|
|
56
|
+
// Handle array of sub-instructions (e.g. serial, options, search)
|
|
57
|
+
if (Array.isArray(instruction)) {
|
|
58
|
+
return instruction.map(step => executeInstruction({ instruction: step, context, spec, showLog }));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Auto-normalize legacy dataRole to jsonPath
|
|
62
|
+
if (instruction.destinationPath?.dataRole && !instruction.destinationPath?.jsonPath) {
|
|
63
|
+
instruction.destinationPath.jsonPath = `$..*[?@.attributes['data-role'] == '${instruction.destinationPath.dataRole}']`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Skip if instruction has skipIf condition and target already exists
|
|
67
|
+
if (instruction.skipIf && query(context.base, instruction.skipIf, { returnArray: true }).length > 0) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// If destination node does not exist in current base (e.g. footer row on a table without footer), safely skip
|
|
72
|
+
if (!checkDestinationExists(instruction.destinationPath, context)) {
|
|
73
|
+
if (showLog) {
|
|
74
|
+
console.warn(`Destination node not found for ${instruction.destinationPath?.jsonPath}, skipping instruction.`);
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const type = instruction.type ?? (instruction.templateKey?.columnKey ? "rowOnColumn" : "single");
|
|
80
|
+
|
|
81
|
+
// 1. rowOnColumn instruction (e.g. buildBody for table)
|
|
82
|
+
if (type === "rowOnColumn") {
|
|
83
|
+
const columnKey = instruction?.templateKey?.columnKey;
|
|
84
|
+
const rowKey = instruction?.templateKey?.rowKey;
|
|
85
|
+
const columnSpec = spec[columnKey];
|
|
86
|
+
const rowSpec = spec[rowKey];
|
|
87
|
+
|
|
88
|
+
const columns = context.data?.columns ?? context.columns;
|
|
89
|
+
const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows);
|
|
90
|
+
|
|
91
|
+
const result = rowOnColumnFunc({
|
|
92
|
+
columns,
|
|
93
|
+
data: rows,
|
|
94
|
+
columnSpec,
|
|
95
|
+
rowSpec
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
writePath(context, instruction.destinationPath, result, showLog);
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 2. columnOnRow instruction (e.g. buildSelectWithOptions, buildDataListWithOptions)
|
|
103
|
+
if (type === "columnOnRow") {
|
|
104
|
+
const columnKey = instruction?.templateKey?.columnKey;
|
|
105
|
+
const rowKey = instruction?.templateKey?.rowKey;
|
|
106
|
+
const columnSpec = spec[columnKey];
|
|
107
|
+
const rowSpec = spec[rowKey];
|
|
108
|
+
|
|
109
|
+
const columns = context.data?.columns ?? context.columns ?? [];
|
|
110
|
+
const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
|
|
111
|
+
|
|
112
|
+
const result = columns.map(column => {
|
|
113
|
+
const container = applyTemplate(column, columnSpec);
|
|
114
|
+
|
|
115
|
+
let optionsList = [];
|
|
116
|
+
if (Array.isArray(column.options) && column.options.length > 0) {
|
|
117
|
+
optionsList = column.options.map(opt =>
|
|
118
|
+
typeof opt === "object" ? opt : { rowValue: opt }
|
|
119
|
+
);
|
|
120
|
+
} else if (Array.isArray(rows) && rows.length > 0) {
|
|
121
|
+
optionsList = rows.map(r => ({
|
|
122
|
+
...r,
|
|
123
|
+
rowValue: r[column.columnName] ?? r.value ?? r
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
container.children = optionsList.map(item => applyTemplate(item, rowSpec));
|
|
128
|
+
return container;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
|
|
132
|
+
context[instruction.destinationPath.fileName] = result;
|
|
133
|
+
} else {
|
|
134
|
+
writePath(context, instruction.destinationPath, result, showLog);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 3. single instruction (e.g. buildColgroup, buildHeader, buildFooter, buildFormField, buildOptionsArray)
|
|
140
|
+
const template = instruction.templateKey ? spec[instruction.templateKey] : null;
|
|
141
|
+
const source = resolveSource(instruction.sourcePath, context);
|
|
142
|
+
|
|
143
|
+
let result;
|
|
144
|
+
if (instruction.func === null || instruction.func === undefined) {
|
|
145
|
+
result = structuredClone(template);
|
|
146
|
+
} else if (transformerFunctions[instruction.func]) {
|
|
147
|
+
result = transformerFunctions[instruction.func](source, template);
|
|
148
|
+
} else {
|
|
149
|
+
throw new Error(`Unknown function: ${instruction.func}`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// If destination filePath is empty (e.g. writing directly into an array base like selectOptionsOnlyBase)
|
|
153
|
+
if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
|
|
154
|
+
if (Array.isArray(context[instruction.destinationPath.fileName])) {
|
|
155
|
+
context[instruction.destinationPath.fileName].push(...(Array.isArray(result) ? result : [result]));
|
|
156
|
+
} else {
|
|
157
|
+
context[instruction.destinationPath.fileName] = result;
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
writePath(context, instruction.destinationPath, result, showLog);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return result;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const startFunc = ({
|
|
167
|
+
instructionMainKey,
|
|
168
|
+
instructionSubKey,
|
|
169
|
+
data,
|
|
170
|
+
options,
|
|
171
|
+
showLog = false
|
|
172
|
+
} = {}) => {
|
|
173
|
+
const definition = definitions[instructionMainKey]?.[instructionSubKey];
|
|
174
|
+
if (!definition) {
|
|
175
|
+
throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const baseKey = definition.baseFromSpec;
|
|
179
|
+
const instructionsToRun = definition.instructions ?? [];
|
|
180
|
+
const optionDefs = (definition.options && typeof definition.options === "object")
|
|
181
|
+
? definition.options
|
|
182
|
+
: (definitions[instructionMainKey]?.options ?? instructions.options?.[definition.options] ?? {});
|
|
183
|
+
|
|
184
|
+
const base = structuredClone(spec[baseKey]);
|
|
185
|
+
|
|
186
|
+
const context = { base, data };
|
|
187
|
+
|
|
188
|
+
instructionsToRun.forEach(instructionKeyOrObj => {
|
|
189
|
+
if (showLog) console.log("instruction : ", instructionKeyOrObj);
|
|
190
|
+
|
|
191
|
+
const instruction = typeof instructionKeyOrObj === "string"
|
|
192
|
+
? instructions[instructionKeyOrObj]
|
|
193
|
+
: instructionKeyOrObj;
|
|
194
|
+
|
|
195
|
+
if (!instruction) {
|
|
196
|
+
if (showLog) console.warn(`Instruction not found: ${instructionKeyOrObj}`);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
executeInstruction({ instruction, context, spec, showLog });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
runOptions({
|
|
204
|
+
base: context?.base,
|
|
205
|
+
options,
|
|
206
|
+
optionDefs,
|
|
207
|
+
instructions,
|
|
208
|
+
spec,
|
|
209
|
+
executeInstruction: ({ instruction }) => executeInstruction({ instruction, context, spec, showLog }),
|
|
210
|
+
showLog
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
return context?.base;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export default startFunc;
|
|
217
|
+
|
|
218
|
+
registerGlobal(startFunc);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import meta from "./meta.js";
|
|
2
|
+
|
|
3
|
+
const registerGlobal = (renderFunc) => {
|
|
4
|
+
if (typeof globalThis === "undefined" || !renderFunc) return;
|
|
5
|
+
|
|
6
|
+
globalThis.ks ??= {};
|
|
7
|
+
globalThis.ks.jsonRenderers ??= {};
|
|
8
|
+
globalThis.ks.jsonRenderers.runInstructions ??= {};
|
|
9
|
+
|
|
10
|
+
globalThis.ks.jsonRenderers.runInstructions.render = {
|
|
11
|
+
meta,
|
|
12
|
+
renderFunc
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default registerGlobal;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2
|
+
|
|
3
|
+
const getFeatureOptions = (options, featureKey) => {
|
|
4
|
+
if (!isObject(options)) return null;
|
|
5
|
+
if (options[featureKey] !== undefined) return options[featureKey];
|
|
6
|
+
const prefixed = `show${featureKey.charAt(0).toUpperCase()}${featureKey.slice(1)}`;
|
|
7
|
+
if (options[prefixed] !== undefined) return options[prefixed];
|
|
8
|
+
return null;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 2-level loop for applying option switches from definitions.json:
|
|
13
|
+
* Level 1: Loop over feature groups (search, serial, options)
|
|
14
|
+
* Level 2: Loop over switches inside the feature (showLabel, showHelper, showInput...)
|
|
15
|
+
*/
|
|
16
|
+
const runOptions = ({
|
|
17
|
+
base,
|
|
18
|
+
options = {},
|
|
19
|
+
optionDefs = {},
|
|
20
|
+
instructions = {},
|
|
21
|
+
spec = {},
|
|
22
|
+
executeInstruction,
|
|
23
|
+
showLog = false
|
|
24
|
+
} = {}) => {
|
|
25
|
+
if (!base || !isObject(options) || !isObject(optionDefs) || typeof executeInstruction !== "function") {
|
|
26
|
+
return base;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Level 1: Loop over feature groups (e.g. search, serial, options)
|
|
30
|
+
for (const [featureKey, featureSwitches] of Object.entries(optionDefs)) {
|
|
31
|
+
if (!isObject(featureSwitches)) continue;
|
|
32
|
+
|
|
33
|
+
const userFeature = getFeatureOptions(options, featureKey);
|
|
34
|
+
if (!userFeature) continue;
|
|
35
|
+
|
|
36
|
+
const isBool = typeof userFeature === "boolean" && userFeature === true;
|
|
37
|
+
const isObj = isObject(userFeature);
|
|
38
|
+
if (!isBool && !isObj) continue;
|
|
39
|
+
|
|
40
|
+
// Check if any switch in this feature group is active
|
|
41
|
+
const hasActiveSwitch = Object.entries(featureSwitches).some(
|
|
42
|
+
([switchKey]) => switchKey !== "initialize" && (isBool || userFeature[switchKey] === true)
|
|
43
|
+
);
|
|
44
|
+
if (!hasActiveSwitch) continue;
|
|
45
|
+
|
|
46
|
+
// Step 1: Run initialize instruction first if present (e.g. "searchFrame")
|
|
47
|
+
if (featureSwitches.initialize && instructions[featureSwitches.initialize]) {
|
|
48
|
+
executeInstruction({
|
|
49
|
+
instruction: instructions[featureSwitches.initialize]
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Level 2: Loop over switches and execute each enabled instruction (e.g. "searchLabel")
|
|
54
|
+
for (const [switchKey, instructionKey] of Object.entries(featureSwitches)) {
|
|
55
|
+
if (switchKey === "initialize") continue;
|
|
56
|
+
|
|
57
|
+
const isEnabled = isBool || userFeature[switchKey] === true;
|
|
58
|
+
if (isEnabled && instructions[instructionKey]) {
|
|
59
|
+
executeInstruction({
|
|
60
|
+
instruction: instructions[instructionKey]
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return base;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export { runOptions };
|
|
70
|
+
export default runOptions;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { query } from "jsonpathly";
|
|
2
|
+
|
|
3
|
+
const hasMatch = (base, expression) => {
|
|
4
|
+
if (typeof expression !== "string" || !expression.trim()) return false;
|
|
5
|
+
return query(base, expression, { returnArray: true }).length > 0;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const matchesSkipCondition = (base, skipIf) => {
|
|
9
|
+
if (!skipIf) return false;
|
|
10
|
+
const expressions = Array.isArray(skipIf) ? skipIf : [skipIf];
|
|
11
|
+
return expressions.some(expression => hasMatch(base, expression));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const instructionsWithTargets = (instructions = [], base) => {
|
|
15
|
+
const list = Array.isArray(instructions) ? instructions : [];
|
|
16
|
+
|
|
17
|
+
return list.filter(instruction => {
|
|
18
|
+
const destinationPath = instruction?.destinationPath;
|
|
19
|
+
const root = destinationPath?.fileName
|
|
20
|
+
? (destinationPath.fileName === "base" ? base : undefined)
|
|
21
|
+
: base;
|
|
22
|
+
|
|
23
|
+
if (!destinationPath?.jsonPath) return false;
|
|
24
|
+
if (!hasMatch(root, destinationPath.jsonPath)) return false;
|
|
25
|
+
if (matchesSkipCondition(base, instruction.skipIf)) return false;
|
|
26
|
+
|
|
27
|
+
return true;
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default instructionsWithTargets;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import rowOnColumnFunc from "json-transformer-funcs-row-on-column";
|
|
2
|
+
import fromCollection from "json-transformer-funcs-one-dimension";
|
|
3
|
+
import applyTemplate from "json-transformer-funcs-primitive";
|
|
4
|
+
import writePath from "json-write-path";
|
|
5
|
+
import { query } from "jsonpathly";
|
|
6
|
+
|
|
7
|
+
import { spec, instructions, definitions } from "spec-repo";
|
|
8
|
+
|
|
9
|
+
import runOptions from "./runOptions/index.js";
|
|
10
|
+
|
|
11
|
+
const transformerFunctions = {
|
|
12
|
+
fromCollection,
|
|
13
|
+
applyTemplate
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const resolveSource = (sourcePath, context) => {
|
|
17
|
+
if (!sourcePath) return null;
|
|
18
|
+
|
|
19
|
+
const root = context[sourcePath.fileName];
|
|
20
|
+
if (root === undefined) {
|
|
21
|
+
throw new Error(`Context entry not found: ${sourcePath.fileName}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (sourcePath.jsonPath) {
|
|
25
|
+
const matches = query(root, sourcePath.jsonPath, { returnArray: true });
|
|
26
|
+
if (matches.length === 0) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
return matches.length === 1 ? matches[0] : matches;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(sourcePath.filePath) && sourcePath.filePath.length > 0) {
|
|
33
|
+
return sourcePath.filePath.reduce((curr, key) => (curr ? curr[key] : undefined), root);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return root;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const checkDestinationExists = (destinationPath, context) => {
|
|
40
|
+
if (!destinationPath) return true;
|
|
41
|
+
const root = context[destinationPath.fileName];
|
|
42
|
+
if (root === undefined) return false;
|
|
43
|
+
|
|
44
|
+
if (destinationPath.jsonPath) {
|
|
45
|
+
const matches = query(root, destinationPath.jsonPath, { returnArray: true });
|
|
46
|
+
return matches.length > 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const executeInstruction = ({ instruction, context, spec, showLog = false }) => {
|
|
53
|
+
if (!instruction) return null;
|
|
54
|
+
|
|
55
|
+
// Handle array of sub-instructions (e.g. serial, options, search)
|
|
56
|
+
if (Array.isArray(instruction)) {
|
|
57
|
+
return instruction.map(step => executeInstruction({ instruction: step, context, spec, showLog }));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Auto-normalize legacy dataRole to jsonPath
|
|
61
|
+
if (instruction.destinationPath?.dataRole && !instruction.destinationPath?.jsonPath) {
|
|
62
|
+
instruction.destinationPath.jsonPath = `$..*[?@.attributes['data-role'] == '${instruction.destinationPath.dataRole}']`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// If destination node does not exist in current base (e.g. footer row on a table without footer), safely skip
|
|
66
|
+
if (!checkDestinationExists(instruction.destinationPath, context)) {
|
|
67
|
+
if (showLog) {
|
|
68
|
+
console.warn(`Destination node not found for ${instruction.destinationPath?.jsonPath}, skipping instruction.`);
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const type = instruction.type ?? (instruction.templateKey?.columnKey ? "rowOnColumn" : "single");
|
|
74
|
+
|
|
75
|
+
// 1. rowOnColumn instruction (e.g. buildBody for table)
|
|
76
|
+
if (type === "rowOnColumn") {
|
|
77
|
+
const columnKey = instruction?.templateKey?.columnKey;
|
|
78
|
+
const rowKey = instruction?.templateKey?.rowKey;
|
|
79
|
+
const columnSpec = spec[columnKey];
|
|
80
|
+
const rowSpec = spec[rowKey];
|
|
81
|
+
|
|
82
|
+
const columns = context.data?.columns ?? context.columns;
|
|
83
|
+
const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows);
|
|
84
|
+
|
|
85
|
+
const result = rowOnColumnFunc({
|
|
86
|
+
columns,
|
|
87
|
+
data: rows,
|
|
88
|
+
columnSpec,
|
|
89
|
+
rowSpec
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
writePath(context, instruction.destinationPath, result, showLog);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 2. columnOnRow instruction (e.g. buildSelectWithOptions, buildDataListWithOptions)
|
|
97
|
+
if (type === "columnOnRow") {
|
|
98
|
+
const columnKey = instruction?.templateKey?.columnKey;
|
|
99
|
+
const rowKey = instruction?.templateKey?.rowKey;
|
|
100
|
+
const columnSpec = spec[columnKey];
|
|
101
|
+
const rowSpec = spec[rowKey];
|
|
102
|
+
|
|
103
|
+
const columns = context.data?.columns ?? context.columns ?? [];
|
|
104
|
+
const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
|
|
105
|
+
|
|
106
|
+
const result = columns.map(column => {
|
|
107
|
+
const container = applyTemplate(column, columnSpec);
|
|
108
|
+
|
|
109
|
+
let optionsList = [];
|
|
110
|
+
if (Array.isArray(column.options) && column.options.length > 0) {
|
|
111
|
+
optionsList = column.options.map(opt =>
|
|
112
|
+
typeof opt === "object" ? opt : { rowValue: opt }
|
|
113
|
+
);
|
|
114
|
+
} else if (Array.isArray(rows) && rows.length > 0) {
|
|
115
|
+
optionsList = rows.map(r => ({
|
|
116
|
+
...r,
|
|
117
|
+
rowValue: r[column.columnName] ?? r.value ?? r
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
container.children = optionsList.map(item => applyTemplate(item, rowSpec));
|
|
122
|
+
return container;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
|
|
126
|
+
context[instruction.destinationPath.fileName] = result;
|
|
127
|
+
} else {
|
|
128
|
+
writePath(context, instruction.destinationPath, result, showLog);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 3. single instruction (e.g. buildColgroup, buildHeader, buildFooter, buildFormField, buildOptionsArray)
|
|
134
|
+
const template = instruction.templateKey ? spec[instruction.templateKey] : null;
|
|
135
|
+
const source = resolveSource(instruction.sourcePath, context);
|
|
136
|
+
|
|
137
|
+
let result;
|
|
138
|
+
if (instruction.func === null || instruction.func === undefined) {
|
|
139
|
+
result = template;
|
|
140
|
+
} else if (transformerFunctions[instruction.func]) {
|
|
141
|
+
result = transformerFunctions[instruction.func](source, template);
|
|
142
|
+
} else {
|
|
143
|
+
throw new Error(`Unknown function: ${instruction.func}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// If destination filePath is empty (e.g. writing directly into an array base like selectOptionsOnlyBase)
|
|
147
|
+
if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
|
|
148
|
+
if (Array.isArray(context[instruction.destinationPath.fileName])) {
|
|
149
|
+
context[instruction.destinationPath.fileName].push(...(Array.isArray(result) ? result : [result]));
|
|
150
|
+
} else {
|
|
151
|
+
context[instruction.destinationPath.fileName] = result;
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
writePath(context, instruction.destinationPath, result, showLog);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return result;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const startFunc = ({
|
|
161
|
+
instructionMainKey,
|
|
162
|
+
instructionSubKey,
|
|
163
|
+
data,
|
|
164
|
+
options,
|
|
165
|
+
showLog = false
|
|
166
|
+
} = {}) => {
|
|
167
|
+
const definition = definitions[instructionMainKey]?.[instructionSubKey];
|
|
168
|
+
if (!definition) {
|
|
169
|
+
throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const baseKey = definition.baseFromSpec;
|
|
173
|
+
const instructionsToRun = definition.instructions ?? [];
|
|
174
|
+
const optionDefs = definition.options
|
|
175
|
+
?? definitions._optionSets?.[definition.optionsRef]
|
|
176
|
+
?? {};
|
|
177
|
+
|
|
178
|
+
const base = structuredClone(spec[baseKey]);
|
|
179
|
+
|
|
180
|
+
const context = { base, data };
|
|
181
|
+
|
|
182
|
+
instructionsToRun.forEach(instructionKeyOrObj => {
|
|
183
|
+
if (showLog) console.log("instruction : ", instructionKeyOrObj);
|
|
184
|
+
|
|
185
|
+
const instruction = typeof instructionKeyOrObj === "string"
|
|
186
|
+
? instructions[instructionKeyOrObj]
|
|
187
|
+
: instructionKeyOrObj;
|
|
188
|
+
|
|
189
|
+
if (!instruction) {
|
|
190
|
+
if (showLog) console.warn(`Instruction not found: ${instructionKeyOrObj}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
executeInstruction({ instruction, context, spec, showLog });
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
runOptions({ base: context?.base, options, optionDefs, showLog });
|
|
198
|
+
|
|
199
|
+
return context?.base;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export default startFunc;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { instructions as defaultInstructions, spec as defaultSpec } from "spec-repo";
|
|
2
|
+
import runSingleInstructions from "json-instruction-single";
|
|
3
|
+
import instructionsWithTargets from "./instructionsWithTargets/index.js";
|
|
4
|
+
|
|
5
|
+
const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
6
|
+
|
|
7
|
+
const resolve = (reference, library) => {
|
|
8
|
+
if (Array.isArray(reference)) return reference.flatMap(item => resolve(item, library));
|
|
9
|
+
if (typeof reference === "string") return resolve(library[reference], library);
|
|
10
|
+
return [];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const execute = (reference, context, library, showLog) => {
|
|
14
|
+
const resolved = resolve(reference, library);
|
|
15
|
+
const ready = instructionsWithTargets(resolved, context.base);
|
|
16
|
+
|
|
17
|
+
if (showLog) console.log("[runOptions]", { resolved: resolved.length, ready: ready.length });
|
|
18
|
+
if (ready.length) runSingleInstructions(ready, context, showLog);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Apply the enabled option switches defined by spec-repo. */
|
|
22
|
+
const runOptions = ({
|
|
23
|
+
base,
|
|
24
|
+
options = {},
|
|
25
|
+
optionDefs = {},
|
|
26
|
+
instructions = defaultInstructions,
|
|
27
|
+
showLog = false
|
|
28
|
+
} = {}) => {
|
|
29
|
+
if (!base || !isObject(options)) return base;
|
|
30
|
+
|
|
31
|
+
const context = { base, spec: structuredClone(defaultSpec) };
|
|
32
|
+
|
|
33
|
+
for (const [feature, switches] of Object.entries(optionDefs)) {
|
|
34
|
+
const selected = options[feature];
|
|
35
|
+
if (!isObject(selected) || !isObject(switches)) continue;
|
|
36
|
+
|
|
37
|
+
const enabled = Object.entries(selected)
|
|
38
|
+
.filter(([name, value]) => value === true && name !== "initialize" && switches[name]);
|
|
39
|
+
|
|
40
|
+
if (!enabled.length) continue;
|
|
41
|
+
|
|
42
|
+
if (switches.initialize) execute(switches.initialize, context, instructions, showLog);
|
|
43
|
+
for (const [name] of enabled) {
|
|
44
|
+
execute(switches[name], context, instructions, showLog);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return base;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { runOptions };
|
|
52
|
+
export default runOptions;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { query } from "jsonpathly";
|
|
2
|
+
|
|
3
|
+
const hasMatch = (base, expression) => {
|
|
4
|
+
if (typeof expression !== "string" || !expression.trim()) return false;
|
|
5
|
+
return query(base, expression, { returnArray: true }).length > 0;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const matchesSkipCondition = (base, skipIf) => {
|
|
9
|
+
if (!skipIf) return false;
|
|
10
|
+
const expressions = Array.isArray(skipIf) ? skipIf : [skipIf];
|
|
11
|
+
return expressions.some(expression => hasMatch(base, expression));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const instructionsWithTargets = (instructions = [], base) => {
|
|
15
|
+
const list = Array.isArray(instructions) ? instructions : [];
|
|
16
|
+
|
|
17
|
+
return list.filter(instruction => {
|
|
18
|
+
const destinationPath = instruction?.destinationPath;
|
|
19
|
+
const root = destinationPath?.fileName
|
|
20
|
+
? (destinationPath.fileName === "base" ? base : undefined)
|
|
21
|
+
: base;
|
|
22
|
+
|
|
23
|
+
if (!destinationPath?.jsonPath) return false;
|
|
24
|
+
if (!hasMatch(root, destinationPath.jsonPath)) return false;
|
|
25
|
+
if (matchesSkipCondition(base, instruction.skipIf)) return false;
|
|
26
|
+
|
|
27
|
+
return true;
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default instructionsWithTargets;
|