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
This diff is collapsed.
{
"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