@9of9solutions/replikator 1.0.5 → 1.0.6
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/main.js +94 -72
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -1,133 +1,152 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// --- Pour recréer __dirname en ESM ---
|
|
3
|
+
// --- Pour recréer __dirname en ESM (utile pour localiser ce fichier) ---
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
|
-
import path
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// --- Constantes ---
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
import chokidar from 'chokidar';
|
|
8
|
+
import inquirer from 'inquirer';
|
|
9
|
+
import winston from 'winston';
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = path.dirname(__filename);
|
|
13
|
+
|
|
14
|
+
// On stocke la config dans le dossier de ce script
|
|
16
15
|
const CONFIG_PATH = path.resolve(__dirname, 'config.json');
|
|
17
16
|
|
|
18
|
-
//
|
|
17
|
+
// Logger console + fichier (sync.log)
|
|
19
18
|
const logger = winston.createLogger({
|
|
20
19
|
transports: [
|
|
21
20
|
new winston.transports.Console({ format: winston.format.simple() })
|
|
22
21
|
]
|
|
23
22
|
});
|
|
24
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Recherche récursive d'un dossier `folderName` contenant à la fois
|
|
26
|
+
* un sous-dossier `src` et un `package.json`, à partir de `root`.
|
|
27
|
+
* Renvoie le chemin absolu du dossier/src, ou null si introuvable.
|
|
28
|
+
*/
|
|
25
29
|
async function findSrcWithPkg(root, folderName) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (srcStat.isDirectory() && pkgStat.isFile()) {
|
|
41
|
-
return srcDir;
|
|
42
|
-
}
|
|
43
|
-
} catch {
|
|
44
|
-
// soit src n'existe pas, soit package.json n'existe pas
|
|
30
|
+
for (const entry of await fs.readdir(root, { withFileTypes: true })) {
|
|
31
|
+
if (!entry.isDirectory()) continue;
|
|
32
|
+
const fullPath = path.join(root, entry.name);
|
|
33
|
+
|
|
34
|
+
if (entry.name === folderName) {
|
|
35
|
+
const srcDir = path.join(fullPath, 'src');
|
|
36
|
+
const pkgFile = path.join(fullPath, 'package.json');
|
|
37
|
+
try {
|
|
38
|
+
const [srcStat, pkgStat] = await Promise.all([
|
|
39
|
+
fs.stat(srcDir),
|
|
40
|
+
fs.stat(pkgFile)
|
|
41
|
+
]);
|
|
42
|
+
if (srcStat.isDirectory() && pkgStat.isFile()) {
|
|
43
|
+
return srcDir;
|
|
45
44
|
}
|
|
45
|
+
} catch {
|
|
46
|
+
// src ou package.json pas présent → on continue
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
-
// Sinon on descend plus bas
|
|
49
|
-
const nested = await findSrcWithPkg(fullPath, folderName);
|
|
50
|
-
if (nested) return nested;
|
|
51
48
|
}
|
|
52
|
-
|
|
49
|
+
|
|
50
|
+
// descendre plus bas
|
|
51
|
+
const nested = await findSrcWithPkg(fullPath, folderName);
|
|
52
|
+
if (nested) return nested;
|
|
53
53
|
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Charge la config si elle existe, ou la crée via prompt.
|
|
59
|
+
* La config stockée contient des chemins relatifs à process.cwd().
|
|
60
|
+
* Renvoie un objet { src, targets, ignore } où src/targets sont absolus.
|
|
61
|
+
*/
|
|
56
62
|
async function loadConfigOrPrompt() {
|
|
63
|
+
const projectRoot = process.cwd();
|
|
64
|
+
|
|
65
|
+
// 1) si config.json existe, on la lit et on rebuild les absolus
|
|
57
66
|
if (await fs.pathExists(CONFIG_PATH)) {
|
|
58
|
-
|
|
67
|
+
const raw = await fs.readJson(CONFIG_PATH);
|
|
68
|
+
return {
|
|
69
|
+
src: path.resolve(projectRoot, raw.src),
|
|
70
|
+
targets: raw.targets.map(rel => path.resolve(projectRoot, rel)),
|
|
71
|
+
ignore: raw.ignore
|
|
72
|
+
};
|
|
59
73
|
}
|
|
60
74
|
|
|
75
|
+
// 2) Sinon on demande les infos
|
|
61
76
|
const answers = await inquirer.prompt([
|
|
62
77
|
{
|
|
63
|
-
name:
|
|
64
|
-
type:
|
|
65
|
-
message: 'Chemin du répertoire à copier ?',
|
|
78
|
+
name: 'src',
|
|
79
|
+
type: 'input',
|
|
80
|
+
message: 'Chemin du répertoire à copier (depuis la racine du projet) ?',
|
|
66
81
|
default: 'packages/shared',
|
|
67
|
-
validate: i => fs.pathExistsSync(path.resolve(i)) || 'Le dossier n’existe pas'
|
|
82
|
+
validate: i => fs.pathExistsSync(path.resolve(projectRoot, i)) || 'Le dossier n’existe pas'
|
|
68
83
|
},
|
|
69
84
|
{
|
|
70
|
-
name:
|
|
71
|
-
type:
|
|
85
|
+
name: 'count',
|
|
86
|
+
type: 'number',
|
|
72
87
|
message: `Combien d'applications cibles ?`,
|
|
73
88
|
default: 1,
|
|
74
89
|
validate: n => n > 0 || 'Au moins 1'
|
|
75
90
|
},
|
|
76
91
|
{
|
|
77
|
-
name:
|
|
78
|
-
type:
|
|
92
|
+
name: 'targets',
|
|
93
|
+
type: 'input',
|
|
79
94
|
message: a => `Entrez les ${a.count} noms d'applications cibles (virgule séparatrice) :`,
|
|
80
|
-
filter:
|
|
95
|
+
filter: i => i.split(',').map(s => s.trim()),
|
|
81
96
|
validate: (input, a) =>
|
|
82
97
|
input.length === a.count || `Il faut ${a.count} applications.`
|
|
83
98
|
}
|
|
84
99
|
]);
|
|
85
|
-
|
|
100
|
+
|
|
101
|
+
// 3) On résout les chemins absolus
|
|
102
|
+
const absSrc = path.resolve(projectRoot, answers.src);
|
|
103
|
+
let absTargets;
|
|
86
104
|
try {
|
|
87
|
-
|
|
105
|
+
absTargets = await Promise.all(
|
|
88
106
|
answers.targets.map(async t => {
|
|
89
|
-
const folder = await findSrcWithPkg(
|
|
107
|
+
const folder = await findSrcWithPkg(projectRoot, t);
|
|
90
108
|
if (!folder) {
|
|
91
109
|
throw new Error(`Impossible de trouver l'application "${t}"`);
|
|
92
110
|
}
|
|
111
|
+
// on prend le dossier "shared" à l'intérieur de folder
|
|
93
112
|
return path.join(folder, 'shared');
|
|
94
113
|
})
|
|
95
114
|
);
|
|
96
115
|
} catch (err) {
|
|
97
|
-
console.error(`\
|
|
116
|
+
console.error(`\n⚠️ Erreur : ${err.message}\n`);
|
|
98
117
|
process.exit(1);
|
|
99
118
|
}
|
|
100
119
|
|
|
120
|
+
// 4) On convertit en relatifs **par rapport à projectRoot**
|
|
121
|
+
const relSrc = path.relative(projectRoot, absSrc);
|
|
122
|
+
const relTargets = absTargets.map(abs => path.relative(projectRoot, abs));
|
|
101
123
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const relSrc = path.relative(baseDir, path.resolve(answers.src));
|
|
105
|
-
const relTargets = targets.map(abs => path.relative(baseDir, abs));
|
|
106
|
-
|
|
107
|
-
const config = {
|
|
108
|
-
src: relSrc,
|
|
109
|
-
targets: relTargets,
|
|
110
|
-
ignore: []
|
|
111
|
-
};
|
|
112
|
-
await fs.writeJson(CONFIG_PATH, config, { spaces: 2 });
|
|
124
|
+
// 5) On écrit la config une fois, en relatif
|
|
125
|
+
const config = { src: relSrc, targets: relTargets, ignore: [] };
|
|
113
126
|
await fs.writeJson(CONFIG_PATH, config, { spaces: 2 });
|
|
114
127
|
logger.info(`Configuration enregistrée dans ${CONFIG_PATH}`);
|
|
115
|
-
|
|
128
|
+
|
|
129
|
+
// 6) On renvoie les chemins absolus pour la suite
|
|
130
|
+
return { src: absSrc, targets: absTargets, ignore: [] };
|
|
116
131
|
}
|
|
117
132
|
|
|
118
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Lance la copie initiale puis met en place le watcher.
|
|
135
|
+
*/
|
|
119
136
|
async function main() {
|
|
120
137
|
const { src, targets, ignore } = await loadConfigOrPrompt();
|
|
121
138
|
|
|
122
|
-
//
|
|
123
|
-
await Promise.all(
|
|
124
|
-
|
|
125
|
-
fs.
|
|
139
|
+
// Copie initiale
|
|
140
|
+
await Promise.all(
|
|
141
|
+
targets.map(dest =>
|
|
142
|
+
fs.ensureDir(dest).then(() =>
|
|
143
|
+
fs.copy(src, dest, { overwrite: true })
|
|
144
|
+
)
|
|
126
145
|
)
|
|
127
|
-
)
|
|
146
|
+
);
|
|
128
147
|
logger.info('Copie initiale terminée.');
|
|
129
148
|
|
|
130
|
-
//
|
|
149
|
+
// Watcher
|
|
131
150
|
const watcher = chokidar.watch(src, { ignored: ignore, persistent: true });
|
|
132
151
|
watcher
|
|
133
152
|
.on('add', p => syncFile('add', p, src, targets))
|
|
@@ -136,6 +155,9 @@ async function main() {
|
|
|
136
155
|
.on('error', e => logger.error('Watcher error:', e));
|
|
137
156
|
}
|
|
138
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Synchronise un fichier ajouté/modifié/supprimé auprès de toutes les targets.
|
|
160
|
+
*/
|
|
139
161
|
async function syncFile(event, filePath, srcDir, targets) {
|
|
140
162
|
const rel = path.relative(srcDir, filePath);
|
|
141
163
|
await Promise.all(targets.map(async destRoot => {
|
|
@@ -151,7 +173,7 @@ async function syncFile(event, filePath, srcDir, targets) {
|
|
|
151
173
|
}));
|
|
152
174
|
}
|
|
153
175
|
|
|
154
|
-
|
|
176
|
+
// Lancement du script
|
|
155
177
|
main().catch(err => {
|
|
156
178
|
logger.error('Erreur fatale :', err);
|
|
157
179
|
process.exit(1);
|