diff --git a/bin/index.js b/bin/index.js index f3cfdc1..99ad101 100644 --- a/bin/index.js +++ b/bin/index.js @@ -1,13 +1,15 @@ #!/usr/bin/env node -const fs = require('fs-extra') -const yargs = require('yargs') -const { basename, extname, join } = require('path') -const { getLatestId, getComic } = require('../lib/xkcd') -const { homePage, comicPage } = require('../lib/html') -const { pad, progress } = require('../lib/helpers') +import fs from 'fs-extra' +import yargs from 'yargs' +import * as url from 'url' +import { hideBin } from 'yargs/helpers' +import { basename, extname, join } from 'path' +import { getLatestId, getComic } from '../lib/xkcd.js' +import { homePage, comicPage } from '../lib/html.js' +import { pad, progress } from '../lib/helpers.js' -const argv = yargs +const argv = yargs(hideBin(process.argv)) .usage('$0', 'Clones XKCD comics. By default it only downloads the missing comics.') .scriptName('xkcd-clone') .option('dir', { @@ -31,8 +33,8 @@ async function write ({ data, img }, dir, latest) { await fs.outputFile(join(dir, 'index.html'), comicPage(data, latest, hasImage)) if (hasImage) { - await fs.outputFile(join(dir, basename(data.img)), img) - await fs.outputFile(join(dir, `image${extname(data.img)}`), img) + await fs.outputFile(join(dir, basename(data.img)), Buffer.from(img)) + await fs.outputFile(join(dir, `image${extname(data.img)}`), Buffer.from(img)) } } catch (err) { await fs.remove(dir) @@ -118,11 +120,13 @@ async function run () { progress('📦 Some comics fetched\n') } + const currDirectory = url.fileURLToPath(new URL('.', import.meta.url)) + added = added.sort((a, b) => a.num - b.num) await fs.remove(join(argv.dir, 'latest')) - await fs.copy(join(argv.dir, latest.toString()), join(argv.dir, 'latest')) - await fs.copyFile(join(__dirname, '../node_modules/tachyons/css/tachyons.min.css'), join(argv.dir, 'tachyons.css')) - await fs.copyFile(join(__dirname, '../node_modules/tachyons-columns/css/tachyons-columns.min.css'), join(argv.dir, 'tachyons-columns.css')) + await fs.copy(join(argv.dir, pad(latest, 4)), join(argv.dir, 'latest')) + await fs.copyFile(join(currDirectory, '../node_modules/tachyons/css/tachyons.min.css'), join(argv.dir, 'tachyons.css')) + await fs.copyFile(join(currDirectory, '../node_modules/tachyons-columns/css/tachyons-columns.min.css'), join(argv.dir, 'tachyons-columns.css')) await fs.outputFile(join(argv.dir, 'index.html'), homePage(added)) } diff --git a/lib/helpers.js b/lib/helpers.js index 25e0117..a05803e 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,4 +1,4 @@ -const progress = (str) => { +export const progress = (str) => { if (process.stdout.isTTY) { process.stdout.clearLine() process.stdout.cursorTo(0) @@ -8,7 +8,7 @@ const progress = (str) => { } } -const pad = (str, max) => { +export const pad = (str, max) => { str = str.toString() return str.length < max ? pad('0' + str, max) : str } @@ -19,12 +19,6 @@ const tagsToReplace = { '>': '>' } -const escapeHtml = (html) => { +export const escapeHtml = (html) => { html.replace(/[&<>]/g, tag => tagsToReplace[tag] || tag) } - -module.exports = { - progress, - escapeHtml, - pad -} diff --git a/lib/html.js b/lib/html.js index 3e8c904..37f804d 100644 --- a/lib/html.js +++ b/lib/html.js @@ -1,5 +1,5 @@ -const { basename } = require('path') -const { pad, escapeHtml } = require('./helpers') +import { basename } from 'path' +import { pad, escapeHtml } from './helpers.js' const credits = '
This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License.
Originally from xkcd.com.