Last active
July 28, 2021 16:59
-
-
Save RussellDash332/73ebeddb1228bfd3f07d77f2acaaa8f8 to your computer and use it in GitHub Desktop.
index.js for my first Medium post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const Mustache = require('mustache'); | |
const fetch = require('node-fetch'); | |
const fs = require('fs'); | |
const MUSTACHE_MAIN_DIR = './main.mustache'; | |
let DATA = { | |
name: 'Russell', | |
date: new Date().toLocaleDateString('en-GB', { | |
weekday: 'long', | |
month: 'long', | |
day: 'numeric', | |
hour: 'numeric', | |
minute: 'numeric', | |
timeZoneName: 'short', | |
timeZone: 'Asia/Singapore', | |
}), | |
}; | |
async function setWeatherInformation() { | |
await fetch( | |
`https://api.openweathermap.org/data/2.5/weather?q=Singapore&appid=${process.env.OPEN_WEATHER_MAP_KEY}&units=metric` | |
) | |
.then(r => r.json()) | |
.then(r => { | |
DATA.temperature = Math.round(r.main.temp); | |
DATA.weather = r.weather[0].description; | |
}); | |
} | |
async function generateReadMe() { | |
await fs.readFile(MUSTACHE_MAIN_DIR, (err, data) => { | |
if (err) throw err; | |
const output = Mustache.render(data.toString(), DATA); | |
fs.writeFileSync('README.md', output); | |
}); | |
} | |
async function action() { | |
await setWeatherInformation(); | |
await generateReadMe(); | |
} | |
action(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment