convertir-formato-asistencia 1.2.0 → 1.2.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.
- package/.github/workflows/ci.yml +35 -0
- package/.github/workflows/npm-publish.yml +43 -0
- package/README.md +1 -1
- package/index.js +10 -11
- package/index.test.js +201 -0
- package/package.json +10 -7
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
|
|
3
|
+
|
|
4
|
+
name: Node.js CI
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "main" ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ "main" ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
test:
|
|
14
|
+
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
|
|
17
|
+
strategy:
|
|
18
|
+
matrix:
|
|
19
|
+
node-version: [20.x, 22.x]
|
|
20
|
+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- name: "Instalar pnpm"
|
|
26
|
+
uses: pnpm/action-setup@v4
|
|
27
|
+
|
|
28
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
29
|
+
uses: actions/setup-node@v4
|
|
30
|
+
with:
|
|
31
|
+
node-version: ${{ matrix.node-version }}
|
|
32
|
+
cache: 'pnpm'
|
|
33
|
+
|
|
34
|
+
- run: pnpm install --frozen-lockfile
|
|
35
|
+
- run: pnpm run test
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
+
|
|
4
|
+
name: Node.js Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
release:
|
|
8
|
+
types: [created]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: "Instalar pnpm"
|
|
17
|
+
uses: pnpm/action-setup@v4
|
|
18
|
+
|
|
19
|
+
- uses: actions/setup-node@v4
|
|
20
|
+
with:
|
|
21
|
+
node-version: 20
|
|
22
|
+
cache: 'pnpm'
|
|
23
|
+
- run: pnpm install --frozen-lockfile
|
|
24
|
+
- run: pnpm run test
|
|
25
|
+
|
|
26
|
+
publish-npm:
|
|
27
|
+
needs: test
|
|
28
|
+
runs-on: ubuntu-latest
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@v4
|
|
31
|
+
|
|
32
|
+
- name: "Instalar pnpm"
|
|
33
|
+
uses: pnpm/action-setup@v4
|
|
34
|
+
|
|
35
|
+
- uses: actions/setup-node@v4
|
|
36
|
+
with:
|
|
37
|
+
node-version: 20
|
|
38
|
+
cache: 'pnpm'
|
|
39
|
+
registry-url: https://registry.npmjs.org/
|
|
40
|
+
- run: pnpm install --frozen-lockfile
|
|
41
|
+
- run: pnpm publish --access public --no-git-checks
|
|
42
|
+
env:
|
|
43
|
+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Convertir Formato de Asistencia
|
|
1
|
+
# Convertir Formato de Asistencia [](https://github.com/jjaljuria/Convertir_Formato_Asistencia/actions/workflows/ci.yml)
|
|
2
2
|
|
|
3
3
|
Herramienta simple en Node.js para convertir registros de asistencia (XLS/XLSX) a un reporte de asistencia en formato Excel más legible.
|
|
4
4
|
|
package/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import ExcelJS from 'exceljs';
|
|
3
|
+
import { fileURLToPath } from 'url'
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import xlsx from 'xlsx';
|
|
6
|
+
import fs from 'node:fs';
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
// --- FUNCIONES DE AYUDA ---
|
|
@@ -204,10 +205,10 @@ async function generarReporte(rutaEntrada, rutaSalida, usuarioConfig = {}) {
|
|
|
204
205
|
}
|
|
205
206
|
}
|
|
206
207
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
208
|
+
// convertir import.meta.url a path de archivo
|
|
209
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
210
|
+
|
|
211
|
+
if (path.resolve(process.argv[1] || '') === __filename) {
|
|
211
212
|
const rutaEntrada = process.argv[2];
|
|
212
213
|
const rutaSalida = process.argv[3];
|
|
213
214
|
|
|
@@ -216,6 +217,4 @@ if (require.main === module) {
|
|
|
216
217
|
})();
|
|
217
218
|
}
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
generarReporte
|
|
221
|
-
}
|
|
220
|
+
export { generarReporte };
|
package/index.test.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { generarReporte } from './index.js';
|
|
3
|
+
import ExcelJS from 'exceljs'; // Importar después de mockear para usar el mock
|
|
4
|
+
import xlsx from 'xlsx';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
|
|
7
|
+
// Mock de las librerías externas (deben declararse antes de importar el módulo bajo prueba)
|
|
8
|
+
vi.mock('node:fs', async (importOrigin) => {
|
|
9
|
+
const actual = await importOrigin();
|
|
10
|
+
const existsSyncMock = vi.fn();
|
|
11
|
+
return {
|
|
12
|
+
...actual,
|
|
13
|
+
default: {
|
|
14
|
+
...actual.default,
|
|
15
|
+
existsSync: existsSyncMock,
|
|
16
|
+
},
|
|
17
|
+
existsSync: existsSyncMock,
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
vi.mock('xlsx', async (importOriginal) => {
|
|
22
|
+
const actual = await importOriginal();
|
|
23
|
+
const readFileMock = vi.fn();
|
|
24
|
+
const sheetToJsonMock = vi.fn();
|
|
25
|
+
return {
|
|
26
|
+
...actual,
|
|
27
|
+
default: {
|
|
28
|
+
...(actual && actual.default ? actual.default : {}),
|
|
29
|
+
readFile: readFileMock,
|
|
30
|
+
utils: {
|
|
31
|
+
...((actual && actual.default && actual.default.utils) || {}),
|
|
32
|
+
sheet_to_json: sheetToJsonMock,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
readFile: readFileMock,
|
|
36
|
+
utils: {
|
|
37
|
+
sheet_to_json: sheetToJsonMock,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
vi.mock('exceljs', () => {
|
|
43
|
+
const mockWorksheet = {
|
|
44
|
+
columns: [],
|
|
45
|
+
getRow: vi.fn(() => ({
|
|
46
|
+
font: {},
|
|
47
|
+
fill: {},
|
|
48
|
+
})),
|
|
49
|
+
addRow: vi.fn(),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const mockWorkbookInstance = {
|
|
53
|
+
xlsx: {
|
|
54
|
+
writeFile: vi.fn(),
|
|
55
|
+
},
|
|
56
|
+
addWorksheet: vi.fn(() => mockWorksheet),
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Use a regular (non-arrow) function wrapped with vi.fn so it can be used as a constructor
|
|
60
|
+
const MockedWorkbook = vi.fn(function MockedWorkbook() {
|
|
61
|
+
return mockWorkbookInstance;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
__esModule: true,
|
|
66
|
+
default: { Workbook: MockedWorkbook },
|
|
67
|
+
Workbook: MockedWorkbook, // For destructured imports
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
describe('generarReporte', () => {
|
|
74
|
+
// Limpiar mocks antes de cada test
|
|
75
|
+
beforeEach(() => {
|
|
76
|
+
vi.clearAllMocks();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('debería ser una función', () => {
|
|
80
|
+
expect(typeof generarReporte).toBe('function');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('debería registrar un error si falta rutaEntrada', async () => {
|
|
84
|
+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
85
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
86
|
+
|
|
87
|
+
await generarReporte(undefined, 'ruta/salida.xlsx');
|
|
88
|
+
|
|
89
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("❌ ERROR: Faltan argumentos.");
|
|
90
|
+
expect(consoleLogSpy).toHaveBeenCalledWith("Uso correcto: node index.js <ruta_entrada> <ruta_salida>");
|
|
91
|
+
consoleErrorSpy.mockRestore();
|
|
92
|
+
consoleLogSpy.mockRestore();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('debería registrar un error si falta rutaSalida', async () => {
|
|
96
|
+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
97
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
98
|
+
|
|
99
|
+
await generarReporte('ruta/entrada.xls', undefined);
|
|
100
|
+
|
|
101
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("❌ ERROR: Faltan argumentos.");
|
|
102
|
+
expect(consoleLogSpy).toHaveBeenCalledWith("Uso correcto: node index.js <ruta_entrada> <ruta_salida>");
|
|
103
|
+
consoleErrorSpy.mockRestore();
|
|
104
|
+
consoleLogSpy.mockRestore();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('debería registrar un error si el archivo de entrada no existe', async () => {
|
|
108
|
+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
109
|
+
fs.existsSync.mockReturnValue(false);
|
|
110
|
+
|
|
111
|
+
await generarReporte('ruta/entrada.xls', 'ruta/salida.xlsx');
|
|
112
|
+
|
|
113
|
+
expect(fs.existsSync).toHaveBeenCalledWith('ruta/entrada.xls');
|
|
114
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("❌ ERROR: El archivo de entrada no existe en: ruta/entrada.xls");
|
|
115
|
+
consoleErrorSpy.mockRestore();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('debería registrar un error si el archivo de entrada está vacío', async () => {
|
|
119
|
+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
120
|
+
fs.existsSync.mockReturnValue(true);
|
|
121
|
+
xlsx.readFile.mockReturnValue({ SheetNames: ['Sheet1'], Sheets: { Sheet1: {} } });
|
|
122
|
+
xlsx.utils.sheet_to_json.mockReturnValue([]);
|
|
123
|
+
|
|
124
|
+
await generarReporte('ruta/entrada.xls', 'ruta/salida.xlsx');
|
|
125
|
+
|
|
126
|
+
expect(xlsx.readFile).toHaveBeenCalledWith('ruta/entrada.xls', { cellDates: true });
|
|
127
|
+
expect(xlsx.utils.sheet_to_json).toHaveBeenCalledWith(expect.any(Object));
|
|
128
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("❌ ERROR CRÍTICO:", "El archivo de entrada está vacío o no se leyó correctamente.");
|
|
129
|
+
consoleErrorSpy.mockRestore();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('debería generar un reporte correctamente con datos válidos', async () => {
|
|
133
|
+
const mockData = [
|
|
134
|
+
{ 'ID de usuario': 1, 'Nombre': 'Jose', 'Fecha/Hora': new Date('2026-04-01T08:00:00.000Z'), 'Departamento': 'IT' },
|
|
135
|
+
{ 'ID de usuario': 1, 'Nombre': 'Jose', 'Fecha/Hora': new Date('2026-04-01T17:00:00.000Z'), 'Departamento': 'IT' },
|
|
136
|
+
{ 'ID de usuario': 2, 'Nombre': 'Maria', 'Fecha/Hora': new Date('2026-04-01T09:00:00.000Z'), 'Departamento': 'HR' },
|
|
137
|
+
];
|
|
138
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
139
|
+
|
|
140
|
+
fs.existsSync.mockReturnValue(true);
|
|
141
|
+
xlsx.readFile.mockReturnValue({ SheetNames: ['Sheet1'], Sheets: { Sheet1: {} } });
|
|
142
|
+
xlsx.utils.sheet_to_json.mockReturnValue(mockData);
|
|
143
|
+
|
|
144
|
+
await generarReporte('ruta/entrada.xls', 'ruta/salida.xlsx');
|
|
145
|
+
|
|
146
|
+
expect(fs.existsSync).toHaveBeenCalledWith('ruta/entrada.xls');
|
|
147
|
+
expect(xlsx.readFile).toHaveBeenCalledWith('ruta/entrada.xls', { cellDates: true });
|
|
148
|
+
expect(xlsx.utils.sheet_to_json).toHaveBeenCalledWith(expect.any(Object));
|
|
149
|
+
expect(ExcelJS.Workbook).toHaveBeenCalledTimes(1);
|
|
150
|
+
const mockWorkbookInstance = ExcelJS.Workbook.mock.results[0].value; // Obtener la instancia mockeada
|
|
151
|
+
expect(mockWorkbookInstance.addWorksheet).toHaveBeenCalledWith('IT');
|
|
152
|
+
expect(mockWorkbookInstance.addWorksheet).toHaveBeenCalledWith('HR');
|
|
153
|
+
expect(mockWorkbookInstance.xlsx.writeFile).toHaveBeenCalledWith('ruta/salida.xlsx');
|
|
154
|
+
expect(consoleLogSpy).toHaveBeenCalledWith("✨ ¡PROCESO COMPLETADO! El archivo se generó correctamente.");
|
|
155
|
+
|
|
156
|
+
consoleLogSpy.mockRestore();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('debería usar la configuración de usuario para los nombres de las columnas', async () => {
|
|
160
|
+
const mockData = [
|
|
161
|
+
{ 'Custom ID': 1, 'Custom Name': 'Jose', 'Custom DateTime': new Date('2026-04-01T08:00:00.000Z'), 'Custom Dept': 'IT' },
|
|
162
|
+
];
|
|
163
|
+
const usuarioConfig = {
|
|
164
|
+
entrada: {
|
|
165
|
+
id: 'Custom ID',
|
|
166
|
+
nombre: 'Custom Name',
|
|
167
|
+
fechaHora: 'Custom DateTime',
|
|
168
|
+
departamento: 'Custom Dept'
|
|
169
|
+
},
|
|
170
|
+
salida: {
|
|
171
|
+
id: 'ID Personal',
|
|
172
|
+
nombre: 'Nombre Completo',
|
|
173
|
+
fecha: 'Fecha de Asistencia',
|
|
174
|
+
entrada: 'Hora de Entrada',
|
|
175
|
+
salida: 'Hora de Salida',
|
|
176
|
+
departamento: 'Departamento de la Empresa',
|
|
177
|
+
estado: 'Estado de Asistencia'
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
181
|
+
|
|
182
|
+
fs.existsSync.mockReturnValue(true);
|
|
183
|
+
xlsx.readFile.mockReturnValue({ SheetNames: ['Sheet1'], Sheets: { Sheet1: {} } });
|
|
184
|
+
xlsx.utils.sheet_to_json.mockReturnValue(mockData);
|
|
185
|
+
|
|
186
|
+
await generarReporte('ruta/entrada.xls', 'ruta/salida.xlsx', usuarioConfig);
|
|
187
|
+
|
|
188
|
+
expect(fs.existsSync).toHaveBeenCalledWith('ruta/entrada.xls');
|
|
189
|
+
expect(xlsx.readFile).toHaveBeenCalledWith('ruta/entrada.xls', { cellDates: true });
|
|
190
|
+
expect(xlsx.utils.sheet_to_json).toHaveBeenCalledWith(expect.any(Object));
|
|
191
|
+
|
|
192
|
+
expect(ExcelJS.Workbook).toHaveBeenCalledTimes(1);
|
|
193
|
+
const mockWorkbookInstance = ExcelJS.Workbook.mock.results[0].value; // Obtener la instancia mockeada
|
|
194
|
+
expect(mockWorkbookInstance.addWorksheet).toHaveBeenCalledWith('IT');
|
|
195
|
+
|
|
196
|
+
expect(mockWorkbookInstance.xlsx.writeFile).toHaveBeenCalledWith('ruta/salida.xlsx');
|
|
197
|
+
expect(consoleLogSpy).toHaveBeenCalledWith("✨ ¡PROCESO COMPLETADO! El archivo se generó correctamente.");
|
|
198
|
+
|
|
199
|
+
consoleLogSpy.mockRestore();
|
|
200
|
+
});
|
|
201
|
+
});
|
package/package.json
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "convertir-formato-asistencia",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"bin": {
|
|
7
8
|
"cofa": "./index.js"
|
|
8
9
|
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
-
},
|
|
12
10
|
"keywords": [],
|
|
13
11
|
"author": "",
|
|
14
|
-
"license": "
|
|
15
|
-
"packageManager": "pnpm@10.26.2",
|
|
12
|
+
"license": "MIT",
|
|
16
13
|
"dependencies": {
|
|
17
14
|
"exceljs": "^4.4.0",
|
|
18
15
|
"xlsx": "^0.18.5"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"vitest": "^4.1.5"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "vitest"
|
|
19
22
|
}
|
|
20
|
-
}
|
|
23
|
+
}
|