@saulwade/swl-ses 1.3.8 → 1.4.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.
Files changed (128) hide show
  1. package/CLAUDE.md +12 -4
  2. package/README.md +1 -1
  3. package/bin/swl-mcp-server.js +187 -187
  4. package/bin/swl-webhook-server.js +198 -0
  5. package/comandos/swl/.evolved.json +22 -22
  6. package/comandos/swl/adoptar-proyecto.md +21 -1
  7. package/comandos/swl/claudemd.md +14 -1
  8. package/comandos/swl/contribuir.md +233 -233
  9. package/comandos/swl/exportar-vault.md +108 -0
  10. package/comandos/swl/nuevo-proyecto.md +24 -2
  11. package/gateway/adapters/base.js +109 -0
  12. package/gateway/adapters/discord.js +167 -0
  13. package/gateway/adapters/email.js +221 -0
  14. package/gateway/adapters/slack.js +192 -0
  15. package/gateway/adapters/telegram.js +183 -0
  16. package/gateway/adapters/webhook.js +113 -0
  17. package/gateway/adapters/whatsapp.js +214 -0
  18. package/gateway/agent-executor.js +322 -0
  19. package/gateway/command-relay.js +271 -0
  20. package/gateway/cron/jobs.js +263 -0
  21. package/gateway/cron/scheduler.js +322 -0
  22. package/gateway/cron/store.js +335 -0
  23. package/gateway/index.js +320 -0
  24. package/gateway/lib/event-channel.js +191 -0
  25. package/gateway/session.js +131 -0
  26. package/gateway/webhook-server.js +324 -0
  27. package/habilidades/backend-production-resilience/SKILL.md +288 -288
  28. package/habilidades/benchmark-memoria/SKILL.md +186 -186
  29. package/habilidades/build-errors-nextjs/SKILL.md +55 -1
  30. package/habilidades/diagrama-arquitectura/assets/template.html +276 -276
  31. package/habilidades/doubt-driven-review/SKILL.md +171 -171
  32. package/habilidades/doubt-driven-review/recursos/EXAMPLES.md +130 -130
  33. package/habilidades/eval-framework/SKILL.md +212 -212
  34. package/habilidades/extractor-de-aprendizajes/SKILL.md +20 -10
  35. package/habilidades/harness-claude-code/SKILL.md +299 -299
  36. package/habilidades/infra-github-actions/SKILL.md +166 -166
  37. package/habilidades/legacy-code-rescue/SKILL.md +267 -267
  38. package/habilidades/manejo-errores/.evolved.json +8 -8
  39. package/habilidades/meta-skills-estandar/recursos/convencion-examples.md +93 -93
  40. package/habilidades/meta-skills-estandar/recursos/skills-as-agents.md +163 -163
  41. package/habilidades/nextjs-testing/SKILL.md +89 -5
  42. package/habilidades/node-experto/SKILL.md +37 -1
  43. package/habilidades/patrones-python/SKILL.md +229 -229
  44. package/habilidades/patrones-python/recursos/patrones-avanzados.md +469 -469
  45. package/habilidades/planear-fase/SKILL.md +319 -319
  46. package/habilidades/react-experto/SKILL.md +45 -4
  47. package/habilidades/release-semver/.evolved.json +8 -8
  48. package/habilidades/tdd-workflow/SKILL.md +36 -4
  49. package/habilidades/testing-python/SKILL.md +340 -340
  50. package/hooks/claudemd-bloat-detector.js +161 -161
  51. package/hooks/inyeccion-contexto.js +8 -3
  52. package/hooks/lib/agent-routing.js +107 -107
  53. package/hooks/lib/auto-consolidator.js +335 -335
  54. package/hooks/lib/error-classifier.js +308 -308
  55. package/hooks/lib/merkle-audit.js +96 -96
  56. package/hooks/lib/provenance-tracker.js +191 -191
  57. package/hooks/lib/rate-limit-ip.js +177 -0
  58. package/hooks/lib/rate-limit-tracker.js +253 -253
  59. package/hooks/lib/resource-quota.js +122 -122
  60. package/hooks/lib/retry-jitter.js +165 -165
  61. package/hooks/lib/skill-auditor.js +588 -588
  62. package/hooks/lib/sync-status.js +228 -228
  63. package/hooks/lib/taint-tracker.js +107 -107
  64. package/hooks/lib/text-similarity.js +241 -241
  65. package/hooks/lib/toon-compressor.js +245 -245
  66. package/hooks/lib/webhook-dedup.js +184 -0
  67. package/hooks/lib/webhook-verify.js +123 -0
  68. package/hooks/proteccion-rutas.js +120 -15
  69. package/hooks/registro-turnos.js +209 -209
  70. package/hooks/sugerir-regenerar-inventario.js +170 -170
  71. package/hooks/validar-formato-post-subagente.js +140 -140
  72. package/hooks/validar-memoria-hook.js +218 -218
  73. package/instintos/prompt-appendices.yaml +57 -57
  74. package/manifiestos/agent-output-schemas.json +57 -57
  75. package/manifiestos/modulos.json +1 -0
  76. package/manifiestos/skills-lock.json +34 -34
  77. package/package.json +5 -3
  78. package/plantillas/auditor-veto-template.md +105 -105
  79. package/plantillas/github-workflows/README.md +47 -47
  80. package/plantillas/github-workflows/release-please.yml +44 -44
  81. package/plantillas/github-workflows/swl-ci.yml +107 -107
  82. package/plantillas/github-workflows/swl-security.yml +51 -51
  83. package/plugin.json +1 -1
  84. package/reglas/analisis-previo-tareas-grandes.md +172 -172
  85. package/reglas/arreglar-al-detectar.md +147 -147
  86. package/reglas/fragmentos-compartidos.md +152 -152
  87. package/reglas/harness-claude-code.md +213 -213
  88. package/reglas/usar-context7.md +226 -226
  89. package/reglas/usar-sistema-swl.md +251 -0
  90. package/schemas/diary-entry.schema.json +80 -80
  91. package/scripts/benchmark-memoria.js +167 -167
  92. package/scripts/comandos/skills.js +251 -2
  93. package/scripts/configurar-branch-protection.js +418 -418
  94. package/scripts/detectar-aprendizajes-duplicados.js +151 -151
  95. package/scripts/field-report.js +199 -199
  96. package/scripts/generar-checklists-consolidados.js +273 -273
  97. package/scripts/generar-inventario.js +420 -420
  98. package/scripts/generar-matriz-lenguajes.js +271 -271
  99. package/scripts/lib/artefactos-python.js +43 -43
  100. package/scripts/lib/benchmark-metrics.js +160 -160
  101. package/scripts/lib/budget-enforcer.js +252 -252
  102. package/scripts/lib/configurar-ci.js +380 -380
  103. package/scripts/lib/contadores-inventario.js +217 -217
  104. package/scripts/lib/detectar-stack-detallado.js +307 -307
  105. package/scripts/lib/diary-entry.js +234 -234
  106. package/scripts/lib/eval-metrics-store.js +218 -218
  107. package/scripts/lib/eval-quality.js +171 -171
  108. package/scripts/lib/eval-schemas.js +144 -144
  109. package/scripts/lib/eval-self-correct.js +106 -106
  110. package/scripts/lib/eval-validator.js +185 -185
  111. package/scripts/lib/jaccard-similarity.js +98 -98
  112. package/scripts/lib/longmemeval-runner.js +125 -125
  113. package/scripts/lib/npm-version.js +261 -261
  114. package/scripts/lib/paquetes-conocidos.js +50 -50
  115. package/scripts/lib/prompt-builder.js +264 -264
  116. package/scripts/lib/rrf-fusion.js +175 -175
  117. package/scripts/lib/scoring-instintos.js +277 -277
  118. package/scripts/lib/semantic-search.js +252 -252
  119. package/scripts/limpiar-artefactos-python.js +131 -131
  120. package/scripts/mcp-server/README.md +128 -128
  121. package/scripts/mcp-server/handlers.js +206 -206
  122. package/scripts/migrar-csv-a-array.js +168 -168
  123. package/scripts/migrar-fase-dominio.js +201 -201
  124. package/scripts/publicar.js +511 -511
  125. package/scripts/run-eval.js +141 -141
  126. package/scripts/validar-manifest.js +195 -195
  127. package/scripts/validar-userland-vacio.js +110 -110
  128. package/scripts/verificar-release.js +110 -0
