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

✨ ignore failed images for non-production Netlify deploys #654

Merged
Merged
Show file tree
Hide file tree
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
68 changes: 68 additions & 0 deletions download-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const {promisify} = require('util');
const {pipeline: pipeline_} = require('stream');
const {createWriteStream} = require('fs');
const {mkdir, unlink} = require('fs').promises;
const path = require('path');
const {performance} = require('perf_hooks');
const Queue = require('p-queue').default;
const chalk = require('chalk');
const got = require('got').default.extend({
timeout: 30000,
retry: 1
});

const pipeline = promisify(pipeline_);

const getTime = start => {
const end = performance.now();

const diff = end - start;
let humanTime = `${diff.toFixed(2)}ms`;

if (diff > 60000) {
humanTime = `${(diff / 60000).toFixed(0)}m`;
} else if (diff > 1000) {
humanTime = `${(diff / 1000).toFixed(1)}s`;
}

return humanTime;
};

async function downloadSingleImage({url, errors, outFile}) {
const start = performance.now();
try {
console.log(`${chalk.yellow('Downloading')} ${chalk.cyan(url)} to ${chalk.magenta(outFile)}`);
await pipeline(got.stream({url}), createWriteStream(outFile));
const time = getTime(start);
console.log(`${chalk.green('Downloaded')} ${chalk.cyan(url)} [${time}]`);
} catch (error) {
const time = getTime(start);
console.log(`${chalk.red('Failed downloading')} ${chalk.cyan(url)} [${time}]: ${error.message}`);
errors.push(error);

try {
await unlink(outFile);
} catch {}
}
}

module.exports = async function (list, dest) {
const queue = new Queue({concurrency: 15});
const errors = [];

await mkdir(dest, {recursive: true});

for (const {url, file} of list) {
queue.add(() => downloadSingleImage({url, errors, outFile: path.join(dest, file)}));
}

await queue.onIdle();

const totalErrors = errors.length;
const errorText = totalErrors > 0 ? chalk.red(totalErrors) : chalk.green(totalErrors);
const plural = totalErrors === 1 ? '' : 's';

console.log(`Downloaded swag-images with ${errorText} error${plural}`);

return !(!process.env.NETLIFY || (process.env.NETLIFY && process.env.CONTEXT === 'production'));
};
10 changes: 6 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const htmlmin = require('gulp-htmlmin');
const stylus = require('gulp-stylus');
const webserver = require('gulp-webserver');
const concat = require('gulp-concat');
const download = require('gulp-download-stream');
const responsive = require('gulp-responsive');
const merge = require('merge-stream');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const {readFile} = require('fs').promises;

const {swagList, swagImages} = require('./get-data');
const downloadImages = require('./download-images');

const PRODUCTION = process.env.NODE_ENV === 'production';

Expand Down Expand Up @@ -120,9 +120,11 @@ gulp.task('swag-img:clean', () => {
return del('dist/assets/swag-img/*');
});

gulp.task('swag-img:download', () => {
return download(swagImages)
.pipe(gulp.dest('dist/assets/swag-img'));
gulp.task('swag-img:download', async () => {
const success = await downloadImages(swagImages, 'dist/assets/swag-img');
if (!success) {
throw new Error('Failed to download images');
}
});

gulp.task('swag-img:optimize', cb => {
Expand Down
Loading