create-astratra-app 0.1.0 → 0.1.1

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
@@ -49,5 +49,9 @@ npm run dev:web
49
49
 
50
50
  Pour le template `api`, seul `npm run dev:api` est cree.
51
51
 
52
+ Si le port `4000` est deja occupe, l'API essaie automatiquement le port
53
+ suivant et ecrit l'URL choisie dans `.astratra/api.json`. Lance `dev:api`
54
+ avant `dev:web` pour que Vite lise la bonne URL.
55
+
52
56
  Avant la production, il faut remplacer `JWT_SECRET`, brancher de vrais stores
53
57
  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.0",
3
+ "version": "0.1.1",
4
4
  "description": "CLI generator for new Astratra SaaS applications.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -57,10 +57,12 @@ function packageJson(projectName, template) {
57
57
 
58
58
  function apiServer() {
59
59
  return `import express from 'express';
60
+ import fs from 'node:fs';
61
+ import path from 'node:path';
60
62
  import saasKit from '@astratra/saas-kit';
61
63
 
62
64
  const { createSaasApp, createMemorySettingsStore } = saasKit;
63
- const port = Number(process.env.PORT || 4000);
65
+ const preferredPort = Number(process.env.PORT || 4000);
64
66
  const host = process.env.HOST || '127.0.0.1';
65
67
  const allowedOrigins = new Set((process.env.CORS_ORIGIN || 'http://127.0.0.1:5173,http://localhost:5173')
66
68
  .split(',')
@@ -94,18 +96,57 @@ app.use(createSaasApp({
94
96
  notify: async (message) => ({ delivered: true, message })
95
97
  }));
96
98
 
97
- app.listen(port, host, () => {
98
- console.log(\`Astratra API listening on http://\${host}:\${port}\`);
99
- console.log('Seeded owner: owner@example.test / password');
100
- console.log('Seeded member: member@example.test / password');
101
- });
99
+ function writeApiRuntimeConfig(port) {
100
+ const configDir = path.resolve('.astratra');
101
+ fs.mkdirSync(configDir, { recursive: true });
102
+ fs.writeFileSync(path.join(configDir, 'api.json'), JSON.stringify({
103
+ apiUrl: \`http://\${host}:\${port}\`
104
+ }, null, 2));
105
+ }
106
+
107
+ function listen(port, attemptsLeft = 20) {
108
+ const server = app.listen(port, host, () => {
109
+ writeApiRuntimeConfig(port);
110
+ console.log(\`Astratra API listening on http://\${host}:\${port}\`);
111
+ console.log('Seeded owner: owner@example.test / password');
112
+ console.log('Seeded member: member@example.test / password');
113
+ });
114
+
115
+ server.on('error', (error) => {
116
+ if (error.code === 'EADDRINUSE' && attemptsLeft > 0 && !process.env.PORT) {
117
+ const nextPort = port + 1;
118
+ console.warn(\`Port \${port} is already in use, trying \${nextPort}...\`);
119
+ listen(nextPort, attemptsLeft - 1);
120
+ return;
121
+ }
122
+
123
+ console.error(\`Unable to start Astratra API on http://\${host}:\${port}: \${error.message}\`);
124
+ process.exit(1);
125
+ });
126
+ }
127
+
128
+ listen(preferredPort);
102
129
  `;
103
130
  }
104
131
 
105
132
  function viteConfig() {
106
- return `import react from '@vitejs/plugin-react';
133
+ return `import fs from 'node:fs';
134
+ import path from 'node:path';
135
+ import react from '@vitejs/plugin-react';
107
136
  import { defineConfig } from 'vite';
108
137
 
138
+ function readApiUrl() {
139
+ try {
140
+ const configPath = path.resolve('.astratra/api.json');
141
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
142
+ return config.apiUrl || 'http://127.0.0.1:4000';
143
+ } catch {
144
+ return 'http://127.0.0.1:4000';
145
+ }
146
+ }
147
+
148
+ process.env.VITE_API_URL = process.env.VITE_API_URL || readApiUrl();
149
+
109
150
  export default defineConfig({
110
151
  root: 'web',
111
152
  plugins: [react()],
@@ -176,6 +217,10 @@ npm install
176
217
  npm run dev:api
177
218
  \`\`\`
178
219
  ${webSteps}
220
+ Si le port \`4000\` est deja occupe, l'API essaie automatiquement le port
221
+ suivant et ecrit l'URL choisie dans \`.astratra/api.json\`. Lance \`dev:api\`
222
+ avant \`dev:web\` pour que Vite lise la bonne URL.
223
+
179
224
  Comptes de test :
180
225
 
181
226
  - \`owner@example.test\` / \`password\`
@@ -228,6 +273,7 @@ export function createProject({ targetDir, template = 'fullstack', force = false
228
273
 
229
274
  writeFile(absoluteTarget, 'package.json', `${packageJson(projectName, template)}\n`);
230
275
  writeFile(absoluteTarget, 'api/server.js', apiServer());
276
+ writeFile(absoluteTarget, '.gitignore', 'node_modules\n.env\n.astratra\n');
231
277
  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');
232
278
  writeFile(absoluteTarget, 'README.md', readme(projectName, template));
233
279