@9of9solutions/replikator 1.0.8 → 1.0.9
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 +79 -123
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -7,8 +7,6 @@ import fs from 'fs-extra';
|
|
|
7
7
|
import chokidar from 'chokidar';
|
|
8
8
|
import inquirer from 'inquirer';
|
|
9
9
|
import winston from 'winston';
|
|
10
|
-
import { Command } from 'commander';
|
|
11
|
-
import degit from 'degit';
|
|
12
10
|
|
|
13
11
|
const __filename = fileURLToPath(import.meta.url);
|
|
14
12
|
const __dirname = path.dirname(__filename);
|
|
@@ -57,76 +55,88 @@ async function findSrcWithPkg(root, folderName) {
|
|
|
57
55
|
}
|
|
58
56
|
|
|
59
57
|
/**
|
|
60
|
-
*
|
|
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
61
|
*/
|
|
62
|
-
async function
|
|
62
|
+
async function loadConfigOrPrompt() {
|
|
63
63
|
const projectRoot = process.cwd();
|
|
64
64
|
|
|
65
|
-
//
|
|
66
|
-
let src, targets, ignore;
|
|
67
|
-
// 1) si config existe
|
|
65
|
+
// 1) si config.json existe, on la lit et on rebuild les absolus
|
|
68
66
|
if (await fs.pathExists(CONFIG_PATH)) {
|
|
69
67
|
const raw = await fs.readJson(CONFIG_PATH);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
name: 'src',
|
|
78
|
-
type: 'input',
|
|
79
|
-
message: 'Chemin du répertoire à copier (depuis la racine du projet) ?',
|
|
80
|
-
default: 'packages/shared',
|
|
81
|
-
validate: i => fs.pathExistsSync(path.resolve(projectRoot, i)) || 'Le dossier n’existe pas'
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
name: 'count',
|
|
85
|
-
type: 'number',
|
|
86
|
-
message: `Combien d'applications cibles ?`,
|
|
87
|
-
default: 1,
|
|
88
|
-
validate: n => n > 0 || 'Au moins 1'
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
name: 'targets',
|
|
92
|
-
type: 'input',
|
|
93
|
-
message: a => `Entrez les ${a.count} noms d'applications cibles (virgule séparatrice) :`,
|
|
94
|
-
filter: i => i.split(',').map(s => s.trim()),
|
|
95
|
-
validate: (input, a) =>
|
|
96
|
-
input.length === a.count || `Il faut ${a.count} applications.`
|
|
97
|
-
}
|
|
98
|
-
]);
|
|
99
|
-
|
|
100
|
-
const absSrc = path.resolve(projectRoot, answers.src);
|
|
101
|
-
let absTargets;
|
|
102
|
-
try {
|
|
103
|
-
absTargets = await Promise.all(
|
|
104
|
-
answers.targets.map(async t => {
|
|
105
|
-
const folder = await findSrcWithPkg(projectRoot, t);
|
|
106
|
-
if (!folder) {
|
|
107
|
-
throw new Error(`Impossible de trouver l'application "${t}"`);
|
|
108
|
-
}
|
|
109
|
-
return path.join(folder, 'shared');
|
|
110
|
-
})
|
|
111
|
-
);
|
|
112
|
-
} catch (err) {
|
|
113
|
-
console.error(`\n⚠️ Erreur : ${err.message}\n`);
|
|
114
|
-
process.exit(1);
|
|
115
|
-
}
|
|
68
|
+
return {
|
|
69
|
+
src: path.resolve(projectRoot, raw.src),
|
|
70
|
+
targets: raw.targets.map(rel => path.resolve(projectRoot, rel)),
|
|
71
|
+
ignore: raw.ignore
|
|
72
|
+
};
|
|
73
|
+
}
|
|
116
74
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
75
|
+
// 2) Sinon on demande les infos
|
|
76
|
+
const answers = await inquirer.prompt([
|
|
77
|
+
{
|
|
78
|
+
name: 'src',
|
|
79
|
+
type: 'input',
|
|
80
|
+
message: 'Chemin du répertoire à copier (depuis la racine du projet) ?',
|
|
81
|
+
default: 'packages/shared',
|
|
82
|
+
validate: i => fs.pathExistsSync(path.resolve(projectRoot, i)) || 'Le dossier n’existe pas'
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'count',
|
|
86
|
+
type: 'number',
|
|
87
|
+
message: `Combien d'applications cibles ?`,
|
|
88
|
+
default: 1,
|
|
89
|
+
validate: n => n > 0 || 'Au moins 1'
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'targets',
|
|
93
|
+
type: 'input',
|
|
94
|
+
message: a => `Entrez les ${a.count} noms d'applications cibles (virgule séparatrice) :`,
|
|
95
|
+
filter: i => i.split(',').map(s => s.trim()),
|
|
96
|
+
validate: (input, a) =>
|
|
97
|
+
input.length === a.count || `Il faut ${a.count} applications.`
|
|
98
|
+
}
|
|
99
|
+
]);
|
|
122
100
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
101
|
+
// 3) On résout les chemins absolus
|
|
102
|
+
const absSrc = path.resolve(projectRoot, answers.src);
|
|
103
|
+
let absTargets;
|
|
104
|
+
try {
|
|
105
|
+
absTargets = await Promise.all(
|
|
106
|
+
answers.targets.map(async t => {
|
|
107
|
+
const folder = await findSrcWithPkg(projectRoot, t);
|
|
108
|
+
if (!folder) {
|
|
109
|
+
throw new Error(`Impossible de trouver l'application "${t}"`);
|
|
110
|
+
}
|
|
111
|
+
// on prend le dossier "shared" à l'intérieur de folder
|
|
112
|
+
return path.join(folder, 'shared');
|
|
113
|
+
})
|
|
114
|
+
);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
console.error(`\n⚠️ Erreur : ${err.message}\n`);
|
|
117
|
+
process.exit(1);
|
|
127
118
|
}
|
|
128
119
|
|
|
129
|
-
//
|
|
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));
|
|
123
|
+
|
|
124
|
+
// 5) On écrit la config une fois, en relatif
|
|
125
|
+
const config = { src: relSrc, targets: relTargets, ignore: [] };
|
|
126
|
+
await fs.writeJson(CONFIG_PATH, config, { spaces: 2 });
|
|
127
|
+
logger.info(`Configuration enregistrée dans ${CONFIG_PATH}`);
|
|
128
|
+
|
|
129
|
+
// 6) On renvoie les chemins absolus pour la suite
|
|
130
|
+
return { src: absSrc, targets: absTargets, ignore: [] };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Lance la copie initiale puis met en place le watcher.
|
|
135
|
+
*/
|
|
136
|
+
async function main() {
|
|
137
|
+
const { src, targets, ignore } = await loadConfigOrPrompt();
|
|
138
|
+
|
|
139
|
+
// Copie initiale
|
|
130
140
|
await Promise.all(
|
|
131
141
|
targets.map(dest =>
|
|
132
142
|
fs.ensureDir(dest).then(() =>
|
|
@@ -136,7 +146,7 @@ async function watchCommand() {
|
|
|
136
146
|
);
|
|
137
147
|
logger.info('Copie initiale terminée.');
|
|
138
148
|
|
|
139
|
-
//
|
|
149
|
+
// Watcher
|
|
140
150
|
const watcher = chokidar.watch(src, { ignored: ignore, persistent: true });
|
|
141
151
|
watcher
|
|
142
152
|
.on('add', p => syncFile('add', p, src, targets))
|
|
@@ -163,62 +173,8 @@ async function syncFile(event, filePath, srcDir, targets) {
|
|
|
163
173
|
}));
|
|
164
174
|
}
|
|
165
175
|
|
|
166
|
-
//
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
process.exit(1);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
console.log(`\n📂 Création du dossier ${projectName}…`);
|
|
175
|
-
const emitter = degit('9of9solutions/replikator-template', {
|
|
176
|
-
cache: false,
|
|
177
|
-
force: true,
|
|
178
|
-
verbose: true
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
try {
|
|
182
|
-
await emitter.clone(projectPath);
|
|
183
|
-
console.log('✔️ Template cloné dans', projectName);
|
|
184
|
-
console.log('\n📦 Installation des dépendances…');
|
|
185
|
-
process.chdir(projectPath);
|
|
186
|
-
// installe via pnpm si disponible, sinon npm
|
|
187
|
-
const installer = fs.existsSync('pnpm-lock.yaml') ? 'pnpm install' : 'npm install';
|
|
188
|
-
require('child_process').execSync(installer, { stdio: 'inherit' });
|
|
189
|
-
console.log('\n✅ Projet initialisé !');
|
|
190
|
-
} catch (err) {
|
|
191
|
-
console.error('✖️ Échec du scaffolding:', err);
|
|
192
|
-
process.exit(1);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// --- CLI setup via Commander ---
|
|
197
|
-
const program = new Command();
|
|
198
|
-
|
|
199
|
-
program
|
|
200
|
-
.name('replikator')
|
|
201
|
-
.description('CLI de sync et scaffolding Replikator')
|
|
202
|
-
.version('1.0.0');
|
|
203
|
-
|
|
204
|
-
// commande `init`
|
|
205
|
-
program
|
|
206
|
-
.command('init <projectName>')
|
|
207
|
-
.description('Scaffold un nouveau projet depuis le template Replikator')
|
|
208
|
-
.action(initCommand);
|
|
209
|
-
|
|
210
|
-
// commande `watch` (identique au default)
|
|
211
|
-
program
|
|
212
|
-
.command('watch')
|
|
213
|
-
.description('Lance la synchronisation (copy & watch)')
|
|
214
|
-
.action(async () => { await watchCommand(); });
|
|
215
|
-
|
|
216
|
-
// si aucun sous‑commande, on fait la watch
|
|
217
|
-
if (process.argv.length <= 2) {
|
|
218
|
-
watchCommand().catch(err => {
|
|
219
|
-
logger.error('Erreur fatale :', err);
|
|
220
|
-
process.exit(1);
|
|
221
|
-
});
|
|
222
|
-
} else {
|
|
223
|
-
program.parseAsync(process.argv);
|
|
224
|
-
}
|
|
176
|
+
// Lancement du script
|
|
177
|
+
main().catch(err => {
|
|
178
|
+
logger.error('Erreur fatale :', err);
|
|
179
|
+
process.exit(1);
|
|
180
|
+
});
|