Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types generation (NGC-1465) #2475

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Evaluation, Rule, RuleNode, Situation } from 'publicodes'
import { Categories as GeneratedCategories } from './types/categories.d'
import { DottedName as GeneratedDottedName } from './types/dottedNames'
import { Subcategories as GeneratedSubcategories } from './types/subcategories.d'

import { DottedName } from './types/dottedNames'
export { DottedName } from './types/dottedNames'
export type DottedName = GeneratedDottedName
export type Categories = GeneratedCategories
export type Subcategories = GeneratedSubcategories

export type NGCRuleNode = RuleNode & {
rawNode: NGCRule
Expand Down Expand Up @@ -71,24 +75,22 @@ export type SupportedRegions = Record<RegionCode, SupportedRegion>

export type NGCRule = Omit<
Rule,
'formule' | 'question' | 'références' | 'valeur'
'formule' | 'question' | 'valeur' | 'description' | 'note'
> & {
formule?: Rule['formule'] | number
question?: Rule['question'] | null
références?: Rule['références'] | string[]
valeur?: Rule['valeur'] | number
description?: Rule['description'] | string
note?: Rule['note'] | string
abréviation?: string
mosaique?: MosaiqueNode
type?: string
action?: { dépasse: string[] }
icônes?: string
sévérité?: string
dottedName?: DottedName
plus?: boolean
aide?: string
inactif?: string
résumé?: string
plancher?: number
plafond?: string | number
avertissement?: string
}

Expand Down
66 changes: 25 additions & 41 deletions prepack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const everyModelFolder = 'public'
const originModelFile = 'co2-model.FR-lang.fr.json'

const destPath = 'nosgestesclimat.model.json'
const rawData = fs.readFileSync(destPath)
const model = JSON.parse(rawData)

console.log('➡️ Preparing package ...')

const everyModelPath = path.join(process.cwd(), everyModelFolder)

// Copying the origin model file to the root of the package (for use in others publicodes projects)
fs.copyFileSync(
path.join(process.cwd(), `${everyModelFolder}/${originModelFile}`),
Expand All @@ -23,66 +23,50 @@ fs.copyFileSync(
// Generating main index file (it only export types)
fs.writeFileSync('index.js', `export * from './index.d.ts';`)

// Generate the DottedName type
// Create a types folder at the root of the package
if (!fs.existsSync('types')) {
fs.mkdirSync('types')
}
fs.writeFileSync('./types/dottedNames.d.ts', generateTypes(destPath))

console.log(`✅ dottedNames types generated`)
console.log('➡️ Packaging done')
// Generate the DottedName type
fs.writeFileSync('./types/dottedNames.d.ts', generateDottedNamesType(model))

// Generate the Categories type
fs.writeFileSync('./types/categories.d.ts', generateCategoriesTypes(model))

function generateTypes(destPath) {
const rawData = fs.readFileSync(destPath)
const model = JSON.parse(rawData)
// Generate the Subcategories type
fs.writeFileSync(
'./types/subcategories.d.ts',
generateSubcategoriesTypes(model)
)

console.log(`✅ dottedNames, categories and subcategories types generated`)
console.log('➡️ Packaging done')

function generateDottedNamesType(model) {
const dFile = `
export type DottedName =
${Object.keys(model)
.map((dottedName) => ` | "${dottedName}"`)
.join('\n')}`

return dFile
}
// Generate types for the Categories and Subcategories dottedNames
const categories = [
'transport',
'alimentation',
'logement',
'divers',
'services sociétaux'
]
function generateCategoriesTypes() {

function generateCategoriesTypes(model) {
const categories = model['bilan']?.formule.somme
const dFile = `
export type Categories = ${categories.map((category) => ` | "${category}"`).join('\n')}
`

return dFile
}

function generateSubcategoriesTypes(destPath) {
const rawData = fs.readFileSync(destPath)
const model = JSON.parse(rawData)

const subcategories = categories.flatMap((category) =>
Object.keys(model).filter(
(dottedName) =>
dottedName.startsWith(category + ' . ') &&
dottedName.split(' . ').length === 2
)
function generateSubcategoriesTypes(model) {
const subcategories = Object.keys(model).filter((dottedName) =>
dottedName.startsWith('ui . pédagogie . sous catégories . ')
)

const dFile = `
export type Subcategories =
${subcategories.map((subcategory) => ` | "${subcategory}"`).join('\n')}
`

export type Subcategories =
${subcategories.map((subcategory) => ` | "${subcategory}"`).join('\n')}
`
return dFile
}

fs.writeFileSync('./types/categories.d.ts', generateCategoriesTypes())
fs.writeFileSync(
'./types/subcategories.d.ts',
generateSubcategoriesTypes(destPath)
)
Loading