-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlayaair-cmd-guetzli.js
152 lines (133 loc) · 2.89 KB
/
layaair-cmd-guetzli.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const path = require("path");
const program = require("commander");
const fs = require("fs");
const os = require("os");
const spawn = require("child_process").spawn;
const
{
printOk,
printErr,
printQuotation,
tr
} = require("./print.js");
program
.version("0.1.1")
.option('-i --input <input>', tr("resource directory."))
.option('-q --quality <quality>', tr("quality, more than 84."))
.parse(process.argv);
let resource_dir;
// specify resource directory via -p
if (program.input)
resource_dir = program.input;
// specify project directory as positional argument
else if (program.args.length == 1)
resource_dir = program.args[0];
if (!resource_dir)
{
printErr("You must specify resource directory");
process.exit(1);
}
resource_dir = path.resolve(resource_dir);
//////////////////////////////
// traverse input directory //
//////////////////////////////
let files = [];
readdir(resource_dir);
function readdir(dir)
{
let result = fs.readdirSync(dir);
result.forEach(function(filename)
{
let absFile = path.join(dir, filename);
let stats = fs.statSync(absFile);
if (stats.isFile())
{
if (filename.endsWith(".jpeg") ||
filename.endsWith(".jpg"))
files.push(absFile);
}
else if (stats.isDirectory())
{
readdir(absFile);
}
});
}
if(files.length == 0) exit(0);
files.forEach(function(file)
{
printQuotation(file);
});
encodeFile(files.pop());
function encodeFile(file)
{
let ext_name = path.extname(file);
let base_name = path.basename(file, ext_name);
let out_name = path.join(path.dirname(file), `${base_name}_compressed${ext_name}`);
encode(file, out_name, function(err_code)
{
if(files.length)
encodeFile(files.pop());
if (err_code)
{
printErr(`Failed to encode ${file}`)
}
else
{
fs.unlink(file, (err) =>
{
if (err)
{
printErr(`Failed to delete ${file}`);
}
else
{
fs.rename(out_name, file, (err) =>
{
if (err)
printErr(`Failed to rename ${file}`);
else
printOk(`${file} compressed.`);
});
}
});
}
});
};
//////////////////////////////////////////////////
// call external excutable web-resource-manager //
//////////////////////////////////////////////////
function encode(f_in, f_out, onclose)
{
let args = [];
if (program.quality)
args.push("--quality", program.quality);
args.push(f_in, f_out);
let executable;
switch(os.platform())
{
case "win32":
executable = "guetzli_windows_x86-64.exe";
break;
case "linux":
executable = "guetzli_linux_x86-64";
break;
case "darwin":
executable = "guetzli_darwin_x86-64";
break;
default:
printErr("Platform does not support.");
exit(1);
}
let sp = spawn(
path.join(__dirname, "tools", "guetzli", executable),
args);
// sp.stdout.on("data", (data) =>
// {
// printQuotation(data.toString());
// });
sp.stderr.on("data", (data) =>
{
printErr(data.toString());
});
sp.on("close", onclose);
}