Commit 99a4b5bc authored by wildfirecode's avatar wildfirecode

1

parents
node_modules
\ No newline at end of file
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/dbtinypng.js",
"args": [
"--input",
"./a",
"--output",
"./aOutput",
],
// "args": [
// "--input",
// "./src/Resource",
// "--output",
// "./src/ResourceOutput",
// ],
// "cwd": "/Users/wanghongyuan/duiba-games/game/game3dDemo/"
"cwd": "/Users/wanghongyuan/testtinypng"
}
]
}
\ No newline at end of file
#!/usr/bin/env node
var argv = require('yargs')
.option('o', {
alias: 'output',
demand: true,
describe: '输出路径',
type: 'string'
})
.option('i', {
alias: 'input',
demand: true,
describe: '输入路径',
type: 'string'
})
.usage('Usage: dbtinypng [options]')
.example('dbtinypng -i ./input -o ./output')
.help('h')
.alias('h', 'help')
.argv;
const tinypng =require('./tinypng');
tinypng(argv.i,argv.o);
\ No newline at end of file
const APIs = [
'q2A1B6tuX79ojovWKgRIJGXsC5NY0g3A',
'0el5fWM1LGmLbcisgrATCKARdy3fgMHG'
];
module.exports = (imgNums, callback) => {
require('./validateAll')(APIs, (results) => {
results.forEach(val => {
val[1] = 500 - 10 - val[1];
});
console.log(`剩余配额信息:${JSON.stringify(results)}`)
const ret = [];
let left = imgNums;
for (const item of results) {
const [key, amount] = item;
if (amount >= left) {
ret.push([key, left]);
left = 0;
break;
} else {
left -= amount;
ret.push([key, amount]);
}
}
if (left > 0)
throw new Error('配额不够了,再去申请些API');
console.log(`将消耗配额信息:${JSON.stringify(ret)}`)
callback(ret);
})
}
\ No newline at end of file
{
"name": "dbtinypng",
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"ansi-regex": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/ansi-regex/download/ansi-regex-3.0.0.tgz",
"integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg="
},
"camelcase": {
"version": "4.1.0",
"resolved": "http://registry.npm.taobao.org/camelcase/download/camelcase-4.1.0.tgz",
"integrity": "sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0="
},
"cliui": {
"version": "4.1.0",
"resolved": "http://registry.npm.taobao.org/cliui/download/cliui-4.1.0.tgz",
"integrity": "sha1-NIQi2+gtgAswIu709qwQvy5NG0k=",
"requires": {
"string-width": "^2.1.1",
"strip-ansi": "^4.0.0",
"wrap-ansi": "^2.0.0"
}
},
"code-point-at": {
"version": "1.1.0",
"resolved": "http://registry.npm.taobao.org/code-point-at/download/code-point-at-1.1.0.tgz",
"integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c="
},
"cross-spawn": {
"version": "5.1.0",
"resolved": "http://registry.npm.taobao.org/cross-spawn/download/cross-spawn-5.1.0.tgz",
"integrity": "sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk=",
"requires": {
"lru-cache": "^4.0.1",
"shebang-command": "^1.2.0",
"which": "^1.2.9"
}
},
"decamelize": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/decamelize/download/decamelize-2.0.0.tgz",
"integrity": "sha1-ZW17vICUxMeI6lPFhAkIycfQY8c=",
"requires": {
"xregexp": "4.0.0"
}
},
"execa": {
"version": "0.7.0",
"resolved": "http://registry.npm.taobao.org/execa/download/execa-0.7.0.tgz",
"integrity": "sha1-lEvs00zEHuMqY6n68nrVpl/Fl3c=",
"requires": {
"cross-spawn": "^5.0.1",
"get-stream": "^3.0.0",
"is-stream": "^1.1.0",
"npm-run-path": "^2.0.0",
"p-finally": "^1.0.0",
"signal-exit": "^3.0.0",
"strip-eof": "^1.0.0"
}
},
"find-up": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/find-up/download/find-up-3.0.0.tgz",
"integrity": "sha1-SRafHXmTQwZG2mHsxa41XCHJe3M=",
"requires": {
"locate-path": "^3.0.0"
}
},
"get-caller-file": {
"version": "1.0.2",
"resolved": "http://registry.npm.taobao.org/get-caller-file/download/get-caller-file-1.0.2.tgz",
"integrity": "sha1-9wLmMSfn4jHBYKgMFVSstw1QR+U="
},
"get-stream": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/get-stream/download/get-stream-3.0.0.tgz",
"integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="
},
"invert-kv": {
"version": "1.0.0",
"resolved": "http://registry.npm.taobao.org/invert-kv/download/invert-kv-1.0.0.tgz",
"integrity": "sha1-EEqOSqym09jNFXqO+L+rLXo//bY="
},
"is-fullwidth-code-point": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/is-fullwidth-code-point/download/is-fullwidth-code-point-2.0.0.tgz",
"integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8="
},
"is-stream": {
"version": "1.1.0",
"resolved": "http://registry.npm.taobao.org/is-stream/download/is-stream-1.1.0.tgz",
"integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ="
},
"isexe": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/isexe/download/isexe-2.0.0.tgz",
"integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA="
},
"lcid": {
"version": "1.0.0",
"resolved": "http://registry.npm.taobao.org/lcid/download/lcid-1.0.0.tgz",
"integrity": "sha1-MIrMr6C8SDo4Z7S28rlQYlHRuDU=",
"requires": {
"invert-kv": "^1.0.0"
}
},
"locate-path": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/locate-path/download/locate-path-3.0.0.tgz",
"integrity": "sha1-2+w7OrdZdYBxtY/ln8QYca8hQA4=",
"requires": {
"p-locate": "^3.0.0",
"path-exists": "^3.0.0"
}
},
"lru-cache": {
"version": "4.1.3",
"resolved": "http://registry.npm.taobao.org/lru-cache/download/lru-cache-4.1.3.tgz",
"integrity": "sha1-oRdc80lt/IQ2wVbDNLSVWZK85pw=",
"requires": {
"pseudomap": "^1.0.2",
"yallist": "^2.1.2"
}
},
"mem": {
"version": "1.1.0",
"resolved": "http://registry.npm.taobao.org/mem/download/mem-1.1.0.tgz",
"integrity": "sha1-Xt1StIXKHZAP5kiVUFOZoN+kX3Y=",
"requires": {
"mimic-fn": "^1.0.0"
}
},
"mimic-fn": {
"version": "1.2.0",
"resolved": "http://registry.npm.taobao.org/mimic-fn/download/mimic-fn-1.2.0.tgz",
"integrity": "sha1-ggyGo5M0ZA6ZUWkovQP8qIBX0CI="
},
"minimist": {
"version": "0.0.8",
"resolved": "http://registry.npm.taobao.org/minimist/download/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
},
"mkdirp": {
"version": "0.5.1",
"resolved": "http://registry.npm.taobao.org/mkdirp/download/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"requires": {
"minimist": "0.0.8"
}
},
"npm-run-path": {
"version": "2.0.2",
"resolved": "http://registry.npm.taobao.org/npm-run-path/download/npm-run-path-2.0.2.tgz",
"integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=",
"requires": {
"path-key": "^2.0.0"
}
},
"number-is-nan": {
"version": "1.0.1",
"resolved": "http://registry.npm.taobao.org/number-is-nan/download/number-is-nan-1.0.1.tgz",
"integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0="
},
"os-locale": {
"version": "2.1.0",
"resolved": "http://registry.npm.taobao.org/os-locale/download/os-locale-2.1.0.tgz",
"integrity": "sha1-QrwpAKa1uL0XN2yOiCtlr8zyS/I=",
"requires": {
"execa": "^0.7.0",
"lcid": "^1.0.0",
"mem": "^1.1.0"
}
},
"p-finally": {
"version": "1.0.0",
"resolved": "http://registry.npm.taobao.org/p-finally/download/p-finally-1.0.0.tgz",
"integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4="
},
"p-limit": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/p-limit/download/p-limit-2.0.0.tgz",
"integrity": "sha1-5iTtVO6MRgp3izyfNnBJb/ileuw=",
"requires": {
"p-try": "^2.0.0"
}
},
"p-locate": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/p-locate/download/p-locate-3.0.0.tgz",
"integrity": "sha1-Mi1poFwCZLJZl9n0DNiokasAZKQ=",
"requires": {
"p-limit": "^2.0.0"
}
},
"p-try": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/p-try/download/p-try-2.0.0.tgz",
"integrity": "sha1-hQgLuHxkaI+keZb+j3376CEXYLE="
},
"path-exists": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/path-exists/download/path-exists-3.0.0.tgz",
"integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU="
},
"path-key": {
"version": "2.0.1",
"resolved": "http://registry.npm.taobao.org/path-key/download/path-key-2.0.1.tgz",
"integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A="
},
"promise-nodeify": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/promise-nodeify/download/promise-nodeify-3.0.0.tgz",
"integrity": "sha1-WlCQFijjwQWff+EXMmbRADJAXd8="
},
"proxying-agent": {
"version": "2.2.1",
"resolved": "http://registry.npm.taobao.org/proxying-agent/download/proxying-agent-2.2.1.tgz",
"integrity": "sha1-0wz5i2zzxQ6WrU9PWVUT4RznV6s="
},
"pseudomap": {
"version": "1.0.2",
"resolved": "http://registry.npm.taobao.org/pseudomap/download/pseudomap-1.0.2.tgz",
"integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM="
},
"require-directory": {
"version": "2.1.1",
"resolved": "http://registry.npm.taobao.org/require-directory/download/require-directory-2.1.1.tgz",
"integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I="
},
"require-main-filename": {
"version": "1.0.1",
"resolved": "http://registry.npm.taobao.org/require-main-filename/download/require-main-filename-1.0.1.tgz",
"integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE="
},
"set-blocking": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/set-blocking/download/set-blocking-2.0.0.tgz",
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc="
},
"shebang-command": {
"version": "1.2.0",
"resolved": "http://registry.npm.taobao.org/shebang-command/download/shebang-command-1.2.0.tgz",
"integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=",
"requires": {
"shebang-regex": "^1.0.0"
}
},
"shebang-regex": {
"version": "1.0.0",
"resolved": "http://registry.npm.taobao.org/shebang-regex/download/shebang-regex-1.0.0.tgz",
"integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM="
},
"signal-exit": {
"version": "3.0.2",
"resolved": "http://registry.npm.taobao.org/signal-exit/download/signal-exit-3.0.2.tgz",
"integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0="
},
"string-width": {
"version": "2.1.1",
"resolved": "http://registry.npm.taobao.org/string-width/download/string-width-2.1.1.tgz",
"integrity": "sha1-q5Pyeo3BPSjKyBXEYhQ6bZASrp4=",
"requires": {
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^4.0.0"
}
},
"strip-ansi": {
"version": "4.0.0",
"resolved": "http://registry.npm.taobao.org/strip-ansi/download/strip-ansi-4.0.0.tgz",
"integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
"requires": {
"ansi-regex": "^3.0.0"
}
},
"strip-eof": {
"version": "1.0.0",
"resolved": "http://registry.npm.taobao.org/strip-eof/download/strip-eof-1.0.0.tgz",
"integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8="
},
"tinify": {
"version": "1.5.0",
"resolved": "http://registry.npm.taobao.org/tinify/download/tinify-1.5.0.tgz",
"integrity": "sha1-a6LqfognyEXSY947RvTbe61VWSA=",
"requires": {
"promise-nodeify": ">= 0.1",
"proxying-agent": ">= 2.1"
}
},
"which": {
"version": "1.3.1",
"resolved": "http://registry.npm.taobao.org/which/download/which-1.3.1.tgz",
"integrity": "sha1-pFBD1U9YBTFtqNYvn1CRjT2nCwo=",
"requires": {
"isexe": "^2.0.0"
}
},
"which-module": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/which-module/download/which-module-2.0.0.tgz",
"integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho="
},
"wrap-ansi": {
"version": "2.1.0",
"resolved": "http://registry.npm.taobao.org/wrap-ansi/download/wrap-ansi-2.1.0.tgz",
"integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=",
"requires": {
"string-width": "^1.0.1",
"strip-ansi": "^3.0.1"
},
"dependencies": {
"ansi-regex": {
"version": "2.1.1",
"resolved": "http://registry.npm.taobao.org/ansi-regex/download/ansi-regex-2.1.1.tgz",
"integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8="
},
"is-fullwidth-code-point": {
"version": "1.0.0",
"resolved": "http://registry.npm.taobao.org/is-fullwidth-code-point/download/is-fullwidth-code-point-1.0.0.tgz",
"integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=",
"requires": {
"number-is-nan": "^1.0.0"
}
},
"string-width": {
"version": "1.0.2",
"resolved": "http://registry.npm.taobao.org/string-width/download/string-width-1.0.2.tgz",
"integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=",
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
"strip-ansi": "^3.0.0"
}
},
"strip-ansi": {
"version": "3.0.1",
"resolved": "http://registry.npm.taobao.org/strip-ansi/download/strip-ansi-3.0.1.tgz",
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
"requires": {
"ansi-regex": "^2.0.0"
}
}
}
},
"xregexp": {
"version": "4.0.0",
"resolved": "http://registry.npm.taobao.org/xregexp/download/xregexp-4.0.0.tgz",
"integrity": "sha1-5pgYneSd0qGMxWh7BeF8jkOUMCA="
},
"y18n": {
"version": "4.0.0",
"resolved": "http://registry.npm.taobao.org/y18n/download/y18n-4.0.0.tgz",
"integrity": "sha1-le+U+F7MgdAHwmThkKEg8KPIVms="
},
"yallist": {
"version": "2.1.2",
"resolved": "http://registry.npm.taobao.org/yallist/download/yallist-2.1.2.tgz",
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI="
},
"yargs": {
"version": "12.0.1",
"resolved": "http://registry.npm.taobao.org/yargs/download/yargs-12.0.1.tgz",
"integrity": "sha1-ZDLlYSO7Tnw1YhFUAemDdAYCYcI=",
"requires": {
"cliui": "^4.0.0",
"decamelize": "^2.0.0",
"find-up": "^3.0.0",
"get-caller-file": "^1.0.1",
"os-locale": "^2.0.0",
"require-directory": "^2.1.1",
"require-main-filename": "^1.0.1",
"set-blocking": "^2.0.0",
"string-width": "^2.0.0",
"which-module": "^2.0.0",
"y18n": "^3.2.1 || ^4.0.0",
"yargs-parser": "^10.1.0"
}
},
"yargs-parser": {
"version": "10.1.0",
"resolved": "http://registry.npm.taobao.org/yargs-parser/download/yargs-parser-10.1.0.tgz",
"integrity": "sha1-cgImW4n36eny5XZeD+c1qQXtuqg=",
"requires": {
"camelcase": "^4.1.0"
}
}
}
}
{
"name": "dbtinypng",
"bin": {
"dbtinypng": "dbtinypng.js"
},
"dependencies": {
"mkdirp": "^0.5.1",
"tinify": "^1.5.0",
"yargs": "^12.0.1"
}
}
const path = require('path');
var walk = require("./walk");
module.exports = (input, output) => {
walk(input, (err, result) => {
let count = 0;
let imgURLs = result.filter(
val => ['.jpg', '.png'].indexOf(path.extname(val)) != -1);
imgURLs = imgURLs.map(val => {
let inputSplited = input.split(path.sep);
let splited = val.split(path.sep);
return splited.slice(inputSplited.length - 1).join(path.sep);
})
if (imgURLs.length == 0) { console.log('没有需要处理的图片.'); return; }
console.log(`图片遍历完毕,共有${imgURLs.length}张`);
console.log('正在获取API信息...');
require('./getAPIs')(imgURLs.length, (result) => {
console.log('正在和server通信进行图片压缩...');
let current = result.pop();
for (const url of imgURLs) {
if (current[1] == 0) {
current = result.pop()
}
current[1]--;
const tinify = require('tinify');
tinify.key = current[0];
var mkdirp = require("mkdirp");
var url2 = url.split(path.sep);
url2.pop();
url2 = url2.join(path.sep);
url2 = path.join(output, url2);
mkdirp(url2, function (err) {
if (err) console.error(err)
else {
var source = tinify.fromFile(path.join(input, url));
source.toFile(path.join(output, url), () => {
console.log(`${++count}/${imgURLs.length}`, path.join(output, url));
if(count == imgURLs.length)
console.log('处理完毕.谢谢使用.')
});
}
});
}
});
});
}
\ No newline at end of file
module.exports = (key, callback) => {
var tinify = require("tinify");
tinify.key = key;
tinify.validate(function (err) {
if (err) throw err;
callback(key,tinify.compressionCount);
})
}
\ No newline at end of file
const validate = require('./validate');
module.exports = (keys, callback) => {
let counter = 0;
const result = [];
for (const key of keys) {
validate(key, (key, compressionCount) => {
counter++;
result.push([key, compressionCount]);
if (counter == keys.length) {
callback(result)
}
})
}
}
\ No newline at end of file
var fs = require('fs');
var path = require('path');
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
var file2 = path.join(dir,file);
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file2, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file2);
if (!--pending) done(null, results);
}
});
});
});
};
module.exports = walk;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment