Skip to content

Commit

Permalink
Add compress-images action
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 1, 2017
1 parent fbbbae9 commit f88d309
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
Binary file added bin/pngout
Binary file not shown.
Binary file added bin/pngout.exe
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "medic-configurer-beta",
"version": "1.0.23",
"version": "1.0.24",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
"test": "grunt jshint test"
},
"bin": {
"medic-conf": "bin/medic-conf"
"medic-conf": "bin/medic-conf",
"medic-pngout": "bin/pngout"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions src/cli/supported-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = [
'backup-app-settings',
'backup-forms',
'compile-app-settings',
'compress-images',
'convert-forms',
'delete-forms',
'upload-app-settings',
Expand Down
22 changes: 22 additions & 0 deletions src/fn/compress-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const exec = require('../lib/exec-promise');
const fs = require('../lib/sync-fs');
const info = require('../lib/log').info;
const trace = require('../lib/log').trace;

module.exports = (project/*, couchUrl*/) => {
return fs.recurseFiles(project)
.filter(name => name.endsWith('.png'))
.map(f => { console.log('compressing png: ', f); return f; })
.reduce((promiseChain, png) =>
promiseChain
.then(() => info('Compressing PNG:', png, '…'))
.then(() =>
exec('pngout', `'${png}'`)
.then(() => trace('Compressed', png))
.catch(e => {
if(e.status === 2) {
info('Unable to compress further.');
} else throw e;
})),
Promise.resolve());
};
20 changes: 14 additions & 6 deletions src/lib/exec-promise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;

module.exports = (...args) =>
new Promise((resolve, reject) =>
exec(args.join(' '), err => {
return err ? reject(err) : resolve();
}));
// TODO might be important to sanitise input to `exec()` to prevent injection
// attacks by devious tech leads.

module.exports = (...args) => {
try {
execSync(args.join(' '), {
stdio: ['ignore', process.stdout, process.stderr],
});
return Promise.resolve();
} catch(e) {
return Promise.reject(e);
}
};
23 changes: 23 additions & 0 deletions src/lib/sync-fs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs');
const path = require('path');
const trace = require('../lib/log').trace;
const warn = require('../lib/log').warn;

function read(path) {
Expand All @@ -19,6 +21,26 @@ function readJson(path) {
}
}

function recurseFiles(dir, files) {
if(!files) files = [];

fs.readdirSync(dir)
.forEach(name => {
const f = path.join(dir, name);
try {
const stat = fs.statSync(f);

if(stat.isDirectory()) recurseFiles(f, files);
else files.push(f);
} catch(e) {
if(e.code === 'ENOENT') trace('Ignoring file (err ENOENT - may be a symlink):', f);
else throw e;
}
});

return files;
}

function withoutExtension(fileName) {
const extensionStart = fileName.lastIndexOf('.');
return extensionStart === -1 ? fileName : fileName.substring(0, extensionStart);
Expand All @@ -30,6 +52,7 @@ module.exports = {
read: read,
readJson: readJson,
readBinary: path => fs.readFileSync(path),
recurseFiles: recurseFiles,
readdir: fs.readdirSync,
withoutExtension: withoutExtension,
write: (path, content) => fs.writeFileSync(path, content, 'utf8'),
Expand Down

0 comments on commit f88d309

Please sign in to comment.