Skip to content

Commit

Permalink
Add support for Promises in "onSaveSpritesheet"
Browse files Browse the repository at this point in the history
Closes #66.
  • Loading branch information
niksy committed Oct 17, 2016
1 parent c5a5ece commit 131f06b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -119,7 +119,11 @@ Built-in filters:

###### hooks.onSaveSpritesheet

> Hook that allows to rewrite the filepath of produced spritesheet.
> Hook that allows to rewrite the data of produced spritesheet.
> If returned value is string, it is used as filepath value, and if returned value is object, it is used as value which will be merged with current spritesheet data.
> Returned value can also be Promise which should return either string or object.
- Default: `null`
- Required: `false`
Expand Down
26 changes: 18 additions & 8 deletions src/core.js
Expand Up @@ -324,17 +324,27 @@ export function runSpritesmith(opts, images) {
*/
export function saveSpritesheets(opts, images, spritesheets) {
return Promise.each(spritesheets, (spritesheet) => {
spritesheet.path = _.isFunction(opts.hooks.onSaveSpritesheet)
? opts.hooks.onSaveSpritesheet(opts, spritesheet)
: makeSpritesheetPath(opts, spritesheet);
return (
_.isFunction(opts.hooks.onSaveSpritesheet) ?
Promise.resolve(opts.hooks.onSaveSpritesheet(opts, spritesheet)) :
Promise.resolve(makeSpritesheetPath(opts, spritesheet))
)
.then(( res ) => {

if ( _.isString(res) ) {
spritesheet.path = res;
} else {
_.assign(spritesheet, res);
}

if (!spritesheet.path) {
throw new Error('postcss-sprites: Spritesheet requires a relative path.');
}
if (!spritesheet.path) {
throw new Error('postcss-sprites: Spritesheet requires a relative path.');
}

spritesheet.path = spritesheet.path.replace(/\\/g, '/');
spritesheet.path = spritesheet.path.replace(/\\/g, '/');

return fs.outputFileAsync(spritesheet.path, spritesheet.image);
return fs.outputFileAsync(spritesheet.path, spritesheet.image);
});
}).then(spritesheets => {
return [opts, images, spritesheets];
});
Expand Down
18 changes: 18 additions & 0 deletions test/08-save-spritesheets.js
Expand Up @@ -105,3 +105,21 @@ test('should throw error if path is empty', async (t) => {

t.throws(saveSpritesheets(images, t.context.opts, spritesheets));
});

test('should use Promise result provided by book', async (t) => {
const cssContents = await readFileAsync('./fixtures/basic/style.css');
const ast = postcss.parse(cssContents, { from: './fixtures/basic/style.css' });
let images, spritesheets, opts;

t.context.opts.spritePath = './build/on-save-hook/';
t.context.opts.hooks.onSaveSpritesheet = (pluginOpts, spritesheetGroups) => {
return new Promise(( resolve ) => setTimeout(() => resolve(Promise.resolve(path.join(pluginOpts.spritePath, 'custom-name.png'))), 0));
}

[ opts, images ] = await extractImages(ast, t.context.opts);
[ opts, images, spritesheets ] = await runSpritesmith(t.context.opts, images);
[ opts, images, spritesheets ] = await saveSpritesheets(t.context.opts, images, spritesheets);

t.deepEqual(spritesheets[0].path, 'build/on-save-hook/custom-name.png');
t.truthy(fs.statAsync('./build/on-save-hook/custom-name.png'));
});

0 comments on commit 131f06b

Please sign in to comment.