Skip to content

Commit

Permalink
Convert src from coffescript to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeschastny committed Oct 11, 2023
1 parent fe76170 commit 657b8ce
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 220 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

out.json

lib/
test/*.js
test/*/*.js
node_modules/
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
"type": "git",
"url": "https://github.com/Intervox/node-webp.git"
},
"main": "lib/index.js",
"main": "src/index.js",
"scripts": {
"clean": "rm -rf lib/ cwebp-*.tgz",
"test": "mocha test/*.coffee",
"list-files": "tar -tzf `npm pack | tail -n 1`",
"prepack": "npm run clean && coffee -o lib/ -cb src/",
"postpublish": "git push origin +HEAD:latest --follow-tags"
},
"engines": {
Expand Down
17 changes: 0 additions & 17 deletions src/args.coffee

This file was deleted.

23 changes: 23 additions & 0 deletions src/args.js
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._);
}
};
13 changes: 0 additions & 13 deletions src/index.coffee

This file was deleted.

8 changes: 8 additions & 0 deletions src/index.js
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 });
59 changes: 0 additions & 59 deletions src/io.coffee

This file was deleted.

60 changes: 60 additions & 0 deletions src/io.js
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;
}
};
38 changes: 0 additions & 38 deletions src/utils.coffee

This file was deleted.

52 changes: 52 additions & 0 deletions src/utils.js
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;
};
33 changes: 0 additions & 33 deletions src/webp.coffee

This file was deleted.

38 changes: 38 additions & 0 deletions src/webp.js
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 };
Loading

0 comments on commit 657b8ce

Please sign in to comment.