create-astratra-app 0.1.0 → 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 +10 -0
- package/package.json +1 -1
- package/src/createProject.js +124 -10
package/README.md
CHANGED
|
@@ -43,11 +43,21 @@ 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
|
|
|
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.
|
|
61
|
+
|
|
52
62
|
Avant la production, il faut remplacer `JWT_SECRET`, brancher de vrais stores
|
|
53
63
|
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'
|
|
@@ -57,10 +58,12 @@ function packageJson(projectName, template) {
|
|
|
57
58
|
|
|
58
59
|
function apiServer() {
|
|
59
60
|
return `import express from 'express';
|
|
61
|
+
import fs from 'node:fs';
|
|
62
|
+
import path from 'node:path';
|
|
60
63
|
import saasKit from '@astratra/saas-kit';
|
|
61
64
|
|
|
62
65
|
const { createSaasApp, createMemorySettingsStore } = saasKit;
|
|
63
|
-
const
|
|
66
|
+
const preferredPort = Number(process.env.PORT || 4000);
|
|
64
67
|
const host = process.env.HOST || '127.0.0.1';
|
|
65
68
|
const allowedOrigins = new Set((process.env.CORS_ORIGIN || 'http://127.0.0.1:5173,http://localhost:5173')
|
|
66
69
|
.split(',')
|
|
@@ -94,18 +97,57 @@ app.use(createSaasApp({
|
|
|
94
97
|
notify: async (message) => ({ delivered: true, message })
|
|
95
98
|
}));
|
|
96
99
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
100
|
+
function writeApiRuntimeConfig(port) {
|
|
101
|
+
const configDir = path.resolve('.astratra');
|
|
102
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
103
|
+
fs.writeFileSync(path.join(configDir, 'api.json'), JSON.stringify({
|
|
104
|
+
apiUrl: \`http://\${host}:\${port}\`
|
|
105
|
+
}, null, 2));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function listen(port, attemptsLeft = 20) {
|
|
109
|
+
const server = app.listen(port, host, () => {
|
|
110
|
+
writeApiRuntimeConfig(port);
|
|
111
|
+
console.log(\`Astratra API listening on http://\${host}:\${port}\`);
|
|
112
|
+
console.log('Seeded owner: owner@example.test / password');
|
|
113
|
+
console.log('Seeded member: member@example.test / password');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
server.on('error', (error) => {
|
|
117
|
+
if (error.code === 'EADDRINUSE' && attemptsLeft > 0 && !process.env.PORT) {
|
|
118
|
+
const nextPort = port + 1;
|
|
119
|
+
console.warn(\`Port \${port} is already in use, trying \${nextPort}...\`);
|
|
120
|
+
listen(nextPort, attemptsLeft - 1);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
console.error(\`Unable to start Astratra API on http://\${host}:\${port}: \${error.message}\`);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
listen(preferredPort);
|
|
102
130
|
`;
|
|
103
131
|
}
|
|
104
132
|
|
|
105
133
|
function viteConfig() {
|
|
106
|
-
return `import
|
|
134
|
+
return `import fs from 'node:fs';
|
|
135
|
+
import path from 'node:path';
|
|
136
|
+
import react from '@vitejs/plugin-react';
|
|
107
137
|
import { defineConfig } from 'vite';
|
|
108
138
|
|
|
139
|
+
function readApiUrl() {
|
|
140
|
+
try {
|
|
141
|
+
const configPath = path.resolve('.astratra/api.json');
|
|
142
|
+
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
143
|
+
return config.apiUrl || 'http://127.0.0.1:4000';
|
|
144
|
+
} catch {
|
|
145
|
+
return 'http://127.0.0.1:4000';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
process.env.VITE_API_URL = process.env.VITE_API_URL || readApiUrl();
|
|
150
|
+
|
|
109
151
|
export default defineConfig({
|
|
110
152
|
root: 'web',
|
|
111
153
|
plugins: [react()],
|
|
@@ -117,6 +159,66 @@ export default defineConfig({
|
|
|
117
159
|
`;
|
|
118
160
|
}
|
|
119
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
|
+
|
|
120
222
|
function webIndex() {
|
|
121
223
|
return `<!doctype html>
|
|
122
224
|
<html lang="fr">
|
|
@@ -152,9 +254,16 @@ createRoot(document.getElementById('root')).render(
|
|
|
152
254
|
function readme(projectName, template) {
|
|
153
255
|
const webSteps = template === 'fullstack'
|
|
154
256
|
? `
|
|
155
|
-
|
|
257
|
+
Pour lancer l'API et le frontend ensemble :
|
|
156
258
|
|
|
157
259
|
\`\`\`bash
|
|
260
|
+
npm run dev
|
|
261
|
+
\`\`\`
|
|
262
|
+
|
|
263
|
+
Tu peux aussi les lancer separement :
|
|
264
|
+
|
|
265
|
+
\`\`\`bash
|
|
266
|
+
npm run dev:api
|
|
158
267
|
npm run dev:web
|
|
159
268
|
\`\`\`
|
|
160
269
|
`
|
|
@@ -176,6 +285,10 @@ npm install
|
|
|
176
285
|
npm run dev:api
|
|
177
286
|
\`\`\`
|
|
178
287
|
${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.
|
|
291
|
+
|
|
179
292
|
Comptes de test :
|
|
180
293
|
|
|
181
294
|
- \`owner@example.test\` / \`password\`
|
|
@@ -228,10 +341,12 @@ export function createProject({ targetDir, template = 'fullstack', force = false
|
|
|
228
341
|
|
|
229
342
|
writeFile(absoluteTarget, 'package.json', `${packageJson(projectName, template)}\n`);
|
|
230
343
|
writeFile(absoluteTarget, 'api/server.js', apiServer());
|
|
344
|
+
writeFile(absoluteTarget, '.gitignore', 'node_modules\n.env\n.astratra\n');
|
|
231
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');
|
|
232
346
|
writeFile(absoluteTarget, 'README.md', readme(projectName, template));
|
|
233
347
|
|
|
234
348
|
if (template === 'fullstack') {
|
|
349
|
+
writeFile(absoluteTarget, 'scripts/dev.js', devScript());
|
|
235
350
|
writeFile(absoluteTarget, 'vite.config.js', viteConfig());
|
|
236
351
|
writeFile(absoluteTarget, 'web/index.html', webIndex());
|
|
237
352
|
writeFile(absoluteTarget, 'web/src/main.jsx', webMain());
|
|
@@ -242,8 +357,7 @@ export function createProject({ targetDir, template = 'fullstack', force = false
|
|
|
242
357
|
nextSteps: [
|
|
243
358
|
`cd ${targetDir}`,
|
|
244
359
|
'npm install',
|
|
245
|
-
'npm run dev:api'
|
|
246
|
-
...(template === 'fullstack' ? ['npm run dev:web'] : [])
|
|
360
|
+
template === 'fullstack' ? 'npm run dev' : 'npm run dev:api'
|
|
247
361
|
]
|
|
248
362
|
};
|
|
249
363
|
}
|