Skip to content

Commit

Permalink
fix(build): use the parsed rules to generate the types
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Jul 23, 2024
1 parent 111255f commit 4f2a345
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ const destPath = "model-template.model.json"

// Resolves all rules and their dependencies into a single JSON object
const model = getModelFromSource(srcFiles, { verbose: true })
let engine

// Try to create a new engine with the model to check for parsing errors
try {
const _ = new Engine(model)
engine = new Engine(model)
} catch (e) {
console.error(`❌ There is an error in the model:`)
console.error(e)
Expand All @@ -30,18 +31,18 @@ writeFileSync(
`import rules from "./${destPath}" assert { type: "json" };
export default rules;
`
`,
)
console.log(`✅ index.js generated`)

// Generate an index.d.ts file to export the model types
// where each rule name is a case in the DottedName type
let indexDTypes = Object.keys(model).reduce(
let indexDTypes = Object.keys(engine.getParsedRules()).reduce(
(acc, dottedName) => acc + `| "${dottedName}"\n`,
`import { Rule } from "publicodes";
export type DottedName =
`
`,
)

indexDTypes += `
Expand Down

0 comments on commit 4f2a345

Please sign in to comment.