Last active
August 24, 2016 15:23
-
-
Save ptmt/91689d0f2bb8506cc9d9 to your computer and use it in GitHub Desktop.
node --harmony utils/generate_images.js
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'; | |
const IMAGES_DIR = './images' | |
const ASSETS_DIR = './iOS/Images.xcassets' | |
const EXTENSIONS = ['jpg', 'jpeg', 'png'] | |
var fs = require('fs') | |
var gm = require('gm') | |
function convertAndCopy(source, scaleFactor, imageName, imageExt) { | |
let per = scaleFactor === 3 ? '100%' : scaleFactor === 2 ? '50%' : '25%' | |
let newName = imageName + '@' + scaleFactor + 'x.' + imageExt | |
let target = ASSETS_DIR + '/' + imageName + '.imageset/' + newName | |
gm(source) | |
.resize(per) | |
.write(target, (err) => { | |
if (!err) { | |
console.log(target, 'has been created') | |
} | |
}) | |
return newName | |
} | |
fs.readdirSync(IMAGES_DIR) | |
.filter(f=> EXTENSIONS.filter(e=> f.indexOf(e))) | |
.forEach(image => { | |
let imageName = image.split('.')[0]; | |
let imageExt = image.split('.')[1]; | |
// create a directory | |
try { | |
fs.mkdirSync(ASSETS_DIR + '/' + imageName + '.imageset') | |
} catch (e) { | |
// already exist | |
} | |
// generate json | |
let contentsJson = fs.readFileSync('./utils/Contents.json', {encoding: 'utf8'}) | |
let tasks = [] | |
var factors = [1, 2, 3]; | |
factors.forEach(scaleFactor => { | |
let newFilename = convertAndCopy(IMAGES_DIR + '/' + image, scaleFactor, imageName, imageExt) | |
contentsJson = contentsJson.replace('{FILENAME' + scaleFactor + '}', newFilename) | |
}) | |
fs.writeFileSync(ASSETS_DIR + '/' + imageName + '.imageset/Contents.json', contentsJson) | |
}) | |
console.log('Please rebuild the XCode project!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What version of node are you running this on? And which version of the
gm
dependency are you relying on?I get a syntax error with
node 0.12.2
: