poad 2.60.0 → 2.61.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/global/create.js +40 -27
- package/global/start.js +6 -0
- package/global/stop.js +52 -19
- package/package.json +1 -1
- package/poad.js +25 -0
- package/project/cli.js +1 -1
- package/project/db/diff.js +99 -1
- package/project/flows.js +133 -39
- package/project/logs.js +92 -43
- package/project/migrate.js +2 -1
- package/project/upgrade.js +6 -0
- package/test/flows.test.js +256 -0
- package/test/help.test.js +18 -0
- package/test/project-lock.test.js +51 -0
- package/utils/banner.js +6 -4
- package/utils/help-details.js +35 -5
- package/utils/help.js +60 -8
- package/utils/project-lock.js +67 -0
- package/utils/reference.js +6 -3
package/global/create.js
CHANGED
|
@@ -128,34 +128,47 @@ function modeOf(opts, project) {
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
function usage(chalk) {
|
|
131
|
+
// Coupée à la largeur du terminal (utils/layout.js), comme les autres fiches : elle figure aussi dans poad --help
|
|
132
|
+
const { row, paragraph, terminalColumns } = require('../utils/layout');
|
|
133
|
+
const columns = terminalColumns();
|
|
131
134
|
const c = chalk.cyanBright;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
135
|
+
const out = [chalk.bold(' Usage :')];
|
|
136
|
+
const forms = [
|
|
137
|
+
['poad create <project> [--template t] [--app web]', 'a new project: poad/ (backend) and apps/<app> (first front-end)'],
|
|
138
|
+
['poad create <app> --template t [--dir <folder>]', 'inside a poad project: one more front-end, apps/<app>'],
|
|
139
|
+
['poad create <folder> --flat [--template t]', 'one front-end with poad/ inside it (the former layout)'],
|
|
140
|
+
];
|
|
141
|
+
for (const [form, text] of forms) {
|
|
142
|
+
out.push(` ${c(form)}`);
|
|
143
|
+
out.push(...paragraph([{ text, style: chalk.grey }], { columns, indent: 5 }));
|
|
144
|
+
}
|
|
145
|
+
out.push(' ');
|
|
146
|
+
out.push(' One poad backend, several front-ends:');
|
|
147
|
+
for (const l of [
|
|
148
|
+
' my-project/',
|
|
149
|
+
' poad/ backend (config, migrations, functions, flows)',
|
|
150
|
+
' apps/web/ Next.js poad create web --template next',
|
|
151
|
+
' apps/admin/ Angular poad create admin --template angular',
|
|
152
|
+
' package.json workspaces apps/*; bun start runs poad, then each front-end',
|
|
153
|
+
])
|
|
154
|
+
out.push(chalk.grey(` ${l}`));
|
|
155
|
+
out.push(' ');
|
|
156
|
+
const options = [
|
|
157
|
+
['--template', 'angular, next, nuxt or vanilla (asked when omitted in a terminal; angular otherwise)'],
|
|
158
|
+
['--app', 'name of the first front-end of a new project (default: web): its folder apps/<app>, and the application name of its public variables (PUBLIC_* with app=<name>, /env/v1?app=<name>)'],
|
|
159
|
+
['--dir', 'inside a project: folder of the new front-end (default: apps/<app>)'],
|
|
160
|
+
['--flat', 'the former layout: package.json and poad/ in the same folder'],
|
|
161
|
+
['--yes', 'no question'],
|
|
162
|
+
['--no-install', 'do not install the dependencies (bun start does it later)'],
|
|
163
|
+
['--no-git', 'no git repository, no commit (a front-end added to a project never commits)'],
|
|
164
|
+
['--pm', 'bun, npm or pnpm (default: bun if installed, else npm)'],
|
|
165
|
+
['--from', 'a local template (npm pack archive, or folder), to develop a template'],
|
|
166
|
+
];
|
|
167
|
+
for (const [k, v] of options) out.push(...row({ name: k, nameStyle: c, nameWidth: 14, columns, dots: false, segments: v }));
|
|
168
|
+
out.push(' ');
|
|
169
|
+
out.push(...paragraph([{ text: 'Each front-end gets a development port of its own: poad.port in its package.json (from the template, else its framework default), moved past the ports of the other front-ends and of the poad services. A front-end reads the public variables of its application name (package.json poad.app).' }], { columns }));
|
|
170
|
+
out.push(' ');
|
|
171
|
+
console.log(out.join('\n'));
|
|
159
172
|
}
|
|
160
173
|
|
|
161
174
|
/** Modèle choisi (question dans un terminal) : { template, metadata } ; null si abandon */
|
package/global/start.js
CHANGED
|
@@ -28,6 +28,12 @@ module.exports = async (args = {}) => {
|
|
|
28
28
|
const gateway = require('../utils/gateway');
|
|
29
29
|
const root = findPoadDir(process.cwd());
|
|
30
30
|
if (!root) return console.log(chalk.redBright('You are not in a poad project directory.'));
|
|
31
|
+
// Une seule commande à la fois sur les conteneurs du projet (utils/project-lock.js)
|
|
32
|
+
const lock = require('../utils/project-lock').acquire(root, 'start');
|
|
33
|
+
if (!lock.ok) {
|
|
34
|
+
process.exitCode = 1;
|
|
35
|
+
return console.log(chalk.redBright(require('../utils/project-lock').busyMessage(lock.held)));
|
|
36
|
+
}
|
|
31
37
|
const config = require('../utils/config').readConfig(root);
|
|
32
38
|
const containerName = config.project_uuid;
|
|
33
39
|
// Registre des projets de la machine : dernière utilisation (inscrit s'il n'y était pas)
|
package/global/stop.js
CHANGED
|
@@ -16,12 +16,34 @@ module.exports = async (args) => {
|
|
|
16
16
|
const runner = require('../utils/runner');
|
|
17
17
|
const root = findPoadDir(process.cwd());
|
|
18
18
|
if (!root) return console.log(require('chalk-v2').redBright('You are not in a poad project directory.'));
|
|
19
|
+
// Une seule commande à la fois sur les conteneurs du projet (utils/project-lock.js)
|
|
20
|
+
const lock = require('../utils/project-lock').acquire(root, 'stop');
|
|
21
|
+
if (!lock.ok) {
|
|
22
|
+
process.exitCode = 1;
|
|
23
|
+
return console.log(require('chalk-v2').redBright(require('../utils/project-lock').busyMessage(lock.held)));
|
|
24
|
+
}
|
|
19
25
|
const config = require('../utils/config').readConfig(root);
|
|
20
26
|
const containerName = config.project_uuid;
|
|
21
27
|
const ora = require('ora');
|
|
22
28
|
const chalk = require('chalk-v2');
|
|
23
29
|
console.log(' ');
|
|
24
30
|
|
|
31
|
+
// Docker qui ne répond pas n'est pas un projet arrêté : le dire, code 1
|
|
32
|
+
try {
|
|
33
|
+
await docker.ping();
|
|
34
|
+
} catch (err) {
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
return console.log(chalk.redBright(`Docker does not answer (${(err && err.code) || (err && err.message) || err}): start Docker, then run "poad stop" again.`));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Un conteneur déjà arrêté (304) ou déjà retiré (404) entre la vérification et l'arrêt : c'est fait
|
|
40
|
+
const gone = (err) => Boolean(err) && (err.statusCode === 304 || err.statusCode === 404);
|
|
41
|
+
/** Échec réel d'un arrêt : la cause, et un code de sortie non nul */
|
|
42
|
+
const failed = (progress, what, err) => {
|
|
43
|
+
process.exitCode = 1;
|
|
44
|
+
progress.fail(chalk.redBright(`Failed to stop ${what}: ${(err && (err.json && err.json.message)) || (err && err.message) || err}`));
|
|
45
|
+
};
|
|
46
|
+
|
|
25
47
|
const stopKeycloak = async () => {
|
|
26
48
|
const shared = require('../utils/keycloak-shared');
|
|
27
49
|
if (shared.isShared(config)) {
|
|
@@ -44,8 +66,9 @@ module.exports = async (args) => {
|
|
|
44
66
|
try {
|
|
45
67
|
await docker.getContainer(name).remove({ force: true });
|
|
46
68
|
progress.succeed(chalk.bold('Keycloak stopped'));
|
|
47
|
-
} catch {
|
|
48
|
-
progress.
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (gone(err)) progress.succeed(chalk.bold('Keycloak stopped'));
|
|
71
|
+
else failed(progress, 'Keycloak', err);
|
|
49
72
|
}
|
|
50
73
|
return true;
|
|
51
74
|
};
|
|
@@ -57,8 +80,9 @@ module.exports = async (args) => {
|
|
|
57
80
|
try {
|
|
58
81
|
await docker.getContainer(name).stop();
|
|
59
82
|
progress.succeed(chalk.bold('Valkey stopped'));
|
|
60
|
-
} catch {
|
|
61
|
-
progress.
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (gone(err)) progress.succeed(chalk.bold('Valkey stopped'));
|
|
85
|
+
else failed(progress, 'Valkey', err);
|
|
62
86
|
}
|
|
63
87
|
return true;
|
|
64
88
|
};
|
|
@@ -73,43 +97,52 @@ module.exports = async (args) => {
|
|
|
73
97
|
await docker.getContainer(name).stop();
|
|
74
98
|
for (let i = 0; i < 20 && (await isDockerExist(name)); i++) await new Promise((r) => setTimeout(r, 500));
|
|
75
99
|
progress.succeed(chalk.bold('Flows runner stopped'));
|
|
76
|
-
} catch {
|
|
77
|
-
progress.
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (gone(err)) progress.succeed(chalk.bold('Flows runner stopped'));
|
|
102
|
+
else failed(progress, 'the flows runner', err);
|
|
78
103
|
}
|
|
79
104
|
return true;
|
|
80
105
|
};
|
|
81
106
|
|
|
82
107
|
if (runner.wanted(args)) {
|
|
83
|
-
if (!(await stopRunner())) console.log(chalk.
|
|
108
|
+
if (!(await stopRunner())) console.log(chalk.grey('The flows runner is not running: nothing to stop.'));
|
|
84
109
|
return;
|
|
85
110
|
}
|
|
86
111
|
if (kc.wanted(args)) {
|
|
87
|
-
if (!(await stopKeycloak())) console.log(chalk.
|
|
112
|
+
if (!(await stopKeycloak())) console.log(chalk.grey('Keycloak is not running: nothing to stop.'));
|
|
88
113
|
return;
|
|
89
114
|
}
|
|
90
115
|
if (valkey.wanted(args)) {
|
|
91
|
-
if (!(await stopValkey())) console.log(chalk.
|
|
116
|
+
if (!(await stopValkey())) console.log(chalk.grey('Valkey is not running: nothing to stop.'));
|
|
92
117
|
return;
|
|
93
118
|
}
|
|
94
119
|
|
|
95
120
|
// L'exécuteur d'abord : il rend compte à l'engine, par Valkey et par HTTP
|
|
96
|
-
await stopRunner();
|
|
97
|
-
await stopKeycloak();
|
|
98
|
-
await stopValkey();
|
|
121
|
+
const runnerStopped = await stopRunner();
|
|
122
|
+
const kcStopped = await stopKeycloak();
|
|
123
|
+
const valkeyStopped = await stopValkey();
|
|
99
124
|
const engine = await isDockerExist(containerName);
|
|
100
125
|
if (engine) {
|
|
101
|
-
const progress = ora('Stopping
|
|
102
|
-
|
|
103
|
-
await docker.getContainer(containerName).stop();
|
|
126
|
+
const progress = ora('Stopping the engine').start();
|
|
127
|
+
const waitGone = async () => {
|
|
104
128
|
// AutoRemove : le conteneur disparaît juste après l'arrêt ; le réseau
|
|
105
129
|
// ne peut être supprimé qu'ensuite
|
|
106
130
|
for (let i = 0; i < 20 && (await isDockerExist(containerName)); i++) await new Promise((r) => setTimeout(r, 500));
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
131
|
+
};
|
|
132
|
+
try {
|
|
133
|
+
await docker.getContainer(containerName).stop();
|
|
134
|
+
await waitGone();
|
|
135
|
+
progress.succeed(chalk.bold('Engine stopped'));
|
|
136
|
+
} catch (err) {
|
|
137
|
+
if (gone(err)) {
|
|
138
|
+
await waitGone();
|
|
139
|
+
progress.succeed(chalk.bold('Engine stopped'));
|
|
140
|
+
} else failed(progress, 'the engine', err);
|
|
110
141
|
}
|
|
111
142
|
}
|
|
112
143
|
const mailpit = await mail.stopMailpit(docker, config);
|
|
144
|
+
if (mailpit) console.log(`${chalk.greenBright('✔')} ${chalk.bold('Mailpit stopped')}`);
|
|
113
145
|
await mail.removeNetwork(docker, config);
|
|
114
|
-
|
|
146
|
+
// Rien ne tournait : le projet est déjà arrêté, ce n'est pas une erreur
|
|
147
|
+
if (!engine && !mailpit && !runnerStopped && !kcStopped && !valkeyStopped) console.log(chalk.grey('The project is already stopped: nothing to stop.'));
|
|
115
148
|
};
|
package/package.json
CHANGED
package/poad.js
CHANGED
|
@@ -120,6 +120,8 @@ const checkVersion = async () => {
|
|
|
120
120
|
// complète pour « poad --help », « poad -h » et « poad help ».
|
|
121
121
|
const displayBanner = ({ full = false } = {}) => {
|
|
122
122
|
const lines = require('./utils/banner').render({ full, inProject: Boolean(POAD_PATH), version: package.version });
|
|
123
|
+
// poad --help : la liste, puis la fiche de chaque commande (usage, options, notes)
|
|
124
|
+
if (full) lines.push(...require('./utils/help').allHelpLines());
|
|
123
125
|
console.log(lines.join('\n'));
|
|
124
126
|
};
|
|
125
127
|
|
|
@@ -304,8 +306,31 @@ const helpGuard = () => {
|
|
|
304
306
|
return true;
|
|
305
307
|
};
|
|
306
308
|
|
|
309
|
+
// Version : `poad --version` ou `poad -v` (premier mot seulement) affiche la
|
|
310
|
+
// version du CLI et s'arrête, sans vérification sur npm ni commande.
|
|
311
|
+
const VERSION_FLAGS = ['--version', '-v'];
|
|
312
|
+
const versionGuard = () => {
|
|
313
|
+
if (!VERSION_FLAGS.includes(process.argv[2])) return false;
|
|
314
|
+
console.log(package.version);
|
|
315
|
+
return true;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// Dernier filet : une erreur que la commande n'a pas traitée devient une phrase
|
|
319
|
+
// (la trace complète avec POAD_DEBUG=1), jamais une trace Node brute.
|
|
320
|
+
const fatal = (err) => {
|
|
321
|
+
// Ctrl-C dans une question : pas une erreur
|
|
322
|
+
if (err && err.name === 'ExitPromptError') process.exit(130);
|
|
323
|
+
console.error(chalk.redBright(`poad: unexpected error: ${(err && err.message) || err}`));
|
|
324
|
+
if (process.env.POAD_DEBUG) console.error((err && err.stack) || err);
|
|
325
|
+
else console.error(chalk.grey(' POAD_DEBUG=1 poad … shows the details.'));
|
|
326
|
+
process.exit(1);
|
|
327
|
+
};
|
|
328
|
+
process.on('unhandledRejection', fatal);
|
|
329
|
+
process.on('uncaughtException', fatal);
|
|
330
|
+
|
|
307
331
|
// Main function.
|
|
308
332
|
const main = async () => {
|
|
333
|
+
if (versionGuard()) return;
|
|
309
334
|
if (helpGuard()) return;
|
|
310
335
|
let version = await checkVersion();
|
|
311
336
|
const current_version = require(__dirname + '/package.json').version;
|
package/project/cli.js
CHANGED
|
@@ -68,7 +68,7 @@ module.exports = {
|
|
|
68
68
|
migration: 'Database migrations: migration new <name>, migration list',
|
|
69
69
|
functions: 'Functions of the project (JS/TS, served on /functions/v1/<name>): new, list, types',
|
|
70
70
|
function: 'Alias of poad functions',
|
|
71
|
-
flows: 'Flows of the project (automations in poad/flows/<name>.json, engine 0.18.0+, needs Valkey): new, list, run, runs, show, cancel',
|
|
71
|
+
flows: 'Flows of the project (automations in poad/flows/<name>.json, engine 0.18.0+, needs Valkey): new, list, run, runs, show, cancel; --env <name> for poad.io',
|
|
72
72
|
flow: 'Alias of poad flows',
|
|
73
73
|
/* status: `[AI] Vérification du statut de l'environnement`,
|
|
74
74
|
gen: {
|
package/project/db/diff.js
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// poad db diff <nom> les écrit dans poad/migrations/<horodatage>_<nom>.sql
|
|
4
4
|
// (déjà appliqués : ils ne sont pas rejoués)
|
|
5
5
|
// poad db diff --discard les oublie (ils restent dans la base)
|
|
6
|
+
//
|
|
7
|
+
// La liste commence par un résumé, un objet par ligne : + ajouté, ~ modifié,
|
|
8
|
+
// - supprimé, · sans effet (créé puis supprimé…), puis le SQL de chaque changement.
|
|
6
9
|
module.exports = async (args = {}) => {
|
|
7
10
|
const chalk = require('chalk-v2');
|
|
8
11
|
const path = require('path');
|
|
@@ -30,7 +33,7 @@ module.exports = async (args = {}) => {
|
|
|
30
33
|
process.exitCode = 1;
|
|
31
34
|
return console.log(chalk.yellowBright(`The change journal needs engine 0.11.0 or later (this project: ${config.engine}): "poad upgrade".`));
|
|
32
35
|
}
|
|
33
|
-
if (flag('json')) return console.log(JSON.stringify(c, null, 2));
|
|
36
|
+
if (flag('json')) return console.log(JSON.stringify({ ...c, summary: summarize(c) }, null, 2));
|
|
34
37
|
if (!c.entries.length) {
|
|
35
38
|
if (c.drift) {
|
|
36
39
|
process.exitCode = 1;
|
|
@@ -47,6 +50,10 @@ module.exports = async (args = {}) => {
|
|
|
47
50
|
const byId = new Map(c.entries.map((e) => [e.id, e]));
|
|
48
51
|
console.log(chalk.bold(`${c.units.length} structure change(s) not in a migration`));
|
|
49
52
|
console.log(' ');
|
|
53
|
+
for (const line of summaryLines(summarize(c), chalk)) console.log(line);
|
|
54
|
+
console.log(' ');
|
|
55
|
+
console.log(chalk.bold('SQL'));
|
|
56
|
+
console.log(' ');
|
|
50
57
|
for (const u of c.units) {
|
|
51
58
|
const e = byId.get(u.keep.length ? u.keep[u.keep.length - 1] : u.ids[u.ids.length - 1]);
|
|
52
59
|
const when = new Date(e.at).toLocaleString();
|
|
@@ -65,6 +72,7 @@ module.exports = async (args = {}) => {
|
|
|
65
72
|
}
|
|
66
73
|
const file = await migrations.saveChanges(root, client, name, c.sql, c.upTo);
|
|
67
74
|
console.log(`${chalk.greenBright('✔')} ${chalk.bold(path.relative(process.cwd(), file))} ${chalk.grey(`(${c.entries.length} change(s), already applied here)`)}`);
|
|
75
|
+
for (const line of summaryLines(summarize(c).filter((i) => i.change !== 'none'), chalk)) console.log(line);
|
|
68
76
|
console.log(chalk.grey(' Review it, then commit it with your branch.'));
|
|
69
77
|
} catch (err) {
|
|
70
78
|
process.exitCode = 1;
|
|
@@ -73,3 +81,93 @@ module.exports = async (args = {}) => {
|
|
|
73
81
|
await client.end().catch(() => {});
|
|
74
82
|
}
|
|
75
83
|
};
|
|
84
|
+
|
|
85
|
+
// ------------------------------------------------------------------ résumé
|
|
86
|
+
// Les entrées du journal (poad_migrations.journal) portent les commandes
|
|
87
|
+
// (« CREATE TABLE », « DROP INDEX », « GRANT »…) et les objets touchés, sans
|
|
88
|
+
// dire quel objet va avec quelle commande : l'objet principal est celui que
|
|
89
|
+
// l'instruction nomme, les autres (séquence, clé primaire…) suivent.
|
|
90
|
+
|
|
91
|
+
// Ordre de préférence pour l'objet principal d'une instruction qui en touche plusieurs
|
|
92
|
+
const KINDS = ['schema', 'extension', 'table', 'view', 'materialized view', 'foreign table', 'type', 'domain', 'function', 'procedure', 'aggregate', 'trigger', 'policy', 'publication', 'index', 'sequence'];
|
|
93
|
+
|
|
94
|
+
/** Type d'objet d'une commande : « CREATE TABLE » → table ; GRANT, COMMENT… → null. Pure. */
|
|
95
|
+
const kindOf = (command) => {
|
|
96
|
+
const m = /^(?:CREATE|ALTER|DROP)\s+(.+)$/i.exec(String(command || '').trim());
|
|
97
|
+
return m ? m[1].toLowerCase() : null;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const escapeRe = (t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
101
|
+
/** Dernier élément d'une identité (public.todos → todos ; « p on public.t » → p). Pure. */
|
|
102
|
+
const shortName = (identity) => {
|
|
103
|
+
const head = String(identity).split(' on ')[0];
|
|
104
|
+
const parts = head.match(/"(?:[^"]|"")+"|[^.]+/g) || [head];
|
|
105
|
+
return parts[parts.length - 1].replace(/^"|"$/g, '').replace(/\(.*$/, '');
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** Détail d'un ALTER TABLE : colonne ajoutée, supprimée, modifiée, renommage, RLS. Pure. */
|
|
109
|
+
const alterDetail = (statement) => {
|
|
110
|
+
const st = String(statement).replace(/\s+/g, ' ');
|
|
111
|
+
const id = '("(?:[^"]|"")+"|[A-Za-z_][\\w$]*)';
|
|
112
|
+
const unq = (t) => t.replace(/^"|"$/g, '');
|
|
113
|
+
let m;
|
|
114
|
+
if ((m = new RegExp(`\\brename\\s+(?:column\\s+)?${id}\\s+to\\s+${id}`, 'i').exec(st))) return `column ${unq(m[1])} renamed to ${unq(m[2])}`;
|
|
115
|
+
if ((m = new RegExp(`\\brename\\s+to\\s+${id}`, 'i').exec(st))) return `renamed to ${unq(m[1])}`;
|
|
116
|
+
if ((m = new RegExp(`\\badd\\s+(?:column\\s+)?(?:if\\s+not\\s+exists\\s+)?${id}`, 'i').exec(st)) && !/^(constraint|primary|unique|foreign|check|exclude)$/i.test(m[1])) return `column ${unq(m[1])} added`;
|
|
117
|
+
if ((m = new RegExp(`\\badd\\s+constraint\\s+${id}`, 'i').exec(st))) return `constraint ${unq(m[1])} added`;
|
|
118
|
+
if ((m = new RegExp(`\\bdrop\\s+constraint\\s+(?:if\\s+exists\\s+)?${id}`, 'i').exec(st))) return `constraint ${unq(m[1])} removed`;
|
|
119
|
+
if ((m = new RegExp(`\\bdrop\\s+(?:column\\s+)?(?:if\\s+exists\\s+)?${id}`, 'i').exec(st))) return `column ${unq(m[1])} removed`;
|
|
120
|
+
if ((m = new RegExp(`\\balter\\s+(?:column\\s+)?${id}\\s+(?:set|drop|type|add)\\b`, 'i').exec(st))) return `column ${unq(m[1])} changed`;
|
|
121
|
+
if ((m = /\b(enable|disable)\s+row\s+level\s+security/i.exec(st))) return `row level security ${m[1].toLowerCase()}d`;
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Un changement du journal, lisible : { change: added|changed|removed, kind, object, others, detail }. Pure.
|
|
127
|
+
*/
|
|
128
|
+
const describe = (entry) => {
|
|
129
|
+
const commands = (entry.commands || []).map(String);
|
|
130
|
+
const objects = (entry.objects || []).filter(Boolean).map(String);
|
|
131
|
+
const statement = String(entry.statement || '');
|
|
132
|
+
const rank = (k) => (KINDS.includes(k) ? KINDS.indexOf(k) : KINDS.length);
|
|
133
|
+
const main = [...commands].sort((a, b) => rank(kindOf(a)) - rank(kindOf(b)))[0] || '';
|
|
134
|
+
const verb = main.split(/\s+/)[0].toUpperCase();
|
|
135
|
+
const kind = kindOf(main) || (verb === 'GRANT' || verb === 'REVOKE' ? 'privileges' : verb === 'COMMENT' ? 'comment' : main.toLowerCase() || 'object');
|
|
136
|
+
// L'objet que l'instruction nomme ; à défaut, le premier
|
|
137
|
+
const named = objects.find((o) => new RegExp(`(^|[^\\w$])"?${escapeRe(shortName(o))}"?($|[^\\w$])`, 'i').test(statement));
|
|
138
|
+
const object = named || objects[0] || '';
|
|
139
|
+
const change = verb === 'DROP' ? 'removed' : verb === 'CREATE' && !/^\s*create\s+or\s+replace\b/i.test(statement) ? 'added' : 'changed';
|
|
140
|
+
let detail = null;
|
|
141
|
+
if (main.toUpperCase() === 'ALTER TABLE') detail = alterDetail(statement);
|
|
142
|
+
else if (verb === 'GRANT' || verb === 'REVOKE') detail = verb === 'GRANT' ? 'granted' : 'revoked';
|
|
143
|
+
else if (/^\s*create\s+or\s+replace\b/i.test(statement)) detail = 'created or replaced';
|
|
144
|
+
return { change, kind, object, others: objects.filter((o) => o !== object), detail };
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** Résumé de changes() : un élément par changement gardé, et les groupes sans effet. Pure. */
|
|
148
|
+
const summarize = (c) => {
|
|
149
|
+
const byId = new Map((c.entries || []).map((e) => [e.id, e]));
|
|
150
|
+
const out = [];
|
|
151
|
+
for (const u of c.units || []) {
|
|
152
|
+
if (u.effect === 'none') {
|
|
153
|
+
out.push({ change: 'none', kind: u.kind, object: u.target, others: [], detail: `${u.ids.length} changes, no net effect` });
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
for (const id of u.keep) if (byId.has(id)) out.push(describe(byId.get(id)));
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** Lignes du résumé, alignées. Pure (au style près). */
|
|
162
|
+
const summaryLines = (items, chalk) => {
|
|
163
|
+
const SIGN = { added: ['+', 'greenBright'], changed: ['~', 'yellowBright'], removed: ['-', 'redBright'], none: ['·', 'grey'] };
|
|
164
|
+
const kw = Math.max(0, ...items.map((i) => i.kind.length));
|
|
165
|
+
const ow = Math.min(48, Math.max(0, ...items.map((i) => i.object.length)));
|
|
166
|
+
return items.map((i) => {
|
|
167
|
+
const [sign, color] = SIGN[i.change];
|
|
168
|
+
const extra = [i.detail, i.others.length ? `with ${i.others.slice(0, 3).join(', ')}${i.others.length > 3 ? ` (+${i.others.length - 3})` : ''}` : ''].filter(Boolean).join('; ');
|
|
169
|
+
return ` ${chalk[color](sign)} ${i.kind.padEnd(kw)} ${chalk.bold(extra ? i.object.padEnd(ow) : i.object)}${extra ? ` ${chalk.grey(extra)}` : ''}`;
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
Object.assign(module.exports, { describe, summarize, summaryLines, alterDetail, kindOf, shortName });
|