forked from scionoftech/webp-converter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpconverter.js
executable file
·85 lines (73 loc) · 1.91 KB
/
webpconverter.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
const exec = require('child_process').execFile; // get child_process module
/**
* @param {string} command — name of binary package to be executed
* @return {function(string, string, string|null): Promise<string>}
*/
const command = function (command) {
return function (inputImage, outputImage, options = '') {
return new Promise((resolve, reject) => {
/**
* Detect OS
* @type {string}
*/
const executor = getExecutorPath(command);
if (!executor) {
reject(`Unsupported platform: ${process.platform} ${process.arch}`);
return;
}
/**
* Compose query array
* @type {Array}
*/
let query = [];
query.push(inputImage);
if (options) {
options.split(/\s+/).forEach(option => {
query.push(option);
});
}
query.push('-o');
query.push(outputImage);
/**
* Run target command
*/
exec(getExecutorPath(command), query, function (error, stdout, stderr) {
if (error){
reject(error);
return;
}
resolve(outputImage);
});
});
}
};
/**
* Compose path to binary file or return null
* @param {string} appName
* @return {string|null}
*/
const getExecutorPath = (appName) => {
switch (process.platform) {
/** macOS */
case 'darwin':
return `${__dirname}/lib/libwebp_osx/bin/${appName}`;
/** Linux */
case 'linux':
return `${__dirname}/lib/libwebp_linux/bin/${appName}`;
/** Windows */
case 'win32':
if (process.arch === 'x64') {
return `${__dirname}/lib/libwebp_win64/bin/${appName}.exe`;
}
return `${__dirname}/lib/libwebp_win32/bin/${appName}.exe`;
/** Any other systems */
default:
return;
}
};
module.exports = {
cwebp: command('cwebp'),
dwebp: command('dwebp'),
// gif2webp: command('gif2webp'),
// img2webp: command('img2webp'),
};