Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: do not fail and exit properly on errors #4

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ const prettyMs = require('pretty-ms');

const getMetaTag = (html, property) => {
const regex = new RegExp(`<meta[^>]*property=["|']${property}["|'][^>]*>`, 'i');
const result = regex.exec(html);

return regex.exec(html)[0];
if (!result) {
throw new Error(`Missing ${property}`);
}

return result[0];
};

const getMetaTagContent = metaTagHtml => {
const regex = /content=["]([^"]*)["]/i;
const contentRegex = /content=["]([^"]*)["]/i;
const result = contentRegex.exec(metaTagHtml);

if (!result) {
throw new Error(`Missing content attribute in ${chalk.bold(metaTagHtml)}`);
}

return regex.exec(metaTagHtml)[1];
return result[1];
};

module.exports = bundler => {
Expand All @@ -23,28 +33,41 @@ module.exports = bundler => {
return;
}
console.log('');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, good catch


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('✖') : '✨ ';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a space in '✖' here to keep the style consistent with the '✨ ' symbol?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it looks better without a space, as spinner.fail does not have a space either:

Screenshot 2020-03-19 at 17 20 38

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
});
});
};