-
Notifications
You must be signed in to change notification settings - Fork 70
/
prepack.mjs
73 lines (58 loc) · 1.92 KB
/
prepack.mjs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
*
* TODO: this script should be abstracted into a standalone bin in @publicodes/tools
*/
import fs from 'fs'
import path from 'path'
const everyModelFolder = 'public'
const originModelFile = 'co2-model.FR-lang.fr.json'
const destPath = 'nosgestesclimat.model.json'
console.log('➡️ Preparing package ...')
// 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}`),
destPath
)
const rawData = fs.readFileSync(destPath)
const model = JSON.parse(rawData)
// Generating main index file (it only export types)
fs.writeFileSync('index.js', `export * from './index.d.ts';`)
if (!fs.existsSync('types')) {
fs.mkdirSync('types')
}
// Generate the DottedName type
fs.writeFileSync('./types/dottedNames.d.ts', generateDottedNamesType(model))
// Generate the Categories type
fs.writeFileSync('./types/categories.d.ts', generateCategoriesTypes(model))
// 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
}
function generateCategoriesTypes(model) {
const categories = model['bilan']?.formule.somme
const dFile = `
export type Categories = ${categories.map((category) => ` | "${category}"`).join('\n')}
`
return dFile
}
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')}
`
return dFile
}