2024-01-25 16:56:36 +00:00
|
|
|
import { existsSync, mkdirSync, writeFileSync } from 'fs'
|
2022-05-24 17:00:18 +00:00
|
|
|
import path from 'path'
|
2023-06-15 13:57:48 +00:00
|
|
|
|
2024-01-25 16:46:31 +00:00
|
|
|
import { getModelFromSource } from '@publicodes/tools/compilation'
|
2022-05-24 17:00:18 +00:00
|
|
|
import Engine from 'publicodes'
|
2020-11-18 17:53:37 +00:00
|
|
|
|
2022-01-27 17:28:48 +00:00
|
|
|
const outDir = './dist'
|
2024-01-25 16:56:36 +00:00
|
|
|
const rules = getModelFromSource(path.resolve('./règles'), {
|
2024-01-25 16:46:31 +00:00
|
|
|
verbose: true,
|
|
|
|
})
|
2020-11-18 17:53:37 +00:00
|
|
|
|
2022-02-14 16:57:18 +00:00
|
|
|
export default function writeJSFile() {
|
2023-09-25 18:02:13 +00:00
|
|
|
const json = JSON.stringify(JSON.stringify(rules))
|
2023-12-15 11:12:26 +00:00
|
|
|
const names = Object.keys(new Engine(rules).getParsedRules())
|
2023-09-25 18:02:13 +00:00
|
|
|
const jsString = `export const json = /*@__PURE__*/ ${json};\nexport default /*@__PURE__*/ JSON.parse(json);`
|
2024-01-25 16:56:36 +00:00
|
|
|
// Create folder if doesn't exist
|
|
|
|
const folder = path.resolve(outDir)
|
|
|
|
if (!existsSync(folder)) {
|
|
|
|
mkdirSync(folder)
|
|
|
|
}
|
2024-01-25 16:46:31 +00:00
|
|
|
writeFileSync(path.resolve(outDir, 'index.js'), jsString)
|
|
|
|
writeFileSync(
|
2020-11-18 17:53:37 +00:00
|
|
|
path.resolve(outDir, 'names.ts'),
|
|
|
|
`\nexport type Names = ${names.map((name) => `"${name}"`).join('\n | ')}\n`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeJSFile()
|