@saulwade/swl-ses 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/CLAUDE.md +1 -1
  2. package/README.md +1 -1
  3. package/agentes/nemesis-auditor-swl.md +161 -161
  4. package/bin/swl-mcp-server.js +187 -187
  5. package/comandos/swl/.evolved.json +22 -22
  6. package/comandos/swl/contribuir.md +233 -233
  7. package/comandos/swl/nemesis.md +122 -122
  8. package/gateway/lib/event-channel.js +191 -191
  9. package/habilidades/backend-production-resilience/SKILL.md +288 -288
  10. package/habilidades/benchmark-memoria/SKILL.md +186 -186
  11. package/habilidades/diagrama-arquitectura/assets/template.html +276 -276
  12. package/habilidades/doubt-driven-review/SKILL.md +171 -171
  13. package/habilidades/doubt-driven-review/recursos/EXAMPLES.md +130 -130
  14. package/habilidades/eval-framework/SKILL.md +212 -212
  15. package/habilidades/feynman-auditor-swl/SKILL.md +123 -123
  16. package/habilidades/feynman-auditor-swl/recursos/preguntas-language-agnostic.md +108 -108
  17. package/habilidades/harness-claude-code/SKILL.md +299 -299
  18. package/habilidades/infra-github-actions/SKILL.md +166 -166
  19. package/habilidades/legacy-code-rescue/SKILL.md +267 -267
  20. package/habilidades/manejo-errores/.evolved.json +8 -8
  21. package/habilidades/meta-skills-estandar/recursos/convencion-examples.md +93 -93
  22. package/habilidades/meta-skills-estandar/recursos/skills-as-agents.md +163 -163
  23. package/habilidades/patrones-python/SKILL.md +229 -229
  24. package/habilidades/patrones-python/recursos/patrones-avanzados.md +469 -469
  25. package/habilidades/planear-fase/SKILL.md +319 -319
  26. package/habilidades/release-semver/.evolved.json +8 -8
  27. package/habilidades/state-inconsistency-auditor-swl/SKILL.md +166 -166
  28. package/habilidades/state-inconsistency-auditor-swl/recursos/coupled-state-patterns.md +147 -147
  29. package/habilidades/testing-python/SKILL.md +340 -340
  30. package/habilidades/web-fetcher-routing/SKILL.md +75 -75
  31. package/hooks/claudemd-bloat-detector.js +161 -161
  32. package/hooks/lib/agent-routing.js +107 -107
  33. package/hooks/lib/auto-consolidator.js +335 -335
  34. package/hooks/lib/error-classifier.js +308 -308
  35. package/hooks/lib/merkle-audit.js +96 -96
  36. package/hooks/lib/provenance-tracker.js +191 -191
  37. package/hooks/lib/rate-limit-tracker.js +253 -253
  38. package/hooks/lib/resource-quota.js +122 -122
  39. package/hooks/lib/retry-jitter.js +165 -165
  40. package/hooks/lib/security-net.js +201 -201
  41. package/hooks/lib/skill-auditor.js +588 -588
  42. package/hooks/lib/sync-status.js +228 -228
  43. package/hooks/lib/taint-tracker.js +107 -107
  44. package/hooks/lib/text-similarity.js +241 -241
  45. package/hooks/lib/toon-compressor.js +245 -245
  46. package/hooks/registro-turnos.js +209 -209
  47. package/hooks/sugerir-regenerar-inventario.js +170 -170
  48. package/hooks/validar-formato-post-subagente.js +140 -140
  49. package/hooks/validar-memoria-hook.js +218 -218
  50. package/instintos/prompt-appendices.yaml +57 -57
  51. package/manifiestos/agent-output-schemas.json +57 -57
  52. package/manifiestos/modulos.json +11 -6
  53. package/manifiestos/perfiles.json +2 -1
  54. package/manifiestos/skills-lock.json +1114 -1114
  55. package/package.json +1 -1
  56. package/plantillas/auditor-veto-template.md +105 -105
  57. package/plantillas/github-workflows/README.md +47 -47
  58. package/plantillas/github-workflows/release-please.yml +44 -44
  59. package/plantillas/github-workflows/swl-ci.yml +107 -107
  60. package/plantillas/github-workflows/swl-security.yml +51 -51
  61. package/plugin.json +9 -1
  62. package/reglas/analisis-previo-tareas-grandes.md +172 -172
  63. package/reglas/arreglar-al-detectar.md +147 -147
  64. package/reglas/fragmentos-compartidos.md +152 -152
  65. package/reglas/harness-claude-code.md +213 -213
  66. package/reglas/usar-context7.md +226 -226
  67. package/schemas/diary-entry.schema.json +80 -80
  68. package/scripts/audit-tools/audit-history.js +330 -330
  69. package/scripts/audit-tools/bundle-tracker.js +290 -290
  70. package/scripts/audit-tools/canary-monitor.js +352 -352
  71. package/scripts/audit-tools/code-profiler.js +605 -605
  72. package/scripts/audit-tools/dep-doctor.js +320 -320
  73. package/scripts/audit-tools/env-validator.js +206 -206
  74. package/scripts/audit-tools/lib/fs-walk.js +48 -48
  75. package/scripts/audit-tools/lib/output.js +23 -23
  76. package/scripts/audit-tools/migration-checker.js +392 -392
  77. package/scripts/audit-tools/pentest-scanner.js +1436 -1436
  78. package/scripts/benchmark-memoria.js +167 -167
  79. package/scripts/configurar-branch-protection.js +418 -418
  80. package/scripts/detectar-aprendizajes-duplicados.js +151 -151
  81. package/scripts/field-report.js +199 -199
  82. package/scripts/generar-checklists-consolidados.js +273 -273
  83. package/scripts/generar-inventario.js +420 -420
  84. package/scripts/generar-matriz-lenguajes.js +271 -271
  85. package/scripts/lib/artefactos-python.js +43 -43
  86. package/scripts/lib/benchmark-metrics.js +160 -160
  87. package/scripts/lib/budget-enforcer.js +252 -252
  88. package/scripts/lib/configurar-ci.js +380 -380
  89. package/scripts/lib/contadores-inventario.js +217 -217
  90. package/scripts/lib/detectar-stack-detallado.js +307 -307
  91. package/scripts/lib/diary-entry.js +234 -234
  92. package/scripts/lib/eval-metrics-store.js +218 -218
  93. package/scripts/lib/eval-quality.js +171 -171
  94. package/scripts/lib/eval-schemas.js +144 -144
  95. package/scripts/lib/eval-self-correct.js +106 -106
  96. package/scripts/lib/eval-validator.js +185 -185
  97. package/scripts/lib/jaccard-similarity.js +98 -98
  98. package/scripts/lib/longmemeval-runner.js +125 -125
  99. package/scripts/lib/manifiestos.js +42 -1
  100. package/scripts/lib/npm-version.js +261 -261
  101. package/scripts/lib/paquetes-conocidos.js +50 -50
  102. package/scripts/lib/prompt-builder.js +264 -264
  103. package/scripts/lib/rrf-fusion.js +175 -175
  104. package/scripts/lib/scoring-instintos.js +277 -277
  105. package/scripts/lib/semantic-search.js +252 -252
  106. package/scripts/limpiar-artefactos-python.js +131 -131
  107. package/scripts/mcp-server/README.md +128 -128
  108. package/scripts/mcp-server/handlers.js +206 -206
  109. package/scripts/migrar-csv-a-array.js +168 -168
  110. package/scripts/migrar-fase-dominio.js +201 -201
  111. package/scripts/publicar.js +511 -511
  112. package/scripts/run-eval.js +141 -141
  113. package/scripts/validar-manifest.js +231 -195
  114. package/scripts/validar-userland-vacio.js +110 -110
@@ -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
+ }