diff --git a/dark-sky.js b/dark-sky.js index 3299358..5519528 100644 --- a/dark-sky.js +++ b/dark-sky.js @@ -1,7 +1,7 @@ const log = console.log const fs = require('fs') const {table, getBorderCharacters} = require('table') -const argv = require('yargs').argv +// const argv = require('yargs').argv const TABLE_CONFIG = { // columns: { @@ -14,7 +14,7 @@ const TABLE_CONFIG = { const HIDE_PRECIP_LESS_THAN = 10 // const CREDIT_MSG = "Powered by Dark Sky: https://darksky.net/poweredby/" // const CREDIT_MSG = "https://darksky.net/poweredby • https://smol.gq/wthr-src\n" -const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n" +const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n\n" // https://medium.com/@osiolabs/read-write-json-files-with-node-js-92d03cc82824 const jsonReader = (filePath, cb) => { @@ -33,17 +33,18 @@ const jsonReader = (filePath, cb) => { const round5 = x => { return Math.ceil(x / 5) * 5 } -const logWeatherJson = (jsonFile, loc) => { +const getWeatherTable = (jsonFile, loc, orientation) => { return new Promise((resolve, reject) => { jsonReader(jsonFile, (err, weatherInfo) => { if (err) { reject(err) } - log(loc) + let output = loc + '\n' + // log(loc) weatherInfo['hourly']['data'][0] = weatherInfo['currently'] - if (argv.v) { + if (orientation == 'v') { const infoList = [['H', '°C', 'R','%P']] for (let i = 0; i < 12; i++) { const hourInfo = weatherInfo['hourly']['data'][i] @@ -58,7 +59,8 @@ const logWeatherJson = (jsonFile, loc) => { precipText, ]) } - log(table(infoList, TABLE_CONFIG)); + output += table(infoList, TABLE_CONFIG) + // log(table(infoList, TABLE_CONFIG)) } else { // horizontal const hoursList = ['H'] @@ -79,17 +81,33 @@ const logWeatherJson = (jsonFile, loc) => { const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1))) tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+` tempsList[minTempIndex] = `${tempsList[minTempIndex]}-` - log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG)); + output += table([hoursList, tempsList, rainList, precipList], + TABLE_CONFIG) + // log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG)) } - resolve() + resolve(output) }) }) } const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit } -log(CREDIT_MSG) -logTablePromises = [ - logWeatherJson('./toronto.json', 'Toronto'), - logWeatherJson('./markham.json', 'Markham'), + +const getTablePromises = [ + getWeatherTable('./markham.json', 'Markham', 'h'), + getWeatherTable('./markham.json', 'Markham', 'v'), + getWeatherTable('./toronto.json', 'Toronto', 'h'), + getWeatherTable('./toronto.json', 'Toronto', 'v'), ] -Promise.all(logTablePromises).then(results => {}) +Promise.all(getTablePromises).then(tables => { + const mText = tables[0] + const mvText = tables[1] + const tText = tables[2] + const tvText = tables[3] + const writeErrorHandler = error => { if (error) { throw error } } + fs.writeFile('mt.txt', CREDIT_MSG + mText + '\n' + tText, writeErrorHandler) + fs.writeFile('mtv.txt', CREDIT_MSG + mvText + '\n' + tvText, writeErrorHandler) + fs.writeFile('m.txt', CREDIT_MSG + mText, writeErrorHandler) + fs.writeFile('mv.txt', CREDIT_MSG + mvText, writeErrorHandler) + fs.writeFile('t.txt', CREDIT_MSG + tText, writeErrorHandler) + fs.writeFile('tv.txt', CREDIT_MSG + tvText, writeErrorHandler) +}) diff --git a/dark-sky.sh b/dark-sky.sh index c27ec62..346637b 100755 --- a/dark-sky.sh +++ b/dark-sky.sh @@ -12,5 +12,4 @@ update-weather-json() { update-weather-json "43.8180904,-79.3350555" "markham" update-weather-json "43.6596426,-79.3976676" "toronto" -node dark-sky.js > dark-sky.txt -# node dark-sky.js +node dark-sky.js