forked from openimis/openimis-fe_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openimis-config.js
138 lines (122 loc) · 4.55 KB
/
openimis-config.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const fs = require("fs");
const pkg = require("./package.json");
function processLocales(config) {
var locales = fs.createWriteStream("./src/locales.js");
let localeByLang = config["locales"].reduce((lcs, lc) => {
lc.languages.forEach((lg) => (lcs[lg] = lc.intl));
return lcs;
}, {});
let filesByLang = config["locales"].reduce((fls, lc) => {
lc.languages.forEach((lg) => (fls[lg] = lc.fileNames));
return fls;
}, {});
locales.write(`export const locales = ${JSON.stringify(config["locales"].map((lc) => lc.intl))}`);
locales.write(`\nexport const fileNamesByLang = ${JSON.stringify(filesByLang)}`);
locales.write(`/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */`);
locales.write(`\nexport default ${JSON.stringify(localeByLang)}`);
}
function getConfig() {
// Try to get the configuration from the args
if (process.argv[2]) {
console.log(` load configuration from '${process.argv[2]}'`);
return JSON.parse(fs.readFileSync(process.argv[2], "utf-8"));
} else if (process.env.OPENIMIS_CONF_JSON) {
console.log(` load configuration from env`);
return JSON.parse(process.env.OPENIMIS_CONF_JSON);
} else if (fs.existsSync("./openimis.json")) {
console.log(` load configuration from ./openimis.json`);
return JSON.parse(fs.readFileSync("./openimis.json", "utf-8"));
} else {
throw new Error(
"No configuration file found. Please provide a configuration in the CLI or in the OPENIMIS_CONF_JSON environment variable",
);
}
}
function processModules(modules) {
const stream = fs.createWriteStream("./src/modules.js");
stream.write(`
export const packages = [
${modules.map(({ moduleName }) => `"${moduleName}"`).join(",\n ")}
];\n
`);
stream.write(`
export function loadModules (cfg = {}) {
return [
${modules
.map(
({ name, logicalName, moduleName }) =>
`require("${moduleName}").${name ?? "default"}(cfg["${logicalName}"] || {})`,
)
.join(",\n ")}
];\n
}
`);
stream.end();
}
/*
"@openimis/fe-core@git+https://github.com/openimis/openimis-fe-core_js.git#develop"
=> moduleName="@openimis/fe-core" and version="git+https://github.com/openimis/openimis-fe-core_js.git#develop"
"@openimis/fe-language_my@git+ssh://git@github.com:BLSQ/openimis-fe-language_my_js.git#main"
=> moduleName="@openimis/fe-language_my" and version="git+ssh://git@github.com:BLSQ/openimis-fe-language_my_js.git#main"
"@openimis/fe-core@>=1.5.1"
=> moduleName="@openimis/fe-core" and version=">=1.5.1"
*/
function splitModuleNameAndVersion(str) {
// Finding the index of the first '@' symbol
let firstAtIndex = str.indexOf('@');
// Finding the index of the second '@' symbol
let secondAtIndex = str.indexOf('@', firstAtIndex + 1);
let moduleName, version;
if (secondAtIndex !== -1) {
// If there is a second '@', split based on its position
moduleName = str.substring(0, secondAtIndex);
version = str.substring(secondAtIndex + 1);
} else {
// If there is no second '@', the entire string is the moduleName
moduleName = str;
version = '';
}
return { moduleName, version };
}
function main() {
/*
Load openIMIS configuration. Configuration is taken from args if provided or from the environment variable
*/
// Remove @openimis dependencies from package.json
console.log("Remove @openimis dependencies from package.json");
for (const key in pkg.dependencies) {
if (key.startsWith("@openimis/")) {
// This only covers modules made from the openIMIS organization
console.log(` removed ${key}`);
delete pkg.dependencies[key];
}
}
// Get openIMIS configuration from args
console.log("Load configuration");
const config = getConfig();
console.log("Process Locales");
processLocales(config);
console.log("Process Modules");
const modules = [];
for (const module of config.modules) {
const { npm, name, logicalName } = module;
let ver = splitModuleNameAndVersion(npm);
// Find version number
if (ver.version === '') {
throw new Error(` Module ${npm} has no version set.`);
}
console.log(` added "${ver.moduleName}": ${ver.version}`);
pkg.dependencies[ver.moduleName] = ver.version;
modules.push({
moduleName: ver.moduleName,
verison: ver.version,
name,
npm,
logicalName: logicalName || npm.match(/([^/]*)\/([^@]*).*/)[2],
});
}
processModules(modules);
console.log("Save package.json");
fs.writeFileSync("./package.json", JSON.stringify(pkg, null, 2), { encoding: "utf-8", flag: "w" });
}
main();