Created
August 10, 2018 21:20
-
-
Save leopic/1fa2c8580fd00003cf3d80756f3a086b to your computer and use it in GitHub Desktop.
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
'use strict'; | |
// Module dependencies | |
var _ = require('lodash'), | |
fs = require('fs'), | |
gm = require('gm'), | |
path = require('path'); | |
// Global variables | |
var EXECUTION_PATH = process.cwd() + path.sep, | |
MODULE_PATH = __dirname + path.sep; | |
// File and directory names | |
var CONTENTS_JSON = 'Contents.json', | |
SUCCESS_BANNER = 'banner.txt', | |
HELP_BANNER = 'help.txt', | |
ICON_PREFIX = 'Icon-', | |
THUMBS_OUTPUT_DIR = 'AppIcon.appiconset'; | |
// File paths | |
var OUTPUT_DIR_PATH = EXECUTION_PATH + THUMBS_OUTPUT_DIR + path.sep, | |
CONTENTS_JSON_OUTPUT_PATH = OUTPUT_DIR_PATH + CONTENTS_JSON, | |
CONTENTS_JSON_PATH = MODULE_PATH + CONTENTS_JSON, | |
HELP_BANNER_PATH = MODULE_PATH + HELP_BANNER, | |
SUCCESS_BANNER_PATH = MODULE_PATH + SUCCESS_BANNER, | |
THUMBS_PATH = OUTPUT_DIR_PATH + ICON_PREFIX; | |
/** | |
* Generates all thumbnails. | |
*/ | |
var generateThumbs = function (baseIconPath) { | |
var totalReady = 0, | |
sizes = { | |
20: [1, 2, 3], | |
29: [1, 2, 3], | |
40: [1, 2, 3], | |
60: [2, 3], | |
76: [1, 2], | |
'83.5': [2], | |
1024: [1] | |
}; | |
var totalImages = _.reduce(sizes, function (memo, num) { | |
return memo + num.length; | |
}, 0); | |
console.log('Creating icons.'); | |
_.forEach(sizes, function (multipliers, dimension) { | |
dimension = parseFloat(dimension); | |
_.forEach(multipliers, function (multiplier) { | |
var fileName = THUMBS_PATH + dimension, | |
size = dimension * multiplier, | |
quality = 100; | |
fileName += (multiplier > 1 ? ('@' + multiplier + 'x') : '') + '.png'; | |
gm(baseIconPath).thumb(size, size, fileName, quality, function (err) { | |
if (err) { | |
console.error(err); | |
return | |
} | |
totalReady++; | |
(totalImages === totalReady) && success(); | |
}); | |
}); | |
}); | |
}; | |
/** | |
* Displays the help banner | |
*/ | |
var showHelpBanner = function () { | |
fs.readFile(HELP_BANNER_PATH, 'utf8', function (err, helpBanner) { | |
console.log(err || helpBanner); | |
}); | |
}; | |
/** | |
* The path to the icon is incorrect. | |
*/ | |
var baseIconPathDoesNotExist = function () { | |
console.error('Please provide a base icon using the --base-icon option.'); | |
}; | |
/** | |
* Creates the directory to house all icons. | |
* @param baseIconPath | |
*/ | |
var outputPathDoesNotExist = function (baseIconPath) { | |
console.log('Creating output directory.'); | |
fs.mkdir(OUTPUT_DIR_PATH, function (err) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
fs.readdir(OUTPUT_DIR_PATH, function (error) { | |
if (error) { | |
console.error(error); | |
return; | |
} | |
generateThumbs(baseIconPath); | |
}); | |
}); | |
}; | |
/** | |
* Called upon successfully creating all thumbnails. | |
*/ | |
var success = function () { | |
fs.readFile(SUCCESS_BANNER_PATH, 'utf8', function (err, successBanner) { | |
if (!err) { | |
console.log(successBanner); | |
fs.createReadStream(CONTENTS_JSON_PATH) | |
.pipe(fs.createWriteStream(CONTENTS_JSON_OUTPUT_PATH)); | |
} else { | |
console.log(err); | |
} | |
}); | |
}; | |
/** | |
* Script entry point. | |
* Verifies base icon path and output dir paths exist and | |
* shows either the help banner or calls generateThumbs. | |
*/ | |
var init = function (args) { | |
var baseIconPath = args.iconPath, | |
helpFlag = args.helpFlag; | |
if (helpFlag || !baseIconPath) { | |
showHelpBanner(); | |
return; | |
} | |
fs.exists(baseIconPath, function (exists) { | |
if (!exists) { | |
baseIconPathDoesNotExist(); | |
return; | |
} | |
fs.readdir(OUTPUT_DIR_PATH, function (err) { | |
if (!err) { | |
generateThumbs(baseIconPath); | |
return; | |
} | |
var directoryDoesNotExist = _.includes([-2, 34], err.errno); | |
if (directoryDoesNotExist) { | |
outputPathDoesNotExist(baseIconPath); | |
return; | |
} | |
console.error('Unable to compute', err); | |
}); | |
}); | |
}; | |
module.exports = init; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment