Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
102 lines (95 loc) · 6.42 KB
/
Copy pathcli.js
File metadata and controls
102 lines (95 loc) · 6.42 KB
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
#!/usr/bin/env node
// art-preflight CLI.
// node cli.js check <folder> [--design WxH] [--out <dir>] [--fast]
// node cli.js fix <folder> [--out <dir>]
// node cli.js apply <folder> --ids <id,id,...> [--findings <findings.json>] [--out <dir>]
// Only the findings a person picked; there is no "apply everything" switch.
// Writes out/<utc>-<slug>/findings.json, report.html and images/ (copies of the pictures the report
// shows); fix also writes fixed/. Never writes into the input folder.
"use strict";
var fs = require("fs");
var path = require("path");
var { check } = require("./lib/check");
var { fix } = require("./lib/fix");
var { html, copyImages } = require("./lib/report");
var { apply } = require("./lib/apply");
function main(argv) {
var cmd = argv[0];
var args = { _: [] };
for (var i = 1; i < argv.length; i += 1) {
if (argv[i].slice(0, 2) === "--") {
var name = argv[i].slice(2);
if (name === "fast") { args[name] = true; continue; }
args[name] = argv[i + 1]; i += 1;
}
else args._.push(argv[i]);
}
var input = args._[0];
if ((cmd !== "check" && cmd !== "fix" && cmd !== "apply") || !input) {
console.log("usage: node cli.js check|fix <folder> [--design WxH] [--out <dir>] [--fast]\n node cli.js apply <folder> --ids <id,...> [--findings <findings.json>] [--out <dir>]");
return 2;
}
var root = path.resolve(input);
if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) { console.error("不是資料夾:" + input); return 2; }
var design = null;
if (args.design) {
var m = /^(\d+)\s*[x×*]\s*(\d+)$/i.exec(args.design);
if (!m) { console.error("--design 要寫成 寬x高,例如 1280x720"); return 2; }
design = { w: Number(m[1]), h: Number(m[2]), source: "--design 參數" };
}
if (cmd === "apply" && !args.ids) { console.error("apply 要給 --ids:報告的「可以自動修正的項目」列出編號,選好要修的再執行"); return 2; }
var doc = args.findings ? JSON.parse(fs.readFileSync(path.resolve(args.findings), "utf8")) : check(root, { design: design, fast: !!args.fast });
if (args.fast) doc.summary.push("--fast:沒有解碼 PNG,soft-downscale 和圖集透明邊沒有執行。");
var stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z");
var slug = doc.input.name.replace(/[^\w.-]+/g, "-").slice(0, 40) || "input";
var outDir = path.resolve(args.out || path.join(__dirname, "out"), stamp + "-" + slug);
var rel = path.relative(root, outDir);
if (!rel.startsWith("..") && !path.isAbsolute(rel)) { console.error("輸出資料夾不能在輸入資料夾裡面"); return 2; }
fs.mkdirSync(outDir, { recursive: true });
if (cmd === "fix") {
var last = 0;
doc.fixed = fix(root, doc, outDir, { progress: function (i, n) {
var now = Date.now();
if (process.stderr.isTTY && (now - last > 500 || i === n)) { last = now; process.stderr.write("\rfix " + i + "/" + n + (i === n ? "\n" : "")); }
} });
var ok = doc.fixed.filter(function (x) { return x.pixelsSame; });
var saved = ok.reduce(function (s, x) { return s + x.before - x.after; }, 0);
var nMeta = ok.filter(function (x) { return x.steps.indexOf("metadata") >= 0; }).length;
var nPng = ok.filter(function (x) { return x.steps.some(function (s) { return /^recompress/.test(s); }); }).length;
doc.summary.push("fix:寫出 " + ok.length + " 個檔案到 fixed/(拿掉中繼資料 " + nMeta + " 個、PNG 重新壓縮 " + nPng + " 個),共少了 " + (saved >= 1048576 ? Math.round(saved / 104857.6) / 10 + " MB" : Math.round(saved / 1024) + " KB") + "(" + doc.runtime.downloadLabel + " 約快 " + Math.round(saved * 8 / (doc.runtime.downloadMbps * 1e6) * 10) / 10 + " 秒),解碼後像素逐位元組相同" + (ok.length < doc.fixed.length ? ";" + (doc.fixed.length - ok.length) + " 個比對不同,沒有寫出" : "") + "。");
}
if (cmd === "apply") {
var ids = String(args.ids).split(",").map(function (x) { return x.trim(); }).filter(Boolean);
var res = apply(root, doc, ids, outDir);
var T0 = JSON.parse(fs.readFileSync(path.join(__dirname, "protocol", "thresholds.json"), "utf8"));
fs.writeFileSync(path.join(outDir, "applied.json"), JSON.stringify({ tool: "art-preflight", version: require("./package.json").version, findingsVersion: doc.version, input: doc.input.name,
thresholds: { sampleSec: T0.fillLayers.sampleSec, alphaVisible: T0.fillLayers.alphaVisible, alphaHiddenMax: T0.alphaHidden.maxAlpha, minSec: T0.alphaHidden.minSec }, results: res }, null, 2));
// The report again, now with what apply wrote.
doc.applied = res;
fs.writeFileSync(path.join(outDir, "findings.json"), JSON.stringify(doc, null, 2));
fs.writeFileSync(path.join(outDir, "report.html"), html(doc, { images: copyImages(root, doc, outDir) }));
console.log("report: " + path.join(outDir, "report.html"));
res.forEach(function (r) {
console.log((r.ok ? "✓ " : "✗ ") + (r.file || r.id) + (r.ok ? ":" + (r.changes || []).length + " 處" : ":" + r.reason));
(r.skipped || []).forEach(function (x) { console.log(" 略過 " + x); });
});
var hw = res.filter(function (r) { return r.ok && r.hiddenDrawn; });
if (hw.length) {
var hb = hw.reduce(function (s, r) { return s + r.hiddenDrawn.before; }, 0), ha = hw.reduce(function (s, r) { return s + r.hiddenDrawn.after; }, 0);
console.log("- 看不見卻還在畫的量:" + Math.round(hb * 10) / 10 + " → " + Math.round(ha * 10) / 10 + " " + hw[0].hiddenDrawn.unit + "(" + hw.length + " 個骨架;每段動畫逐格比對過,看得到的畫面不變)");
}
console.log("applied: " + path.join(outDir, "applied.json"));
console.log("fixed: " + path.join(outDir, "fixed"));
return res.some(function (r) { return r.ok; }) ? 0 : 1;
}
fs.writeFileSync(path.join(outDir, "findings.json"), JSON.stringify(doc, null, 2));
// The report shows the input's own pictures, copied to images/ so the folder can be passed on.
fs.writeFileSync(path.join(outDir, "report.html"), html(doc, { images: copyImages(root, doc, outDir) }));
doc.summary.forEach(function (s) { console.log("- " + s); });
console.log("findings: " + path.join(outDir, "findings.json"));
console.log("report: " + path.join(outDir, "report.html"));
if (cmd === "fix") console.log("fixed: " + path.join(outDir, "fixed"));
return 0;
}
if (require.main === module) process.exitCode = main(process.argv.slice(2));
module.exports = { main: main };