Skip to content

Commit 2bc5d7a

Browse files
committed
Add verbose option
Closes #65
1 parent 2142930 commit 2bc5d7a

12 files changed

+61
-19
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ Built-in filters:
181181

182182
> A [svg-sprite](https://github.com/jkphl/svg-sprite#configuration-basics) configuration.
183183
184+
###### verbose
185+
186+
> Prints the plugin output to the console.
187+
188+
- Default: `false`
189+
- Required: `false`
190+
184191
----
185192

186193
### The Image

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"dependencies": {
3535
"bluebird": "^3.1.1",
36+
"debug": "^2.6.0",
3637
"fs-extra": "^0.26.4",
3738
"lodash": "^4.0.0",
3839
"postcss": "^5.0.14",

src/core.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs-extra';
33
import postcss from 'postcss';
44
import Promise from 'bluebird';
55
import _ from 'lodash';
6+
import debug from 'debug';
67
import RasterFactory from './factories/raster';
78
import VectorFactory from './factories/vector';
89

@@ -69,7 +70,8 @@ export const defaults = {
6970
svg: {
7071
precision: 5
7172
}
72-
}
73+
},
74+
verbose: false
7375
};
7476

7577
/**
@@ -87,8 +89,11 @@ export function prepareFilterBy(opts, result) {
8789
opts.filterBy.unshift(image => {
8890
return fs.statAsync(image.path)
8991
.catch(() => {
90-
result.warn(`skip ${image.url} because doesn't exist.`);
91-
throw new Error(`Skip ${image.url} because doesn't exist.`);
92+
const message = `Skip ${image.url} because doesn't exist.`;
93+
94+
opts.logger(message);
95+
96+
throw new Error(message);
9297
});
9398
});
9499
}
@@ -135,6 +140,8 @@ export function prepareGroupBy(opts) {
135140
export function extractImages(root, opts, result) {
136141
let images = [];
137142

143+
opts.logger('Extracting the images...');
144+
138145
// Search for background & background image declartions
139146
root.walkRules((rule) => {
140147
const styleFilePath = opts.relativeTo === RELATIVE_TO_RULE ? rule.source.input.file : root.source.input.file;
@@ -175,7 +182,7 @@ export function extractImages(root, opts, result) {
175182

176183
images.push(image);
177184
} else {
178-
result.warn(`skip ${image.url} because isn't supported.`);
185+
opts.logger(`Skip ${image.url} because isn't supported.`)
179186
}
180187
}
181188
});
@@ -193,6 +200,8 @@ export function extractImages(root, opts, result) {
193200
* @return {Promise}
194201
*/
195202
export function applyFilterBy(opts, images) {
203+
opts.logger('Applying the filters...');
204+
196205
return Promise.reduce(opts.filterBy, (images, filterFn) => {
197206
return Promise.filter(images, (image) => {
198207
return filterFn(image)
@@ -209,6 +218,8 @@ export function applyFilterBy(opts, images) {
209218
* @return {Promise}
210219
*/
211220
export function applyGroupBy(opts, images) {
221+
opts.logger('Applying the groups...');
222+
212223
return Promise.reduce(opts.groupBy, (images, groupFn) => {
213224
return Promise.map(images, (image) => {
214225
return groupFn(image)
@@ -286,6 +297,8 @@ export function setTokens(root, opts, images) {
286297
* @return {Promise}
287298
*/
288299
export function runSpritesmith(opts, images) {
300+
opts.logger('Generating the spritesheets...');
301+
289302
return new Promise((resolve, reject) => {
290303
const promises = _.chain(images)
291304
.groupBy((image) => {
@@ -327,24 +340,25 @@ export function runSpritesmith(opts, images) {
327340
* @return {Promise}
328341
*/
329342
export function saveSpritesheets(opts, images, spritesheets) {
343+
opts.logger('Saving the spritesheets...');
344+
330345
return Promise.each(spritesheets, (spritesheet) => {
331346
return (
332347
_.isFunction(opts.hooks.onSaveSpritesheet) ?
333348
Promise.resolve(opts.hooks.onSaveSpritesheet(opts, spritesheet)) :
334349
Promise.resolve(makeSpritesheetPath(opts, spritesheet))
335350
)
336351
.then(( res ) => {
352+
if (!res) {
353+
throw new Error('postcss-sprites: Spritesheet requires a relative path.');
354+
}
337355

338356
if ( _.isString(res) ) {
339357
spritesheet.path = res;
340358
} else {
341359
_.assign(spritesheet, res);
342360
}
343361

344-
if (!spritesheet.path) {
345-
throw new Error('postcss-sprites: Spritesheet requires a relative path.');
346-
}
347-
348362
spritesheet.path = spritesheet.path.replace(/\\/g, '/');
349363

350364
return fs.outputFileAsync(spritesheet.path, spritesheet.image);
@@ -392,6 +406,8 @@ export function mapSpritesheetProps(opts, images, spritesheets) {
392406
* @return {Promise}
393407
*/
394408
export function updateReferences(root, opts, images, spritesheets) {
409+
opts.logger('Replacing the references...');
410+
395411
root.walkComments((comment) => {
396412
let rule, image;
397413

@@ -577,3 +593,17 @@ export function makeSpritesheetPath(opts, { groups, extension }) {
577593
export function isToken(commentValue) {
578594
return commentValue.indexOf(COMMENT_TOKEN_PREFIX) > -1;
579595
}
596+
597+
/**
598+
* Create a logger that can be disabled in runtime.
599+
*
600+
* @param {Boolean} enabled
601+
* @return {Function}
602+
*/
603+
export function createLogger(enabled) {
604+
if (enabled) {
605+
debug.enable('postcss-sprites');
606+
}
607+
608+
return debug('postcss-sprites');
609+
}

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
runSpritesmith,
1212
saveSpritesheets,
1313
mapSpritesheetProps,
14-
updateReferences
14+
updateReferences,
15+
createLogger
1516
} from './core';
1617

1718
/**
@@ -22,6 +23,9 @@ export default postcss.plugin('postcss-sprites', (options = {}) => {
2223
// Extend defaults
2324
const opts = _.merge({}, defaults, options);
2425

26+
// Setup the logger
27+
opts.logger = createLogger(opts.verbose);
28+
2529
// Prepare filter & group functions
2630
prepareFilterBy(opts, result);
2731
prepareGroupBy(opts);
@@ -36,7 +40,7 @@ export default postcss.plugin('postcss-sprites', (options = {}) => {
3640
.spread((opts, images, spritesheets) => mapSpritesheetProps(opts, images, spritesheets))
3741
.spread((opts, images, spritesheets) => updateReferences(css, opts, images, spritesheets))
3842
.spread((root, opts, images, spritesheets) => {
39-
result.warn(`${spritesheets.length} ${spritesheets.length > 1 ? 'spritesheets' : 'spritesheet'} generated.`);
43+
opts.logger(`${spritesheets.length} ${spritesheets.length > 1 ? 'spritesheets' : 'spritesheet'} generated.`);
4044
})
4145
.catch((err) => {
4246
console.error(`postcss-sprites: An error occurred while processing files - ${err.message}`);

test/03-extract-images.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test.beforeEach((t) => {
1111
`;
1212

1313
t.context.ast = postcss.parse(input, { from: '/tmp/test.css' });
14-
t.context.opts = Object.assign({}, defaults);
14+
t.context.opts = Object.assign({ logger() {} }, defaults);
1515
});
1616

1717
test('should convert rules to image objects', async (t) => {

test/04-apply-filter-by.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import _ from 'lodash';
55
import { defaults, prepareFilterBy, extractImages, applyFilterBy } from '../lib/core';
66

77
test.beforeEach(async (t) => {
8-
t.context.opts = _.merge({}, defaults);
8+
t.context.opts = _.merge({ logger() {} }, defaults);
99

1010
const input = `
1111
.selector-a { background-image: url(circle.png); }

test/05-apply-group-by.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test.beforeEach((t) => {
1010
.selector-b { background: url(square@2x.png) no-repeat 0 0; }
1111
`;
1212

13-
t.context.opts = _.merge({}, defaults);
13+
t.context.opts = _.merge({ logger() {} }, defaults);
1414
t.context.ast = postcss.parse(input, { from: '/tmp/test.css' });
1515
});
1616

test/06-set-tokens.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defaults, extractImages, setTokens } from '../lib/core';
55

66
async function run(input) {
77
let root = postcss.parse(input, { from: '/tmp/test.css' });
8-
let opts = _.merge({}, defaults);
8+
let opts = _.merge({ logger() {} }, defaults);
99
let images;
1010

1111
[ opts, images ] = await extractImages(root, opts);

test/07-run-spritesmith.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { defaults, extractImages, prepareGroupBy, applyGroupBy, runSpritesmith }
88
const readFileAsync = Promise.promisify(fs.readFile);
99

1010
test.beforeEach((t) => {
11-
t.context.opts = _.merge({}, defaults);
11+
t.context.opts = _.merge({ logger() {} }, defaults);
1212
});
1313

1414
test('should generate spritesheets', async (t) => {

test/08-save-spritesheets.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
const readFileAsync = Promise.promisify(fs.readFile);
1717

1818
test.beforeEach((t) => {
19-
t.context.opts = _.merge({}, defaults);
19+
t.context.opts = _.merge({ logger() {} }, defaults);
2020
});
2121

2222
test('should save spritesheets', async (t) => {
@@ -103,7 +103,7 @@ test('should throw error if path is empty', async (t) => {
103103
[ opts, images ] = await extractImages(ast, t.context.opts);
104104
[ opts, images, spritesheets ] = await runSpritesmith(t.context.opts, images);
105105

106-
t.throws(saveSpritesheets(images, t.context.opts, spritesheets));
106+
t.throws(saveSpritesheets(t.context.opts, images, spritesheets));
107107
});
108108

109109
test('should use Promise result provided by book', async (t) => {

0 commit comments

Comments
 (0)