poad 2.12.0 → 2.14.0

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.
@@ -0,0 +1,72 @@
1
+ // Modifications ciblées d'un fichier TOML simple (config.toml, secrets.toml) :
2
+ // le texte est modifié ligne à ligne, les commentaires et l'ordre restent.
3
+ // Valeurs : chaîne, nombre, booléen ; null retire la clé.
4
+ const fs = require('fs');
5
+
6
+ const format = (v) => (typeof v === 'string' ? JSON.stringify(v) : String(v));
7
+
8
+ /** Bornes [début, fin) des lignes de la section (début = ligne d'en-tête), ou null */
9
+ function sectionRange(lines, section) {
10
+ const head = lines.findIndex((l) => new RegExp(`^\\s*\\[${section.replace(/\./g, '\\.')}\\]\\s*$`).test(l));
11
+ if (head < 0) return null;
12
+ let end = head + 1;
13
+ while (end < lines.length && !/^\s*\[[^\]]+\]\s*$/.test(lines[end])) end++;
14
+ return [head, end];
15
+ }
16
+
17
+ /** Applique { clé: valeur | null } à la section (créée au besoin) d'un texte TOML */
18
+ function setInText(text, section, values) {
19
+ const lines = text.replace(/\s*$/, '').split(/\r?\n/);
20
+ let range = sectionRange(lines, section);
21
+ const entries = Object.entries(values);
22
+ if (!range) {
23
+ const add = entries.filter(([, v]) => v !== null && v !== undefined);
24
+ if (!add.length) return text;
25
+ if (lines.length && lines[lines.length - 1].trim()) lines.push('');
26
+ lines.push(`[${section}]`, ...add.map(([k, v]) => `${k} = ${format(v)}`));
27
+ return lines.join('\n') + '\n';
28
+ }
29
+ for (const [key, value] of entries) {
30
+ range = sectionRange(lines, section);
31
+ const at = lines.slice(range[0] + 1, range[1]).findIndex((l) => new RegExp(`^\\s*${key}\\s*=`).test(l));
32
+ const index = at < 0 ? -1 : range[0] + 1 + at;
33
+ if (value === null || value === undefined) {
34
+ if (index >= 0) lines.splice(index, 1);
35
+ } else if (index >= 0) {
36
+ lines[index] = `${key} = ${format(value)}`;
37
+ } else {
38
+ // Après la dernière ligne non vide de la section
39
+ let insert = range[1];
40
+ while (insert > range[0] + 1 && !lines[insert - 1].trim()) insert--;
41
+ lines.splice(insert, 0, `${key} = ${format(value)}`);
42
+ }
43
+ }
44
+ return lines.join('\n') + '\n';
45
+ }
46
+
47
+ /** Retire une section entière */
48
+ function removeSectionInText(text, section) {
49
+ const lines = text.split(/\r?\n/);
50
+ const range = sectionRange(lines, section);
51
+ if (!range) return text;
52
+ lines.splice(range[0], range[1] - range[0]);
53
+ return lines.join('\n').replace(/\n{3,}/g, '\n\n').replace(/\s*$/, '') + '\n';
54
+ }
55
+
56
+ function setKeys(file, section, values, { mode } = {}) {
57
+ const text = fs.existsSync(file) ? fs.readFileSync(file, 'utf-8') : '';
58
+ const updated = setInText(text, section, values);
59
+ if (updated !== text) fs.writeFileSync(file, updated, mode ? { mode } : undefined);
60
+ if (mode) fs.chmodSync(file, mode);
61
+ return updated !== text;
62
+ }
63
+
64
+ function removeSection(file, section) {
65
+ if (!fs.existsSync(file)) return false;
66
+ const text = fs.readFileSync(file, 'utf-8');
67
+ const updated = removeSectionInText(text, section);
68
+ if (updated !== text) fs.writeFileSync(file, updated);
69
+ return updated !== text;
70
+ }
71
+
72
+ module.exports = { setKeys, removeSection, setInText, removeSectionInText };