forked from excellenteasy/android-icon-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·78 lines (69 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict'
var icons = require('android-icons')
var lwip = require('lwip')
var Q = require('q')
var path = require('path')
var colors = require('colors')
function openImage (path) {
var q = Q.defer()
lwip.open(path, function (err, image) {
if (err) {
q.reject(err)
}
q.resolve(image)
})
return q.promise
}
function clone (image) {
var q = Q.defer()
image.clone(function (err, clone) {
if (err) {
q.reject(err)
}
q.resolve(clone)
})
return q.promise
}
function resize (icon, image) {
var q = Q.defer()
image.resize(icon.width, function (err, resized) {
if (err) return q.reject(err)
q.resolve(resized)
})
return q.promise
}
function writeFile (path, image) {
var q = Q.defer()
image.writeFile(path, function (err) {
if (err) return q.reject(err)
q.resolve(path)
})
return q.promise
}
function successMessage (icon, output, path) {
return console.info(colors.green('OK'), 'Image resized to', icon.width, 'x', icon.width, 'and written to', path)
}
function errorMessage (e) {
var message = typeof e === 'string' ? e : (e.msg || e.message)
console.error(colors.red('ERROR.'), message)
}
function transformAll (icons, output, image) {
return Q.all(icons
.map(transform.bind(null, image, output))
)
}
function transform (image, output, icon) {
var out = output ? path.join(output, icon.name) : false
return clone(image)
.then(resize.bind(null, icon))
.then(writeFile.bind(null, out))
.then(successMessage.bind(null, icon, output))
.catch(errorMessage)
}
module.exports = function (input, output) {
if (!input) {
errorMessage(new Error('`input` parameter is required.'))
}
output = output || process.cwd()
return openImage(input).then(transformAll.bind(null, icons(), output))
}