-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
47 lines (36 loc) · 1.07 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
import { writeFileSync } from 'fs'
import { getModelFromSource } from '@publicodes/tools/compilation'
import { disabledLogger } from '@publicodes/tools'
import Engine from 'publicodes'
const srcFiles = 'rules/**/*.publicodes'
const destPath = 'publicodes-acv-numerique.model.json'
const model = getModelFromSource(srcFiles, { verbose: true })
try {
new Engine(model, { logger: disabledLogger })
} catch (e) {
console.error(`❌ Model compilation failed:\n${e.message}\n`)
process.exit(-1)
}
writeFileSync(destPath, JSON.stringify(model, null, 2))
console.log(`✅ ${destPath} generated`)
writeFileSync(
'index.js',
`
import rules from "./${destPath}" assert { type: "json" };
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 rules: Record<DottedName, Rule>
export default rules;
`
writeFileSync('index.d.ts', indexDTypes)
console.log(`✅ index.d.ts generated`)