-
Notifications
You must be signed in to change notification settings - Fork 66
/
prepack.mjs
43 lines (32 loc) · 1.12 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
/**
*
* 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 ...')
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}`),
destPath
)
// Generating main index file (it only export types)
fs.writeFileSync('index.js', `export * from './index.d.ts';`)
// Generate the DottedName type
fs.writeFileSync('./dottedNames.d.ts', generateTypes(destPath))
console.log(`✅ dottedNames types generated`)
console.log('➡️ Packaging done')
function generateTypes(destPath) {
const rawData = fs.readFileSync(destPath)
const model = JSON.parse(rawData)
const dFile = `
export type DottedName =
${Object.keys(model)
.map((dottedName) => ` | "${dottedName}"`)
.join('\n')}`
return dFile
}