-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe76170
commit 657b8ce
Showing
14 changed files
with
254 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
|
||
out.json | ||
|
||
lib/ | ||
test/*.js | ||
test/*/*.js | ||
node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
command(...args) { | ||
this._args._.push(...args); | ||
return this; | ||
}, | ||
_arg(key, ...vals) { | ||
this._args[key] = vals; | ||
return this; | ||
}, | ||
args() { | ||
const args = []; | ||
if (this._args.preset) { | ||
args.push('-preset', ...this._args.preset); | ||
} | ||
for (const key in this._args) { | ||
if (key === '_' || key === '-' || key === 'preset') { | ||
continue; | ||
} | ||
args.push(`-${key}`, ...this._args[key]); | ||
} | ||
return args.concat(this._args._); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { CWebp, DWebp } = require('./webp'); | ||
|
||
const pkg = path.resolve(__dirname, '../package.json'); | ||
const { version } = JSON.parse(fs.readFileSync(pkg, 'utf8')); | ||
|
||
module.exports = Object.assign(CWebp, { CWebp, DWebp, version }); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const streamToBuffer = require('raw-body'); | ||
const { Buffer } = require('buffer'); | ||
const { Stream, PassThrough } = require('stream'); | ||
|
||
const bindCallback = (promise, next) => { | ||
if (typeof next === 'function') { | ||
promise.then( | ||
(result) => process.nextTick(next, null, result), | ||
(err) => process.nextTick(next, err) | ||
); | ||
} | ||
return promise; | ||
}; | ||
|
||
module.exports = { | ||
_write(source, outname = '-') { | ||
const stdin = typeof source !== 'string'; | ||
const stdout = outname === '-'; | ||
const args = [ | ||
...this.args(), | ||
'-o', outname, | ||
'--', (stdin ? '-' : source) | ||
]; | ||
if (!stdin) { | ||
return this._spawn(args, stdin, stdout); | ||
} else if (Buffer.isBuffer(source)) { | ||
const res = this._spawn(args, stdin, stdout); | ||
res.stdin.end(source); | ||
return res; | ||
} else if (source instanceof Stream) { | ||
const res = this._spawn(args, stdin, stdout); | ||
source.pipe(res.stdin); | ||
return res; | ||
} else { | ||
return { | ||
promise: Promise.reject(new Error('Mailformed source')) | ||
}; | ||
} | ||
}, | ||
write(outname, next) { | ||
const promise = outname | ||
? (this._write(this.source, outname)).promise | ||
: Promise.reject(new Error('outname in not specified')); | ||
return bindCallback(promise, next); | ||
}, | ||
toBuffer(next) { | ||
return bindCallback(streamToBuffer(this.stream()), next); | ||
}, | ||
stream() { | ||
const outstream = new PassThrough(); | ||
const { promise, stdout } = this._write(this.source, '-'); | ||
if (stdout) { | ||
stdout.pipe(outstream, { end: false }); | ||
} | ||
promise | ||
.then(() => outstream.end()) | ||
.catch((err) => outstream.destroy(err)); | ||
return outstream; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
exports.mixin = (cls, proto) => { | ||
for (const name in proto) { | ||
cls.prototype[name] = proto[name]; | ||
} | ||
}; | ||
|
||
exports.compile = (methods) => { | ||
const proto = {}; | ||
for (const name in methods) { | ||
const { key = name, type, description, exclude, aliases } = methods[name]; | ||
const typeArray = Array.isArray(type) ? type : [type || 'string']; | ||
|
||
const method = type === 'boolean' ? function(val) { | ||
if (val || arguments.length === 0) { | ||
if (exclude) { | ||
[].concat(exclude).forEach((k) => { | ||
delete this._args[methods[k].key || k]; | ||
}); | ||
} | ||
this._args[key] = []; | ||
} else { | ||
delete this._args[key]; | ||
} | ||
return this; | ||
} : function(...args) { | ||
if (args.length < typeArray.length) { | ||
throw new Error('Not enough arguments'); | ||
} | ||
const vals = []; | ||
typeArray.forEach((t) => { | ||
let val = args.shift(); | ||
if (t === 'number') { | ||
const nval = Number(val); | ||
if (Number.isFinite(nval)) { | ||
val = nval; | ||
} | ||
} | ||
if (typeof val !== t) { | ||
throw new Error(`Expected ${t}, got ${typeof val}`); | ||
} | ||
vals.push(val); | ||
}); | ||
this._args[key] = vals; | ||
return this; | ||
}; | ||
proto[name] = Object.assign(method, { description }); | ||
(aliases || []).forEach((alias) => { | ||
proto[alias] = proto[name]; | ||
}); | ||
} | ||
return proto; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { inherits } = require('util'); | ||
const { mixin, compile } = require('./utils'); | ||
const Wrapper = require('./wrapper'); | ||
const methods = require('../methods.json'); | ||
|
||
function CWebp(source, bin) { | ||
if (!(this instanceof CWebp)) { | ||
return new CWebp(source, bin); | ||
} | ||
Wrapper.call(this, source, bin); | ||
if (this.constructor.verbose) { | ||
this._args.v = []; | ||
} | ||
} | ||
|
||
inherits(CWebp, Wrapper); | ||
|
||
mixin(CWebp, compile(methods.global)); | ||
mixin(CWebp, compile(methods.cwebp)); | ||
|
||
CWebp.bin = 'cwebp'; | ||
CWebp.verbose = false; | ||
|
||
function DWebp(source, bin) { | ||
if (!(this instanceof DWebp)) { | ||
return new DWebp(source, bin); | ||
} | ||
Wrapper.call(this, source, bin); | ||
} | ||
|
||
inherits(DWebp, Wrapper); | ||
|
||
mixin(DWebp, compile(methods.global)); | ||
mixin(DWebp, compile(methods.dwebp)); | ||
|
||
DWebp.bin = 'dwebp'; | ||
|
||
module.exports = { CWebp, DWebp }; |
Oops, something went wrong.