create-astratra-app 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -43,6 +43,12 @@ npm create astratra-app@latest my-api -- --template api
43
43
  ```bash
44
44
  cd my-app
45
45
  npm install
46
+ npm run dev
47
+ ```
48
+
49
+ Tu peux aussi lancer les deux serveurs separement :
50
+
51
+ ```bash
46
52
  npm run dev:api
47
53
  npm run dev:web
48
54
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-astratra-app",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "CLI generator for new Astratra SaaS applications.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -46,6 +46,7 @@ function packageJson(projectName, template) {
46
46
  private: true,
47
47
  type: 'module',
48
48
  scripts: {
49
+ ...(template === 'fullstack' ? { dev: 'node scripts/dev.js' } : {}),
49
50
  'dev:api': 'node api/server.js',
50
51
  ...(template === 'fullstack' ? { 'dev:web': 'vite --host 127.0.0.1' } : {}),
51
52
  start: 'node api/server.js'
@@ -158,6 +159,66 @@ export default defineConfig({
158
159
  `;
159
160
  }
160
161
 
162
+ function devScript() {
163
+ return `import fs from 'node:fs';
164
+ import { spawn } from 'node:child_process';
165
+
166
+ const isWindows = process.platform === 'win32';
167
+ const npmCommand = isWindows ? 'npm.cmd' : 'npm';
168
+ const apiConfigPath = '.astratra/api.json';
169
+ const timeoutMs = 15000;
170
+ const pollMs = 150;
171
+ const startedAt = Date.now();
172
+ const children = [];
173
+ let shuttingDown = false;
174
+
175
+ function run(label, args) {
176
+ const child = spawn(npmCommand, args, {
177
+ stdio: 'inherit',
178
+ env: process.env
179
+ });
180
+ children.push(child);
181
+ child.on('exit', (code, signal) => {
182
+ if (!shuttingDown && code !== 0) {
183
+ console.error(\`\${label} exited with \${signal || code}.\`);
184
+ shutdown(code || 1);
185
+ }
186
+ });
187
+ return child;
188
+ }
189
+
190
+ function shutdown(code = 0) {
191
+ shuttingDown = true;
192
+ for (const child of children) {
193
+ if (!child.killed) child.kill('SIGTERM');
194
+ }
195
+ process.exit(code);
196
+ }
197
+
198
+ function waitForApiConfig() {
199
+ if (fs.existsSync(apiConfigPath)) {
200
+ run('web', ['run', 'dev:web']);
201
+ return;
202
+ }
203
+
204
+ if (Date.now() - startedAt > timeoutMs) {
205
+ console.error(\`Timed out waiting for \${apiConfigPath}. Is dev:api still running?\`);
206
+ shutdown(1);
207
+ return;
208
+ }
209
+
210
+ setTimeout(waitForApiConfig, pollMs);
211
+ }
212
+
213
+ process.on('SIGINT', () => shutdown(0));
214
+ process.on('SIGTERM', () => shutdown(0));
215
+
216
+ fs.rmSync(apiConfigPath, { force: true });
217
+ run('api', ['run', 'dev:api']);
218
+ waitForApiConfig();
219
+ `;
220
+ }
221
+
161
222
  function webIndex() {
162
223
  return `<!doctype html>
163
224
  <html lang="fr">
@@ -193,9 +254,16 @@ createRoot(document.getElementById('root')).render(
193
254
  function readme(projectName, template) {
194
255
  const webSteps = template === 'fullstack'
195
256
  ? `
196
- Dans un deuxieme terminal :
257
+ Pour lancer l'API et le frontend ensemble :
258
+
259
+ \`\`\`bash
260
+ npm run dev
261
+ \`\`\`
262
+
263
+ Tu peux aussi les lancer separement :
197
264
 
198
265
  \`\`\`bash
266
+ npm run dev:api
199
267
  npm run dev:web
200
268
  \`\`\`
201
269
  `
@@ -278,6 +346,7 @@ export function createProject({ targetDir, template = 'fullstack', force = false
278
346
  writeFile(absoluteTarget, 'README.md', readme(projectName, template));
279
347
 
280
348
  if (template === 'fullstack') {
349
+ writeFile(absoluteTarget, 'scripts/dev.js', devScript());
281
350
  writeFile(absoluteTarget, 'vite.config.js', viteConfig());
282
351
  writeFile(absoluteTarget, 'web/index.html', webIndex());
283
352
  writeFile(absoluteTarget, 'web/src/main.jsx', webMain());
@@ -288,8 +357,7 @@ export function createProject({ targetDir, template = 'fullstack', force = false
288
357
  nextSteps: [
289
358
  `cd ${targetDir}`,
290
359
  'npm install',
291
- 'npm run dev:api',
292
- ...(template === 'fullstack' ? ['npm run dev:web'] : [])
360
+ template === 'fullstack' ? 'npm run dev' : 'npm run dev:api'
293
361
  ]
294
362
  };
295
363
  }