|
1 | | -const fs = require('fs'); |
| 1 | +const fs = require("fs"); |
| 2 | +const pkg = require("./package.json"); |
2 | 3 |
|
3 | 4 | function processLocales(config) { |
4 | | - var locales = fs.createWriteStream('./src/locales.js'); |
5 | | - let localeByLang = config['locales'].reduce( |
6 | | - (lcs, lc) => { |
7 | | - lc.languages.forEach((lg) => lcs[lg] = lc.intl); |
8 | | - return lcs |
9 | | - }, |
10 | | - {} |
11 | | - ); |
12 | | - let filesByLang = config['locales'].reduce( |
13 | | - (fls, lc) => { |
14 | | - lc.languages.forEach((lg) => fls[lg] = lc.fileNames); |
15 | | - return fls |
16 | | - }, |
17 | | - {} |
18 | | - ); |
19 | | - locales.write(`export const locales = ${JSON.stringify(config['locales'].map((lc) => lc.intl))}`); |
20 | | - locales.write(`\nexport const fileNamesByLang = ${JSON.stringify(filesByLang)}`); |
21 | | - locales.write(`/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */`); |
22 | | - locales.write(`\nexport default ${JSON.stringify(localeByLang)}`); |
| 5 | + var locales = fs.createWriteStream("./src/locales.js"); |
| 6 | + let localeByLang = config["locales"].reduce((lcs, lc) => { |
| 7 | + lc.languages.forEach((lg) => (lcs[lg] = lc.intl)); |
| 8 | + return lcs; |
| 9 | + }, {}); |
| 10 | + let filesByLang = config["locales"].reduce((fls, lc) => { |
| 11 | + lc.languages.forEach((lg) => (fls[lg] = lc.fileNames)); |
| 12 | + return fls; |
| 13 | + }, {}); |
| 14 | + locales.write(`export const locales = ${JSON.stringify(config["locales"].map((lc) => lc.intl))}`); |
| 15 | + locales.write(`\nexport const fileNamesByLang = ${JSON.stringify(filesByLang)}`); |
| 16 | + locales.write(`/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */`); |
| 17 | + locales.write(`\nexport default ${JSON.stringify(localeByLang)}`); |
23 | 18 | } |
24 | 19 |
|
25 | | -function getModuleLogicalName(module) { |
26 | | - return module.logicalName || module.npm.match(/([^/]*)\/([^@]*).*/)[2]; |
| 20 | +function getConfig() { |
| 21 | + // Try to get the configuration from the args |
| 22 | + if (process.argv[2]) { |
| 23 | + console.log(` load configuration from '${process.argv[2]}'`); |
| 24 | + return JSON.parse(fs.readFileSync(process.argv[2], "utf-8")); |
| 25 | + } else if (process.env.OPENIMIS_CONF_JSON) { |
| 26 | + console.log(` load configuration from env`); |
| 27 | + return JSON.parse(process.env.OPENIMIS_CONF_JSON); |
| 28 | + } else if (fs.existsSync("./openimis.json")) { |
| 29 | + console.log(` load configuration from ./openimis.json`); |
| 30 | + return JSON.parse(fs.readFileSync("./openimis.json", "utf-8")); |
| 31 | + } else { |
| 32 | + throw new Error( |
| 33 | + "No configuration file found. Please provide a configuration in the CLI or in the OPENIMIS_CONF_JSON environment variable", |
| 34 | + ); |
| 35 | + } |
27 | 36 | } |
28 | 37 |
|
29 | | -function processModules(config, packageConfig) { |
30 | | - var srcModules = fs.createWriteStream('./src/modules.js'); |
31 | | - config['modules'].forEach((module) => { |
32 | | - let lib = module.npm.substring(0, module.npm.lastIndexOf('@')); |
33 | | - let version = module.npm.substring( module.npm.lastIndexOf('@')+1); |
34 | | - srcModules.write(`import { ${module.name} } from '${lib}';\n`); |
35 | | - packageConfig.dependencies[lib] = version; |
36 | | - }); |
37 | | - srcModules.write("\nexport const versions = [\n\t"); |
38 | | - srcModules.write(config["modules"].map((module) => `"${module.npm}"`).join(",\n\t")); |
39 | | - srcModules.write("\n];\nexport const modules = (cfg) => [\n\t"); |
40 | | - srcModules.write( |
41 | | - config["modules"] |
42 | | - .map((module) => `${module.name}((cfg && cfg["${getModuleLogicalName(module)}"]) || {})`) |
43 | | - .join(",\n\t") |
44 | | - ); |
45 | | - srcModules.write("\n];"); |
46 | | - srcModules.end(); |
47 | | - return packageConfig; |
| 38 | +function processModules(modules) { |
| 39 | + const stream = fs.createWriteStream("./src/modules.js"); |
48 | 40 |
|
49 | | -} |
| 41 | + stream.write(` |
| 42 | +export const packages = [ |
| 43 | + ${modules.map(({ moduleName }) => `"${moduleName}"`).join(",\n ")} |
| 44 | +];\n |
| 45 | +`); |
50 | 46 |
|
51 | | -function applyConfig(config, packageConfig) { |
52 | | - processLocales(config); |
53 | | - packageConfig = processModules(config, packageConfig); |
| 47 | + stream.write(` |
| 48 | +export function loadModules (cfg = {}) { |
| 49 | + return [ |
| 50 | + ${modules |
| 51 | + .map( |
| 52 | + ({ name, logicalName, moduleName }) => |
| 53 | + `require("${moduleName}").${name ?? "default"}(cfg["${logicalName}"] || {})`, |
| 54 | + ) |
| 55 | + .join(",\n ")} |
| 56 | + ];\n |
54 | 57 | } |
55 | | -// Configuration load |
| 58 | +`); |
56 | 59 |
|
57 | | -function cleanDeps(packageConfig){ |
58 | | - for (const key in packageConfig.dependencies) { |
59 | | - if (key.startsWith('@openimis')) delete packageConfig.dependencies[key]; |
60 | | - } |
61 | | - return packageConfig; |
| 60 | + stream.end(); |
62 | 61 | } |
63 | 62 |
|
64 | | -fs.readFile('package.json', 'utf8', function read(err, data) { |
65 | | - if (err) throw err; |
66 | | - let packageConfig = cleanDeps(JSON.parse(data)); |
67 | | - try { |
68 | | - if(!process.env.OPENIMIS_CONF_JSON)throw 'OPENIMIS_CONF_JSON not set !'; |
69 | | - JSON.parse(process.env.OPENIMIS_CONF_JSON); |
70 | | - applyConfig(JSON.parse(process.env.OPENIMIS_CONF_JSON), packageConfig); |
71 | | - } catch (e) { |
| 63 | +function main() { |
| 64 | + /* |
| 65 | + Load openIMIS configuration. Configuration is taken from args if provided or from the environment variable |
| 66 | + */ |
| 67 | + |
| 68 | + // Remove @openimis dependencies from package.json |
| 69 | + console.log("Remove @openimis dependencies from package.json"); |
| 70 | + for (const key in pkg.dependencies) { |
| 71 | + if (key.startsWith("@openimis/")) { |
| 72 | + // This only covers modules made from the openIMIS organization |
| 73 | + console.log(` removed ${key}`); |
| 74 | + delete pkg.dependencies[key]; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Get openIMIS configuration from args |
| 79 | + console.log("Load configuration"); |
| 80 | + const config = getConfig(); |
| 81 | + |
| 82 | + console.log("Process Locales"); |
| 83 | + processLocales(config); |
| 84 | + |
| 85 | + console.log("Process Modules"); |
| 86 | + const modules = []; |
| 87 | + for (const module of config.modules) { |
| 88 | + const { npm, name, logicalName } = module; |
| 89 | + // Find version number |
| 90 | + const moduleName = npm.substring(0, npm.lastIndexOf("@")); |
| 91 | + if (npm.lastIndexOf("@") <= 0) { |
| 92 | + throw new Error(` Module ${moduleName} has no version set.`); |
| 93 | + } |
| 94 | + const version = npm.substring(npm.lastIndexOf("@") + 1); |
| 95 | + console.log(` added "${moduleName}": ${version}`); |
| 96 | + pkg.dependencies[moduleName] = version; |
| 97 | + modules.push({ |
| 98 | + moduleName, |
| 99 | + version, |
| 100 | + name, |
| 101 | + npm, |
| 102 | + logicalName: logicalName || npm.match(/([^/]*)\/([^@]*).*/)[2], |
| 103 | + }); |
| 104 | + } |
| 105 | + processModules(modules); |
| 106 | +} |
72 | 107 |
|
73 | | - |
74 | | - var configFile = process.argv[2]; |
75 | | - if (configFile === null || configFile === '' | configFile === undefined){ |
76 | | - configFile = './openimis.json'; |
77 | | - } |
78 | | - console.log("Using file %s, Env variable OPENIMIS_CONF_JSON not valid: %s", configFile, process.env.OPENIMIS_CONF_JSON) |
79 | | - fs.readFile(configFile, 'utf8', function read(err, data) { |
80 | | - if (err) throw err; |
81 | | - config = JSON.parse(data); |
82 | | - applyConfig(config, packageConfig); |
83 | | - }); |
84 | | - } |
85 | | -}); |
| 108 | +main(); |
0 commit comments