@accordproject/template-engine 2.2.0 → 2.3.0
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/dist/index.d.ts +7 -77
- package/dist/index.js +38 -736
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +35 -736
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/runtime.tgz +0 -0
package/dist/index.mjs
CHANGED
|
@@ -4,15 +4,13 @@ import { Introspector, ModelManager, Factory, Serializer, ModelUtil } from '@acc
|
|
|
4
4
|
import dayjs from 'dayjs';
|
|
5
5
|
import utc from 'dayjs/plugin/utc';
|
|
6
6
|
import { TemplateMarkModel, ConcertoMetaModel, CommonMarkModel, CiceroMarkModel } from '@accordproject/markdown-common';
|
|
7
|
-
import { templatemarkutil } from '@accordproject/markdown-template';
|
|
8
7
|
import { createDefaultMapFromNodeModules, createDefaultMapFromCDN } from '@typescript/vfs';
|
|
9
8
|
import { twoslasher } from '@typescript/twoslash';
|
|
10
9
|
import { CodeGen } from '@accordproject/concerto-codegen';
|
|
11
|
-
import { InMemoryWriter
|
|
10
|
+
import { InMemoryWriter } from '@accordproject/concerto-util';
|
|
11
|
+
import { templatemarkutil } from '@accordproject/markdown-template';
|
|
12
|
+
import { existsSync, mkdirSync, rmSync } from 'fs';
|
|
12
13
|
import * as lzstring from 'lz-string';
|
|
13
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
14
|
-
import * as tar from 'tar';
|
|
15
|
-
import { Readable } from 'stream';
|
|
16
14
|
|
|
17
15
|
function booleanDrafter(value) {
|
|
18
16
|
if (value) {
|
|
@@ -199,6 +197,37 @@ const NAVIGATION_NODES = [
|
|
|
199
197
|
`${TemplateMarkModel.NAMESPACE}.OptionalDefinition`,
|
|
200
198
|
`${TemplateMarkModel.NAMESPACE}.ClauseDefinition`
|
|
201
199
|
];
|
|
200
|
+
|
|
201
|
+
function ensureDirSync(path) {
|
|
202
|
+
!existsSync(path) && mkdirSync(path, { recursive: true });
|
|
203
|
+
}
|
|
204
|
+
function removeSync(path) {
|
|
205
|
+
rmSync(path, { recursive: true, force: true });
|
|
206
|
+
}
|
|
207
|
+
function writeFunctionToString(templateClass, functionName, returnType, code) {
|
|
208
|
+
let result = "";
|
|
209
|
+
result += "/// ---cut---\n";
|
|
210
|
+
result += `export function ${functionName}(data:TemplateModel.I${templateClass.getName()}, library:any, options:GenerationOptions) : ${returnType} {
|
|
211
|
+
`;
|
|
212
|
+
result += " const now = dayjs(options?.now);\n";
|
|
213
|
+
result += " const locale = options?.locale;\n";
|
|
214
|
+
templateClass.getProperties().forEach((p) => {
|
|
215
|
+
result += ` const ${p.getName()} = data.${p.getName()};
|
|
216
|
+
`;
|
|
217
|
+
});
|
|
218
|
+
result += " " + code.trim() + "\n";
|
|
219
|
+
result += "}\n";
|
|
220
|
+
result += "\n";
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
function nameUserCode(templateMarkDom) {
|
|
224
|
+
return traverse(templateMarkDom).map(function(x) {
|
|
225
|
+
if (x && (x.$class === `${TemplateMarkModel.NAMESPACE}.ConditionalDefinition` && x.condition || `${TemplateMarkModel.NAMESPACE}.ClauseDefinition` && x.condition)) {
|
|
226
|
+
x.functionName = `condition_${this.path.join("_")}`;
|
|
227
|
+
}
|
|
228
|
+
this.update(x);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
202
231
|
function getTemplateClassDeclaration(modelManager, templateConceptFqn) {
|
|
203
232
|
const introspector = new Introspector(modelManager);
|
|
204
233
|
try {
|
|
@@ -309,34 +338,6 @@ var CodeType = /* @__PURE__ */ ((CodeType2) => {
|
|
|
309
338
|
return CodeType2;
|
|
310
339
|
})(CodeType || {});
|
|
311
340
|
|
|
312
|
-
function ensureDirSync(path) {
|
|
313
|
-
!existsSync(path) && mkdirSync(path, { recursive: true });
|
|
314
|
-
}
|
|
315
|
-
function writeFunctionToString(templateClass, functionName, returnType, code) {
|
|
316
|
-
let result = "";
|
|
317
|
-
result += "/// ---cut---\n";
|
|
318
|
-
result += `export function ${functionName}(data:TemplateModel.I${templateClass.getName()}, library:any, options:GenerationOptions) : ${returnType} {
|
|
319
|
-
`;
|
|
320
|
-
result += " const now = dayjs(options?.now);\n";
|
|
321
|
-
result += " const locale = options?.locale;\n";
|
|
322
|
-
templateClass.getProperties().forEach((p) => {
|
|
323
|
-
result += ` const ${p.getName()} = data.${p.getName()};
|
|
324
|
-
`;
|
|
325
|
-
});
|
|
326
|
-
result += " " + code.trim() + "\n";
|
|
327
|
-
result += "}\n";
|
|
328
|
-
result += "\n";
|
|
329
|
-
return result;
|
|
330
|
-
}
|
|
331
|
-
function nameUserCode(templateMarkDom) {
|
|
332
|
-
return traverse(templateMarkDom).map(function(x) {
|
|
333
|
-
if (x && (x.$class === `${TemplateMarkModel.NAMESPACE}.ConditionalDefinition` && x.condition || `${TemplateMarkModel.NAMESPACE}.ClauseDefinition` && x.condition)) {
|
|
334
|
-
x.functionName = `condition_${this.path.join("_")}`;
|
|
335
|
-
}
|
|
336
|
-
this.update(x);
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
|
|
340
341
|
const CODE_NODES = [
|
|
341
342
|
`${TemplateMarkModel.NAMESPACE}.FormulaDefinition`,
|
|
342
343
|
`${TemplateMarkModel.NAMESPACE}.ConditionalDefinition`,
|
|
@@ -708,707 +709,5 @@ class TemplateMarkInterpreter {
|
|
|
708
709
|
}
|
|
709
710
|
}
|
|
710
711
|
|
|
711
|
-
|
|
712
|
-
fw.writeLine(level, `// ${ModelUtil.getShortName(concept.$class)} ${concept.name ? `(${concept.name})` : ""}`);
|
|
713
|
-
}
|
|
714
|
-
const RUNTIME_DIR = "runtime";
|
|
715
|
-
function writeImports(fw, level, templateClass) {
|
|
716
|
-
fw.writeLine(level, `import * as CommonMark from './${CommonMarkModel.NAMESPACE}';`);
|
|
717
|
-
fw.writeLine(level, `import * as CiceroMark from './${CiceroMarkModel.NAMESPACE}';`);
|
|
718
|
-
fw.writeLine(level, `import * as TemplateModel from './${templateClass.getNamespace()}';`);
|
|
719
|
-
fw.writeLine(level, "import * as UserCode from './usercode';");
|
|
720
|
-
fw.writeLine(level, `import { getDrafter as $getDrafter } from './${RUNTIME_DIR}/drafting';`);
|
|
721
|
-
fw.writeLine(level, `import * as Runtime from './${RUNTIME_DIR}/TypeScriptRuntime';`);
|
|
722
|
-
}
|
|
723
|
-
function writeProlog(fw, level, templateClass) {
|
|
724
|
-
fw.writeLine(level, `export function generator(data:TemplateModel.I${templateClass.getName()}, library:any, options:Runtime.GenerationOptions) : CommonMark.IDocument {`);
|
|
725
|
-
fw.writeLine(level, `const docNode = {$class: '${CommonMarkModel.NAMESPACE}.Document', xmlns: '${CommonMarkModel.NAMESPACE}', nodes: []} as CommonMark.INode;`);
|
|
726
|
-
fw.writeLine(level, "const $nodes:CommonMark.INode[] = [];");
|
|
727
|
-
fw.writeLine(level, "const $data:any[] = [];");
|
|
728
|
-
fw.writeLine(level, "let $result:any = docNode;");
|
|
729
|
-
fw.writeLine(level, "Runtime.push($data, data);");
|
|
730
|
-
}
|
|
731
|
-
function writeEpilog(fw, level) {
|
|
732
|
-
fw.writeLine(level, "return $result as CommonMark.IDocument;");
|
|
733
|
-
fw.writeLine(level, "}");
|
|
734
|
-
}
|
|
735
|
-
function writeOpenGenerateScope(fw, level) {
|
|
736
|
-
fw.writeLine(level, "$result = (() => {");
|
|
737
|
-
}
|
|
738
|
-
function writeCloseGenerateScope(fw, level) {
|
|
739
|
-
fw.writeLine(level, "})();");
|
|
740
|
-
fw.writeLine(level, "Runtime.addChild($nodes, $result);");
|
|
741
|
-
}
|
|
742
|
-
function writeOpenNodeScope(fw, level) {
|
|
743
|
-
fw.writeLine(level, "Runtime.push($nodes, $result);");
|
|
744
|
-
}
|
|
745
|
-
function writeCloseNodeScope(fw, level) {
|
|
746
|
-
fw.writeLine(level, "$result = Runtime.pop($nodes);");
|
|
747
|
-
}
|
|
748
|
-
function writeOpenDataScope(fw, level, name, allowUndefined = false) {
|
|
749
|
-
fw.writeLine(level, `Runtime.push($data, Runtime.peekProperty($data, '${name}', ${allowUndefined}));`);
|
|
750
|
-
}
|
|
751
|
-
function writeCloseDataScope(fw, level) {
|
|
752
|
-
fw.writeLine(level, "Runtime.pop($data);");
|
|
753
|
-
}
|
|
754
|
-
function getTypeScriptType(ctoFqn) {
|
|
755
|
-
const ns = ModelUtil.getNamespace(ctoFqn);
|
|
756
|
-
const type = ModelUtil.getShortName(ctoFqn);
|
|
757
|
-
if (ns === CommonMarkModel.NAMESPACE) {
|
|
758
|
-
return `CommonMark.I${type}`;
|
|
759
|
-
} else if (ns === CiceroMarkModel.NAMESPACE) {
|
|
760
|
-
return `CiceroMark.I${type}`;
|
|
761
|
-
} else if (ns === TemplateMarkModel.NAMESPACE) {
|
|
762
|
-
return `CiceroMark.I${type.substring(0, type.length - "Definition".length)}`;
|
|
763
|
-
} else {
|
|
764
|
-
throw new Error(`No type mapping for ${ctoFqn}`);
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
function makeCiceroMark(templateMarkNode, deep = false) {
|
|
768
|
-
if (!templateMarkNode.$class) {
|
|
769
|
-
throw new Error("Not a concerto type.");
|
|
770
|
-
}
|
|
771
|
-
const json = JSON.parse(JSON.stringify(templateMarkNode));
|
|
772
|
-
delete json.elementType;
|
|
773
|
-
if (!deep) {
|
|
774
|
-
delete json.nodes;
|
|
775
|
-
} else {
|
|
776
|
-
if (json.nodes) {
|
|
777
|
-
json.nodes = json.nodes.map((n) => makeCiceroMark(n, deep));
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
const ns = ModelUtil.getNamespace(json.$class);
|
|
781
|
-
const type = ModelUtil.getShortName(json.$class);
|
|
782
|
-
if (ns === TemplateMarkModel.NAMESPACE) {
|
|
783
|
-
json.$class = `${CiceroMarkModel.NAMESPACE}.${type.substring(0, type.length - "Definition".length)}`;
|
|
784
|
-
}
|
|
785
|
-
return json;
|
|
786
|
-
}
|
|
787
|
-
class AbstractComplexCompiler {
|
|
788
|
-
leaf;
|
|
789
|
-
constructor(leaf) {
|
|
790
|
-
this.leaf = leaf;
|
|
791
|
-
}
|
|
792
|
-
isLeaf() {
|
|
793
|
-
return this.leaf;
|
|
794
|
-
}
|
|
795
|
-
enter(fw, level, templateMarkNode) {
|
|
796
|
-
writeDebug(fw, level, templateMarkNode);
|
|
797
|
-
writeOpenNodeScope(fw, level);
|
|
798
|
-
}
|
|
799
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
800
|
-
exit(fw, level) {
|
|
801
|
-
writeCloseNodeScope(fw, level);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
class AbstractSimpleCompiler extends AbstractComplexCompiler {
|
|
805
|
-
constructor(leaf) {
|
|
806
|
-
super(leaf);
|
|
807
|
-
}
|
|
808
|
-
generate(fw, level, templateMarkNode) {
|
|
809
|
-
const node = this.getNode(templateMarkNode);
|
|
810
|
-
if (this.isLeaf()) {
|
|
811
|
-
node.nodes = [];
|
|
812
|
-
}
|
|
813
|
-
writeOpenGenerateScope(fw, level);
|
|
814
|
-
fw.writeLine(level, `return ${JSON.stringify(node)} as ${getTypeScriptType(node.$class)};`);
|
|
815
|
-
writeCloseGenerateScope(fw, level);
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
class PassThrough extends AbstractSimpleCompiler {
|
|
819
|
-
constructor(leaf) {
|
|
820
|
-
super(leaf);
|
|
821
|
-
}
|
|
822
|
-
getNode(templateMarkNode) {
|
|
823
|
-
return makeCiceroMark(templateMarkNode);
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
class Ignore {
|
|
827
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
828
|
-
enter(fw, level, templateMarkNode) {
|
|
829
|
-
}
|
|
830
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
831
|
-
generate(fw, level, templateMarkNode) {
|
|
832
|
-
}
|
|
833
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
834
|
-
exit(fw, level, templateMarkNode) {
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
class Clause extends AbstractComplexCompiler {
|
|
839
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.ClauseDefinition`;
|
|
840
|
-
constructor() {
|
|
841
|
-
super(false);
|
|
842
|
-
}
|
|
843
|
-
enter(fw, level, templateMarkNode) {
|
|
844
|
-
writeDebug(fw, level, templateMarkNode);
|
|
845
|
-
writeOpenNodeScope(fw, level);
|
|
846
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
847
|
-
}
|
|
848
|
-
generate(fw, level, templateMarkNode) {
|
|
849
|
-
const clone = makeCiceroMark(templateMarkNode);
|
|
850
|
-
delete clone.condition;
|
|
851
|
-
delete clone.functionName;
|
|
852
|
-
const para = { $class: `${CommonMarkModel.NAMESPACE}.Paragraph` };
|
|
853
|
-
writeOpenGenerateScope(fw, level);
|
|
854
|
-
if (templateMarkNode.condition) {
|
|
855
|
-
fw.writeLine(level, `const isTrue:boolean = UserCode.${templateMarkNode.functionName}(data,library,options)`);
|
|
856
|
-
} else {
|
|
857
|
-
fw.writeLine(level, "const isTrue:boolean = true;");
|
|
858
|
-
}
|
|
859
|
-
fw.writeLine(level, "if(isTrue) {");
|
|
860
|
-
fw.writeLine(level, `return ${JSON.stringify(clone)} as ${getTypeScriptType(clone.$class)};`);
|
|
861
|
-
fw.writeLine(level, "}");
|
|
862
|
-
fw.writeLine(level, "else {");
|
|
863
|
-
fw.writeLine(level, `return ${JSON.stringify(para)} as ${getTypeScriptType(para.$class)};`);
|
|
864
|
-
fw.writeLine(level, "}");
|
|
865
|
-
writeCloseGenerateScope(fw, level);
|
|
866
|
-
delete templateMarkNode.condition;
|
|
867
|
-
}
|
|
868
|
-
exit(fw, level) {
|
|
869
|
-
writeCloseDataScope(fw, level);
|
|
870
|
-
writeCloseNodeScope(fw, level);
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
class Conditional extends AbstractComplexCompiler {
|
|
875
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.ConditionalDefinition`;
|
|
876
|
-
constructor() {
|
|
877
|
-
super(false);
|
|
878
|
-
}
|
|
879
|
-
generate(fw, level, templateMarkNode) {
|
|
880
|
-
const clone = makeCiceroMark(templateMarkNode, true);
|
|
881
|
-
delete clone.condition;
|
|
882
|
-
delete clone.dependencies;
|
|
883
|
-
delete clone.functionName;
|
|
884
|
-
clone.isTrue = true;
|
|
885
|
-
writeOpenGenerateScope(fw, level);
|
|
886
|
-
if (templateMarkNode.condition) {
|
|
887
|
-
fw.writeLine(level, `const isTrue:boolean = UserCode.${templateMarkNode.functionName}(data,library,options)`);
|
|
888
|
-
} else {
|
|
889
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
890
|
-
fw.writeLine(level, "const isTrue:boolean = Runtime.peek($data);");
|
|
891
|
-
writeCloseDataScope(fw, level);
|
|
892
|
-
}
|
|
893
|
-
fw.writeLine(level, `const curNode = ${JSON.stringify(clone)} as ${getTypeScriptType(templateMarkNode.$class)};`);
|
|
894
|
-
fw.writeLine(level, "(curNode as CiceroMark.IConditional).isTrue = isTrue;");
|
|
895
|
-
fw.writeLine(level, "return curNode;");
|
|
896
|
-
writeCloseGenerateScope(fw, level);
|
|
897
|
-
delete templateMarkNode.condition;
|
|
898
|
-
delete templateMarkNode.whenTrue;
|
|
899
|
-
delete templateMarkNode.whenFalse;
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
class ContractDefinition extends AbstractSimpleCompiler {
|
|
904
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.ContractDefinition`;
|
|
905
|
-
constructor() {
|
|
906
|
-
super(false);
|
|
907
|
-
}
|
|
908
|
-
getNode(templateMarkNode) {
|
|
909
|
-
return {
|
|
910
|
-
$class: `${CiceroMarkModel.NAMESPACE}.Contract`,
|
|
911
|
-
name: templateMarkNode.name,
|
|
912
|
-
nodes: []
|
|
913
|
-
};
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
class FormattedVariable extends AbstractComplexCompiler {
|
|
918
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.FormattedVariableDefinition`;
|
|
919
|
-
constructor() {
|
|
920
|
-
super(true);
|
|
921
|
-
}
|
|
922
|
-
generate(fw, level, templateMarkNode) {
|
|
923
|
-
const clone = makeCiceroMark(templateMarkNode);
|
|
924
|
-
writeOpenGenerateScope(fw, level);
|
|
925
|
-
if (templateMarkNode.name !== "this") {
|
|
926
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
927
|
-
}
|
|
928
|
-
fw.writeLine(level, `const drafter = $getDrafter('${templateMarkNode.elementType}');`);
|
|
929
|
-
fw.writeLine(level, `const text = drafter ? drafter(Runtime.peek($data), '${templateMarkNode.format}') : JSON.stringify(Runtime.peek($data)) as string;`);
|
|
930
|
-
if (templateMarkNode.name !== "this") {
|
|
931
|
-
writeCloseDataScope(fw, level);
|
|
932
|
-
}
|
|
933
|
-
fw.writeLine(level, `const variable:any = ${JSON.stringify(clone)};`);
|
|
934
|
-
fw.writeLine(level, "variable.value = text;");
|
|
935
|
-
fw.writeLine(level, `return variable as ${getTypeScriptType(clone.$class)};`);
|
|
936
|
-
writeCloseGenerateScope(fw, level);
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
class Formula extends AbstractComplexCompiler {
|
|
941
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.FormulaDefinition`;
|
|
942
|
-
constructor() {
|
|
943
|
-
super(true);
|
|
944
|
-
}
|
|
945
|
-
generate(fw, level, templateMarkNode) {
|
|
946
|
-
const clone = makeCiceroMark(templateMarkNode);
|
|
947
|
-
writeOpenGenerateScope(fw, level);
|
|
948
|
-
fw.writeLine(level, `const formulaResult = UserCode.${templateMarkNode.name}(data,library,options);`);
|
|
949
|
-
fw.writeLine(level, "const text = JSON.stringify(formulaResult);");
|
|
950
|
-
fw.writeLine(level, `const formula:any = ${JSON.stringify(clone)};`);
|
|
951
|
-
fw.writeLine(level, "formula.value = text;");
|
|
952
|
-
fw.writeLine(level, `formula.code = ${JSON.stringify(templateMarkNode.code.contents)};`);
|
|
953
|
-
fw.writeLine(level, `return formula as ${getTypeScriptType(clone.$class)};`);
|
|
954
|
-
writeCloseGenerateScope(fw, level);
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
class Join extends AbstractComplexCompiler {
|
|
959
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.JoinDefinition`;
|
|
960
|
-
constructor() {
|
|
961
|
-
super(true);
|
|
962
|
-
}
|
|
963
|
-
generate(fw, level, templateMarkNode) {
|
|
964
|
-
writeOpenGenerateScope(fw, level);
|
|
965
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
966
|
-
fw.writeLine(level, `const joinDef = ${JSON.stringify(templateMarkNode)};`);
|
|
967
|
-
fw.writeLine(level, "const text = Runtime.joinList(Runtime.peek($data), joinDef, options);");
|
|
968
|
-
writeCloseDataScope(fw, level);
|
|
969
|
-
fw.writeLine(level, `return { $class: '${CommonMarkModel.NAMESPACE}.Text', text: text } as ${getTypeScriptType(CommonMarkModel.NAMESPACE + ".Text")};`);
|
|
970
|
-
writeCloseGenerateScope(fw, level);
|
|
971
|
-
delete templateMarkNode.nodes;
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
class ListBlock extends AbstractComplexCompiler {
|
|
976
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.ListBlockDefinition`;
|
|
977
|
-
constructor() {
|
|
978
|
-
super(false);
|
|
979
|
-
}
|
|
980
|
-
enter(fw, level, templateMarkNode) {
|
|
981
|
-
writeDebug(fw, level, templateMarkNode);
|
|
982
|
-
writeOpenNodeScope(fw, level);
|
|
983
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
984
|
-
}
|
|
985
|
-
generate(fw, level, templateMarkNode, doIt) {
|
|
986
|
-
if (templateMarkNode?.nodes?.[0]) {
|
|
987
|
-
const clone = makeCiceroMark(templateMarkNode?.nodes?.[0]);
|
|
988
|
-
writeOpenGenerateScope(fw, level);
|
|
989
|
-
fw.writeLine(level, `const result = ${JSON.stringify(clone)} as ${getTypeScriptType(templateMarkNode.$class)};`);
|
|
990
|
-
fw.writeLine(level, "Runtime.peek($data).forEach( (item:any) => {");
|
|
991
|
-
fw.writeLine(level, "Runtime.push($data,item);");
|
|
992
|
-
if (templateMarkNode.nodes?.[0].nodes?.[0]) {
|
|
993
|
-
doIt(fw, level + 1, templateMarkNode.nodes[0].nodes[0]);
|
|
994
|
-
delete templateMarkNode.nodes;
|
|
995
|
-
}
|
|
996
|
-
fw.writeLine(level, "Runtime.pop($data);");
|
|
997
|
-
fw.writeLine(level, "});");
|
|
998
|
-
fw.writeLine(level, "return result;");
|
|
999
|
-
writeCloseGenerateScope(fw, level);
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
exit(fw, level) {
|
|
1003
|
-
writeCloseDataScope(fw, level);
|
|
1004
|
-
writeCloseNodeScope(fw, level);
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
class Optional extends AbstractComplexCompiler {
|
|
1009
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.OptionalDefinition`;
|
|
1010
|
-
constructor() {
|
|
1011
|
-
super(false);
|
|
1012
|
-
}
|
|
1013
|
-
enter(fw, level, templateMarkNode) {
|
|
1014
|
-
writeDebug(fw, level, templateMarkNode);
|
|
1015
|
-
writeOpenNodeScope(fw, level);
|
|
1016
|
-
writeOpenDataScope(fw, level, templateMarkNode.name, true);
|
|
1017
|
-
}
|
|
1018
|
-
generate(fw, level, templateMarkNode, doIt) {
|
|
1019
|
-
const clone = makeCiceroMark(templateMarkNode);
|
|
1020
|
-
delete clone.nodes;
|
|
1021
|
-
delete clone.whenSome;
|
|
1022
|
-
delete clone.whenNone;
|
|
1023
|
-
writeOpenGenerateScope(fw, level);
|
|
1024
|
-
fw.writeLine(level, `const result = ${JSON.stringify(clone)} as ${getTypeScriptType(templateMarkNode.$class)};`);
|
|
1025
|
-
fw.writeLine(level, "const hasSome:boolean = !(Runtime.peek($data) === null || Runtime.peek($data) === undefined);");
|
|
1026
|
-
fw.writeLine(level, "(result as CiceroMark.IOptional).hasSome = hasSome;");
|
|
1027
|
-
fw.writeLine(level, "if(hasSome) {");
|
|
1028
|
-
doIt(fw, level + 1, { $class: `${CommonMarkModel.NAMESPACE}.Paragraph`, nodes: templateMarkNode.whenSome });
|
|
1029
|
-
fw.writeLine(level, "(result as CiceroMark.IOptional).whenSome = $result.nodes[0];");
|
|
1030
|
-
fw.writeLine(level, "(result as CiceroMark.IOptional).whenNone = $result.nodes[0];");
|
|
1031
|
-
fw.writeLine(level, "return result;");
|
|
1032
|
-
fw.writeLine(level, "}");
|
|
1033
|
-
fw.writeLine(level, "else {");
|
|
1034
|
-
doIt(fw, level + 1, { $class: `${CommonMarkModel.NAMESPACE}.Paragraph`, nodes: templateMarkNode.whenNone });
|
|
1035
|
-
fw.writeLine(level, "(result as CiceroMark.IOptional).whenNone = $result.nodes[0];");
|
|
1036
|
-
fw.writeLine(level, "(result as CiceroMark.IOptional).whenSome = $result.nodes[0];");
|
|
1037
|
-
fw.writeLine(level, "return result;");
|
|
1038
|
-
fw.writeLine(level, "}");
|
|
1039
|
-
writeCloseGenerateScope(fw, level);
|
|
1040
|
-
delete templateMarkNode.nodes;
|
|
1041
|
-
delete templateMarkNode.whenSome;
|
|
1042
|
-
delete templateMarkNode.whenNone;
|
|
1043
|
-
}
|
|
1044
|
-
exit(fw, level) {
|
|
1045
|
-
writeCloseDataScope(fw, level);
|
|
1046
|
-
writeCloseNodeScope(fw, level);
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
class Variable extends AbstractComplexCompiler {
|
|
1051
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.VariableDefinition`;
|
|
1052
|
-
constructor() {
|
|
1053
|
-
super(true);
|
|
1054
|
-
}
|
|
1055
|
-
generate(fw, level, templateMarkNode) {
|
|
1056
|
-
const clone = makeCiceroMark(templateMarkNode);
|
|
1057
|
-
writeOpenGenerateScope(fw, level);
|
|
1058
|
-
if (templateMarkNode.name !== "this") {
|
|
1059
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
1060
|
-
}
|
|
1061
|
-
fw.writeLine(level, "const text = JSON.stringify(Runtime.peek($data)) as string;");
|
|
1062
|
-
if (templateMarkNode.name !== "this") {
|
|
1063
|
-
writeCloseDataScope(fw, level);
|
|
1064
|
-
}
|
|
1065
|
-
fw.writeLine(level, `const variable:any = ${JSON.stringify(clone)};`);
|
|
1066
|
-
fw.writeLine(level, "variable.value = text;");
|
|
1067
|
-
fw.writeLine(level, `return variable as ${getTypeScriptType(clone.$class)};`);
|
|
1068
|
-
writeCloseGenerateScope(fw, level);
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
class With extends AbstractComplexCompiler {
|
|
1073
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.WithDefinition`;
|
|
1074
|
-
constructor() {
|
|
1075
|
-
super(false);
|
|
1076
|
-
}
|
|
1077
|
-
enter(fw, level, templateMarkNode) {
|
|
1078
|
-
writeDebug(fw, level, templateMarkNode);
|
|
1079
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
1080
|
-
}
|
|
1081
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
|
|
1082
|
-
generate(fw, level, templateMarkNode) {
|
|
1083
|
-
}
|
|
1084
|
-
exit(fw, level) {
|
|
1085
|
-
writeCloseDataScope(fw, level);
|
|
1086
|
-
}
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
class EnumVariable extends AbstractComplexCompiler {
|
|
1090
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.EnumVariableDefinition`;
|
|
1091
|
-
constructor() {
|
|
1092
|
-
super(true);
|
|
1093
|
-
}
|
|
1094
|
-
generate(fw, level, templateMarkNode) {
|
|
1095
|
-
const clone = makeCiceroMark(templateMarkNode);
|
|
1096
|
-
writeOpenGenerateScope(fw, level);
|
|
1097
|
-
if (templateMarkNode.name !== "this") {
|
|
1098
|
-
writeOpenDataScope(fw, level, templateMarkNode.name);
|
|
1099
|
-
}
|
|
1100
|
-
fw.writeLine(level, "const text = JSON.stringify(Runtime.peek($data)) as string;");
|
|
1101
|
-
if (templateMarkNode.name !== "this") {
|
|
1102
|
-
writeCloseDataScope(fw, level);
|
|
1103
|
-
}
|
|
1104
|
-
fw.writeLine(level, `const variable:any = ${JSON.stringify(clone)};`);
|
|
1105
|
-
fw.writeLine(level, "variable.value = text;");
|
|
1106
|
-
fw.writeLine(level, `return variable as ${getTypeScriptType(clone.$class)};`);
|
|
1107
|
-
writeCloseGenerateScope(fw, level);
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
const NODE_COMPILERS = {};
|
|
1112
|
-
NODE_COMPILERS[ContractDefinition.TYPE] = new ContractDefinition();
|
|
1113
|
-
NODE_COMPILERS[Variable.TYPE] = new Variable();
|
|
1114
|
-
NODE_COMPILERS[EnumVariable.TYPE] = new EnumVariable();
|
|
1115
|
-
NODE_COMPILERS[FormattedVariable.TYPE] = new FormattedVariable();
|
|
1116
|
-
NODE_COMPILERS[Formula.TYPE] = new Formula();
|
|
1117
|
-
NODE_COMPILERS[Conditional.TYPE] = new Conditional();
|
|
1118
|
-
NODE_COMPILERS[Join.TYPE] = new Join();
|
|
1119
|
-
NODE_COMPILERS[With.TYPE] = new With();
|
|
1120
|
-
NODE_COMPILERS[Clause.TYPE] = new Clause();
|
|
1121
|
-
NODE_COMPILERS[Optional.TYPE] = new Optional();
|
|
1122
|
-
NODE_COMPILERS[ListBlock.TYPE] = new ListBlock();
|
|
1123
|
-
NODE_COMPILERS[`${TemplateMarkModel.NAMESPACE}.Code`] = new Ignore();
|
|
1124
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Document`] = new Ignore();
|
|
1125
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Paragraph`] = new PassThrough(false);
|
|
1126
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Text`] = new PassThrough(true);
|
|
1127
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Attribute`] = new PassThrough(false);
|
|
1128
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.TagInfo`] = new PassThrough(false);
|
|
1129
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.CodeBlock`] = new PassThrough(false);
|
|
1130
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Code`] = new PassThrough(false);
|
|
1131
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.HtmlInline`] = new PassThrough(false);
|
|
1132
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.HtmlBlock`] = new PassThrough(false);
|
|
1133
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Emph`] = new PassThrough(false);
|
|
1134
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Strong`] = new PassThrough(false);
|
|
1135
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.BlockQuote`] = new PassThrough(false);
|
|
1136
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Heading`] = new PassThrough(false);
|
|
1137
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.ThematicBreak`] = new PassThrough(false);
|
|
1138
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Softbreak`] = new PassThrough(false);
|
|
1139
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Linebreak`] = new PassThrough(false);
|
|
1140
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Link`] = new PassThrough(false);
|
|
1141
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Image`] = new PassThrough(false);
|
|
1142
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.List`] = new PassThrough(false);
|
|
1143
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Item`] = new PassThrough(false);
|
|
1144
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.Table`] = new PassThrough(false);
|
|
1145
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.TableHead`] = new PassThrough(false);
|
|
1146
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.TableBody`] = new PassThrough(false);
|
|
1147
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.TableRow`] = new PassThrough(false);
|
|
1148
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.TableRow`] = new PassThrough(false);
|
|
1149
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.HeaderCell`] = new PassThrough(false);
|
|
1150
|
-
NODE_COMPILERS[`${CommonMarkModel.NAMESPACE}.TableCell`] = new PassThrough(false);
|
|
1151
|
-
function getCompiler(fqn) {
|
|
1152
|
-
const compiler = NODE_COMPILERS[fqn];
|
|
1153
|
-
if (!compiler) {
|
|
1154
|
-
throw new Error(`No compiler found for node ${fqn}`);
|
|
1155
|
-
}
|
|
1156
|
-
return compiler;
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
const RUNTIME_TGZ_BASE64 = "H4sIAAAAAAAAA+0dy24bydFnfUXHMJacNT18iwC1sleiaFsriTJEab22Yey2ZprkrIczk3mYZhReNsgxXxHkkCBAkFzyRoAgue9xj0GAnPMLqX7Mk8OHSEo2NlMHi9NTXV1dVVPVVd0zlouOrRRVG/dczegX79wElEqlRr2O4G+5US/Tv/Cjyv4KQHfKtVq92qht18qlO6VyvdJooDv1G+EmAZ7jYhtYsT3DIPbqdP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++c/39BRmkEEGGXyY8D/30+R/AHYAAA==";
|
|
1160
|
-
|
|
1161
|
-
class TemplateMarkToTypeScriptCompiler {
|
|
1162
|
-
modelManager;
|
|
1163
|
-
templateClass;
|
|
1164
|
-
constructor(modelManager, templateConceptFqn) {
|
|
1165
|
-
this.modelManager = modelManager;
|
|
1166
|
-
this.templateClass = getTemplateClassDeclaration(this.modelManager, templateConceptFqn);
|
|
1167
|
-
}
|
|
1168
|
-
writeFunction(fw, functionName, returnType, code) {
|
|
1169
|
-
fw.writeLine(0, writeFunctionToString(this.templateClass, functionName, returnType, code));
|
|
1170
|
-
}
|
|
1171
|
-
/**
|
|
1172
|
-
* Get a model manager with templatemark model
|
|
1173
|
-
* @param {string} outputDir the output directory
|
|
1174
|
-
* @returns {ModelManager} the model manager
|
|
1175
|
-
*/
|
|
1176
|
-
getTemplateMarkModelManager() {
|
|
1177
|
-
const modelManager = new ModelManager({ strict: true });
|
|
1178
|
-
modelManager.addCTOModel(ConcertoMetaModel.MODEL, "concertometamodel.cto");
|
|
1179
|
-
modelManager.addCTOModel(CommonMarkModel.MODEL, "commonmark.cto");
|
|
1180
|
-
modelManager.addCTOModel(TemplateMarkModel.MODEL, "templatemark.cto");
|
|
1181
|
-
return modelManager;
|
|
1182
|
-
}
|
|
1183
|
-
/**
|
|
1184
|
-
* Get a model manager with templatemark model
|
|
1185
|
-
* @param {string} outputDir the output directory
|
|
1186
|
-
* @returns {ModelManager} the model manager
|
|
1187
|
-
*/
|
|
1188
|
-
getCiceroMarkModelManager() {
|
|
1189
|
-
const modelManager = new ModelManager({ strict: true });
|
|
1190
|
-
modelManager.addCTOModel(ConcertoMetaModel.MODEL, "concertometamodel.cto");
|
|
1191
|
-
modelManager.addCTOModel(CommonMarkModel.MODEL, "commonmark.cto");
|
|
1192
|
-
modelManager.addCTOModel(CiceroMarkModel.MODEL, "ciceromark.cto");
|
|
1193
|
-
return modelManager;
|
|
1194
|
-
}
|
|
1195
|
-
generateTypeScript(modelManager, outputDir) {
|
|
1196
|
-
const tsVisitor = new CodeGen.TypescriptVisitor();
|
|
1197
|
-
const parameters = {
|
|
1198
|
-
fileWriter: new FileWriter(outputDir)
|
|
1199
|
-
};
|
|
1200
|
-
modelManager.accept(tsVisitor, parameters);
|
|
1201
|
-
}
|
|
1202
|
-
generateSampleData(modelManager, outputDir) {
|
|
1203
|
-
const factory = new Factory(modelManager);
|
|
1204
|
-
const instance = factory.newResource(
|
|
1205
|
-
this.templateClass.getNamespace(),
|
|
1206
|
-
this.templateClass.getName(),
|
|
1207
|
-
this.templateClass.isIdentified() ? "sample" : void 0,
|
|
1208
|
-
{ generate: "sample", includeOptionalFields: true }
|
|
1209
|
-
);
|
|
1210
|
-
writeFileSync(`${outputDir}/sample.json`, JSON.stringify(instance, null, 2));
|
|
1211
|
-
}
|
|
1212
|
-
/**
|
|
1213
|
-
* The main iteration function over templatemark. This function
|
|
1214
|
-
* looks up an INodeCompiler that can handle each templatemark node
|
|
1215
|
-
* and delegates to it for code generation.
|
|
1216
|
-
* Note that the result of code generation (a CiceroMark node) is left in
|
|
1217
|
-
* the $result global varianble.
|
|
1218
|
-
* @param {FileWriter} fw the file writer to use
|
|
1219
|
-
* @param {number} level the indentation level
|
|
1220
|
-
* @param {object} templateMark the templatemark node to iterate over
|
|
1221
|
-
*/
|
|
1222
|
-
static doIt(fw, level, templateMark) {
|
|
1223
|
-
fw.writeLine(level, `// start processing ${ModelUtil.getShortName(templateMark.$class)}`);
|
|
1224
|
-
traverse(templateMark).forEach(function(context) {
|
|
1225
|
-
if (typeof context === "object" && context.$class && typeof context.$class === "string") {
|
|
1226
|
-
const nodeClass = context.$class;
|
|
1227
|
-
const compiler = getCompiler(nodeClass);
|
|
1228
|
-
const sourceLevel = level + 1;
|
|
1229
|
-
this.before(() => {
|
|
1230
|
-
compiler.enter(fw, sourceLevel, context);
|
|
1231
|
-
compiler.generate(fw, sourceLevel, context, TemplateMarkToTypeScriptCompiler.doIt);
|
|
1232
|
-
});
|
|
1233
|
-
this.after(() => {
|
|
1234
|
-
compiler.exit(fw, sourceLevel, context);
|
|
1235
|
-
});
|
|
1236
|
-
}
|
|
1237
|
-
});
|
|
1238
|
-
fw.writeLine(level, `// end processing ${ModelUtil.getShortName(templateMark.$class)}`);
|
|
1239
|
-
}
|
|
1240
|
-
/**
|
|
1241
|
-
* Converts a TemplateMark document to typescript code that is parameterised
|
|
1242
|
-
* by the 'data' argument.
|
|
1243
|
-
*
|
|
1244
|
-
* The strategy is to traverse the template mark json, converting each node into
|
|
1245
|
-
* typescript statements that add an equivalent ciceromark node. Variables will
|
|
1246
|
-
* pull values from 'data'.
|
|
1247
|
-
*
|
|
1248
|
-
* Conditionals and formulae will call the functions produced by
|
|
1249
|
-
* compileUserCode.
|
|
1250
|
-
*
|
|
1251
|
-
* Note that code generation process is itself recursive, for example, when
|
|
1252
|
-
* a list definition node is encountered the list must be iterated over
|
|
1253
|
-
* based on a variable value coming from data, and code must be
|
|
1254
|
-
* generated to produce each list item.
|
|
1255
|
-
*
|
|
1256
|
-
* All internal variables are prefixed with '$' to ensure (?)
|
|
1257
|
-
* that they cannot collide with end user variable names.
|
|
1258
|
-
*
|
|
1259
|
-
* @param {IDocument} templateMark the TemplateMark Concerto resource
|
|
1260
|
-
* @param {string} outputDir the output directory for code
|
|
1261
|
-
*/
|
|
1262
|
-
compileGenerator(templateMark, outputDir) {
|
|
1263
|
-
const fw = new FileWriter(outputDir);
|
|
1264
|
-
fw.openFile("index.ts");
|
|
1265
|
-
fw.writeLine(0, "// generated code");
|
|
1266
|
-
fw.writeLine(0, "import { readFileSync } from 'fs';");
|
|
1267
|
-
fw.writeLine(0, "import { generator } from './generator';");
|
|
1268
|
-
fw.writeLine(0, "if(process.argv.length === 3) {");
|
|
1269
|
-
fw.writeLine(1, "const result = generator( JSON.parse(readFileSync(process.argv[2], 'utf-8')), {}, {});");
|
|
1270
|
-
fw.writeLine(1, "console.log(JSON.stringify(result, null, 2));");
|
|
1271
|
-
fw.writeLine(0, "}");
|
|
1272
|
-
fw.writeLine(0, "else {");
|
|
1273
|
-
fw.writeLine(1, "console.log('First argument is path to JSON data file');");
|
|
1274
|
-
fw.writeLine(0, "}");
|
|
1275
|
-
fw.closeFile();
|
|
1276
|
-
fw.openFile("generator.ts");
|
|
1277
|
-
fw.writeLine(0, "// generated code, do not modify");
|
|
1278
|
-
fw.writeLine(0, "");
|
|
1279
|
-
fw.writeLine(0, "// IMPORTS");
|
|
1280
|
-
writeImports(fw, 0, this.templateClass);
|
|
1281
|
-
fw.writeLine(0, "");
|
|
1282
|
-
fw.writeLine(0, "// GENERATOR");
|
|
1283
|
-
writeProlog(fw, 0, this.templateClass);
|
|
1284
|
-
TemplateMarkToTypeScriptCompiler.doIt(fw, 1, templateMark);
|
|
1285
|
-
writeEpilog(fw, 0);
|
|
1286
|
-
fw.closeFile();
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* Decompresses the base64 zipped runtime into an output folder.
|
|
1290
|
-
* @param {string} outputDir the output directory to copy the runtime to
|
|
1291
|
-
*/
|
|
1292
|
-
copyRuntime(outputDir) {
|
|
1293
|
-
ensureDirSync(`${outputDir}/${RUNTIME_DIR}`);
|
|
1294
|
-
const runtimeBuffer = Buffer.from(RUNTIME_TGZ_BASE64, "base64");
|
|
1295
|
-
const s = new Readable();
|
|
1296
|
-
s.push(runtimeBuffer);
|
|
1297
|
-
s.push(null);
|
|
1298
|
-
s.pipe(
|
|
1299
|
-
tar.x({
|
|
1300
|
-
strip: 2,
|
|
1301
|
-
C: `${outputDir}/${RUNTIME_DIR}`
|
|
1302
|
-
})
|
|
1303
|
-
);
|
|
1304
|
-
}
|
|
1305
|
-
/**
|
|
1306
|
-
* Create package.json for the generated code
|
|
1307
|
-
* @param {string} outputDir the output directory to copy the runtime to
|
|
1308
|
-
*/
|
|
1309
|
-
createPackage(outputDir) {
|
|
1310
|
-
const packageJson = {
|
|
1311
|
-
"name": "generated",
|
|
1312
|
-
"version": "1.0.0",
|
|
1313
|
-
"description": "TypeScript code generated from TemplateMark",
|
|
1314
|
-
"engines": {
|
|
1315
|
-
"node": ">=14",
|
|
1316
|
-
"npm": ">=6"
|
|
1317
|
-
},
|
|
1318
|
-
"keywords": [
|
|
1319
|
-
"accord project",
|
|
1320
|
-
"template",
|
|
1321
|
-
"templatemark"
|
|
1322
|
-
],
|
|
1323
|
-
"dependencies": {
|
|
1324
|
-
"dayjs": "1.11.7",
|
|
1325
|
-
"jsonpath": "^1.1.1"
|
|
1326
|
-
},
|
|
1327
|
-
"devDependencies": {
|
|
1328
|
-
"@types/node": "^20.2.0",
|
|
1329
|
-
"ts-node": "^10.9.1",
|
|
1330
|
-
"typescript": "^5.0.2",
|
|
1331
|
-
"@types/jsonpath": "^0.2.0"
|
|
1332
|
-
},
|
|
1333
|
-
"scripts": {
|
|
1334
|
-
"start": "ts-node index.ts sample.json"
|
|
1335
|
-
}
|
|
1336
|
-
};
|
|
1337
|
-
writeFileSync(`${outputDir}/package.json`, JSON.stringify(packageJson, null, 2));
|
|
1338
|
-
}
|
|
1339
|
-
compile(templateMark, outputDir, options) {
|
|
1340
|
-
ensureDirSync(outputDir);
|
|
1341
|
-
this.generateSampleData(this.modelManager, outputDir);
|
|
1342
|
-
this.generateTypeScript(this.modelManager, outputDir);
|
|
1343
|
-
this.generateTypeScript(this.getTemplateMarkModelManager(), outputDir);
|
|
1344
|
-
this.generateTypeScript(this.getCiceroMarkModelManager(), outputDir);
|
|
1345
|
-
const namedTemplateMark = nameUserCode(templateMark.toJSON ? templateMark.toJSON() : templateMark);
|
|
1346
|
-
this.compileUserCode(namedTemplateMark, outputDir);
|
|
1347
|
-
if (!options?.skipCopyRuntime) {
|
|
1348
|
-
this.copyRuntime(outputDir);
|
|
1349
|
-
}
|
|
1350
|
-
if (!options?.skipGenerator) {
|
|
1351
|
-
this.createPackage(outputDir);
|
|
1352
|
-
this.compileGenerator(namedTemplateMark, outputDir);
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
compileUserCode(templateMark, outputDir) {
|
|
1356
|
-
const fw = new FileWriter(outputDir);
|
|
1357
|
-
fw.openFile("usercode.ts");
|
|
1358
|
-
fw.writeLine(0, "// generated code, do not modify");
|
|
1359
|
-
fw.writeLine(0, "");
|
|
1360
|
-
fw.writeLine(0, "// IMPORTS");
|
|
1361
|
-
fw.writeLine(0, `import * as TemplateModel from './${this.templateClass.getNamespace()}';`);
|
|
1362
|
-
fw.writeLine(0, "import dayjs from 'dayjs';");
|
|
1363
|
-
fw.writeLine(0, "import jp from 'jsonpath';");
|
|
1364
|
-
fw.writeLine(0, "import {GenerationOptions} from './runtime/TypeScriptRuntime';");
|
|
1365
|
-
fw.writeLine(0, "");
|
|
1366
|
-
const templateMarkDom = templateMark;
|
|
1367
|
-
const functionNodes = traverse(templateMarkDom).reduce(function(acc, x) {
|
|
1368
|
-
if (x && x.$class === `${TemplateMarkModel.NAMESPACE}.FormulaDefinition`) {
|
|
1369
|
-
acc.push({
|
|
1370
|
-
name: x.name,
|
|
1371
|
-
code: x.code
|
|
1372
|
-
});
|
|
1373
|
-
}
|
|
1374
|
-
return acc;
|
|
1375
|
-
}, []);
|
|
1376
|
-
const conditionalNodes = traverse(templateMarkDom).reduce(function(acc, x) {
|
|
1377
|
-
if (x && x.$class === `${TemplateMarkModel.NAMESPACE}.ConditionalDefinition` && x.condition) {
|
|
1378
|
-
acc.push({
|
|
1379
|
-
name: x.functionName,
|
|
1380
|
-
code: x.condition
|
|
1381
|
-
});
|
|
1382
|
-
}
|
|
1383
|
-
return acc;
|
|
1384
|
-
}, []);
|
|
1385
|
-
const clauseNodes = traverse(templateMarkDom).reduce(function(acc, x) {
|
|
1386
|
-
if (x && x.$class === `${TemplateMarkModel.NAMESPACE}.ClauseDefinition` && x.condition) {
|
|
1387
|
-
acc.push({
|
|
1388
|
-
name: x.functionName,
|
|
1389
|
-
code: x.condition
|
|
1390
|
-
});
|
|
1391
|
-
}
|
|
1392
|
-
return acc;
|
|
1393
|
-
}, []);
|
|
1394
|
-
fw.writeLine(0, "// FORMULAE");
|
|
1395
|
-
functionNodes.forEach((fun) => {
|
|
1396
|
-
this.writeFunction(fw, `${fun.name}`, "any", fun.code.contents);
|
|
1397
|
-
});
|
|
1398
|
-
fw.writeLine(0, "");
|
|
1399
|
-
fw.writeLine(0, "// CONDITIONALS");
|
|
1400
|
-
conditionalNodes.forEach((fun) => {
|
|
1401
|
-
this.writeFunction(fw, `${fun.name}`, "boolean", fun.code.contents);
|
|
1402
|
-
});
|
|
1403
|
-
fw.writeLine(0, "");
|
|
1404
|
-
fw.writeLine(0, "// CLAUSES");
|
|
1405
|
-
clauseNodes.forEach((fun) => {
|
|
1406
|
-
this.writeFunction(fw, `${fun.name}`, "boolean", fun.code.contents);
|
|
1407
|
-
});
|
|
1408
|
-
fw.writeLine(0, "");
|
|
1409
|
-
fw.closeFile();
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
|
|
1413
|
-
export { TemplateMarkInterpreter, TemplateMarkToTypeScriptCompiler };
|
|
712
|
+
export { TemplateMarkInterpreter, ensureDirSync, getTemplateClassDeclaration, nameUserCode, removeSync, writeFunctionToString };
|
|
1414
713
|
//# sourceMappingURL=index.mjs.map
|