create-astratra-app 0.1.2 → 0.1.4

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
@@ -55,9 +55,14 @@ npm run dev:web
55
55
 
56
56
  Pour le template `api`, seul `npm run dev:api` est cree.
57
57
 
58
- Si le port `4000` est deja occupe, l'API essaie automatiquement le port
59
- suivant et ecrit l'URL choisie dans `.astratra/api.json`. Lance `dev:api`
60
- avant `dev:web` pour que Vite lise la bonne URL.
58
+ Par defaut, l'API demande un port libre au systeme et ecrit l'URL choisie dans
59
+ `.astratra/api.json`. Lance `dev:api` avant `dev:web` pour que Vite lise la
60
+ bonne URL. Pour forcer un port precis, utilise par exemple
61
+ `PORT=4000 npm run dev:api`.
62
+
63
+ En developpement, l'API accepte automatiquement les origins
64
+ `localhost`/`127.0.0.1`, quel que soit le port choisi par Vite. En production,
65
+ definis explicitement `CORS_ORIGIN`.
61
66
 
62
67
  Avant la production, il faut remplacer `JWT_SECRET`, brancher de vrais stores
63
68
  et utiliser une vraie verification de mot de passe.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-astratra-app",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "CLI generator for new Astratra SaaS applications.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -63,17 +63,27 @@ import path from 'node:path';
63
63
  import saasKit from '@astratra/saas-kit';
64
64
 
65
65
  const { createSaasApp, createMemorySettingsStore } = saasKit;
66
- const preferredPort = Number(process.env.PORT || 4000);
66
+ const preferredPort = process.env.PORT ? Number(process.env.PORT) : 0;
67
67
  const host = process.env.HOST || '127.0.0.1';
