create-astratra-app 0.1.1 → 0.1.3
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 +14 -3
- package/package.json +1 -1
- package/src/createProject.js +93 -12
package/README.md
CHANGED
|
@@ -43,15 +43,26 @@ 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
|
```
|
|
49
55
|
|
|
50
56
|
Pour le template `api`, seul `npm run dev:api` est cree.
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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`, meme si Vite choisit un port comme `5174` ou `5175`.
|
|
65
|
+
En production, definis explicitement `CORS_ORIGIN`.
|
|
55
66
|
|
|
56
67
|
Avant la production, il faut remplacer `JWT_SECRET`, brancher de vrais stores
|
|
57
68
|
et utiliser une vraie verification de mot de passe.
|
package/package.json
CHANGED
package/src/createProject.js
CHANGED
|
@@ -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'
|
|
@@ -62,17 +63,27 @@ import path from 'node:path';
|
|
|
62
63
|
import saasKit from '@astratra/saas-kit';
|
|
63
64
|
|
|
64
65
|
const { createSaasApp, createMemorySettingsStore } = saasKit;
|
|
65
|
-
const preferredPort = Number(process.env.PORT
|
|
66
|
+
const preferredPort = process.env.PORT ? Number(process.env.PORT) : 0;
|
|
66
67
|
const host = process.env.HOST || '127.0.0.1';
|
|
67
|
-
const allowedOrigins = new Set((process.env.CORS_ORIGIN || '
|
|
68
|
+
const allowedOrigins = new Set((process.env.CORS_ORIGIN || '')
|
|
68
69
|
.split(',')
|
|
69
70
|
.map((origin) => origin.trim())
|
|
70
71
|
.filter(Boolean));
|
|
71
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
|
+
|
|
72
83
|
const app = express();
|
|
73
84
|
app.use((req, res, next) => {
|
|
74
85
|
const origin = req.headers.origin;
|
|
75
|
-
if (origin && allowedOrigins.has(origin)) {
|
|
86
|
+
if (origin && (allowedOrigins.has(origin) || isAllowedDevOrigin(origin))) {
|
|
76
87
|
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
77
88
|
res.setHeader('Vary', 'Origin');
|
|
78
89
|
}
|
|
@@ -106,8 +117,10 @@ function writeApiRuntimeConfig(port) {
|
|
|
106
117
|
|
|
107
118
|
function listen(port, attemptsLeft = 20) {
|
|
108
119
|
const server = app.listen(port, host, () => {
|
|
109
|
-
|
|
110
|
-
|
|
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}\`);
|
|
111
124
|
console.log('Seeded owner: owner@example.test / password');
|
|
112
125
|
console.log('Seeded member: member@example.test / password');
|
|
113
126
|
});
|
|
@@ -158,6 +171,66 @@ export default defineConfig({
|
|
|
158
171
|
`;
|
|
159
172
|
}
|
|
160
173
|
|
|
174
|
+
function devScript() {
|
|
175
|
+
return `import fs from 'node:fs';
|
|
176
|
+
import { spawn } from 'node:child_process';
|
|
177
|
+
|
|
178
|
+
const isWindows = process.platform === 'win32';
|
|
179
|
+
const npmCommand = isWindows ? 'npm.cmd' : 'npm';
|
|
180
|
+
const apiConfigPath = '.astratra/api.json';
|
|
181
|
+
const timeoutMs = 15000;
|
|
182
|
+
const pollMs = 150;
|
|
183
|
+
const startedAt = Date.now();
|
|
184
|
+
const children = [];
|
|
185
|
+
let shuttingDown = false;
|
|
186
|
+
|
|
187
|
+
function run(label, args) {
|
|
188
|
+
const child = spawn(npmCommand, args, {
|
|
189
|
+
stdio: 'inherit',
|
|
190
|
+
env: process.env
|
|
191
|
+
});
|
|
192
|
+
children.push(child);
|
|
193
|
+
child.on('exit', (code, signal) => {
|
|
194
|
+
if (!shuttingDown && code !== 0) {
|
|
195
|
+
console.error(\`\${label} exited with \${signal || code}.\`);
|
|
196
|
+
shutdown(code || 1);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
return child;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function shutdown(code = 0) {
|
|
203
|
+
shuttingDown = true;
|
|
204
|
+
for (const child of children) {
|
|
205
|
+
if (!child.killed) child.kill('SIGTERM');
|
|
206
|
+
}
|
|
207
|
+
process.exit(code);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function waitForApiConfig() {
|
|
211
|
+
if (fs.existsSync(apiConfigPath)) {
|
|
212
|
+
run('web', ['run', 'dev:web']);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
217
|
+
console.error(\`Timed out waiting for \${apiConfigPath}. Is dev:api still running?\`);
|
|
218
|
+
shutdown(1);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
setTimeout(waitForApiConfig, pollMs);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
process.on('SIGINT', () => shutdown(0));
|
|
226
|
+
process.on('SIGTERM', () => shutdown(0));
|
|
227
|
+
|
|
228
|
+
fs.rmSync(apiConfigPath, { force: true });
|
|
229
|
+
run('api', ['run', 'dev:api']);
|
|
230
|
+
waitForApiConfig();
|
|
231
|
+
`;
|
|
232
|
+
}
|
|
233
|
+
|
|
161
234
|
function webIndex() {
|
|
162
235
|
return `<!doctype html>
|
|
163
236
|
<html lang="fr">
|
|
@@ -193,9 +266,16 @@ createRoot(document.getElementById('root')).render(
|
|
|
193
266
|
function readme(projectName, template) {
|
|
194
267
|
const webSteps = template === 'fullstack'
|
|
195
268
|
? `
|
|
196
|
-
|
|
269
|
+
Pour lancer l'API et le frontend ensemble :
|
|
197
270
|
|
|
198
271
|
\`\`\`bash
|
|
272
|
+
npm run dev
|
|
273
|
+
\`\`\`
|
|
274
|
+
|
|
275
|
+
Tu peux aussi les lancer separement :
|
|
276
|
+
|
|
277
|
+
\`\`\`bash
|
|
278
|
+
npm run dev:api
|
|
199
279
|
npm run dev:web
|
|
200
280
|
\`\`\`
|
|
201
281
|
`
|
|
@@ -217,9 +297,10 @@ npm install
|
|
|
217
297
|
npm run dev:api
|
|
218
298
|
\`\`\`
|
|
219
299
|
${webSteps}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
300
|
+
Par defaut, l'API demande un port libre au systeme et ecrit l'URL choisie
|
|
301
|
+
dans \`.astratra/api.json\`. Lance \`dev:api\` avant \`dev:web\` pour que Vite
|
|
302
|
+
lise la bonne URL. Pour forcer un port precis, utilise par exemple
|
|
303
|
+
\`PORT=4000 npm run dev:api\`.
|
|
223
304
|
|
|
224
305
|
Comptes de test :
|
|
225
306
|
|
|
@@ -274,10 +355,11 @@ export function createProject({ targetDir, template = 'fullstack', force = false
|
|
|
274
355
|
writeFile(absoluteTarget, 'package.json', `${packageJson(projectName, template)}\n`);
|
|
275
356
|
writeFile(absoluteTarget, 'api/server.js', apiServer());
|
|
276
357
|
writeFile(absoluteTarget, '.gitignore', 'node_modules\n.env\n.astratra\n');
|
|
277
|
-
writeFile(absoluteTarget, '.env.example', 'PORT
|
|
358
|
+
writeFile(absoluteTarget, '.env.example', 'PORT=\nHOST=127.0.0.1\nJWT_SECRET=change-me\nCORS_ORIGIN=\n');
|
|
278
359
|
writeFile(absoluteTarget, 'README.md', readme(projectName, template));
|
|
279
360
|
|
|
280
361
|
if (template === 'fullstack') {
|
|
362
|
+
writeFile(absoluteTarget, 'scripts/dev.js', devScript());
|
|
281
363
|
writeFile(absoluteTarget, 'vite.config.js', viteConfig());
|
|
282
364
|
writeFile(absoluteTarget, 'web/index.html', webIndex());
|
|
283
365
|
writeFile(absoluteTarget, 'web/src/main.jsx', webMain());
|
|
@@ -288,8 +370,7 @@ export function createProject({ targetDir, template = 'fullstack', force = false
|
|
|
288
370
|
nextSteps: [
|
|
289
371
|
`cd ${targetDir}`,
|
|
290
372
|
'npm install',
|
|
291
|
-
'npm run dev:api'
|
|
292
|
-
...(template === 'fullstack' ? ['npm run dev:web'] : [])
|
|
373
|
+
template === 'fullstack' ? 'npm run dev' : 'npm run dev:api'
|
|
293
374
|
]
|
|
294
375
|
};
|
|
295
376
|
}
|