Make code prettier

This commit is contained in:
Aleksandr Statciuk 2022-12-29 23:50:36 +03:00
parent 268d150cc5
commit dd6d694491
31 changed files with 1079 additions and 1051 deletions

View file

@ -1,76 +1,76 @@
const chalk = require('chalk')
const libxml = require('libxmljs2')
const { program } = require('commander')
const { logger, file } = require('../../core')
const xsd = `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="site">
<xs:complexType>
<xs:sequence>
<xs:element ref="channels"/>
</xs:sequence>
<xs:attribute name="site" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="channels">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="channel"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="channel">
<xs:complexType mixed="true">
<xs:attribute name="lang" use="required" type="xs:string"/>
<xs:attribute name="site_id" use="required" type="xs:string"/>
<xs:attribute name="xmltv_id" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>`
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
async function main() {
if (!program.args.length) {
logger.error('required argument "filepath" not specified')
}
let errors = []
for (const filepath of program.args) {
if (!filepath.endsWith('.xml')) continue
const xml = await file.read(filepath)
let localErrors = []
try {
const xsdDoc = libxml.parseXml(xsd)
const doc = libxml.parseXml(xml)
if (!doc.validate(xsdDoc)) {
localErrors = doc.validationErrors
}
} catch (error) {
localErrors.push(error)
}
if (localErrors.length) {
logger.info(`\n${chalk.underline(filepath)}`)
localErrors.forEach(error => {
const position = `${error.line}:${error.column}`
logger.error(` ${chalk.gray(position.padEnd(4, ' '))} ${error.message.trim()}`)
})
errors = errors.concat(localErrors)
}
}
if (errors.length) {
logger.error(chalk.red(`\n${errors.length} error(s)`))
process.exit(1)
}
}
main()
const chalk = require('chalk')
const libxml = require('libxmljs2')
const { program } = require('commander')
const { logger, file } = require('../../core')
const xsd = `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="site">
<xs:complexType>
<xs:sequence>
<xs:element ref="channels"/>
</xs:sequence>
<xs:attribute name="site" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="channels">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="channel"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="channel">
<xs:complexType mixed="true">
<xs:attribute name="lang" use="required" type="xs:string"/>
<xs:attribute name="site_id" use="required" type="xs:string"/>
<xs:attribute name="xmltv_id" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>`
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
async function main() {
if (!program.args.length) {
logger.error('required argument "filepath" not specified')
}
let errors = []
for (const filepath of program.args) {
if (!filepath.endsWith('.xml')) continue
const xml = await file.read(filepath)
let localErrors = []
try {
const xsdDoc = libxml.parseXml(xsd)
const doc = libxml.parseXml(xml)
if (!doc.validate(xsdDoc)) {
localErrors = doc.validationErrors
}
} catch (error) {
localErrors.push(error)
}
if (localErrors.length) {
logger.info(`\n${chalk.underline(filepath)}`)
localErrors.forEach(error => {
const position = `${error.line}:${error.column}`
logger.error(` ${chalk.gray(position.padEnd(4, ' '))} ${error.message.trim()}`)
})
errors = errors.concat(localErrors)
}
}
if (errors.length) {
logger.error(chalk.red(`\n${errors.length} error(s)`))
process.exit(1)
}
}
main()

View file

@ -1,68 +1,68 @@
const { parser, logger, api } = require('../../core')
const { program } = require('commander')
const chalk = require('chalk')
const langs = require('langs')
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
async function main() {
await api.channels.load()
const stats = {
files: 0,
errors: 0
}
if (!program.args.length) {
logger.error('required argument "filepath" not specified')
}
for (const filepath of program.args) {
if (!filepath.endsWith('.xml')) continue
const { site, channels } = await parser.parseChannels(filepath)
const bufferById = {}
const bufferBySiteId = {}
const errors = []
for (const channel of channels) {
if (!bufferById[channel.id + channel.lang]) {
bufferById[channel.id + channel.lang] = channel
} else {
errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel })
stats.errors++
}
if (!bufferBySiteId[channel.site_id + channel.lang]) {
bufferBySiteId[channel.site_id + channel.lang] = channel
} else {
errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel })
stats.errors++
}
if (!api.channels.find({ id: channel.id })) {
errors.push({ type: 'wrong_xmltv_id', xmltv_id: channel.id, ...channel })
stats.errors++
}
if (!langs.where('1', channel.lang)) {
errors.push({ type: 'wrong_lang', xmltv_id: channel.id, ...channel })
stats.errors++
}
}
if (errors.length) {
logger.info(chalk.underline(filepath))
console.table(errors, ['type', 'lang', 'xmltv_id', 'site_id', 'name'])
console.log()
stats.files++
}
}
if (stats.errors > 0) {
logger.error(chalk.red(`${stats.errors} error(s) in ${stats.files} file(s)`))
process.exit(1)
}
}
main()
const { parser, logger, api } = require('../../core')
const { program } = require('commander')
const chalk = require('chalk')
const langs = require('langs')
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
async function main() {
await api.channels.load()
const stats = {
files: 0,
errors: 0
}
if (!program.args.length) {
logger.error('required argument "filepath" not specified')
}
for (const filepath of program.args) {
if (!filepath.endsWith('.xml')) continue
const { site, channels } = await parser.parseChannels(filepath)
const bufferById = {}
const bufferBySiteId = {}
const errors = []
for (const channel of channels) {
if (!bufferById[channel.id + channel.lang]) {
bufferById[channel.id + channel.lang] = channel
} else {
errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel })
stats.errors++
}
if (!bufferBySiteId[channel.site_id + channel.lang]) {
bufferBySiteId[channel.site_id + channel.lang] = channel
} else {
errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel })
stats.errors++
}
if (!api.channels.find({ id: channel.id })) {
errors.push({ type: 'wrong_xmltv_id', xmltv_id: channel.id, ...channel })
stats.errors++
}
if (!langs.where('1', channel.lang)) {
errors.push({ type: 'wrong_lang', xmltv_id: channel.id, ...channel })
stats.errors++
}
}
if (errors.length) {
logger.info(chalk.underline(filepath))
console.table(errors, ['type', 'lang', 'xmltv_id', 'site_id', 'name'])
console.log()
stats.files++
}
}
if (stats.errors > 0) {
logger.error(chalk.red(`${stats.errors} error(s) in ${stats.files} file(s)`))
process.exit(1)
}
}
main()

View file

@ -1,13 +1,13 @@
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
const date = {}
date.getUTC = function (d = null) {
if (typeof d === 'string') return dayjs.utc(d).startOf('d')
return dayjs.utc().startOf('d')
}
module.exports = date
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
const date = {}
date.getUTC = function (d = null) {
if (typeof d === 'string') return dayjs.utc(d).startOf('d')
return dayjs.utc().startOf('d')
}
module.exports = date

View file

@ -3,11 +3,11 @@ const { gzip, ungzip } = require('node-gzip')
const zip = {}
zip.compress = async function (string) {
return gzip(string)
return gzip(string)
}
zip.decompress = async function (string) {
return ungzip(string)
return ungzip(string)
}
module.exports = zip