Skip to content

Commit

Permalink
ES2015ify and require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Mar 28, 2017
1 parent 7c17388 commit 4698a7a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 103 deletions.
7 changes: 4 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
'use strict';
var spawn = require('child_process').spawn;
var mozjpeg = require('./');

var input = process.argv.slice(2);
const spawn = require('child_process').spawn;
const mozjpeg = require('.');

const input = process.argv.slice(2);

spawn(mozjpeg, input, {stdio: 'inherit'})
.on('exit', process.exit);
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
'use strict';

module.exports = require('./lib').path();
15 changes: 8 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';
var path = require('path');
var BinWrapper = require('bin-wrapper');
var pkg = require('../package.json');

var url = 'https://raw.githubusercontent.com/imagemin/mozjpeg-bin/v' + pkg.version + '/vendor/';
const path = require('path');
const BinWrapper = require('bin-wrapper');
const pkg = require('../package.json');

const url = `https://raw.githubusercontent.com/imagemin/mozjpeg-bin/v${pkg.version}/vendor/`;

module.exports = new BinWrapper()
.src(url + 'macos/cjpeg', 'darwin')
.src(url + 'linux/cjpeg', 'linux')
.src(url + 'win/cjpeg.exe', 'win32')
.src(`${url}macos/cjpeg`, 'darwin')
.src(`${url}linux/cjpeg`, 'linux')
.src(`${url}win/cjpeg.exe`, 'win32')
.dest(path.join(__dirname, '../vendor'))
.use(process.platform === 'win32' ? 'cjpeg.exe' : 'cjpeg');
26 changes: 13 additions & 13 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
'use strict';
var os = require('os');
var BinBuild = require('bin-build');
var log = require('logalot');
var bin = require('./');

var cpuNum = os.cpus().length;
const os = require('os');
const BinBuild = require('bin-build');
const log = require('logalot');
const bin = require('.');

bin.run(['-version'], function (err) {
const cpuNum = os.cpus().length;

bin.run(['-version'], err => {
if (err) {
log.warn(err.message);
log.warn('mozjpeg pre-build test failed');
log.info('compiling from source');

var cfg = [
const cfg = [
'./configure --disable-shared --disable-dependency-tracking --with-jpeg8',
'--prefix="' + bin.dest() + '" --bindir="' + bin.dest() + '"',
'--libdir="' + bin.dest() + '"'
`--prefix="${bin.dest()}" --bindir="${bin.dest()}" --libdir="${bin.dest()}"`
].join(' ');

var builder = new BinBuild()
const builder = new BinBuild()
.src('https://github.com/mozilla/mozjpeg/releases/download/v3.1/mozjpeg-3.1-release-source.tar.gz')
.cmd('autoreconf -fiv')
.cmd(cfg)
.cmd('make --jobs=' + String(cpuNum))
.cmd('make install --jobs=' + String(cpuNum));
.cmd(`make --jobs=${cpuNum}`)
.cmd(`make install --jobs=${cpuNum}`);

return builder.run(function (err) {
return builder.run(err => {
if (err) {
log.error(err.stack);
return;
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
}
],
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"bin": "cli.js",
"scripts": {
"postinstall": "node lib/install.js",
"test": "xo && mocha"
"test": "xo && ava"
},
"files": [
"index.js",
Expand All @@ -44,16 +44,16 @@
"optimize"
],
"dependencies": {
"bin-build": "^2.0.0",
"bin-build": "^2.2.0",
"bin-wrapper": "^3.0.0",
"logalot": "^2.0.0"
},
"devDependencies": {
"bin-check": "^2.0.0",
"compare-size": "^1.0.1",
"mkdirp": "^0.5.0",
"mocha": "^2.2.4",
"rimraf": "^2.3.2",
"ava": "*",
"bin-check": "^4.0.1",
"compare-size": "^3.0.0",
"execa": "^0.6.3",
"tempy": "^0.1.0",
"xo": "*"
}
}
Binary file modified test/fixtures/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 33 additions & 72 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,53 @@
/* eslint-env mocha */
'use strict';
var assert = require('assert');
var execFile = require('child_process').execFile;
var fs = require('fs');
var os = require('os');
var path = require('path');
var binCheck = require('bin-check');
var BinBuild = require('bin-build');
var compareSize = require('compare-size');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');

var tmp = path.join(__dirname, 'tmp');
var cpuNum = os.cpus().length;

beforeEach(function (cb) {
mkdirp(tmp, cb);
});

afterEach(function (cb) {
rimraf(tmp, {disableGlob: true}, cb);
});

it('rebuild the mozjpeg binaries', function (cb) {
this.timeout(150000);

var cfg = [
const fs = require('fs');
const os = require('os');
const path = require('path');
const test = require('ava');
const execa = require('execa');
const tempy = require('tempy');
const binCheck = require('bin-check');
const BinBuild = require('bin-build');
const compareSize = require('compare-size');
const mozjpeg = require('..');

const cpuNum = os.cpus().length;

test.cb('rebuild the mozjpeg binaries', t => {
const tmp = tempy.directory();
const cfg = [
'./configure --disable-shared --disable-dependency-tracking --with-jpeg8',
'--prefix="' + tmp + '" --bindir="' + tmp + '"',
'--libdir="' + tmp + '"'
`--prefix="${tmp}" --bindir="${tmp}" --libdir="${tmp}"`
].join(' ');

new BinBuild()
.src('https://github.com/mozilla/mozjpeg/releases/download/v3.1/mozjpeg-3.1-release-source.tar.gz')
.cmd('autoreconf -fiv')
.cmd(cfg)
.cmd('make --jobs=' + String(cpuNum))
.cmd('make install --jobs=' + String(cpuNum))
.run(function (err) {
if (err) {
cb(err);
return;
}

assert(fs.statSync(path.join(tmp, 'cjpeg')).isFile());
cb();
.cmd(`make --jobs=${cpuNum}`)
.cmd(`make install --jobs=${cpuNum}`)
.run(err => {
t.ifError(err);
t.true(fs.existsSync(path.join(tmp, 'cjpeg')));
t.end();
});
});

it('return path to binary and verify that it is working', function (cb) {
var args = [
'-outfile', path.join(tmp, 'test.jpg'),
path.join(__dirname, 'fixtures/test.jpg')
];

binCheck(require('../'), args, function (err, works) {
if (err) {
cb(err);
return;
}

assert(works);
cb();
});
test('return path to binary and verify that it is working', async t => {
t.true(await binCheck(mozjpeg, ['-version']));
});

it('minify a JPG', function (cb) {
var src = path.join(__dirname, 'fixtures/test.jpg');
var dest = path.join(tmp, 'test.jpg');
var args = [
test('minify a JPG', async t => {
const tmp = tempy.directory();
const src = path.join(__dirname, 'fixtures/test.jpg');
const dest = path.join(tmp, 'test.jpg');
const args = [
'-outfile', dest,
src
];

execFile(require('../'), args, function (err) {
if (err) {
cb(err);
return;
}

compareSize(src, dest, function (err, res) {
if (err) {
cb(err);
return;
}
await execa(mozjpeg, args);
const res = await compareSize(src, dest);

assert(res[dest] < res[src]);
cb();
});
});
t.true(res[dest] < res[src]);
});

0 comments on commit 4698a7a

Please sign in to comment.