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.

' @@ -9,7 +9,7 @@ const classes = { btn: 'dib navy mh2 pa2 bg-light-blue hover-bg-lightest-blue br2 ba bw1 b--navy no-underline' } -const comicPage = ({ alt, title, transcript, num, img }, latest) => ` +export const comicPage = ({ alt, title, transcript, num, img }, latest) => ` ${num} - ${title} @@ -33,7 +33,7 @@ const comicPage = ({ alt, title, transcript, num, img }, latest) => ` ` -const homePage = (list) => ` +export const homePage = (list) => ` XKCD @@ -71,8 +71,3 @@ const homePage = (list) => ` ${credits} ` - -module.exports = { - comicPage, - homePage -} diff --git a/lib/xkcd.js b/lib/xkcd.js index a32496f..79701b0 100644 --- a/lib/xkcd.js +++ b/lib/xkcd.js @@ -1,7 +1,7 @@ -const fetch = require('node-fetch') -const path = require('path') +import fetch from 'node-fetch' +import path from 'path' -async function getLatestId () { +export async function getLatestId () { const raw = await fetch('https://xkcd.com/info.0.json') const data = await raw.json() return data.num @@ -20,10 +20,10 @@ async function getImage (url) { throw new Error('bad image request') } - return res.buffer() + return res.arrayBuffer() } -async function getComic (id) { +export async function getComic (id) { const raw = await fetch(`https://xkcd.com/${id}/info.0.json`) const data = await raw.json() let img = null @@ -36,8 +36,3 @@ async function getComic (id) { return { data, img } } - -module.exports = { - getLatestId, - getComic -} diff --git a/package.json b/package.json index 0ea4ef7..48001c7 100644 --- a/package.json +++ b/package.json @@ -3,16 +3,17 @@ "version": "1.2.1", "description": "Clone xkcd comics.", "main": "./lib/index.js", + "type": "module", "bin": { "xkcd-clone": "./bin/index.js" }, "license": "MIT", "author": "Henrique Dias ", "dependencies": { - "fs-extra": "^9.0.1", - "node-fetch": "^2.6.1", + "fs-extra": "^10.1.0", + "node-fetch": "^3.2.10", "tachyons": "^4.12.0", "tachyons-columns": "^1.0.5", - "yargs": "^17.0.0" + "yargs": "^17.5.1" } }