-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate-pngs.js
99 lines (85 loc) · 2.7 KB
/
generate-pngs.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
91
92
93
94
95
96
97
98
99
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const isSVG = filePath => {
const fileStats = fs.lstatSync(filePath);
return fileStats.isFile() && path.parse(filePath).ext === '.svg';
};
const extractSVGPaths = fileOrFolder => {
const isDir = fs.lstatSync(fileOrFolder).isDirectory();
if (isDir) {
return fs
.readdirSync(fileOrFolder)
.map(fileName => path.join(fileOrFolder, fileName))
.filter(isSVG);
}
if (isSVG(fileOrFolder)) {
return [fileOrFolder];
}
throw new Error('input path is neither an SVG file nor a directory');
};
const generate = async (inputPath, outputPath, sizes, cmd) => {
if (!inputPath) {
throw new Error(
`input path not provided. Provide an input path as the first argument to the script. Try running: ${cmd} [file|folder]`
);
}
const srcPath = path.resolve(__dirname, inputPath);
const outPath = outputPath ? path.resolve(__dirname, outputPath) : undefined;
const svgPaths = extractSVGPaths(srcPath);
await Promise.all(
svgPaths.map(svgPath => {
const imageName = path.parse(svgPath).name;
const outDir = path.join(outPath || path.dirname(svgPath), imageName);
const image = sharp(svgPath);
return image
.metadata()
.then(({ width }) => {
fs.mkdirSync(outDir, { recursive: true });
return Promise.all(
sizes.map(size => {
const outName = size !== 1 ? `${imageName}@${size}x` : imageName;
const fileOutPath = path.join(outDir, `${outName}.png`);
console.log(`${svgPath} -> ${fileOutPath}`);
return image
.resize(Math.round(width * size))
.png()
.toFile(fileOutPath);
})
);
})
.catch(e => {
console.error(`Unable to process image ${path.basename(svgPath)}`);
throw e;
});
})
);
};
const args = yargs(hideBin(process.argv))
.option('out-dir', {
alias: 'o',
type: 'string',
description: 'Specify where the generated PNGs should be written',
})
.option('sizes', {
alias: 's',
type: 'array',
default: [1, 1.5, 2, 2.5, 3, 3.5, 4],
description: 'Image density sizes to generate',
choices: [1, 1.5, 2, 2.5, 3, 3.5, 4],
})
.command(
'[path]',
'generate PNG(s). If [path] is a folder it generates a PNG for every SVG in the folder'
)
.help().argv;
generate(args._[0], args['out-dir'], args.sizes, `${process.argv0} ${args.$0}`)
.then(() => {
process.exit(0);
})
.catch(e => {
console.error(e);
process.exit(1);
});