diff --git a/index.js b/index.js index 2836113..eed5cad 100644 --- a/index.js +++ b/index.js @@ -7,14 +7,24 @@ const prettyMs = require('pretty-ms'); const getMetaTag = (html, property) => { const regex = new RegExp(`]*property=["|']${property}["|'][^>]*>`, 'i'); + const results = regex.exec(html); - return regex.exec(html)[0]; + if (!results) { + throw new Error(`Missing ${property}`); + } + + return results[0]; }; const getMetaTagContent = metaTagHtml => { - const regex = /content=["]([^"]*)["]/i; + const contentRegex = /content=["]([^"]*)["]/i; + const results = contentRegex.exec(metaTagHtml); + + if (!results) { + throw new Error(`Missing content attribute in ${chalk.bold(metaTagHtml)}`); + } - return regex.exec(metaTagHtml)[1]; + return results[1]; }; module.exports = bundler => { @@ -23,28 +33,41 @@ module.exports = bundler => { return; } console.log(''); + const spinner = ora(chalk.grey('Fixing og:image link')).start(); const start = Date.now(); const htmlPath = path.join(bundler.options.outDir, 'index.html'); const html = fs.readFileSync(htmlPath).toString(); + let hasErrors = false; - const ogImageTag = getMetaTag(html, 'og:image'); - const ogImageContent = getMetaTagContent(ogImageTag); + try { + const ogImageTag = getMetaTag(html, 'og:image'); + const ogImageContent = getMetaTagContent(ogImageTag); - const ogUrlTag = getMetaTag(html, 'og:url'); - const ogUrlContent = getMetaTagContent(ogUrlTag); + const ogUrlTag = getMetaTag(html, 'og:url'); + const ogUrlContent = getMetaTagContent(ogUrlTag); - const absoluteOgImageUrl = url.resolve(ogUrlContent, ogImageContent); - const ogImageTagAbsoluteUrl = ogImageTag.replace(ogImageContent, absoluteOgImageUrl); - const patchedHtml = html.replace(ogImageTag, ogImageTagAbsoluteUrl); + const absoluteOgImageUrl = url.resolve(ogUrlContent, ogImageContent); + const ogImageTagAbsoluteUrl = ogImageTag.replace(ogImageContent, absoluteOgImageUrl); + const patchedHtml = html.replace(ogImageTag, ogImageTagAbsoluteUrl); - fs.writeFileSync(htmlPath, patchedHtml); + fs.writeFileSync(htmlPath, patchedHtml); + } catch (error) { + spinner.fail(error.message); + + hasErrors = true; + } const end = Date.now(); + const symbol = hasErrors ? chalk.red('✖') : '✨ '; + const text = hasErrors ? + chalk.red('Failed to fix og:image link.') : + chalk.green(`Fixed og:image link in ${prettyMs(end - start)}.`); + spinner.stopAndPersist({ - symbol: '✨ ', - text: chalk.green(`Fixed og:image link in ${prettyMs(end - start)}.`) + symbol, + text }); }); };