Skip to content

Commit 4039645

Browse files
authored
Merge pull request #14 from straker/resolveComplete
fix(livingcss): resolve promise when all pages have been parsed
2 parents 7a166de + 429f327 commit 4039645

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function livingcss(source, dest, options) {
9494
tags[tag] = options.tags[tag];
9595
}
9696

97-
Promise.all([
97+
return Promise.all([
9898
// read the handlebars template
9999
new Promise(function(resolve, reject) {
100100
fs.readFile(options.template, 'utf8', function(err, data) {
@@ -129,7 +129,11 @@ function livingcss(source, dest, options) {
129129

130130
context.allSections = context.sections;
131131

132-
if (context.pages.length > 1) {
132+
if (context.pages.length === 0) {
133+
console.warn('Warning: no pages generated from source files.');
134+
}
135+
136+
else if (context.pages.length > 1) {
133137
context.navbar = context.pages.map(function(page) {
134138
return {
135139
name: page.name,
@@ -138,6 +142,8 @@ function livingcss(source, dest, options) {
138142
});
139143
}
140144

145+
var promises = [];
146+
141147
context.pages.forEach(function(page, index) {
142148
// deep copy context for each page
143149
var pageContext = JSON.parse(JSON.stringify(context));
@@ -151,8 +157,14 @@ function livingcss(source, dest, options) {
151157
}
152158

153159
// values[0] = handlebars template
154-
generate(path.join(dest, page.id + '.html'), values[0], pageContext, options);
160+
promises.push(
161+
generate(path.join(dest, page.id + '.html'), values[0], pageContext, options)
162+
);
155163
});
164+
165+
// wait until all promises have returned (either rejected or resolved) before
166+
// returning the last promise
167+
return Promise.all(promises);
156168
})
157169
.catch(function(err) {
158170
console.error(err.stack);

test/data/no-tags.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
* No tag to parse.
3+
*/

test/integration.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*jshint -W030 */
2+
3+
// test the index.js file and that everything works as expected
4+
var expect = require('chai').expect;
5+
var sinon = require('sinon');
6+
var path = require('path');
7+
var livingcss = require('../index');
8+
9+
describe('livingcss', function() {
10+
11+
it('should resolve when all pages have been processed', function(done) {
12+
var file = path.join(__dirname, 'data/page-multiple-tags.css');
13+
var options = {
14+
preprocess: function(context) {
15+
return false;
16+
}
17+
};
18+
19+
sinon.spy(options, 'preprocess');
20+
21+
livingcss(file, '.', options).then(function() {
22+
expect(options.preprocess.called).to.be.true;
23+
expect(options.preprocess.callCount).to.equal(2);
24+
25+
done();
26+
})
27+
.catch(function(e) {
28+
done(e);
29+
});
30+
});
31+
32+
it('should resolve when no pages have been processed', function(done) {
33+
var file = path.join(__dirname, 'data/no-tags.css');
34+
var options = {
35+
preprocess: function(context) {
36+
return false;
37+
}
38+
};
39+
40+
sinon.spy(options, 'preprocess');
41+
42+
livingcss(file, '.', options).then(function() {
43+
expect(options.preprocess.called).to.be.false;
44+
45+
done();
46+
})
47+
.catch(function(e) {
48+
done(e);
49+
});
50+
});
51+
52+
});

0 commit comments

Comments
 (0)