diff --git a/index.js b/index.js index c322979..eae555f 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,29 @@ const fetchId = (id) => { .then(res => res.json()) } +const htmlPage = ({ alt, title, transcript, num, img }) => { + const btnClass = 'dib navy mh2 pa2 bg-light-blue bg-animate hover-bg-lightest-blue br2 ba bw1 b--navy no-underline' + + return ` + + ${num} - ${title} + + + +

${title} #${num}

+ + + + ${alt} +

${transcript}

+ +` +} + const download = async (baseDir, i) => { const num = pad(i, 4) const res = await fetchId(i) @@ -40,35 +63,31 @@ const download = async (baseDir, i) => { await fs.remove(dir) throw err } + + return { + id: i, + title: res.title, + num: num + } } -const htmlPage = ({ alt, title, transcript, num, img }) => { - const btnClass = 'dib navy pa2 bg-light-blue bg-animate hover-bg-blue br2 ba bw1 b--navy no-underline' - - return ` +const homePage = (list) => ` - ${num} - ${title} + XKCD - -

${title} #${num}

+ +

XKCD

- - - ${alt} -

${transcript}

+ ${list.map(({ id, title, num }) => `${id} - ${title}`).join('\n')} ` -} const clone = async ({ baseDir, empty, onlyMissing }) => { console.log(`😊 Going to clone XKCD to ${baseDir}`) let errored = [] + let added = [] try { console.log(`🔍 Finding the latest comic`) @@ -83,7 +102,7 @@ const clone = async ({ baseDir, empty, onlyMissing }) => { existent = fs.readdirSync(baseDir).map(v => Number(v.split('-')[0].trim())) } - for (let i = 1; i <= latest + 1; i++) { + for (let i = 1; i <= 10 + 1; i++) { if (existent.includes(i)) { continue } else if (i === 404) { @@ -97,7 +116,7 @@ const clone = async ({ baseDir, empty, onlyMissing }) => { } try { - await download(baseDir, i) + added.push(await download(baseDir, i)) } catch (err) { errored.push(i) } @@ -110,7 +129,7 @@ const clone = async ({ baseDir, empty, onlyMissing }) => { for (const num of errored) { for (let i = 0; i < 3; i++) { try { - await download(baseDir, i) + added.push(await download(baseDir, i)) break } catch (err) { if (i === 2) { @@ -119,6 +138,9 @@ const clone = async ({ baseDir, empty, onlyMissing }) => { } } } + + added = added.sort((a, b) => a.num - b.num) + await fs.outputFile(join(baseDir, 'index.html'), homePage(added)) } clone({ diff --git a/package.json b/package.json index 928e13d..1eb7a61 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dependencies": { "fs-extra": "^7.0.1", "node-fetch": "^2.4.0", + "tachyons": "^4.11.1", "yargs": "^13.2.2" } }