Skip to content

Commit b9f2419

Browse files
committed
Función que crea archivo csv
1 parent c38d311 commit b9f2419

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/file.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
const csv = require('fast-csv')
4+
5+
// Archivo a enviar
6+
const creaArchivo = async (c,data) => {
7+
const dirFile = path.resolve(__dirname,'../public'),
8+
nameFile = 'usuarios_' + new Date().toLocaleDateString() + '.csv'
9+
pathFile = dirFile + '/' + nameFile
10+
let file
11+
c('Creando archivo...')
12+
fs.createWriteStream(pathFile).close()
13+
c('Archivo creado: ' + nameFile)
14+
c('Escribiendo datos...')
15+
file = await csv.writeToPath(pathFile, data, { headers: true })
16+
file.close()
17+
c('Archivo guardado exitosamente')
18+
c('Ubicación: ' + pathFile)
19+
return { pathFile, nameFile }
20+
}
21+
22+
module.exports = creaArchivo

0 commit comments

Comments
 (0)