forked from leonardoquevedox/capacitor-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ba0feee
Showing
14 changed files
with
1,949 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
/resources/*/ | ||
|
||
# system | ||
.DS_Store | ||
node_modules/ | ||
/dist/ | ||
/typings/ | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Olivab | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# cordova-res-generator | ||
|
||
Automatic icon and splash screen resizing tool for **Cordova**/**ionic**/**PhoneGap** based applications. | ||
|
||
It automatically resizes and copies your ```icon.png``` and ```splash.png``` files for the **iOS**, **Android** platforms. | ||
|
||
It does **NOT require** any external binary libraries. **Javascript only**. | ||
|
||
--- | ||
|
||
### Installation | ||
|
||
$ npm install cordova-res-generator -g | ||
|
||
--- | ||
|
||
### Usage | ||
|
||
Add your ```icon.png``` (1024x1024) and ```splash.png``` (2732x2732) files to the 'resources' folder under the root of your cordova based project. | ||
|
||
Then run: | ||
|
||
$ cordova-res-generator | ||
|
||
or | ||
|
||
$ crgen | ||
|
||
--- | ||
|
||
### License | ||
|
||
MIT |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
// Lib definitions | ||
|
||
var program = require('commander'); | ||
var colors = require('colors'); | ||
|
||
var _ = require('lodash'); | ||
var Q = require('q'); | ||
|
||
var fs = require('fs-extra'); | ||
var path = require('path'); | ||
|
||
var Jimp = require('jimp'); | ||
|
||
// helpers | ||
|
||
var display = { | ||
info: (str) => { | ||
console.log(' ' + str); | ||
}, | ||
success: (str) => { | ||
str = 'V'.green + ' ' + str; | ||
console.log(' ' + str); | ||
}, | ||
error: (str) => { | ||
str = 'X'.red + ' ' + str; | ||
console.log(' ' + str); | ||
}, | ||
header: (str) => { | ||
console.log(''); | ||
console.log(str.cyan.underline); | ||
} | ||
}; | ||
|
||
// app main variables | ||
|
||
var imageObjects; | ||
|
||
// app functions | ||
|
||
function check(_settings) { | ||
display.header('Checking image files and output directory'); | ||
|
||
return getImages(_settings) | ||
.then((iobjs) => { | ||
imageObjects = iobjs; | ||
}) | ||
.then(() => checkOutPutDir(_settings)); | ||
|
||
} | ||
|
||
function getImages(_settings) { | ||
|
||
var _imageObjects = { | ||
icon: null, | ||
splash: null | ||
}; | ||
|
||
return checkIconFile(_settings.iconfile) | ||
.then((image) => { | ||
_imageObjects.icon = image; | ||
}) | ||
.then(() => checkSplashFile(_settings.splashFileName)) | ||
.then((image) => { | ||
_imageObjects.splash = image; | ||
return _imageObjects; | ||
}); | ||
|
||
function checkIconFile(iconFileName) { | ||
var defer = Q.defer(); | ||
|
||
Jimp.read(_settings.iconfile) | ||
.then((image) => { | ||
var width = image.bitmap.width; | ||
var height = image.bitmap.height; | ||
display.info('Icon: ' + width + 'x' + height); | ||
if (width === 1024 && width === height) { | ||
display.success('Icon file ok.') | ||
defer.resolve(image); | ||
} else { | ||
display.error('Bad icon file.') | ||
defer.reject('Bad image format.'); | ||
} | ||
}) | ||
.catch((err) => { | ||
display.error('Could not load icon file'); | ||
defer.reject(err); | ||
}); | ||
|
||
return defer.promise; | ||
} | ||
|
||
function checkSplashFile(splashFileName) { | ||
var defer = Q.defer(); | ||
|
||
Jimp.read(_settings.splashfile) | ||
.then((image) => { | ||
var width = image.bitmap.width; | ||
var height = image.bitmap.height; | ||
display.info('Splash: ' + width + 'x' + height); | ||
if (width === 2732 && width === height) { | ||
display.success('Splash file ok.') | ||
defer.resolve(image); | ||
} else { | ||
display.error('Bad splash file.') | ||
defer.reject('Bad image format.'); | ||
} | ||
}) | ||
.catch((err) => { | ||
display.error('Could not load splash file'); | ||
defer.reject(err); | ||
}); | ||
|
||
return defer.promise; | ||
} | ||
|
||
} | ||
|
||
function checkOutPutDir(_settings) { | ||
var dir = _settings.outputdirectory; | ||
|
||
display.info('Output directory: ' + dir); | ||
|
||
return fs.pathExists(dir) | ||
.then((exists) => { | ||
if (exists) { | ||
display.success('Output directory ok.'); | ||
} else { | ||
display.error('Output directory not found.'); | ||
throw ('Not found: ' + dir); | ||
} | ||
}); | ||
|
||
} | ||
|
||
function generateForConfig(_imageObj, _settings, _config) { | ||
var platformPath = path.join(_settings.outputdirectory, _config.path); | ||
|
||
var transformIcon = (definition) => { | ||
var defer = Q.defer(); | ||
var image = _imageObj.icon.clone(); | ||
|
||
image.resize(definition.size, definition.size) | ||
.write(path.join(platformPath, definition.name)); | ||
|
||
defer.resolve(); | ||
|
||
return defer.promise; | ||
}; | ||
|
||
var transformSplash = (definition) => { | ||
var defer = Q.defer(); | ||
var image = _imageObj.splash.clone(); | ||
|
||
var x = (image.bitmap.width - definition.width) / 2; | ||
var y = (image.bitmap.height - definition.height) / 2; | ||
var width = definition.width; | ||
var height = definition.height; | ||
|
||
image | ||
.crop(x, y, width, height) | ||
.write(path.join(platformPath, definition.name)); | ||
|
||
defer.resolve(); | ||
|
||
return defer.promise; | ||
}; | ||
|
||
return fs.ensureDir(platformPath) | ||
.then(() => { | ||
var definitions = _config.definitions; | ||
var actions = []; | ||
definitions.forEach((def) => { | ||
switch (_config.type) { | ||
case 'icon': | ||
actions.push(transformIcon(def)); | ||
break; | ||
case 'splash': | ||
actions.push(transformSplash(def)); | ||
break; | ||
} | ||
}); | ||
return Q.all(actions) | ||
.then(() => { | ||
display.success('Generated ' + _config.type + ' files for ' + _config.platform); | ||
}); | ||
}); | ||
} | ||
|
||
function generate(_imageObj, _settings) { | ||
|
||
display.header('Generating files'); | ||
|
||
var configs = [ | ||
// android | ||
require('./platforms/icons/android'), | ||
require('./platforms/splash/android'), | ||
// ios | ||
require('./platforms/icons/ios'), | ||
require('./platforms/splash/ios'), | ||
// blackberry10 | ||
require('./platforms/icons/blackberry10'), | ||
]; | ||
|
||
var actions = []; | ||
configs.forEach((config) => { | ||
actions.push(generateForConfig(_imageObj, _settings, config)); | ||
}); | ||
|
||
return Q.all(actions) | ||
.then(() => { | ||
//display.success("Successfully generated all files"); | ||
}); | ||
|
||
} | ||
|
||
function catchErrors(err) { | ||
if (err) | ||
console.log('Error: ', err); | ||
} | ||
|
||
// cli helper configuration | ||
|
||
program | ||
.version('0.1.0') | ||
.description('Generates icon & splash screen for cordova/ionic projects using javascript only.') | ||
.option('-i, --icon [optional]', 'optional icon file path (default: resources/icon.png)') | ||
.option('-s, --splash [optional]', 'optional splash file path (default: resources/splash.png)') | ||
.option('-o, -outputdir [optional]', 'optional output directory (default: current directory, outputs to resources/)') | ||
.parse(process.argv); | ||
|
||
// app settings and default values | ||
|
||
var settings = { | ||
iconfile: program.icon || path.join('.', 'resources', 'icon.png'), | ||
splashfile: program.splash || path.join('.', 'resources', 'splash.png'), | ||
outputdirectory: program.outputdir || path.join('.', 'resources') | ||
}; | ||
|
||
// app entry point | ||
|
||
check(settings) | ||
.then(() => generate(imageObjects, settings)) | ||
.catch((err) => catchErrors(err)); |
Oops, something went wrong.