Skip to content

Instantly share code, notes, and snippets.

@ptmt
Last active August 24, 2016 15:23
Show Gist options
  • Save ptmt/91689d0f2bb8506cc9d9 to your computer and use it in GitHub Desktop.
Save ptmt/91689d0f2bb8506cc9d9 to your computer and use it in GitHub Desktop.
node --harmony utils/generate_images.js
'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!')
@davemo
Copy link

davemo commented May 13, 2015

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:

> node --harmony ./script/generate_xcode_assets.js

/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:269
      proc.stdout.on('data', onOut = function (data) {
                 ^
TypeError: Cannot read property 'on' of undefined
    at gm._spawn (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:269:18)
    at /Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:109:12
    at series (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/node_modules/array-series/index.js:11:36)
    at gm._preprocess (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:185:5)
    at gm.write (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:107:10)
    at convertAndCopy (/Users/davidmosher/code/hsDeckBrowser/script/generate_xcode_assets.js:16:6)
    at /Users/davidmosher/code/hsDeckBrowser/script/generate_xcode_assets.js:44:25
    at Array.forEach (native)
    at /Users/davidmosher/code/hsDeckBrowser/script/generate_xcode_assets.js:43:13
    at Array.forEach (native)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment