poad 2.38.0 → 2.40.0
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/global/cli.js +2 -1
- package/global/create.js +513 -0
- package/global/init.js +17 -5
- package/global/run.js +17 -10
- package/package.json +4 -2
- package/poad.js +26 -1
- package/project/cli.js +3 -3
- package/project/client/index.js +2 -2
- package/project/client/init.js +43 -171
- package/project/info.js +22 -2
- package/templates/workspace/start.mjs +170 -0
- package/test/create.e2e.test.js +73 -0
- package/test/create.test.js +340 -0
- package/test/help-stub.js +29 -0
- package/test/help.test.js +124 -0
- package/test/helpers.js +53 -0
- package/test/offline-stub.js +5 -0
- package/test/registry.test.js +95 -0
- package/test/secrets.test.js +94 -0
- package/test/starter.test.js +122 -0
- package/test/tar.test.js +101 -0
- package/test/workspace.test.js +71 -0
- package/utils/engine.js +10 -5
- package/utils/git.js +1 -1
- package/utils/help.js +80 -0
- package/utils/registry.js +145 -0
- package/utils/report.js +23 -12
- package/utils/services.js +2 -2
- package/utils/starter.js +241 -0
- package/utils/tar.js +180 -0
- package/utils/workspace.js +207 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// Projet poad à plusieurs fronts : un seul backend (poad/ à la racine), une
|
|
2
|
+
// application par dossier sous apps/ (ou ailleurs, déclarée dans workspaces) :
|
|
3
|
+
//
|
|
4
|
+
// mon-projet/
|
|
5
|
+
// poad/ backend (config, migrations, fonctions, flows)
|
|
6
|
+
// apps/web/ Next.js
|
|
7
|
+
// apps/admin/ Angular
|
|
8
|
+
// package.json espace de travail (workspaces: ["apps/*"]), bun start
|
|
9
|
+
// scripts/start.mjs démarre poad puis tous les fronts
|
|
10
|
+
//
|
|
11
|
+
// Partagé par poad create (projet neuf, front ajouté) et poad client init.
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
|
|
15
|
+
// Nom d'application : celui des variables publiques par application du
|
|
16
|
+
// backend (/env/v1?app=<nom>, _poad.variables.app) ; même règle que l'engine
|
|
17
|
+
const APP_RE = /^[a-z][a-z0-9-]{0,62}$/;
|
|
18
|
+
const DEFAULT_APP = 'web';
|
|
19
|
+
const APPS_DIR = 'apps';
|
|
20
|
+
|
|
21
|
+
const readJson = (file) => {
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const writeJson = (file, value) => fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n');
|
|
29
|
+
const posix = (p) => p.split(path.sep).join('/');
|
|
30
|
+
|
|
31
|
+
/** Nom d'application valide, ou une erreur lisible */
|
|
32
|
+
function checkAppName(name) {
|
|
33
|
+
if (!APP_RE.test(String(name || ''))) throw new Error(`"${name}" is not a valid application name: a lowercase letter, then a-z, 0-9 and "-" (web, admin), 63 characters at most`);
|
|
34
|
+
return name;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Nom d'application tiré d'un package.json : poad.app, sinon le nom du paquet (sans scope) ; '' si aucun n'est valide */
|
|
38
|
+
function appNameOf(pkg = {}) {
|
|
39
|
+
const declared = pkg && pkg.poad && pkg.poad.app;
|
|
40
|
+
if (APP_RE.test(String(declared || ''))) return declared;
|
|
41
|
+
const name = String((pkg && pkg.name) || '')
|
|
42
|
+
.replace(/^@[^/]+\//, '')
|
|
43
|
+
.toLowerCase()
|
|
44
|
+
.replace(/[^a-z0-9-]+/g, '-')
|
|
45
|
+
.replace(/^[^a-z]+|-+$/g, '')
|
|
46
|
+
.slice(0, 63);
|
|
47
|
+
return APP_RE.test(name) ? name : '';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Projet poad qui contient dir (poad/config.toml ici ou dans un parent) : { root, config } ou null */
|
|
51
|
+
function findProject(dir) {
|
|
52
|
+
const found = require('./findpoad').findPoadDir(dir);
|
|
53
|
+
return found ? { root: path.dirname(path.dirname(found)), config: found } : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Dossiers désignés par les motifs workspaces (« apps/* » ou un chemin exact) */
|
|
57
|
+
function expandWorkspaces(root, patterns = []) {
|
|
58
|
+
const dirs = [];
|
|
59
|
+
for (const raw of patterns) {
|
|
60
|
+
if (typeof raw !== 'string' || raw.startsWith('!')) continue;
|
|
61
|
+
const p = raw.replace(/\/+$/, '');
|
|
62
|
+
if (p.endsWith('/*')) {
|
|
63
|
+
const base = path.join(root, p.slice(0, -2));
|
|
64
|
+
if (!fs.existsSync(base)) continue;
|
|
65
|
+
for (const n of fs.readdirSync(base).sort()) {
|
|
66
|
+
const d = path.join(base, n);
|
|
67
|
+
if (fs.statSync(d).isDirectory() && fs.existsSync(path.join(d, 'package.json'))) dirs.push(d);
|
|
68
|
+
}
|
|
69
|
+
} else if (!p.includes('*') && fs.existsSync(path.join(root, p, 'package.json'))) dirs.push(path.join(root, p));
|
|
70
|
+
}
|
|
71
|
+
return [...new Set(dirs)];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Motifs workspaces du package.json racine (tableau, ou { packages: [...] }) ; null s'il n'en a pas */
|
|
75
|
+
function workspacesOf(pkg) {
|
|
76
|
+
if (!pkg) return null;
|
|
77
|
+
if (Array.isArray(pkg.workspaces)) return pkg.workspaces;
|
|
78
|
+
if (pkg.workspaces && Array.isArray(pkg.workspaces.packages)) return pkg.workspaces.packages;
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Port de développement d'une application : poad.port, sinon celui d'angular.json */
|
|
83
|
+
function appPort(dir, pkg) {
|
|
84
|
+
const declared = Number(pkg && pkg.poad && pkg.poad.port);
|
|
85
|
+
if (declared) return declared;
|
|
86
|
+
const ng = readJson(path.join(dir, 'angular.json'));
|
|
87
|
+
for (const p of Object.values((ng && ng.projects) || {})) {
|
|
88
|
+
const port = Number(p && p.architect && p.architect.serve && p.architect.serve.options && p.architect.serve.options.port);
|
|
89
|
+
if (port) return port;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Fronts du projet : dossiers des workspaces du package.json racine, apps/*,
|
|
96
|
+
* et la racine elle-même si son package.json est une application (ancienne
|
|
97
|
+
* disposition, poad/ dans le front). [{ dir, rel, name, port, pkg }]
|
|
98
|
+
*/
|
|
99
|
+
function listApps(root) {
|
|
100
|
+
const rootPkg = readJson(path.join(root, 'package.json'));
|
|
101
|
+
const dirs = expandWorkspaces(root, [...(workspacesOf(rootPkg) || []), `${APPS_DIR}/*`]);
|
|
102
|
+
if (rootPkg && !workspacesOf(rootPkg) && rootPkg.scripts && rootPkg.scripts.start) dirs.unshift(root);
|
|
103
|
+
return dirs.map((dir) => {
|
|
104
|
+
const pkg = readJson(path.join(dir, 'package.json')) || {};
|
|
105
|
+
return { dir, rel: posix(path.relative(root, dir)) || '.', name: appNameOf(pkg) || path.basename(dir), port: appPort(dir, pkg), pkg };
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Ports pris par le projet : ses fronts et ses services (poad/config.toml) */
|
|
110
|
+
function usedPorts(root) {
|
|
111
|
+
const ports = new Set(listApps(root).map((a) => a.port).filter(Boolean));
|
|
112
|
+
const config = path.join(root, 'poad', 'config.toml');
|
|
113
|
+
if (fs.existsSync(config)) {
|
|
114
|
+
for (const m of fs.readFileSync(config, 'utf8').matchAll(/^\s*port\s*=\s*(\d+)/gm)) ports.add(Number(m[1]));
|
|
115
|
+
}
|
|
116
|
+
return ports;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Le dossier est-il couvert par les motifs workspaces ? */
|
|
120
|
+
function coveredBy(root, patterns, dir) {
|
|
121
|
+
const rel = posix(path.relative(root, dir));
|
|
122
|
+
return patterns.some((p) => {
|
|
123
|
+
const q = String(p).replace(/\/+$/, '');
|
|
124
|
+
if (q.endsWith('/*')) return path.posix.dirname(rel) === q.slice(0, -2);
|
|
125
|
+
return q === rel;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const START_SCRIPT = 'node scripts/start.mjs';
|
|
130
|
+
|
|
131
|
+
/** package.json racine d'un projet neuf : espace de travail, scripts start et start:<app> */
|
|
132
|
+
function rootPackageJson(name, apps = [], packageManager) {
|
|
133
|
+
const scripts = { start: START_SCRIPT };
|
|
134
|
+
for (const app of apps) scripts[`start:${app}`] = `${START_SCRIPT} ${app}`;
|
|
135
|
+
return {
|
|
136
|
+
name,
|
|
137
|
+
private: true,
|
|
138
|
+
description: 'Projet poad : le backend dans poad/, les fronts dans apps/',
|
|
139
|
+
workspaces: [`${APPS_DIR}/*`],
|
|
140
|
+
scripts,
|
|
141
|
+
...(packageManager ? { packageManager } : {}),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Inscrit un front dans le package.json racine, s'il déclare des workspaces :
|
|
147
|
+
* le dossier (s'il n'est pas déjà couvert) et le script start:<app>.
|
|
148
|
+
* Renvoie { workspace: bool, added: [...] } ; workspace false : rien à faire.
|
|
149
|
+
*/
|
|
150
|
+
function registerApp(root, dir, app) {
|
|
151
|
+
const file = path.join(root, 'package.json');
|
|
152
|
+
const pkg = readJson(file);
|
|
153
|
+
const patterns = workspacesOf(pkg);
|
|
154
|
+
if (!patterns) return { workspace: false, added: [] };
|
|
155
|
+
const added = [];
|
|
156
|
+
if (path.resolve(dir) !== path.resolve(root) && !coveredBy(root, patterns, dir)) {
|
|
157
|
+
patterns.push(posix(path.relative(root, dir)));
|
|
158
|
+
added.push('workspaces');
|
|
159
|
+
}
|
|
160
|
+
pkg.scripts = pkg.scripts || {};
|
|
161
|
+
if (pkg.scripts.start === undefined) {
|
|
162
|
+
pkg.scripts.start = START_SCRIPT;
|
|
163
|
+
added.push('start');
|
|
164
|
+
}
|
|
165
|
+
if (pkg.scripts[`start:${app}`] === undefined) {
|
|
166
|
+
pkg.scripts[`start:${app}`] = `${START_SCRIPT} ${app}`;
|
|
167
|
+
added.push(`start:${app}`);
|
|
168
|
+
}
|
|
169
|
+
if (added.length) writeJson(file, pkg);
|
|
170
|
+
return { workspace: true, added };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** pnpm ne lit pas workspaces : pnpm-workspace.yaml */
|
|
174
|
+
function writePnpmWorkspace(root, patterns = [`${APPS_DIR}/*`]) {
|
|
175
|
+
fs.writeFileSync(path.join(root, 'pnpm-workspace.yaml'), `packages:\n${patterns.map((p) => ` - '${p}'`).join('\n')}\n`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** scripts/start.mjs du projet (copié depuis le CLI ; le projet peut le modifier) */
|
|
179
|
+
function writeStartScript(root) {
|
|
180
|
+
const target = path.join(root, 'scripts', 'start.mjs');
|
|
181
|
+
if (fs.existsSync(target)) return false;
|
|
182
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
183
|
+
fs.copyFileSync(path.join(__dirname, '..', 'templates', 'workspace', 'start.mjs'), target);
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
module.exports = {
|
|
188
|
+
APP_RE,
|
|
189
|
+
DEFAULT_APP,
|
|
190
|
+
APPS_DIR,
|
|
191
|
+
START_SCRIPT,
|
|
192
|
+
checkAppName,
|
|
193
|
+
appNameOf,
|
|
194
|
+
findProject,
|
|
195
|
+
expandWorkspaces,
|
|
196
|
+
workspacesOf,
|
|
197
|
+
appPort,
|
|
198
|
+
listApps,
|
|
199
|
+
usedPorts,
|
|
200
|
+
coveredBy,
|
|
201
|
+
rootPackageJson,
|
|
202
|
+
registerApp,
|
|
203
|
+
writePnpmWorkspace,
|
|
204
|
+
writeStartScript,
|
|
205
|
+
readJson,
|
|
206
|
+
writeJson,
|
|
207
|
+
};
|