generated from publicodes/model-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
79 lines (58 loc) · 1.82 KB
/
build.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { writeFileSync } from "fs"
import { getModelFromSource } from "@publicodes/tools/compilation"
import Engine from "publicodes"
import getUI from "./scripts/compile-ui.js"
import getPersonas from "./scripts/compile-personas.js"
const srcFiles = "rules/**/*.publicodes"
const modelDestPath = "publicodes-evenements.model.json"
const personasDestPath = "publicodes-evenements.personas.json"
const model = getModelFromSource(srcFiles, { verbose: true })
try {
const engine = new Engine(model)
engine.evaluate("resultats . bilan total")
} catch (e) {
console.error(`❌ Model compilation failed:\n${e.message}\n`)
process.exit(-1)
}
writeFileSync(modelDestPath, JSON.stringify(model, null, 2))
console.log(`✅ ${modelDestPath} generated`)
const personas = getPersonas(model)
writeFileSync(personasDestPath, JSON.stringify(personas, null, 2))
console.log(`✅ ${personasDestPath} generated`)
const ui = getUI(model)
writeFileSync(
"index.js",
`
import rules from "./${modelDestPath}" assert { type: "json" };
import personas from "./${personasDestPath}" assert { type: "json" };
export const ui = ${JSON.stringify(ui, null, 2)};
export { personas };
export default rules;
`
)
console.log(`✅ index.js generated`)
let indexDTypes = Object.keys(model).reduce(
(acc, dottedName) => acc + `| "${dottedName}"\n`,
`
import { Rule } from "publicodes";
export type DottedName =
`
)
indexDTypes += `
declare let personas: {
[key: string]: {
titre: string;
description: string;
situation: Situation;
}
}
declare let ui: {
categories: Record<RuleName, {index: number, sub: RuleName[]}>;
questions: Record<RuleName, RuleName[][]>;
}
export { ui, personas }
declare let rules: Record<DottedName, Rule>
export default rules;
`
writeFileSync("index.d.ts", indexDTypes)
console.log(`✅ index.d.ts generated`)