68
- const allowedOrigins = new Set((process.env.CORS_ORIGIN || 'http://127.0.0.1:5173,http://localhost:5173')
68
+ const allowedOrigins = new Set((process.env.CORS_ORIGIN || '')
69
69
  .split(',')
70
70
  .map((origin) => origin.trim())
71
71
  .filter(Boolean));
72
72
 
73
+ function isAllowedDevOrigin(origin) {
74
+ if (process.env.NODE_ENV === 'production') return false;
75
+ try {
76
+ const url = new URL(origin);
77
+ return ['127.0.0.1', 'localhost'].includes(url.hostname);
78
+ } catch {
79
+ return false;
80
+ }
81
+ }
82
+
73
83
  const app = express();
74
84
  app.use((req, res, next) => {
75
85
  const origin = req.headers.origin;
76
- if (origin && allowedOrigins.has(origin)) {
86
+ if (origin && (allowedOrigins.has(origin) || isAllowedDevOrigin(origin))) {
77
87
  res.setHeader('Access-Control-Allow-Origin', origin);
78
88
  res.setHeader('Vary', 'Origin');
79
89
  }
@@ -107,8 +117,10 @@ function writeApiRuntimeConfig(port) {
107
117
 
108
118
  function listen(port, attemptsLeft = 20) {
109
119
  const server = app.listen(port, host, () => {
110
- writeApiRuntimeConfig(port);
111
- console.log(\`Astratra API listening on http://\${host}:\${port}\`);
120
+ const address = server.address();
121
+ const selectedPort = typeof address === 'object' && address ? address.port : port;
122
+ writeApiRuntimeConfig(selectedPort);
123
+ console.log(\`Astratra API listening on http://\${host}:\${selectedPort}\`);
112
124
  console.log('Seeded owner: owner@example.test / password');
113
125
  console.log('Seeded member: member@example.test / password');
114
126
  });
@@ -152,8 +164,7 @@ export default defineConfig({
152
164
  root: 'web',
153
165
  plugins: [react()],
154
166
  server: {
155
- host: '127.0.0.1',
156
- port: 5173
167
+ host: '127.0.0.1'
157
168
  }
158
169
  });
159
170
  `;
@@ -161,6 +172,7 @@ export default defineConfig({
161
172
 
162
173
  function devScript() {
163
174
  return `import fs from 'node:fs';
175
+ import net from 'node:net';
164
176
  import { spawn } from 'node:child_process';
165
177
 
166
178
  const isWindows = process.platform === 'win32';
@@ -187,6 +199,19 @@ function run(label, args) {
187
199
  return child;
188
200
  }
189
201
 
202
+ function findFreePort(host = '127.0.0.1') {
203
+ return new Promise((resolve, reject) => {
204
+ const server = net.createServer();
205
+ server.unref();
206
+ server.on('error', reject);
207
+ server.listen(0, host, () => {
208
+ const address = server.address();
209
+ const port = typeof address === 'object' && address ? address.port : 0;
210
+ server.close(() => resolve(port));
211
+ });
212
+ });
213
+ }
214
+
190
215
  function shutdown(code = 0) {
191
216
  shuttingDown = true;
192
217
  for (const child of children) {
@@ -195,9 +220,10 @@ function shutdown(code = 0) {
195
220
  process.exit(code);
196
221
  }
197
222
 
198
- function waitForApiConfig() {
223
+ async function waitForApiConfig() {
199
224
  if (fs.existsSync(apiConfigPath)) {
200
- run('web', ['run', 'dev:web']);
225
+ const webPort = await findFreePort();
226
+ run('web', ['run', 'dev:web', '--', '--port', String(webPort)]);
201
227
  return;
202
228
  }
203
229
 
@@ -207,7 +233,12 @@ function waitForApiConfig() {
207
233
  return;
208
234
  }
209
235
 
210
- setTimeout(waitForApiConfig, pollMs);
236
+ setTimeout(() => {
237
+ waitForApiConfig().catch((error) => {
238
+ console.error(error.message);
239
+ shutdown(1);
240
+ });
241
+ }, pollMs);
211
242
  }
212
243
 
213
244
  process.on('SIGINT', () => shutdown(0));
@@ -215,7 +246,10 @@ process.on('SIGTERM', () => shutdown(0));
215
246
 
216
247
  fs.rmSync(apiConfigPath, { force: true });
217
248
  run('api', ['run', 'dev:api']);
218
- waitForApiConfig();
249
+ waitForApiConfig().catch((error) => {
250
+ console.error(error.message);
251
+ shutdown(1);
252
+ });
219
253
  `;
220
254
  }
221
255
 
@@ -285,9 +319,10 @@ npm install
285
319
  npm run dev:api
286
320
  \`\`\`
287
321
  ${webSteps}
288
- Si le port \`4000\` est deja occupe, l'API essaie automatiquement le port
289
- suivant et ecrit l'URL choisie dans \`.astratra/api.json\`. Lance \`dev:api\`
290
- avant \`dev:web\` pour que Vite lise la bonne URL.
322
+ Par defaut, l'API demande un port libre au systeme et ecrit l'URL choisie
323
+ dans \`.astratra/api.json\`. Lance \`dev:api\` avant \`dev:web\` pour que Vite
324
+ lise la bonne URL. Pour forcer un port precis, utilise par exemple
325
+ \`PORT=4000 npm run dev:api\`.
291
326
 
292
327
  Comptes de test :
293
328
 
@@ -342,7 +377,7 @@ export function createProject({ targetDir, template = 'fullstack', force = false
342
377
  writeFile(absoluteTarget, 'package.json', `${packageJson(projectName, template)}\n`);
343
378
  writeFile(absoluteTarget, 'api/server.js', apiServer());
344
379
  writeFile(absoluteTarget, '.gitignore', 'node_modules\n.env\n.astratra\n');
345
- writeFile(absoluteTarget, '.env.example', 'PORT=4000\nHOST=127.0.0.1\nJWT_SECRET=change-me\nCORS_ORIGIN=http://127.0.0.1:5173,http://localhost:5173\n');
380
+ writeFile(absoluteTarget, '.env.example', 'PORT=\nHOST=127.0.0.1\nJWT_SECRET=change-me\nCORS_ORIGIN=\n');
346
381
  writeFile(absoluteTarget, 'README.md', readme(projectName, template));
347
382
 
348
383
  if (template === 'fullstack') {