chokibasic 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/buildconf.js +34 -21
package/package.json
CHANGED
package/src/buildconf.js
CHANGED
|
@@ -5,56 +5,69 @@ const pm = require("picomatch");
|
|
|
5
5
|
|
|
6
6
|
const ROOT = process.cwd();
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
|
|
9
|
+
async function getMetadata() {
|
|
10
|
+
let version = "0.0.0";
|
|
11
|
+
try {
|
|
12
|
+
const pkg = JSON.parse(await fs.readFile(path.join(ROOT, "package.json"), "utf8"));
|
|
13
|
+
version = pkg.version || version;
|
|
14
|
+
} catch (e) {}
|
|
15
|
+
|
|
16
|
+
const now = new Date();
|
|
17
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
18
|
+
const dateStr = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
|
19
|
+
|
|
20
|
+
return { version, date: dateStr };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
async function walkAndTransform(node, matchers, metadata) {
|
|
13
25
|
if (Array.isArray(node)) {
|
|
14
|
-
return await Promise.all(node.map(item => walkAndTransform(item, matchers)));
|
|
26
|
+
return await Promise.all(node.map(item => walkAndTransform(item, matchers, metadata)));
|
|
15
27
|
}
|
|
16
28
|
|
|
17
29
|
if (typeof node === 'object' && node !== null) {
|
|
18
30
|
const newNode = {};
|
|
19
31
|
for (const [key, value] of Object.entries(node)) {
|
|
20
|
-
newNode[key] = await walkAndTransform(value, matchers);
|
|
32
|
+
newNode[key] = await walkAndTransform(value, matchers, metadata);
|
|
21
33
|
}
|
|
22
34
|
return newNode;
|
|
23
35
|
}
|
|
24
36
|
|
|
25
37
|
if (typeof node === 'string') {
|
|
26
|
-
|
|
38
|
+
let processedString = node
|
|
39
|
+
.replace(/__VERSION__/g, metadata.version)
|
|
40
|
+
.replace(/__DATE__/g, metadata.date);
|
|
41
|
+
|
|
42
|
+
const fullPath = path.join(ROOT, processedString);
|
|
27
43
|
try {
|
|
28
44
|
const stats = await fs.stat(fullPath);
|
|
29
45
|
if (stats.isFile()) {
|
|
30
46
|
for (const [pattern, callback] of Object.entries(matchers)) {
|
|
31
|
-
|
|
32
|
-
if (isMatch(node)) {
|
|
47
|
+
if (pm(pattern)(processedString)) {
|
|
33
48
|
const content = await fs.readFile(fullPath);
|
|
34
|
-
return await callback(content,
|
|
49
|
+
return await callback(content, processedString);
|
|
35
50
|
}
|
|
36
51
|
}
|
|
37
52
|
}
|
|
38
|
-
} catch (err) {
|
|
39
|
-
|
|
53
|
+
} catch (err) {}
|
|
54
|
+
return processedString;
|
|
40
55
|
}
|
|
41
56
|
|
|
42
57
|
return node;
|
|
43
58
|
}
|
|
44
59
|
|
|
45
|
-
|
|
46
|
-
* Fonction principale buildConf
|
|
47
|
-
*/
|
|
60
|
+
|
|
48
61
|
async function buildConf(src, dst, matchers = {}) {
|
|
49
62
|
try {
|
|
63
|
+
const metadata = await getMetadata();
|
|
50
64
|
const fileContents = await fs.readFile(src, 'utf8');
|
|
51
65
|
const rawData = yaml.load(fileContents);
|
|
52
|
-
const processedData = await walkAndTransform(rawData, matchers);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
console.log(`✅ Build de la configuration réussi : ${dst}`);
|
|
66
|
+
const processedData = await walkAndTransform(rawData, matchers, metadata);
|
|
67
|
+
await fs.writeFile(dst, JSON.stringify(processedData, null, 2), 'utf8');
|
|
68
|
+
console.log(`✅ Build réussi : ${dst} (${metadata.version})`);
|
|
56
69
|
} catch (error) {
|
|
57
|
-
console.error("❌ Erreur
|
|
70
|
+
console.error("❌ Erreur :", error);
|
|
58
71
|
}
|
|
59
72
|
}
|
|
60
73
|
|