-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-images.js
90 lines (66 loc) · 2.83 KB
/
build-images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const path = require('path');
const fs = require('fs').promises;
const del = require('del');
const { splitFileExtension } = require('../utils/images');
const createLoggerFn = require('./utils/logger');
const createImageRenditions = require('./operations/create-image-renditions');
const exportImageMetadata = require('./operations/export-image-metadata');
const logger = {
debug: createLoggerFn(console.debug, ' '),
info: createLoggerFn(console.info, ' '),
warn: createLoggerFn(console.warn, '⚠️'),
error: createLoggerFn(console.error, '🚨')
};
const getImageFilenames = async (pathToDir, allowedFileTypes) => {
const files = await fs.readdir(pathToDir);
const imageFilenames = files.filter(filename => {
const [extension] = splitFileExtension(filename);
return extension && allowedFileTypes.includes(extension.toLowerCase());
});
return imageFilenames;
};
const buildAllImages = async () => {
try {
logger.debug('Starting images build...');
const { imageRenditions, imageBuildConfig } = JSON.parse(await fs.readFile('./config.json'));
logger.debug('Creating output directories...');
const outputDirJobs = imageBuildConfig.output.map(outputPath => async () => {
await del(outputPath);
await fs.mkdir(path.join(outputPath, 'renditions'), { recursive: true });
});
await Promise.all(outputDirJobs.map(job => job()));
logger.debug(`Finding images in directory ${imageBuildConfig.input}...`, '🔎');
const filenames = await getImageFilenames(imageBuildConfig.input, imageBuildConfig.fileTypes);
logger.debug(`Exporting image metadata for ${filenames.length} images...`);
await exportImageMetadata({ logger}, imageBuildConfig, filenames);
logger.debug(`Creating optimised renditions for ${filenames.length} images...`);
await createImageRenditions({ logger }, imageBuildConfig, filenames, imageRenditions);
logger.debug('Finished building images', '✅');
} catch (error) {
logger.error(error);
logger.error('Something went wrong, terminating process');
setImmediate(process.exit.bind(process, 1));
}
};
const buildSingleImage = async (filename) => {
try {
logger.debug('Starting single image build...');
const { imageRenditions, imageBuildConfig } = JSON.parse(await fs.readFile('./config.json'));
logger.debug(`Creating optimised renditions for ${filename}...`);
await createImageRenditions({ logger }, imageBuildConfig, [filename], imageRenditions);
logger.debug('Finished building image', '✅');
} catch (error) {
logger.error(error);
logger.error('Something went wrong, terminating process');
setImmediate(process.exit.bind(process, 1));
}
};
const exec = async () => {
const args = process.argv.slice(2);
if (args.length > 0) {
await buildSingleImage(args[0]);
} else {
await buildAllImages();
}
};
exec();