create-fleetbo-project 1.0.35 → 1.0.37

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.
@@ -4,25 +4,13 @@ const { execSync } = require('child_process');
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
6
  const https = require('https');
7
- const unzipper = require('unzipper');
8
-
9
- // --- Vérification de la version de Node.js ---
10
- const [major, minor] = process.versions.node.split('.').map(parseFloat);
11
- if (major < 16 || (major === 16 && minor < 7)) {
12
- console.error(`\n❌ Erreur : Ce script requiert Node.js v16.7.0 ou une version supérieure pour fonctionner correctement.`);
13
- console.error(` Votre version actuelle est ${process.versions.node}.`);
14
- console.log(` Veuillez mettre à jour votre installation de Node.js.`);
15
- process.exit(1);
16
- }
17
7
 
18
8
  // --- Configuration ---
19
- const repoOwner = 'FleetFleetbo'; // Assurez-vous que c'est le bon nom d'utilisateur GitHub
20
- const repoName = 'dev.fleetbo.io'; // Assurez-vous que c'est le bon nom de dépôt
21
- const branchName = 'master'; // Le nom de la branche est maintenant configurable
9
+ const repoOwner = 'FleetFleetbo';
10
+ const repoName = 'dev.fleetbo.io';
11
+ const branchName = 'master'; // Assurez-vous que c'est la bonne branche
22
12
 
23
- const repoUrl = `https://github.com/${repoOwner}/${repoName}/archive/refs/heads/${branchName}.zip`;
24
-
25
- // URL de votre Cloud Function.
13
+ const repoGitUrl = `https://github.com/${repoOwner}/${repoName}.git`;
26
14
  const bootstrapUrl = 'https://us-central1-myapp-259bf.cloudfunctions.net/bootstrapProject';
27
15
 
28
16
  // --- Analyse des Arguments ---