@@ -1,511 +1,511 @@
1
- #!/usr/bin/env node
2
- // publicar.js — Dual-publish: npmjs.org (canónico) + GitHub Packages (mirror)
3
- // Uso: node scripts/publicar.js [--solo-github | --solo-npmjs | --dry-run]
4
- //
5
- // Tras el rebrand v1.0.0 (rebranding de @saulwadeleon/swl-software-engineering-system):
6
- // - npmjs.org → @saulwade/swl-ses (canónico, scope sin guion)
7
- // - GH Packages → @saul-wade/swl-ses (mirror, scope con guion = org GitHub)
8
- //
9
- // El package.json del repo declara el nombre canónico (npmjs). Para GitHub
10
- // Packages se genera un package.json temporal con scope/registry sobreescritos.
11
- // Exit: 0=éxito, 1=error
12
-
13
- 'use strict';
14
-
15
- const { execFileSync, spawnSync } = require('child_process');
16
- const fs = require('fs');
17
- const path = require('path');
18
- const os = require('os');
19
- const readline = require('readline');
20
-
21
- const {
22
- NOMBRE_GITHUB_MIRROR: GITHUB_NAME,
23
- REGISTRY_GITHUB: GITHUB_REGISTRY,
24
- REGISTRY_NPMJS: NPMJS_REGISTRY,
25
- } = require('./lib/paquetes-conocidos');
26
-
27
- const {
28
- esDirArtefacto,
29
- esArchivoArtefacto,
30
- } = require('./lib/artefactos-python');
31
-
32
- const ROOT = path.resolve(__dirname, '..');
33
- const PKG_PATH = path.join(ROOT, 'package.json');
34
-
35
- /**
36
- * Resuelve el binario de npm a invocar. Usa node + npm-cli.js cuando es
37
- * posible para evitar shell:true en Windows (mismo enfoque que la lib
38
- * scripts/lib/npm-version.js).
39
- */
40
- const NPM_CLI_JS = (() => {
41
- const dir = path.dirname(process.execPath);
42
- for (const c of [
43
- path.join(dir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
44
- path.join(dir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
45
- ]) {
46
- try { if (fs.existsSync(c)) return c; } catch { /* continuar */ }
47
- }
48
- return null;
49
- })();
50
-
51
- function npmExec(args, opts = {}) {
52
- const defaults = { cwd: ROOT, stdio: 'inherit', timeout: 300_000 };
53
- if (NPM_CLI_JS) {
54
- return execFileSync(process.execPath, [NPM_CLI_JS, ...args], { ...defaults, ...opts });
55
- }
56
- // Fallback a npm/npm.cmd como ejecutable nombrado. NO usamos shell:true:
57
- // todos los args son strings ya separados, sin interpolación.
58
- const bin = process.platform === 'win32' ? 'npm.cmd' : 'npm';
59
- return execFileSync(bin, args, { ...defaults, ...opts });
60
- }
61
-
62
- /**
63
- * Variante de npmExec que captura stderr para detección posterior de errores
64
- * estructurados (ej: EOTP). stdout sigue heredado (live-streaming visible al
65
- * usuario) y stderr se pipea a un buffer + se ecoa a process.stderr al final.
66
- *
67
- * Retorna { status, stderr } sin lanzar — el caller inspecciona el status.
68
- */
69
- function npmSpawnCaptureStderr(args, opts = {}) {
70
- const defaults = { cwd: ROOT, timeout: 300_000, env: process.env };
71
- const merged = { ...defaults, ...opts, stdio: ['inherit', 'inherit', 'pipe'] };
72
- let res;
73
- if (NPM_CLI_JS) {
74
- res = spawnSync(process.execPath, [NPM_CLI_JS, ...args], merged);
75
- } else {
76
- const bin = process.platform === 'win32' ? 'npm.cmd' : 'npm';
77
- res = spawnSync(bin, args, merged);
78
- }
79
- const stderr = res.stderr ? String(res.stderr) : '';
80
- // Ecoar stderr al usuario para que vea el diagnóstico de npm aunque se capturó.
81
- if (stderr) process.stderr.write(stderr);
82
- return { status: res.status, signal: res.signal, stderr, error: res.error };
83
- }
84
-
85
- /**
86
- * Detecta si un blob de stderr indica que npm requiere una OTP de 2FA.
87
- * Match defensivo: cubre tanto el código de error explícito (`EOTP`) como el
88
- * mensaje canónico ("requires a one-time password"), porque npm ha cambiado
89
- * el formato del mensaje entre versiones (npm 8 vs 10+).
90
- *
91
- * Pura — testeable sin dependencias. Exportada para tests.
92
- */
93
- function esErrorOTP(stderr) {
94
- if (!stderr) return false;
95
- const blob = String(stderr);
96
- return /\bEOTP\b/.test(blob) || /one-time password/i.test(blob);
97
- }
98
-
99
- /**
100
- * Solicita una OTP al usuario via stdin (readline síncrono).
101
- * Retorna la OTP normalizada (string de 6 dígitos) o null si:
102
- * - stdin no es TTY (CI, pipe) → no se puede pedir interactivamente
103
- * - el usuario cancela con Ctrl+C / línea vacía
104
- * - el formato no es válido (no 6 dígitos)
105
- *
106
- * El caller debe manejar el caso null como "fallback a guía manual".
107
- */
108
- function solicitarOTPInteractiva() {
109
- if (!process.stdin.isTTY) {
110
- process.stderr.write('[publicar] stdin no es TTY: no se puede pedir OTP interactivamente.\n');
111
- process.stderr.write('[publicar] Configurar NPM_CONFIG_OTP=<otp> antes de invocar este script.\n');
112
- return null;
113
- }
114
-
115
- const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
116
- // questionSync vía promesa NO funciona aquí — el script es síncrono.
117
- // Usamos un truco: readline es async-only, así que dejamos al usuario
118
- // el manejo bloqueante leyendo línea por línea con readSync vía read-int.
119
- // Pero readline no expone modo síncrono. Alternativa: leer de stdin con
120
- // fd 0 + readSync. En Windows con TTY funciona; en Linux idem.
121
- try {
122
- process.stderr.write('\nNPM requiere OTP (2FA). Ingresa el código de 6 dígitos de tu autenticador: ');
123
- const otp = leerLineaStdinSync().trim();
124
- rl.close();
125
- if (!/^\d{6}$/.test(otp)) {
126
- process.stderr.write(`\n[publicar] OTP inválida: se esperan 6 dígitos, se recibió: "${otp.slice(0, 20)}"\n`);
127
- return null;
128
- }
129
- return otp;
130
- } catch (err) {
131
- rl.close();
132
- process.stderr.write(`\n[publicar] error leyendo OTP: ${String(err.message).slice(0, 120)}\n`);
133
- return null;
134
- }
135
- }
136
-
137
- /**
138
- * Lectura síncrona de una línea de stdin. Necesario porque readline solo
139
- * expone API asíncrona, pero todo el flujo de publicar.js es síncrono.
140
- * Lee byte por byte hasta encontrar newline o EOF.
141
- */
142
- function leerLineaStdinSync() {
143
- const BUFFER_SIZE = 256;
144
- const buf = Buffer.alloc(BUFFER_SIZE);
145
- let line = '';
146
- while (true) {
147
- let bytesRead;
148
- try {
149
- bytesRead = fs.readSync(0, buf, 0, BUFFER_SIZE, null);
150
- } catch (err) {
151
- // EAGAIN en stdin no-bloqueante: poco común en TTY, pero defensivo.
152
- if (err.code === 'EAGAIN') continue;
153
- throw err;
154
- }
155
- if (bytesRead === 0) break; // EOF
156
- const chunk = buf.slice(0, bytesRead).toString('utf-8');
157
- const nlIdx = chunk.indexOf('\n');
158
- if (nlIdx >= 0) {
159
- line += chunk.slice(0, nlIdx);
160
- break;
161
- }
162
- line += chunk;
163
- }
164
- return line.replace(/\r$/, ''); // Windows envía \r\n
165
- }
166
-
167
- /**
168
- * Ejecuta un `npm publish` con soporte automático de OTP (2FA).
169
- *
170
- * Flujo:
171
- * 1. Si NPM_CONFIG_OTP está definida en el entorno, se usa directamente
172
- * (npm la lee automáticamente — no hay que pasarla como flag).
173
- * 2. Si el publish falla con EOTP/one-time password, se intenta solicitar
174
- * la OTP interactivamente y reintentar con --ignore-scripts (los
175
- * scripts pre-publish ya pasaron en el primer intento).
176
- * 3. Si no se puede obtener OTP (no TTY o usuario cancela), reporta el
177
- * error y retorna false.
178
- *
179
- * Retorna: { ok: boolean, stderr: string }
180
- *
181
- * Exportada para tests.
182
- */
183
- function ejecutarPublishConOTP(args, opts = {}) {
184
- // Intento 1: con OTP de env si existe, sin ella si no.
185
- const intento1 = npmSpawnCaptureStderr(args, opts);
186
- if (intento1.status === 0) {
187
- return { ok: true, stderr: intento1.stderr };
188
- }
189
-
190
- // Si NO es EOTP, no podemos hacer nada — propagamos el fallo.
191
- if (!esErrorOTP(intento1.stderr)) {
192
- return { ok: false, stderr: intento1.stderr };
193
- }
194
-
195
- // Es EOTP. Intentar solicitar OTP interactivamente.
196
- process.stderr.write('\n[publicar] npm rechazó el publish con EOTP (2FA requerida).\n');
197
- const otp = solicitarOTPInteractiva();
198
- if (!otp) {
199
- process.stderr.write('[publicar] No se obtuvo OTP. Aborta. Reintentar con:\n');
200
- process.stderr.write(` NPM_CONFIG_OTP=<otp> node scripts/publicar.js ${process.argv.slice(2).join(' ')}\n`);
201
- return { ok: false, stderr: intento1.stderr };
202
- }
203
-
204
- // Reintento con OTP via env + --ignore-scripts (los pre-publish ya pasaron).
205
- process.stderr.write('\n[publicar] Reintentando publish con OTP recibida...\n');
206
- const envConOTP = { ...(opts.env || process.env), NPM_CONFIG_OTP: otp };
207
- const argsConIgnore = args.includes('--ignore-scripts') ? args : [...args, '--ignore-scripts'];
208
- const intento2 = npmSpawnCaptureStderr(argsConIgnore, { ...opts, env: envConOTP });
209
- if (intento2.status === 0) {
210
- return { ok: true, stderr: intento2.stderr };
211
- }
212
- return { ok: false, stderr: intento2.stderr };
213
- }
214
-
215
- function leerPkg() {
216
- return JSON.parse(fs.readFileSync(PKG_PATH, 'utf-8'));
217
- }
218
-
219
- /**
220
- * Clasifica el error de `npm whoami` capturado en stderr para distinguir
221
- * causas raíz comunes y permitir mensajes accionables al usuario.
222
- *
223
- * Tipos retornados:
224
- * - 'no-token' : no hay _authToken configurado para ese registry.
225
- * - 'token-401' : token presente pero rechazado (expirado/revocado).
226
- * - 'token-403' : token válido pero sin permiso (cuenta sin acceso al scope).
227
- * - 'registry-404' : el registry no responde el endpoint whoami (URL incorrecta).
228
- * - 'desconocido' : error de red, npm no en PATH, timeout, etc.
229
- */
230
- function clasificarErrorAuth(stderr, mensaje) {
231
- const blob = (stderr || '') + '\n' + (mensaje || '');
232
- if (/\b401\b/.test(blob)) return 'token-401';
233
- if (/\b403\b/.test(blob)) return 'token-403';
234
- if (/\b404\b/.test(blob)) return 'registry-404';
235
- if (/ENEEDAUTH|need to authorize/i.test(blob)) return 'no-token';
236
- return 'desconocido';
237
- }
238
-
239
- function verificarLogin(registry) {
240
- try {
241
- const result = npmExec(['whoami', `--registry=${registry}`], {
242
- stdio: ['pipe', 'pipe', 'pipe'],
243
- encoding: 'utf-8',
244
- timeout: 15_000,
245
- });
246
- return { ok: true, usuario: String(result).trim() };
247
- } catch (err) {
248
- // Capturar stderr del proceso para clasificar la causa raíz.
249
- // execFileSync expone stderr en err.stderr cuando stdio fue 'pipe'.
250
- const stderrBuf = err.stderr ? String(err.stderr) : '';
251
- const motivo = (stderrBuf || String(err.message || err)).split('\n')[0].slice(0, 200);
252
- const tipo = clasificarErrorAuth(stderrBuf, err.message);
253
- process.stderr.write(`[verificarLogin ${registry}] ${tipo}: ${motivo}\n`);
254
- return { ok: false, tipo, motivo };
255
- }
256
- }
257
-
258
- /**
259
- * Imprime guía accionable según el tipo de fallo de auth y el registry.
260
- * Se llama desde publicarNpmjs/publicarGitHub cuando verificarLogin falla.
261
- */
262
- function imprimirGuiaAuth(registry, pkgName, tipo) {
263
- const esGithub = /npm\.pkg\.github\.com/.test(registry);
264
- console.error('');
265
- switch (tipo) {
266
- case 'token-401':
267
- console.error('CAUSA: token de autenticación rechazado (HTTP 401 — expirado o revocado).');
268
- if (esGithub) {
269
- console.error('FIX: GitHub Packages NO acepta `npm login`. Genera un nuevo PAT en');
270
- console.error(' Settings → Developer settings → Personal access tokens (classic)');
271
- console.error(' con scopes `read:packages` y `write:packages`, y reemplaza la línea');
272
- console.error(' en ~/.npmrc:');
273
- console.error(' //npm.pkg.github.com/:_authToken=<NUEVO_TOKEN>');
274
- } else {
275
- console.error(`FIX: npm login --registry=${registry}`);
276
- console.error(' (abrirá el navegador para autenticar y reescribirá ~/.npmrc).');
277
- }
278
- break;
279
- case 'token-403':
280
- console.error('CAUSA: token válido pero la cuenta no tiene permiso al scope del paquete.');
281
- console.error(`FIX: Verifica el dueño del scope con:`);
282
- console.error(` npm owner ls ${pkgName} --registry=${registry}`);
283
- console.error(' Si la cuenta autenticada no aparece como owner, autenticate con la');
284
- console.error(' cuenta dueña del scope o pide ser agregado como maintainer.');
285
- break;
286
- case 'registry-404':
287
- console.error('CAUSA: el registry no responde el endpoint whoami (URL probablemente incorrecta).');
288
- console.error(`FIX: Verifica que la URL sea exactamente '${registry}' (incluyendo https:// y trailing slash si aplica).`);
289
- break;
290
- case 'no-token':
291
- console.error('CAUSA: no hay token configurado para este registry en ~/.npmrc.');
292
- if (esGithub) {
293
- console.error('FIX: GitHub Packages requiere PAT manual. Agrega a ~/.npmrc:');
294
- console.error(' //npm.pkg.github.com/:_authToken=<TU_PAT>');
295
- console.error(' (NO uses `npm login` con GitHub Packages — devuelve 404/403.)');
296
- } else {
297
- console.error(`FIX: npm login --registry=${registry}`);
298
- }
299
- break;
300
- default:
301
- console.error('CAUSA: error desconocido al consultar whoami.');
302
- console.error(' Verifica conectividad de red y que `npm` esté en PATH.');
303
- console.error(` Para diagnóstico manual: npm whoami --registry=${registry}`);
304
- }
305
- console.error('');
306
- }
307
-
308
- function copiarDir(src, dest) {
309
- fs.mkdirSync(dest, { recursive: true });
310
- const entries = fs.readdirSync(src, { withFileTypes: true });
311
- for (const entry of entries) {
312
- // Excluir artefactos Python (lista canónica en lib/artefactos-python.js)
313
- if (entry.isDirectory() && esDirArtefacto(entry.name)) continue;
314
- if (!entry.isDirectory() && esArchivoArtefacto(entry.name)) continue;
315
-
316
- const srcPath = path.join(src, entry.name);
317
- const destPath = path.join(dest, entry.name);
318
- if (entry.isDirectory()) {
319
- copiarDir(srcPath, destPath);
320
- } else {
321
- fs.copyFileSync(srcPath, destPath);
322
- }
323
- }
324
- }
325
-
326
- function limpiar(tmpDir) {
327
- try {
328
- fs.rmSync(tmpDir, { recursive: true, force: true });
329
- } catch (err) {
330
- // En Windows el filesystem puede mantener locks transitorios sobre
331
- // archivos recién copiados. La limpieza eventual la hace el SO; no
332
- // bloqueamos el flujo, pero anotamos el path para inspección manual.
333
- process.stderr.write(`[publicar] no se pudo limpiar tmpDir ${tmpDir}: ${String(err.message).slice(0, 80)}\n`);
334
- }
335
- }
336
-
337
- function construirPkgTemporal(pkg, nombre, registry) {
338
- const tmpPkg = {
339
- ...pkg,
340
- name: nombre,
341
- publishConfig: { registry, access: 'public' },
342
- };
343
- // Quitar prepack/prepublishOnly del temp para evitar recursión.
344
- // npm publish desde tmpDir invocaría prepack apuntando a un script
345
- // que ya no existe en ese path, y prepublishOnly correría el suite
346
- // de tests dos veces.
347
- if (tmpPkg.scripts) {
348
- delete tmpPkg.scripts.prepack;
349
- delete tmpPkg.scripts.prepublishOnly;
350
- }
351
- return tmpPkg;
352
- }
353
-
354
- function copiarArchivosFiles(pkg, tmpDir) {
355
- for (const entry of pkg.files || []) {
356
- const src = path.join(ROOT, entry);
357
- const dest = path.join(tmpDir, entry);
358
- if (!fs.existsSync(src)) continue;
359
- if (fs.statSync(src).isDirectory()) {
360
- copiarDir(src, dest);
361
- } else {
362
- fs.mkdirSync(path.dirname(dest), { recursive: true });
363
- fs.copyFileSync(src, dest);
364
- }
365
- }
366
- }
367
-
368
- function copiarMetadatos(tmpDir) {
369
- // npm incluye README/LICENSE/CHANGELOG automáticamente al publicar.
370
- for (const meta of ['README.md', 'LICENSE', 'CHANGELOG.md', 'CHANGELOG-LEGACY.md']) {
371
- const src = path.join(ROOT, meta);
372
- if (fs.existsSync(src)) {
373
- fs.copyFileSync(src, path.join(tmpDir, meta));
374
- }
375
- }
376
- }
377
-
378
- function prepararDirectorioTemporal(pkg, nombrePersonalizado, registryPersonalizado) {
379
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'swl-ses-publish-'));
380
- const tmpPkg = construirPkgTemporal(pkg, nombrePersonalizado, registryPersonalizado);
381
- fs.writeFileSync(path.join(tmpDir, 'package.json'), JSON.stringify(tmpPkg, null, 2));
382
- copiarArchivosFiles(pkg, tmpDir);
383
- copiarMetadatos(tmpDir);
384
- return tmpDir;
385
- }
386
-
387
- function publicarNpmjs(pkg, dryRun) {
388
- console.log('\n=== 1/2 — npmjs.org (canónico) ===');
389
- console.log(`Paquete: ${pkg.name}@${pkg.version}`);
390
- console.log(`Registry: ${NPMJS_REGISTRY}`);
391
-
392
- const auth = verificarLogin(NPMJS_REGISTRY);
393
- if (!auth.ok) {
394
- console.error(`ERROR: No autenticado en npmjs (${auth.tipo}).`);
395
- imprimirGuiaAuth(NPMJS_REGISTRY, pkg.name, auth.tipo);
396
- return false;
397
- }
398
- console.log(`Autenticado como: ${auth.usuario}`);
399
-
400
- const args = ['publish', `--registry=${NPMJS_REGISTRY}`, '--access', 'public'];
401
- if (dryRun) args.push('--dry-run');
402
- const resultado = ejecutarPublishConOTP(args);
403
- if (resultado.ok) {
404
- console.log(`${dryRun ? '[DRY-RUN] ' : ''}OK: ${pkg.name}@${pkg.version} ${dryRun ? 'se publicaría' : 'publicado'} en npmjs`);
405
- return true;
406
- }
407
- console.error(`ERROR publicando en npmjs (ver stderr arriba para diagnóstico de npm).`);
408
- return false;
409
- }
410
-
411
- function publicarGitHub(pkg, dryRun) {
412
- console.log('\n=== 2/2 — GitHub Packages (mirror) ===');
413
- console.log(`Paquete: ${GITHUB_NAME}@${pkg.version}`);
414
- console.log(`Registry: ${GITHUB_REGISTRY}`);
415
-
416
- const auth = verificarLogin(GITHUB_REGISTRY);
417
- if (!auth.ok) {
418
- console.error(`ERROR: No autenticado en GitHub Packages (${auth.tipo}).`);
419
- imprimirGuiaAuth(GITHUB_REGISTRY, GITHUB_NAME, auth.tipo);
420
- return false;
421
- }
422
- console.log(`Autenticado como: ${auth.usuario}`);
423
-
424
- const tmpDir = prepararDirectorioTemporal(pkg, GITHUB_NAME, GITHUB_REGISTRY);
425
-
426
- const args = ['publish', `--registry=${GITHUB_REGISTRY}`];
427
- if (dryRun) args.push('--dry-run');
428
- const resultado = ejecutarPublishConOTP(args, { cwd: tmpDir });
429
- if (resultado.ok) {
430
- console.log(`${dryRun ? '[DRY-RUN] ' : ''}OK: ${GITHUB_NAME}@${pkg.version} ${dryRun ? 'se publicaría' : 'publicado'} en GitHub Packages`);
431
- if (dryRun) console.log(`Directorio temporal: ${tmpDir}`);
432
- limpiar(tmpDir);
433
- return true;
434
- }
435
- console.error(`ERROR publicando en GitHub Packages (ver stderr arriba para diagnóstico de npm).`);
436
- limpiar(tmpDir);
437
- return false;
438
- }
439
-
440
- function parsearArgs(argv) {
441
- return {
442
- dryRun: argv.includes('--dry-run'),
443
- soloGithub: argv.includes('--solo-github'),
444
- soloNpmjs: argv.includes('--solo-npmjs'),
445
- };
446
- }
447
-
448
- function imprimirEncabezado(version, dryRun) {
449
- console.log('╔══════════════════════════════════════════╗');
450
- console.log('║ @saulwade/swl-ses — Publicación dual ║');
451
- console.log('╚══════════════════════════════════════════╝');
452
- console.log(`Versión: ${version}`);
453
- if (dryRun) console.log('Modo: DRY-RUN (sin cambios)');
454
- }
455
-
456
- function avisarSiHayCambiosSinCommit(dryRun) {
457
- try {
458
- const status = String(execFileSync('git', ['status', '--porcelain'], {
459
- cwd: ROOT, encoding: 'utf-8',
460
- })).trim();
461
- if (status && !dryRun) {
462
- console.warn('\nADVERTENCIA: Hay cambios sin commitear.');
463
- console.warn('Se recomienda hacer commit antes de publicar.\n');
464
- }
465
- } catch (err) {
466
- // No es repo git, o git no está disponible. No bloquea el publish.
467
- process.stderr.write(`[publicar] git status falló (no bloqueante): ${String(err.message).slice(0, 80)}\n`);
468
- }
469
- }
470
-
471
- function reportarResultado(pkg, opts, npmjsOk, githubOk) {
472
- console.log('\n=== Resultado ===');
473
- if (!opts.soloGithub) console.log(`npmjs.org: ${npmjsOk ? 'OK' : 'FALLÓ'} — ${pkg.name}@${pkg.version}`);
474
- if (!opts.soloNpmjs) console.log(`GitHub Packages: ${githubOk ? 'OK' : 'FALLÓ'} — ${GITHUB_NAME}@${pkg.version}`);
475
- if (!opts.soloNpmjs && !opts.soloGithub && npmjsOk && githubOk) {
476
- console.log('\nAmbos paquetes publicados. Los usuarios pueden instalar con:');
477
- console.log(` npx ${pkg.name}@latest doctor (canónico, recomendado)`);
478
- console.log(` npx ${GITHUB_NAME}@latest doctor (mirror GitHub Packages)`);
479
- }
480
- }
481
-
482
- function main() {
483
- const opts = parsearArgs(process.argv.slice(2));
484
- const pkg = leerPkg();
485
- imprimirEncabezado(pkg.version, opts.dryRun);
486
- avisarSiHayCambiosSinCommit(opts.dryRun);
487
-
488
- const npmjsOk = opts.soloGithub ? true : publicarNpmjs(pkg, opts.dryRun);
489
- const githubOk = opts.soloNpmjs ? true : publicarGitHub(pkg, opts.dryRun);
490
-
491
- reportarResultado(pkg, opts, npmjsOk, githubOk);
492
- process.exit(npmjsOk && githubOk ? 0 : 1);
493
- }
494
-
495
- // Solo ejecutar el flujo completo cuando el script se invoca como CLI.
496
- // Cuando se importa como módulo (tests), expone helpers para verificar
497
- // invariantes de prepararDirectorioTemporal sin hacer publish real.
498
- if (require.main === module) {
499
- main();
500
- } else {
501
- module.exports = {
502
- prepararDirectorioTemporal,
503
- copiarDir,
504
- limpiar,
505
- esErrorOTP,
506
- ejecutarPublishConOTP,
507
- GITHUB_NAME,
508
- GITHUB_REGISTRY,
509
- NPMJS_REGISTRY,
510
- };
511
- }
1
+ #!/usr/bin/env node
2
+ // publicar.js — Dual-publish: npmjs.org (canónico) + GitHub Packages (mirror)
3
+ // Uso: node scripts/publicar.js [--solo-github | --solo-npmjs | --dry-run]
4
+ //
5
+ // Tras el rebrand v1.0.0 (rebranding de @saulwadeleon/swl-software-engineering-system):
6
+ // - npmjs.org → @saulwade/swl-ses (canónico, scope sin guion)
7
+ // - GH Packages → @saul-wade/swl-ses (mirror, scope con guion = org GitHub)
8
+ //
9
+ // El package.json del repo declara el nombre canónico (npmjs). Para GitHub
10
+ // Packages se genera un package.json temporal con scope/registry sobreescritos.
11
+ // Exit: 0=éxito, 1=error
12
+
13
+ 'use strict';
14
+
15
+ const { execFileSync, spawnSync } = require('child_process');
16
+ const fs = require('fs');
17
+ const path = require('path');
18
+ const os = require('os');
19
+ const readline = require('readline');
20
+
21
+ const {
22
+ NOMBRE_GITHUB_MIRROR: GITHUB_NAME,
23
+ REGISTRY_GITHUB: GITHUB_REGISTRY,
24
+ REGISTRY_NPMJS: NPMJS_REGISTRY,
25
+ } = require('./lib/paquetes-conocidos');
26
+
27
+ const {
28
+ esDirArtefacto,
29
+ esArchivoArtefacto,
30
+ } = require('./lib/artefactos-python');
31
+
32
+ const ROOT = path.resolve(__dirname, '..');
33
+ const PKG_PATH = path.join(ROOT, 'package.json');
34
+
35
+ /**
36
+ * Resuelve el binario de npm a invocar. Usa node + npm-cli.js cuando es
37
+ * posible para evitar shell:true en Windows (mismo enfoque que la lib
38
+ * scripts/lib/npm-version.js).
39
+ */
40
+ const NPM_CLI_JS = (() => {
41
+ const dir = path.dirname(process.execPath);
42
+ for (const c of [
43
+ path.join(dir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
44
+ path.join(dir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
45
+ ]) {
46
+ try { if (fs.existsSync(c)) return c; } catch { /* continuar */ }
47
+ }
48
+ return null;
49
+ })();
50
+
51
+ function npmExec(args, opts = {}) {
52
+ const defaults = { cwd: ROOT, stdio: 'inherit', timeout: 300_000 };
53
+ if (NPM_CLI_JS) {
54
+ return execFileSync(process.execPath, [NPM_CLI_JS, ...args], { ...defaults, ...opts });
55
+ }
56
+ // Fallback a npm/npm.cmd como ejecutable nombrado. NO usamos shell:true:
57
+ // todos los args son strings ya separados, sin interpolación.
58
+ const bin = process.platform === 'win32' ? 'npm.cmd' : 'npm';
59
+ return execFileSync(bin, args, { ...defaults, ...opts });
60
+ }
61
+
62
+ /**
63
+ * Variante de npmExec que captura stderr para detección posterior de errores
64
+ * estructurados (ej: EOTP). stdout sigue heredado (live-streaming visible al
65
+ * usuario) y stderr se pipea a un buffer + se ecoa a process.stderr al final.
66
+ *
67
+ * Retorna { status, stderr } sin lanzar — el caller inspecciona el status.
68
+ */
69
+ function npmSpawnCaptureStderr(args, opts = {}) {
70
+ const defaults = { cwd: ROOT, timeout: 300_000, env: process.env };
71
+ const merged = { ...defaults, ...opts, stdio: ['inherit', 'inherit', 'pipe'] };
72
+ let res;
73
+ if (NPM_CLI_JS) {
74
+ res = spawnSync(process.execPath, [NPM_CLI_JS, ...args], merged);
75
+ } else {
76
+ const bin = process.platform === 'win32' ? 'npm.cmd' : 'npm';
77
+ res = spawnSync(bin, args, merged);
78
+ }
79
+ const stderr = res.stderr ? String(res.stderr) : '';
80
+ // Ecoar stderr al usuario para que vea el diagnóstico de npm aunque se capturó.
81
+ if (stderr) process.stderr.write(stderr);
82
+ return { status: res.status, signal: res.signal, stderr, error: res.error };
83
+ }
84
+
85
+ /**
86
+ * Detecta si un blob de stderr indica que npm requiere una OTP de 2FA.
87
+ * Match defensivo: cubre tanto el código de error explícito (`EOTP`) como el
88
+ * mensaje canónico ("requires a one-time password"), porque npm ha cambiado
89
+ * el formato del mensaje entre versiones (npm 8 vs 10+).
90
+ *
91
+ * Pura — testeable sin dependencias. Exportada para tests.
92
+ */
93
+ function esErrorOTP(stderr) {
94
+ if (!stderr) return false;
95
+ const blob = String(stderr);
96
+ return /\bEOTP\b/.test(blob) || /one-time password/i.test(blob);
97
+ }
98
+
99
+ /**
100
+ * Solicita una OTP al usuario via stdin (readline síncrono).
101
+ * Retorna la OTP normalizada (string de 6 dígitos) o null si:
102
+ * - stdin no es TTY (CI, pipe) → no se puede pedir interactivamente
103
+ * - el usuario cancela con Ctrl+C / línea vacía
104
+ * - el formato no es válido (no 6 dígitos)
105
+ *
106
+ * El caller debe manejar el caso null como "fallback a guía manual".
107
+ */
108
+ function solicitarOTPInteractiva() {
109
+ if (!process.stdin.isTTY) {
110
+ process.stderr.write('[publicar] stdin no es TTY: no se puede pedir OTP interactivamente.\n');
111
+ process.stderr.write('[publicar] Configurar NPM_CONFIG_OTP=<otp> antes de invocar este script.\n');
112
+ return null;
113
+ }
114
+
115
+ const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
116
+ // questionSync vía promesa NO funciona aquí — el script es síncrono.
117
+ // Usamos un truco: readline es async-only, así que dejamos al usuario
118
+ // el manejo bloqueante leyendo línea por línea con readSync vía read-int.
119
+ // Pero readline no expone modo síncrono. Alternativa: leer de stdin con
120
+ // fd 0 + readSync. En Windows con TTY funciona; en Linux idem.
121
+ try {
122
+ process.stderr.write('\nNPM requiere OTP (2FA). Ingresa el código de 6 dígitos de tu autenticador: ');
123
+ const otp = leerLineaStdinSync().trim();
124
+ rl.close();
125
+ if (!/^\d{6}$/.test(otp)) {
126
+ process.stderr.write(`\n[publicar] OTP inválida: se esperan 6 dígitos, se recibió: "${otp.slice(0, 20)}"\n`);
127
+ return null;
128
+ }
129
+ return otp;
130
+ } catch (err) {
131
+ rl.close();
132
+ process.stderr.write(`\n[publicar] error leyendo OTP: ${String(err.message).slice(0, 120)}\n`);
133
+ return null;
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Lectura síncrona de una línea de stdin. Necesario porque readline solo
139
+ * expone API asíncrona, pero todo el flujo de publicar.js es síncrono.
140
+ * Lee byte por byte hasta encontrar newline o EOF.
141
+ */
142
+ function leerLineaStdinSync() {
143
+ const BUFFER_SIZE = 256;
144
+ const buf = Buffer.alloc(BUFFER_SIZE);
145
+ let line = '';
146
+ while (true) {
147
+ let bytesRead;
148
+ try {
149
+ bytesRead = fs.readSync(0, buf, 0, BUFFER_SIZE, null);
150
+ } catch (err) {
151
+ // EAGAIN en stdin no-bloqueante: poco común en TTY, pero defensivo.
152
+ if (err.code === 'EAGAIN') continue;
153
+ throw err;
154
+ }
155
+ if (bytesRead === 0) break; // EOF
156
+ const chunk = buf.slice(0, bytesRead).toString('utf-8');
157
+ const nlIdx = chunk.indexOf('\n');
158
+ if (nlIdx >= 0) {
159
+ line += chunk.slice(0, nlIdx);
160
+ break;
161
+ }
162
+ line += chunk;
163
+ }
164
+ return line.replace(/\r$/, ''); // Windows envía \r\n
165
+ }
166
+
167
+ /**
168
+ * Ejecuta un `npm publish` con soporte automático de OTP (2FA).
169
+ *
170
+ * Flujo:
171
+ * 1. Si NPM_CONFIG_OTP está definida en el entorno, se usa directamente
172
+ * (npm la lee automáticamente — no hay que pasarla como flag).
173
+ * 2. Si el publish falla con EOTP/one-time password, se intenta solicitar
174
+ * la OTP interactivamente y reintentar con --ignore-scripts (los
175
+ * scripts pre-publish ya pasaron en el primer intento).
176
+ * 3. Si no se puede obtener OTP (no TTY o usuario cancela), reporta el
177
+ * error y retorna false.
178
+ *
179
+ * Retorna: { ok: boolean, stderr: string }
180
+ *
181
+ * Exportada para tests.
182
+ */
183
+ function ejecutarPublishConOTP(args, opts = {}) {
184
+ // Intento 1: con OTP de env si existe, sin ella si no.
185
+ const intento1 = npmSpawnCaptureStderr(args, opts);
186
+ if (intento1.status === 0) {
187
+ return { ok: true, stderr: intento1.stderr };
188
+ }
189
+
190
+ // Si NO es EOTP, no podemos hacer nada — propagamos el fallo.
191
+ if (!esErrorOTP(intento1.stderr)) {
192
+ return { ok: false, stderr: intento1.stderr };
193
+ }
194
+
195
+ // Es EOTP. Intentar solicitar OTP interactivamente.
196
+ process.stderr.write('\n[publicar] npm rechazó el publish con EOTP (2FA requerida).\n');
197
+ const otp = solicitarOTPInteractiva();
198
+ if (!otp) {
199
+ process.stderr.write('[publicar] No se obtuvo OTP. Aborta. Reintentar con:\n');
200
+ process.stderr.write(` NPM_CONFIG_OTP=<otp> node scripts/publicar.js ${process.argv.slice(2).join(' ')}\n`);
201
+ return { ok: false, stderr: intento1.stderr };
202
+ }
203
+
204
+ // Reintento con OTP via env + --ignore-scripts (los pre-publish ya pasaron).
205
+ process.stderr.write('\n[publicar] Reintentando publish con OTP recibida...\n');
206
+ const envConOTP = { ...(opts.env || process.env), NPM_CONFIG_OTP: otp };
207
+ const argsConIgnore = args.includes('--ignore-scripts') ? args : [...args, '--ignore-scripts'];
208
+ const intento2 = npmSpawnCaptureStderr(argsConIgnore, { ...opts, env: envConOTP });
209
+ if (intento2.status === 0) {
210
+ return { ok: true, stderr: intento2.stderr };
211
+ }
212
+ return { ok: false, stderr: intento2.stderr };
213
+ }
214
+
215
+ function leerPkg() {
216
+ return JSON.parse(fs.readFileSync(PKG_PATH, 'utf-8'));
217
+ }
218
+
219
+ /**
220
+ * Clasifica el error de `npm whoami` capturado en stderr para distinguir
221
+ * causas raíz comunes y permitir mensajes accionables al usuario.
222
+ *
223
+ * Tipos retornados:
224
+ * - 'no-token' : no hay _authToken configurado para ese registry.
225
+ * - 'token-401' : token presente pero rechazado (expirado/revocado).
226
+ * - 'token-403' : token válido pero sin permiso (cuenta sin acceso al scope).
227
+ * - 'registry-404' : el registry no responde el endpoint whoami (URL incorrecta).
228
+ * - 'desconocido' : error de red, npm no en PATH, timeout, etc.
229
+ */
230
+ function clasificarErrorAuth(stderr, mensaje) {
231
+ const blob = (stderr || '') + '\n' + (mensaje || '');
232
+ if (/\b401\b/.test(blob)) return 'token-401';
233
+ if (/\b403\b/.test(blob)) return 'token-403';
234
+ if (/\b404\b/.test(blob)) return 'registry-404';
235
+ if (/ENEEDAUTH|need to authorize/i.test(blob)) return 'no-token';
236
+ return 'desconocido';
237
+ }
238
+
239
+ function verificarLogin(registry) {
240
+ try {
241
+ const result = npmExec(['whoami', `--registry=${registry}`], {
242
+ stdio: ['pipe', 'pipe', 'pipe'],
243
+ encoding: 'utf-8',
244
+ timeout: 15_000,
245
+ });
246
+ return { ok: true, usuario: String(result).trim() };
247
+ } catch (err) {
248
+ // Capturar stderr del proceso para clasificar la causa raíz.
249
+ // execFileSync expone stderr en err.stderr cuando stdio fue 'pipe'.
250
+ const stderrBuf = err.stderr ? String(err.stderr) : '';
251
+ const motivo = (stderrBuf || String(err.message || err)).split('\n')[0].slice(0, 200);
252
+ const tipo = clasificarErrorAuth(stderrBuf, err.message);
253
+ process.stderr.write(`[verificarLogin ${registry}] ${tipo}: ${motivo}\n`);
254
+ return { ok: false, tipo, motivo };
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Imprime guía accionable según el tipo de fallo de auth y el registry.
260
+ * Se llama desde publicarNpmjs/publicarGitHub cuando verificarLogin falla.
261
+ */
262
+ function imprimirGuiaAuth(registry, pkgName, tipo) {
263
+ const esGithub = /npm\.pkg\.github\.com/.test(registry);
264
+ console.error('');
265
+ switch (tipo) {
266
+ case 'token-401':
267
+ console.error('CAUSA: token de autenticación rechazado (HTTP 401 — expirado o revocado).');
268
+ if (esGithub) {
269
+ console.error('FIX: GitHub Packages NO acepta `npm login`. Genera un nuevo PAT en');
270
+ console.error(' Settings → Developer settings → Personal access tokens (classic)');
271
+ console.error(' con scopes `read:packages` y `write:packages`, y reemplaza la línea');
272
+ console.error(' en ~/.npmrc:');
273
+ console.error(' //npm.pkg.github.com/:_authToken=<NUEVO_TOKEN>');
274
+ } else {
275
+ console.error(`FIX: npm login --registry=${registry}`);
276
+ console.error(' (abrirá el navegador para autenticar y reescribirá ~/.npmrc).');
277
+ }
278
+ break;
279
+ case 'token-403':
280
+ console.error('CAUSA: token válido pero la cuenta no tiene permiso al scope del paquete.');
281
+ console.error(`FIX: Verifica el dueño del scope con:`);
282
+ console.error(` npm owner ls ${pkgName} --registry=${registry}`);
283
+ console.error(' Si la cuenta autenticada no aparece como owner, autenticate con la');
284
+ console.error(' cuenta dueña del scope o pide ser agregado como maintainer.');
285
+ break;
286
+ case 'registry-404':
287
+ console.error('CAUSA: el registry no responde el endpoint whoami (URL probablemente incorrecta).');
288
+ console.error(`FIX: Verifica que la URL sea exactamente '${registry}' (incluyendo https:// y trailing slash si aplica).`);
289
+ break;
290
+ case 'no-token':
291
+ console.error('CAUSA: no hay token configurado para este registry en ~/.npmrc.');
292
+ if (esGithub) {
293
+ console.error('FIX: GitHub Packages requiere PAT manual. Agrega a ~/.npmrc:');
294
+ console.error(' //npm.pkg.github.com/:_authToken=<TU_PAT>');
295
+ console.error(' (NO uses `npm login` con GitHub Packages — devuelve 404/403.)');
296
+ } else {
297
+ console.error(`FIX: npm login --registry=${registry}`);
298
+ }
299
+ break;
300
+ default:
301
+ console.error('CAUSA: error desconocido al consultar whoami.');
302
+ console.error(' Verifica conectividad de red y que `npm` esté en PATH.');
303
+ console.error(` Para diagnóstico manual: npm whoami --registry=${registry}`);
304
+ }
305
+ console.error('');
306
+ }
307
+
308
+ function copiarDir(src, dest) {
309
+ fs.mkdirSync(dest, { recursive: true });
310
+ const entries = fs.readdirSync(src, { withFileTypes: true });
311
+ for (const entry of entries) {
312
+ // Excluir artefactos Python (lista canónica en lib/artefactos-python.js)
313
+ if (entry.isDirectory() && esDirArtefacto(entry.name)) continue;
314
+ if (!entry.isDirectory() && esArchivoArtefacto(entry.name)) continue;
315
+
316
+ const srcPath = path.join(src, entry.name);
317
+ const destPath = path.join(dest, entry.name);
318
+ if (entry.isDirectory()) {
319
+ copiarDir(srcPath, destPath);
320
+ } else {
321
+ fs.copyFileSync(srcPath, destPath);
322
+ }
323
+ }
324
+ }
325
+
326
+ function limpiar(tmpDir) {
327
+ try {
328
+ fs.rmSync(tmpDir, { recursive: true, force: true });
329
+ } catch (err) {
330
+ // En Windows el filesystem puede mantener locks transitorios sobre
331
+ // archivos recién copiados. La limpieza eventual la hace el SO; no
332
+ // bloqueamos el flujo, pero anotamos el path para inspección manual.
333
+ process.stderr.write(`[publicar] no se pudo limpiar tmpDir ${tmpDir}: ${String(err.message).slice(0, 80)}\n`);
334
+ }
335
+ }
336
+
337
+ function construirPkgTemporal(pkg, nombre, registry) {
338
+ const tmpPkg = {
339
+ ...pkg,
340
+ name: nombre,
341
+ publishConfig: { registry, access: 'public' },
342
+ };
343
+ // Quitar prepack/prepublishOnly del temp para evitar recursión.
344
+ // npm publish desde tmpDir invocaría prepack apuntando a un script
345
+ // que ya no existe en ese path, y prepublishOnly correría el suite
346
+ // de tests dos veces.
347
+ if (tmpPkg.scripts) {
348
+ delete tmpPkg.scripts.prepack;
349
+ delete tmpPkg.scripts.prepublishOnly;
350
+ }
351
+ return tmpPkg;
352
+ }
353
+
354
+ function copiarArchivosFiles(pkg, tmpDir) {
355
+ for (const entry of pkg.files || []) {
356
+ const src = path.join(ROOT, entry);
357
+ const dest = path.join(tmpDir, entry);
358
+ if (!fs.existsSync(src)) continue;
359
+ if (fs.statSync(src).isDirectory()) {
360
+ copiarDir(src, dest);
361
+ } else {
362
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
363
+ fs.copyFileSync(src, dest);
364
+ }
365
+ }
366
+ }
367
+
368
+ function copiarMetadatos(tmpDir) {
369
+ // npm incluye README/LICENSE/CHANGELOG automáticamente al publicar.
370
+ for (const meta of ['README.md', 'LICENSE', 'CHANGELOG.md', 'CHANGELOG-LEGACY.md']) {
371
+ const src = path.join(ROOT, meta);
372
+ if (fs.existsSync(src)) {
373
+ fs.copyFileSync(src, path.join(tmpDir, meta));
374
+ }
375
+ }
376
+ }
377
+
378
+ function prepararDirectorioTemporal(pkg, nombrePersonalizado, registryPersonalizado) {
379
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'swl-ses-publish-'));
380
+ const tmpPkg = construirPkgTemporal(pkg, nombrePersonalizado, registryPersonalizado);
381
+ fs.writeFileSync(path.join(tmpDir, 'package.json'), JSON.stringify(tmpPkg, null, 2));
382
+ copiarArchivosFiles(pkg, tmpDir);
383
+ copiarMetadatos(tmpDir);
384
+ return tmpDir;
385
+ }
386
+
387
+ function publicarNpmjs(pkg, dryRun) {
388
+ console.log('\n=== 1/2 — npmjs.org (canónico) ===');
389
+ console.log(`Paquete: ${pkg.name}@${pkg.version}`);
390
+ console.log(`Registry: ${NPMJS_REGISTRY}`);
391
+
392
+ const auth = verificarLogin(NPMJS_REGISTRY);
393
+ if (!auth.ok) {
394
+ console.error(`ERROR: No autenticado en npmjs (${auth.tipo}).`);
395
+ imprimirGuiaAuth(NPMJS_REGISTRY, pkg.name, auth.tipo);
396
+ return false;
397
+ }
398
+ console.log(`Autenticado como: ${auth.usuario}`);
399
+
400
+ const args = ['publish', `--registry=${NPMJS_REGISTRY}`, '--access', 'public'];
401
+ if (dryRun) args.push('--dry-run');
402
+ const resultado = ejecutarPublishConOTP(args);
403
+ if (resultado.ok) {
404
+ console.log(`${dryRun ? '[DRY-RUN] ' : ''}OK: ${pkg.name}@${pkg.version} ${dryRun ? 'se publicaría' : 'publicado'} en npmjs`);
405
+ return true;
406
+ }
407
+ console.error(`ERROR publicando en npmjs (ver stderr arriba para diagnóstico de npm).`);
408
+ return false;
409
+ }
410
+
411
+ function publicarGitHub(pkg, dryRun) {
412
+ console.log('\n=== 2/2 — GitHub Packages (mirror) ===');
413
+ console.log(`Paquete: ${GITHUB_NAME}@${pkg.version}`);
414
+ console.log(`Registry: ${GITHUB_REGISTRY}`);
415
+
416
+ const auth = verificarLogin(GITHUB_REGISTRY);
417
+ if (!auth.ok) {
418
+ console.error(`ERROR: No autenticado en GitHub Packages (${auth.tipo}).`);
419
+ imprimirGuiaAuth(GITHUB_REGISTRY, GITHUB_NAME, auth.tipo);
420
+ return false;
421
+ }
422
+ console.log(`Autenticado como: ${auth.usuario}`);
423
+
424
+ const tmpDir = prepararDirectorioTemporal(pkg, GITHUB_NAME, GITHUB_REGISTRY);
425
+
426
+ const args = ['publish', `--registry=${GITHUB_REGISTRY}`];
427
+ if (dryRun) args.push('--dry-run');
428
+ const resultado = ejecutarPublishConOTP(args, { cwd: tmpDir });
429
+ if (resultado.ok) {
430
+ console.log(`${dryRun ? '[DRY-RUN] ' : ''}OK: ${GITHUB_NAME}@${pkg.version} ${dryRun ? 'se publicaría' : 'publicado'} en GitHub Packages`);
431
+ if (dryRun) console.log(`Directorio temporal: ${tmpDir}`);
432
+ limpiar(tmpDir);
433
+ return true;
434
+ }
435
+ console.error(`ERROR publicando en GitHub Packages (ver stderr arriba para diagnóstico de npm).`);
436
+ limpiar(tmpDir);
437
+ return false;
438
+ }
439
+
440
+ function parsearArgs(argv) {
441
+ return {
442
+ dryRun: argv.includes('--dry-run'),
443
+ soloGithub: argv.includes('--solo-github'),
444
+ soloNpmjs: argv.includes('--solo-npmjs'),
445
+ };
446
+ }
447
+
448
+ function imprimirEncabezado(version, dryRun) {
449
+ console.log('╔══════════════════════════════════════════╗');
450
+ console.log('║ @saulwade/swl-ses — Publicación dual ║');
451
+ console.log('╚══════════════════════════════════════════╝');
452
+ console.log(`Versión: ${version}`);
453
+ if (dryRun) console.log('Modo: DRY-RUN (sin cambios)');
454
+ }
455
+
456
+ function avisarSiHayCambiosSinCommit(dryRun) {
457
+ try {
458
+ const status = String(execFileSync('git', ['status', '--porcelain'], {
459
+ cwd: ROOT, encoding: 'utf-8',
460
+ })).trim();
461
+ if (status && !dryRun) {
462
+ console.warn('\nADVERTENCIA: Hay cambios sin commitear.');
463
+ console.warn('Se recomienda hacer commit antes de publicar.\n');
464
+ }
465
+ } catch (err) {
466
+ // No es repo git, o git no está disponible. No bloquea el publish.
467
+ process.stderr.write(`[publicar] git status falló (no bloqueante): ${String(err.message).slice(0, 80)}\n`);
468
+ }
469
+ }
470
+
471
+ function reportarResultado(pkg, opts, npmjsOk, githubOk) {
472
+ console.log('\n=== Resultado ===');
473
+ if (!opts.soloGithub) console.log(`npmjs.org: ${npmjsOk ? 'OK' : 'FALLÓ'} — ${pkg.name}@${pkg.version}`);
474
+ if (!opts.soloNpmjs) console.log(`GitHub Packages: ${githubOk ? 'OK' : 'FALLÓ'} — ${GITHUB_NAME}@${pkg.version}`);
475
+ if (!opts.soloNpmjs && !opts.soloGithub && npmjsOk && githubOk) {
476
+ console.log('\nAmbos paquetes publicados. Los usuarios pueden instalar con:');
477
+ console.log(` npx ${pkg.name}@latest doctor (canónico, recomendado)`);
478
+ console.log(` npx ${GITHUB_NAME}@latest doctor (mirror GitHub Packages)`);
479
+ }
480
+ }
481
+
482
+ function main() {
483
+ const opts = parsearArgs(process.argv.slice(2));
484
+ const pkg = leerPkg();
485
+ imprimirEncabezado(pkg.version, opts.dryRun);
486
+ avisarSiHayCambiosSinCommit(opts.dryRun);
487
+
488
+ const npmjsOk = opts.soloGithub ? true : publicarNpmjs(pkg, opts.dryRun);
489
+ const githubOk = opts.soloNpmjs ? true : publicarGitHub(pkg, opts.dryRun);
490
+
491
+ reportarResultado(pkg, opts, npmjsOk, githubOk);
492
+ process.exit(npmjsOk && githubOk ? 0 : 1);
493
+ }
494
+
495
+ // Solo ejecutar el flujo completo cuando el script se invoca como CLI.
496
+ // Cuando se importa como módulo (tests), expone helpers para verificar
497
+ // invariantes de prepararDirectorioTemporal sin hacer publish real.
498
+ if (require.main === module) {
499
+ main();
500
+ } else {
501
+ module.exports = {
502
+ prepararDirectorioTemporal,
503
+ copiarDir,
504
+ limpiar,
505
+ esErrorOTP,
506
+ ejecutarPublishConOTP,
507
+ GITHUB_NAME,
508
+ GITHUB_REGISTRY,
509
+ NPMJS_REGISTRY,
510
+ };
511
+ }