diff --git a/scripts/commands/update-readme.js b/scripts/commands/update-readme.js index db6021fe..2b0d0e17 100644 --- a/scripts/commands/update-readme.js +++ b/scripts/commands/update-readme.js @@ -1,14 +1,11 @@ const { file, markdown, parser, logger } = require('../core') +const provinces = require('../data/ca-provinces.json') const countries = require('../data/countries.json') const states = require('../data/us-states.json') -const provinces = require('../data/ca-provinces.json') const { program } = require('commander') const _ = require('lodash') const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs' -const LOG_PATH = `${LOGS_DIR}/update-guides.log` - -let log = [] const options = program .option('-c, --config ', 'Set path to config file', '.readme/config.json') @@ -16,31 +13,28 @@ const options = program .opts() async function main() { - await setUp() - - await generateCountriesTable() - await generateUSStatesTable() - await generateCanadaProvincesTable() - + const records = await getLogRecords() + await generateCountriesTable(records) + await generateUSStatesTable(records) + await generateCanadaProvincesTable(records) await updateReadme() } main() -async function generateCountriesTable() { +async function generateCountriesTable(items = []) { logger.info('Generating countries table...') - const items = log.filter(i => i.gid.length === 2) let rows = [] for (const item of items) { - const code = item.gid.toUpperCase() - const country = countries[code] + const country = countries[item.code] + if (!country) continue rows.push({ flag: country.flag, name: country.name, channels: item.count, - epg: `https://iptv-org.github.io/epg/guides/${item.gid}/${item.site}.epg.xml` + epg: `https://iptv-org.github.io/epg/guides/${item.group}.epg.xml` }) } @@ -52,19 +46,18 @@ async function generateCountriesTable() { await file.create('./.readme/_countries.md', table) } -async function generateUSStatesTable() { +async function generateUSStatesTable(items = []) { logger.info('Generating US states table...') - const items = log.filter(i => i.gid.startsWith('us-')) let rows = [] for (const item of items) { - const code = item.gid.toUpperCase() - const state = states[code] + const state = states[item.code] + if (!state) continue rows.push({ name: state.name, channels: item.count, - epg: `https://iptv-org.github.io/epg/guides/${item.gid}/${item.site}.epg.xml` + epg: `https://iptv-org.github.io/epg/guides/${item.group}.epg.xml` }) } @@ -76,19 +69,18 @@ async function generateUSStatesTable() { await file.create('./.readme/_us-states.md', table) } -async function generateCanadaProvincesTable() { +async function generateCanadaProvincesTable(items = []) { logger.info('Generating Canada provinces table...') - const items = log.filter(i => i.gid.startsWith('ca-')) let rows = [] for (const item of items) { - const code = item.gid.toUpperCase() - const province = provinces[code] + const province = provinces[item.code] + if (!province) continue rows.push({ name: province.name, channels: item.count, - epg: `https://iptv-org.github.io/epg/guides/${item.gid}/${item.site}.epg.xml` + epg: `https://iptv-org.github.io/epg/guides/${item.group}.epg.xml` }) } @@ -108,11 +100,19 @@ async function updateReadme() { await markdown.compile(options.config) } -async function setUp() { - log = await parser.parseLogs(LOG_PATH) +async function getLogRecords() { + const logPath = `${LOGS_DIR}/update-guides.log` + const records = await parser.parseLogs(logPath) - if (!log.length) { - logger.error(`File "${LOG_PATH}" is empty`) + if (!records.length) { + logger.error(`File "${logPath}" is empty`) process.exit(1) } + + return records.map(item => { + const code = item.group.split('/')[0] || '' + item.code = code.toUpperCase() + + return item + }) } diff --git a/tests/__data__/input/logs/update-guides.log b/tests/__data__/input/logs/update-guides.log index 1afbb852..39141054 100644 --- a/tests/__data__/input/logs/update-guides.log +++ b/tests/__data__/input/logs/update-guides.log @@ -1,7 +1,7 @@ -{"gid":"us","site":"magticom.ge","count":74,"status":1} -{"gid":"za","site":"dstv.com","count":1,"status":1} -{"gid":"us-pr","site":"tvtv.us","count":14,"status":1} -{"gid":"us-pr","site":"gatotv.com","count":7,"status":1} -{"gid":"us-pr","site":"directv.com","count":1,"status":1} -{"gid":"ca-nl","site":"tvtv.us","count":1,"status":1} -{"gid":"us","site":"tvtv.us","count":372,"status":1} +{"group":"us/magticom.ge","count":74} +{"group":"za/dstv.com","count":1} +{"group":"us-pr/tvtv.us","count":14} +{"group":"us-pr/gatotv.com","count":7} +{"group":"us-pr/directv.com","count":1} +{"group":"ca-nl/tvtv.us","count":1} +{"group":"us/tvtv.us","count":372} diff --git a/tests/commands/update-readme.test.js b/tests/commands/update-readme.test.js index fbd903ee..b15082a2 100644 --- a/tests/commands/update-readme.test.js +++ b/tests/commands/update-readme.test.js @@ -6,7 +6,7 @@ beforeEach(() => { fs.rmdirSync('tests/__data__/output', { recursive: true }) fs.mkdirSync('tests/__data__/output') - execSync( + const stdout = execSync( 'LOGS_DIR=tests/__data__/input/logs node scripts/commands/update-readme.js --config=tests/__data__/input/_readme.json', { encoding: 'utf8' } )