@saulwade/swl-ses 2.4.1 → 2.4.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.
@@ -1,208 +1,210 @@
1
- 'use strict';
2
-
3
- /**
4
- * Wizard de desinstalación TUI.
5
- * Operación destructiva — requiere doble confirmación:
6
- * 1. Multi-select de instalaciones a desinstalar (default: ninguna preseleccionada)
7
- * 2. Vista previa de qué se eliminará (rutas, conteo de archivos)
8
- * 3. Confirmación con escritura explícita del nombre del runtime (anti-clicaccidental)
9
- * 4. Confirmación final
10
- *
11
- * NUNCA toca _userland/ ni APRENDIZAJES.md por diseño del desinstalador.
12
- */
13
-
14
- const fs = require('fs');
15
- const path = require('path');
16
-
17
- const { selectorUnico } = require('../componentes/selector-unico');
18
- const { selectorMulti } = require('../componentes/selector-multi');
19
- const render = require('../lib/render');
20
- const teclas = require('../lib/teclas');
21
- const { colores, semantico, iconos } = require('../lib/colores');
22
-
23
- function _cargarInstalaciones() {
24
- let detectarRuntimes, cargarEstado;
25
- try {
26
- ({ detectarRuntimes } = require('../../lib/detectar-runtime'));
27
- ({ cargarEstado } = require('../../lib/estado'));
28
- } catch (_) {
29
- return [];
30
- }
31
-
32
- const runtimes = detectarRuntimes();
33
- const filas = [];
34
- for (const runtime of runtimes) {
35
- const candidatos = [
36
- { ruta: runtime.global, scope: 'global', esGlobal: true },
37
- { ruta: path.resolve(runtime.local), scope: 'proyecto', esGlobal: false },
38
- ];
39
- for (const { ruta, scope, esGlobal } of candidatos) {
40
- if (!fs.existsSync(ruta)) continue;
41
- const estado = cargarEstado(ruta);
42
- if (!estado) continue;
43
- filas.push({
44
- id: `${runtime.id}:${scope}`,
45
- runtimeId: runtime.id,
46
- runtimeNombre: runtime.nombre,
47
- scope,
48
- esGlobal,
49
- ruta,
50
- version: estado.versionSistema || 'desconocida',
51
- perfil: estado.perfil || '-',
52
- archivos: estado.archivosInstalados?.length || 0,
53
- });
54
- }
55
- }
56
- return filas;
57
- }
58
-
59
- // ---------------------------------------------------------------------------
60
- // Pantalla de vista previa de eliminación
61
- // ---------------------------------------------------------------------------
62
-
63
- function _pantallaPreview(seleccionadas) {
64
- return new Promise((resolve) => {
65
- if (!render.ES_TTY || !process.stdin.isTTY) {
66
- resolve(true);
67
- return;
68
- }
69
-
70
- function _renderizar() {
71
- render.limpiarPantalla();
72
- const { cols, rows } = render.obtenerDimensiones();
73
-
74
- render.escribirEn(2, 3, semantico.titulo('Vista previa de desinstalación'));
75
- render.escribirEn(3, 3, semantico.warn(
76
- `${iconos.warn} Esta operación es destructiva. Se eliminarán archivos del sistema.`));
77
-
78
- render.escribirEn(5, 3, semantico.enfasis('Instalaciones a eliminar:'));
79
- seleccionadas.forEach((i, idx) => {
80
- const linea =
81
- `${semantico.error(iconos.cross)} ` +
82
- render.rellenarDer(colores.negrita(i.runtimeNombre), 18) +
83
- render.rellenarDer(`(${i.scope})`, 12) +
84
- render.rellenarDer(`v${i.version}`, 12) +
85
- render.rellenarDer(i.perfil, 16) +
86
- colores.dim(`${i.archivos} archivos`);
87
- render.escribirEn(6 + idx, 5, linea);
88
- });
89
-
90
- const filaRutas = 7 + seleccionadas.length;
91
- render.escribirEn(filaRutas, 3, semantico.enfasis('Rutas afectadas:'));
92
- seleccionadas.forEach((i, idx) => {
93
- if (filaRutas + 1 + idx >= rows - 8) return;
94
- render.escribirEn(filaRutas + 1 + idx, 5,
95
- render.recortar(`${iconos.arrow} ${colores.dim(i.ruta)}`, cols - 7));
96
- });
97
-
98
- // Garantías preservadas
99
- const filaPres = filaRutas + seleccionadas.length + 2;
100
- if (filaPres < rows - 4) {
101
- render.escribirEn(filaPres, 3, semantico.exito('Lo que NO se toca:'));
102
- const preservados = [
103
- '_userland/ tu código y configuración personal',
104
- '.planning/APRENDIZAJES.md aprendizajes del proyecto',
105
- '.planning/ — sesiones y contexto del proyecto',
106
- 'Hooks externos no-SWL en settings.json',
107
- ];
108
- preservados.forEach((p, idx) => {
109
- if (filaPres + 1 + idx >= rows - 3) return;
110
- render.escribirEn(filaPres + 1 + idx, 5,
111
- `${semantico.exito(iconos.check)} ${colores.dim(p)}`);
112
- });
113
- }
114
-
115
- render.dibujarPiePagina([
116
- ['Enter', 'continuar a confirmación'],
117
- ['Esc', 'cancelar'],
118
- ]);
119
- }
120
-
121
- render.iniciarModoTui();
122
- _renderizar();
123
- const onResize = () => _renderizar();
124
- process.stdout.on('resize', onResize);
125
-
126
- const teclado = teclas.crearTeclado();
127
- function _finalizar(v) {
128
- teclado.desactivar();
129
- process.stdout.removeListener('resize', onResize);
130
- render.salirModoTui();
131
- resolve(v);
132
- }
133
- teclado.on('return', () => _finalizar(true));
134
- teclado.on('escape', () => _finalizar(false));
135
- teclado.activar();
136
- });
137
- }
138
-
139
- // ---------------------------------------------------------------------------
140
- // Wizard principal
141
- // ---------------------------------------------------------------------------
142
-
143
- async function ejecutarWizardUninstall() {
144
- const instalaciones = _cargarInstalaciones();
145
-
146
- if (instalaciones.length === 0) {
147
- return {
148
- exito: false,
149
- error: 'No se detectaron instalaciones SWL para desinstalar.',
150
- sugerencia: 'Ejecuta swl-ses doctor para diagnosticar.',
151
- };
152
- }
153
-
154
- // 1. Multi-select (NADA preseleccionado por seguridad)
155
- const itemsRuntimes = instalaciones.map(i => ({
156
- valor: i.id,
157
- etiqueta: `${i.runtimeNombre} (${i.scope}) v${i.version} · perfil ${i.perfil}`,
158
- descripcion: `${i.archivos} archivos en ${i.ruta}`,
159
- preseleccionado: false,
160
- }));
161
-
162
- const idsElegidos = await selectorMulti({
163
- titulo: 'Paso 1/3 — ¿Qué instalaciones desinstalar? (espacio para marcar)',
164
- items: itemsRuntimes,
165
- });
166
- if (idsElegidos === null) return { exito: false, cancelado: true };
167
- if (idsElegidos.length === 0) {
168
- return { exito: false, vacio: true, error: 'No seleccionaste nada.' };
169
- }
170
-
171
- const seleccionadas = instalaciones.filter(i => idsElegidos.includes(i.id));
172
-
173
- // 2. Vista previa
174
- const proceder = await _pantallaPreview(seleccionadas);
175
- if (!proceder) return { exito: false, cancelado: true };
176
-
177
- // 3. Doble confirmación
178
- const confirmacion = await selectorUnico({
179
- titulo: `Paso 3/3 ¿Confirmar desinstalación de ${seleccionadas.length} instalación(es)?`,
180
- items: [
181
- {
182
- valor: 'no',
183
- etiqueta: 'No, cancelar y volver al menú',
184
- descripcion: 'Vuelve sin tocar nada.',
185
- },
186
- {
187
- valor: 'si',
188
- etiqueta: `Sí, eliminar ${seleccionadas.length} instalación(es) ahora`,
189
- descripcion:
190
- 'Acción irreversible. Si tienes dudas, escoge "No" y haz backup primero. ' +
191
- 'Recuerda que _userland/ y APRENDIZAJES.md se preservan.',
192
- badge: 'destructivo',
193
- },
194
- ],
195
- indiceDefault: 0, // default seguro: cancelar
196
- });
197
- if (confirmacion !== 'si') return { exito: false, cancelado: true };
198
-
199
- return {
200
- exito: true,
201
- plan: { seleccionadas },
202
- };
203
- }
204
-
205
- module.exports = {
206
- ejecutarWizardUninstall,
207
- _cargarInstalaciones,
208
- };
1
+ 'use strict';
2
+
3
+ /**
4
+ * Wizard de desinstalación TUI.
5
+ * Operación destructiva — requiere doble confirmación:
6
+ * 1. Multi-select de instalaciones a desinstalar (default: ninguna preseleccionada)
7
+ * 2. Vista previa de qué se eliminará (rutas, conteo de archivos)
8
+ * 3. Confirmación con escritura explícita del nombre del runtime (anti-clicaccidental)
9
+ * 4. Confirmación final
10
+ *
11
+ * NUNCA toca _userland/ ni APRENDIZAJES.md por diseño del desinstalador.
12
+ */
13
+
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+
17
+ const { selectorUnico } = require('../componentes/selector-unico');
18
+ const { selectorMulti } = require('../componentes/selector-multi');
19
+ const render = require('../lib/render');
20
+ const teclas = require('../lib/teclas');
21
+ const { colores, semantico, iconos } = require('../lib/colores');
22
+
23
+ function _cargarInstalaciones() {
24
+ let detectarRuntimes, cargarEstado;
25
+ try {
26
+ ({ detectarRuntimes, scopesReales } = require('../../lib/detectar-runtime'));
27
+ ({ cargarEstado } = require('../../lib/estado'));
28
+ } catch (_) {
29
+ return [];
30
+ }
31
+
32
+ const runtimes = detectarRuntimes();
33
+ const filas = [];
34
+ for (const runtime of runtimes) {
35
+ // Dedup + guard anti-fantasma: desde HOME, el local relativo resuelve al
36
+ // global propio (duplicado) o al de OTRO runtime (p.ej. .claude de
37
+ // OpenClaude = global de Claude Code) — ver scopesReales().
38
+ const candidatos = scopesReales(runtime).map(({ dir, esGlobal }) => (
39
+ { ruta: dir, scope: esGlobal ? 'global' : 'proyecto', esGlobal }
40
+ ));
41
+ for (const { ruta, scope, esGlobal } of candidatos) {
42
+ if (!fs.existsSync(ruta)) continue;
43
+ const estado = cargarEstado(ruta);
44
+ if (!estado) continue;
45
+ filas.push({
46
+ id: `${runtime.id}:${scope}`,
47
+ runtimeId: runtime.id,
48
+ runtimeNombre: runtime.nombre,
49
+ scope,
50
+ esGlobal,
51
+ ruta,
52
+ version: estado.versionSistema || 'desconocida',
53
+ perfil: estado.perfil || '-',
54
+ archivos: estado.archivosInstalados?.length || 0,
55
+ });
56
+ }
57
+ }
58
+ return filas;
59
+ }
60
+
61
+ // ---------------------------------------------------------------------------
62
+ // Pantalla de vista previa de eliminación
63
+ // ---------------------------------------------------------------------------
64
+
65
+ function _pantallaPreview(seleccionadas) {
66
+ return new Promise((resolve) => {
67
+ if (!render.ES_TTY || !process.stdin.isTTY) {
68
+ resolve(true);
69
+ return;
70
+ }
71
+
72
+ function _renderizar() {
73
+ render.limpiarPantalla();
74
+ const { cols, rows } = render.obtenerDimensiones();
75
+
76
+ render.escribirEn(2, 3, semantico.titulo('Vista previa de desinstalación'));
77
+ render.escribirEn(3, 3, semantico.warn(
78
+ `${iconos.warn} Esta operación es destructiva. Se eliminarán archivos del sistema.`));
79
+
80
+ render.escribirEn(5, 3, semantico.enfasis('Instalaciones a eliminar:'));
81
+ seleccionadas.forEach((i, idx) => {
82
+ const linea =
83
+ `${semantico.error(iconos.cross)} ` +
84
+ render.rellenarDer(colores.negrita(i.runtimeNombre), 18) +
85
+ render.rellenarDer(`(${i.scope})`, 12) +
86
+ render.rellenarDer(`v${i.version}`, 12) +
87
+ render.rellenarDer(i.perfil, 16) +
88
+ colores.dim(`${i.archivos} archivos`);
89
+ render.escribirEn(6 + idx, 5, linea);
90
+ });
91
+
92
+ const filaRutas = 7 + seleccionadas.length;
93
+ render.escribirEn(filaRutas, 3, semantico.enfasis('Rutas afectadas:'));
94
+ seleccionadas.forEach((i, idx) => {
95
+ if (filaRutas + 1 + idx >= rows - 8) return;
96
+ render.escribirEn(filaRutas + 1 + idx, 5,
97
+ render.recortar(`${iconos.arrow} ${colores.dim(i.ruta)}`, cols - 7));
98
+ });
99
+
100
+ // Garantías preservadas
101
+ const filaPres = filaRutas + seleccionadas.length + 2;
102
+ if (filaPres < rows - 4) {
103
+ render.escribirEn(filaPres, 3, semantico.exito('Lo que NO se toca:'));
104
+ const preservados = [
105
+ '_userland/ — tu código y configuración personal',
106
+ '.planning/APRENDIZAJES.md aprendizajes del proyecto',
107
+ '.planning/ — sesiones y contexto del proyecto',
108
+ 'Hooks externos no-SWL en settings.json',
109
+ ];
110
+ preservados.forEach((p, idx) => {
111
+ if (filaPres + 1 + idx >= rows - 3) return;
112
+ render.escribirEn(filaPres + 1 + idx, 5,
113
+ `${semantico.exito(iconos.check)} ${colores.dim(p)}`);
114
+ });
115
+ }
116
+
117
+ render.dibujarPiePagina([
118
+ ['Enter', 'continuar a confirmación'],
119
+ ['Esc', 'cancelar'],
120
+ ]);
121
+ }
122
+
123
+ render.iniciarModoTui();
124
+ _renderizar();
125
+ const onResize = () => _renderizar();
126
+ process.stdout.on('resize', onResize);
127
+
128
+ const teclado = teclas.crearTeclado();
129
+ function _finalizar(v) {
130
+ teclado.desactivar();
131
+ process.stdout.removeListener('resize', onResize);
132
+ render.salirModoTui();
133
+ resolve(v);
134
+ }
135
+ teclado.on('return', () => _finalizar(true));
136
+ teclado.on('escape', () => _finalizar(false));
137
+ teclado.activar();
138
+ });
139
+ }
140
+
141
+ // ---------------------------------------------------------------------------
142
+ // Wizard principal
143
+ // ---------------------------------------------------------------------------
144
+
145
+ async function ejecutarWizardUninstall() {
146
+ const instalaciones = _cargarInstalaciones();
147
+
148
+ if (instalaciones.length === 0) {
149
+ return {
150
+ exito: false,
151
+ error: 'No se detectaron instalaciones SWL para desinstalar.',
152
+ sugerencia: 'Ejecuta swl-ses doctor para diagnosticar.',
153
+ };
154
+ }
155
+
156
+ // 1. Multi-select (NADA preseleccionado por seguridad)
157
+ const itemsRuntimes = instalaciones.map(i => ({
158
+ valor: i.id,
159
+ etiqueta: `${i.runtimeNombre} (${i.scope}) — v${i.version} · perfil ${i.perfil}`,
160
+ descripcion: `${i.archivos} archivos en ${i.ruta}`,
161
+ preseleccionado: false,
162
+ }));
163
+
164
+ const idsElegidos = await selectorMulti({
165
+ titulo: 'Paso 1/3 — ¿Qué instalaciones desinstalar? (espacio para marcar)',
166
+ items: itemsRuntimes,
167
+ });
168
+ if (idsElegidos === null) return { exito: false, cancelado: true };
169
+ if (idsElegidos.length === 0) {
170
+ return { exito: false, vacio: true, error: 'No seleccionaste nada.' };
171
+ }
172
+
173
+ const seleccionadas = instalaciones.filter(i => idsElegidos.includes(i.id));
174
+
175
+ // 2. Vista previa
176
+ const proceder = await _pantallaPreview(seleccionadas);
177
+ if (!proceder) return { exito: false, cancelado: true };
178
+
179
+ // 3. Doble confirmación
180
+ const confirmacion = await selectorUnico({
181
+ titulo: `Paso 3/3 — ¿Confirmar desinstalación de ${seleccionadas.length} instalación(es)?`,
182
+ items: [
183
+ {
184
+ valor: 'no',
185
+ etiqueta: 'No, cancelar y volver al menú',
186
+ descripcion: 'Vuelve sin tocar nada.',
187
+ },
188
+ {
189
+ valor: 'si',
190
+ etiqueta: `Sí, eliminar ${seleccionadas.length} instalación(es) ahora`,
191
+ descripcion:
192
+ 'Acción irreversible. Si tienes dudas, escoge "No" y haz backup primero. ' +
193
+ 'Recuerda que _userland/ y APRENDIZAJES.md se preservan.',
194
+ badge: 'destructivo',
195
+ },
196
+ ],
197
+ indiceDefault: 0, // default seguro: cancelar
198
+ });
199
+ if (confirmacion !== 'si') return { exito: false, cancelado: true };
200
+
201
+ return {
202
+ exito: true,
203
+ plan: { seleccionadas },
204
+ };
205
+ }
206
+
207
+ module.exports = {
208
+ ejecutarWizardUninstall,
209
+ _cargarInstalaciones,
210
+ };