Merge branch 'replace-eol-char'

This commit is contained in:
Aleksandr Statciuk 2022-02-17 17:36:21 +03:00
commit 50a72a4fea
7 changed files with 4267 additions and 4237 deletions

View file

@ -1,4 +1,6 @@
const csv2json = require('csvtojson') const csv2json = require('csvtojson')
const chalk = require('chalk')
const logger = require('./logger')
const fs = require('mz/fs') const fs = require('mz/fs')
const { const {
Parser, Parser,
@ -10,6 +12,7 @@ const csv2jsonOptions = {
checkColumn: true, checkColumn: true,
trim: true, trim: true,
delimiter: ',', delimiter: ',',
eol: '\r\n',
colParser: { colParser: {
countries: listParser, countries: listParser,
languages: listParser, languages: listParser,
@ -32,10 +35,14 @@ const json2csv = new Parser({
const csv = {} const csv = {}
csv.load = async function (filepath) { csv.fromFile = async function (filepath) {
return csv2json(csv2jsonOptions).fromFile(filepath) return csv2json(csv2jsonOptions).fromFile(filepath)
} }
csv.fromString = async function (filepath) {
return csv2json(csv2jsonOptions).fromString(filepath)
}
csv.save = async function (filepath, data) { csv.save = async function (filepath, data) {
const string = json2csv.parse(data) const string = json2csv.parse(data)

View file

@ -1,6 +1,7 @@
const path = require('path') const path = require('path')
const glob = require('glob') const glob = require('glob')
const fs = require('mz/fs') const fs = require('mz/fs')
const crlf = require('crlf')
const file = {} const file = {}
@ -65,4 +66,13 @@ file.basename = function (filepath) {
return path.basename(filepath) return path.basename(filepath)
} }
file.eol = function (filepath) {
return new Promise((resolve, reject) => {
crlf.get(filepath, null, function (err, endingType) {
if (err) reject(err)
resolve(endingType)
})
})
}
module.exports = file module.exports = file

View file

@ -8,7 +8,7 @@ async function main() {
const files = await file.list(`${DATA_DIR}/*.csv`) const files = await file.list(`${DATA_DIR}/*.csv`)
for (const filepath of files) { for (const filepath of files) {
const filename = file.getFilename(filepath) const filename = file.getFilename(filepath)
const json = await csv.load(filepath).catch(err => { const json = await csv.fromFile(filepath).catch(err => {
logger.error(chalk.red(`\n${err.message} (${filepath})`)) logger.error(chalk.red(`\n${err.message} (${filepath})`))
process.exit(1) process.exit(1)
}) })

View file

@ -21,7 +21,20 @@ async function main() {
] ]
for (const filepath of files) { for (const filepath of files) {
if (!filepath.endsWith('.csv')) continue if (!filepath.endsWith('.csv')) continue
const data = await csv.load(filepath).catch(err => {
const eol = await file.eol(filepath)
if (eol !== 'CRLF') {
logger.error(chalk.red(`\nError: file must have line endings with CRLF (${filepath})`))
process.exit(1)
}
const csvString = await file.read(filepath)
if (/\s+$/.test(csvString)) {
logger.error(chalk.red(`\nError: empty lines at the end of file not allowed (${filepath})`))
process.exit(1)
}
const data = await csv.fromString(csvString).catch(err => {
logger.error(chalk.red(`\n${err.message} (${filepath})`)) logger.error(chalk.red(`\n${err.message} (${filepath})`))
process.exit(1) process.exit(1)
}) })