Skip to content

Commit

Permalink
feat(command/compile): add RuleValue to the genereated d.ts file
Browse files Browse the repository at this point in the history
It's could be used to have a typed evaluate() method
  • Loading branch information
EmileRolley committed Nov 18, 2024
1 parent 3a3f986 commit 7f15aa0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ the package.json file under the \`publicodes\` key. For example:
const serializedRuleTypes = typesEntries
.map(([name, type]) => ` "${name}": ${serializeType(type)}`)
.join(',\n')
const serializedRulesValue = typesEntries
.map(([name, type]) => {
const title = engine.getRule(name)?.rawNode?.titre
return `${title ? ` /** ${title} */\n` : ''} "${name}": ${serializeJSType(type)}`
})
.join(',\n')
// TODO: could be little bit more optimized
const serializedQuestionsRuleTypes = typesEntries
.filter(([name]) => engine.getRule(name).rawNode.question)
.map(([name, type]) => {
Expand Down Expand Up @@ -139,6 +146,14 @@ export type Situation = Partial<{
${serializedRuleTypes}
}>
/**
* Associates for each rule name its corresponding value type (in JavaScript
* form) that will be returned by the {@link Engine.evaluate} method.
*/
export type RuleValue = Partial<{
${serializedRulesValue}
}>
/**
* Subset of the {@link Situation} with only the rules that are questions
* (i.e. input rules).
Expand Down
12 changes: 7 additions & 5 deletions test/commands/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ describe('publicodes compile', () => {
it('should compile with no arguments/flags', async () => {
runInDir('tmp', async (cwd) => {
const { stdout } = await cli.execCommand('compile')
expect(stdout).toContain('Compilation complete!')
expect(fs.existsSync('build')).toBe(true)
expect(fs.existsSync('build/index.js')).toBe(true)
expect(fs.existsSync(`build/${path.basename(cwd)}.model.json`)).toBe(true)
expect(fs.existsSync(`build/index.d.ts`)).toBe(true)
expect(stdout).toContain('Compilation done.')
expect(fs.existsSync('publicodes-build')).toBe(true)
expect(fs.existsSync('publicodes-build/index.js')).toBe(true)
expect(
fs.existsSync(`publicodes-build/${path.basename(cwd)}.model.json`),
).toBe(true)
expect(fs.existsSync(`publicodes-build/index.d.ts`)).toBe(true)
})
})
})

0 comments on commit 7f15aa0

Please sign in to comment.