@@ -46,9 +34,8 @@ if (!bootstrapToken) {
46
34
 
47
35
  const projectName = projectNameArg;
48
36
 
49
- /**
50
- * Fonction pour appeler la Cloud Function et récupérer les clés.
51
- */
37
+ // --- Fonctions Utilitaires ---
38
+
52
39
  function fetchProjectKeys(token) {
53
40
  return new Promise((resolve, reject) => {
54
41
  const postData = JSON.stringify({ token });
@@ -75,91 +62,41 @@ function fetchProjectKeys(token) {
75
62
  });
76
63
  }
77
64
 
78
- /**
79
- * Fonction de copie récursive robuste pour garantir que tous les fichiers sont copiés.
80
- * @param {string} src Le chemin source.
81
- * @param {string} dest Le chemin de destination.
82
- */
83
- function copyRecursiveSync(src, dest) {
84
- const exists = fs.existsSync(src);
85
- const stats = exists && fs.statSync(src);
86
- const isDirectory = exists && stats.isDirectory();
87
- if (isDirectory) {
88
- fs.mkdirSync(dest, { recursive: true });
89
- fs.readdirSync(src).forEach(childItemName => {
90
- copyRecursiveSync(
91
- path.join(src, childItemName),
92
- path.join(dest, childItemName)
93
- );
94
- });
95
- } else {
96
- fs.copyFileSync(src, dest);
97
- }
98
- }
65
+ // --- Fonction Principale ---
99
66
 
100
- /**
101
- * Fonction Principale Asynchrone (Version pro avec téléchargement direct et gestion de fichiers robuste)
102
- */
103
67
  async function setupProject() {
104
68
  console.log(`\nCréation de votre projet Fleetbo "${projectName}"...`);
105
69
  const projectDir = path.join(process.cwd(), projectName);
106
- const tempDir = path.join(process.cwd(), `fleetbo-temp-${Date.now()}`);
107
70
 
108
71
  try {
109
- // Étape 1 : Téléchargement et décompression
110
- console.log(` [1/5] 📥 Téléchargement du template...`);
111
- await new Promise((resolve, reject) => {
112
- https.get(repoUrl, (response) => {
113
- if (response.statusCode === 301 || response.statusCode === 302) {
114
- return https.get(response.headers.location, (redirectResponse) => {
115
- redirectResponse.pipe(unzipper.Extract({ path: tempDir }))
116
- .on('finish', resolve)
117
- .on('error', (err) => reject(new Error(`Erreur de décompression: ${err.message}`)));
118
- }).on('error', (err) => reject(new Error(`Erreur réseau après redirection: ${err.message}`)));
119
- }
120
- if (response.statusCode !== 200) {
121
- return reject(new Error(`Échec du téléchargement. Statut: ${response.statusCode}`));
122
- }
123
- response.pipe(unzipper.Extract({ path: tempDir }))
124
- .on('finish', resolve)
125
- .on('error', (err) => reject(new Error(`Erreur de décompression: ${err.message}`)));
126
- }).on('error', (err) => reject(new Error(`Erreur réseau: ${err.message}`)));
127
- });
128
- console.log(' [2/5] ✅ Template téléchargé. Organisation des fichiers...');
129
-
130
- // Étape 2 bis : Localiser la racine du projet dans le dossier décompressé
131
- const unzippedBaseFolder = fs.readdirSync(tempDir)[0];
132
- let sourceDir = path.join(tempDir, unzippedBaseFolder);
133
-
134
- if (!fs.existsSync(path.join(sourceDir, 'package.json'))) {
135
- const nestedProjectDir = fs.readdirSync(sourceDir).find(file => {
136
- const fullPath = path.join(sourceDir, file);
137
- return fs.statSync(fullPath).isDirectory() && fs.existsSync(path.join(fullPath, 'package.json'));
138
- });
139
- if (!nestedProjectDir) throw new Error('Impossible de trouver le fichier package.json dans le template téléchargé.');
140
- sourceDir = path.join(sourceDir, nestedProjectDir);
141
- }
142
-
143
- // Étape 2 ter : Copier les fichiers vers la destination finale
144
- fs.mkdirSync(projectDir, { recursive: true });
145
- for (const file of fs.readdirSync(sourceDir)) {
146
- copyRecursiveSync(path.join(sourceDir, file), path.join(projectDir, file));
147
- }
148
-
72
+ // Étape 1 : Cloner le dépôt avec Git (plus robuste que le zip)
73
+ console.log(' [1/5] 📥 Clonage du template via Git...');
74
+ execSync(`git clone --depth 1 --branch ${branchName} ${repoGitUrl} "${projectName}"`, { stdio: 'inherit' });
75
+
76
+ // Étape 2 : Se déplacer dans le dossier du projet et nettoyer
77
+ console.log(' [2/5] Template cloné. Configuration du projet...');
149
78
  process.chdir(projectDir);
79
+
80
+ // Supprimer le dossier .git pour commencer avec un projet propre
81
+ fs.rmSync(path.join(projectDir, '.git'), { recursive: true, force: true });
82
+
83
+ // Étape 3 : Récupération des clés de projet
150
84
  console.log(' [3/5] 🔑 Récupération des clés de projet...');
151
85
  const keys = await fetchProjectKeys(bootstrapToken);
152
86
  if (!keys.enterpriseId || !keys.fleetboDBKey) {
153
87
  throw new Error("Les clés reçues du serveur sont invalides.");
154
88
  }
155
89
 
90
+ // Étape 4 : Configuration du fichier .env
156
91
  console.log(' [4/5] ✅ Fichier .env configuré avec succès.');
157
92
  const envContent = `REACT_APP_FLEETBO_DB_KEY=${keys.fleetboDBKey}\nREACT_APP_ENTERPRISE_ID=${keys.enterpriseId}\n`;
158
93
  fs.writeFileSync(path.join(projectDir, '.env'), envContent, 'utf8');
159
94
 
95
+ // Étape 5 : Installation des dépendances
160
96
  console.log(' [5/5] 📦 Installation des dépendances...');
161
97
  execSync('npm install', { stdio: 'inherit' });
162
98
 
99
+ // Personnalisation du package.json
163
100
  const packageJsonPath = path.join(projectDir, 'package.json');
164
101
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
165
102
  packageJson.name = projectName;
@@ -172,13 +109,10 @@ async function setupProject() {
172
109
 
173
110
  } catch (error) {
174
111
  console.error('\n❌ Une erreur est survenue lors de la création du projet :', error.message);
112
+ // Nettoyage en cas d'erreur
175
113
  if (fs.existsSync(projectDir)) {
176
114
  fs.rmSync(projectDir, { recursive: true, force: true });
177
115
  }
178
- } finally {
179
- if (fs.existsSync(tempDir)) {
180
- fs.rmSync(tempDir, { recursive: true, force: true });
181
- }
182
116
  }
183
117
  }
184
118
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-fleetbo-project",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "description": "Creates a new Fleetbo project.",
5
5
  "main": "install-react-template.js",
6
6
  "bin": {