Skip to content

Commit 89cf3e5

Browse files
author
Shuwen Qian
committed
WIP gulp docs build
- Create ad-hoc plugin that handles behavior mixins - Stubs in place for article/module/meta stuff https://i.imgur.com/ChzUb.jpg
1 parent 1563cdb commit 89cf3e5

2 files changed

Lines changed: 125 additions & 1 deletion

File tree

Gulpfile.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ var git = require('gulp-git');
2727
var bump = require('gulp-bump');
2828
var tagVersion = require('gulp-tag-version');
2929
var conventionalChangelog = require('gulp-conventional-changelog');
30+
var ghPages = require('gulp-gh-pages');
31+
var through = require('through2');
32+
var tap = require('gulp-tap');
3033

3134
var SRC = 'src/';
3235
var BUILD = 'build/';
@@ -40,6 +43,10 @@ gulp.task('clean', function() {
4043
return del([BUILD + '**', BUILD_DOCS+ '**', DIST+ '**']);
4144
});
4245

46+
gulp.task('clean:docs', function() {
47+
return del([BUILD_DOCS+'**']);
48+
});
49+
4350
gulp.task('copy', function() {
4451
return gulp.src([SRC + '**/*.+(html|js|woff)', '!' + SRC +'**/example.html'])
4552
.pipe(changed(BUILD))
@@ -158,9 +165,123 @@ gulp.task('docs', function() {
158165
.pipe(marked().on('error',console.log))
159166
.pipe(wrap({src:"./docs/article_template.html"},{},{engine:"hogan"}).on('error',console.log))
160167
.pipe(gulp.dest(BUILD_DOCS));
168+
169+
// return merge(moduleStream, articleStream);
161170
return articles;
162171
});
163172

173+
gulp.task('docs:modules', function() {
174+
function mergeDocArray(doc, behavior) {
175+
var p = {};
176+
if (!behavior) {
177+
return doc;
178+
}
179+
if (!doc && behavior) {
180+
return behavior;
181+
}
182+
behavior.forEach(function(obj) {
183+
if(obj.name) p[obj.name] = obj;
184+
else if(obj.type) p[obj.type] = obj;
185+
});
186+
doc.forEach(function(obj) {
187+
if(obj.name) p[obj.name] = obj;
188+
else if(obj.type) p[obj.type] = obj;
189+
});
190+
return Object.keys(p).map(function(key) {
191+
return p[key];
192+
});
193+
}
194+
195+
// Ad-hoc plugin for injecting behaviors
196+
function injectBehaviorDocs(behaviorsMap) {
197+
return through.obj(function(file, enc, cb) {
198+
if(!file.isBuffer()) this.emit('error', new gutil.PluginError('Buffer required'));
199+
200+
var moduleDoc = JSON.parse(file.contents),
201+
moduleBehaviors = moduleDoc.behaviors;
202+
203+
// Merge behaviors docs with component docs
204+
moduleBehaviors.forEach(function(key) {
205+
var behavior = behaviorsMap[key];
206+
if(moduleDoc && behavior) {
207+
moduleDoc.attributes = mergeDocArray(moduleDoc.attributes, behavior.attributes);
208+
moduleDoc.methods = mergeDocArray(moduleDoc.methods, behavior.methods);
209+
moduleDoc.events = mergeDocArray(moduleDoc.events, behavior.events);
210+
}
211+
});
212+
213+
file.contents = new Buffer(JSON.stringify(moduleDoc), enc);
214+
this.push(file);
215+
cb();
216+
});
217+
}
218+
219+
// Ad-hoc plugin for injecting everything else
220+
function injectDocsMeta(pkg, moduleList, articleList, articleMap) {
221+
return through.obj(function(file, enc, cb) {
222+
var moduleDoc = JSON.parse(file.contents);
223+
224+
// Inject metadata
225+
moduleDoc.revision = pkg.version;
226+
moduleDoc.modules = moduleList;
227+
moduleDoc.articleList = articleList;
228+
moduleDoc.articleMap = articleMap;
229+
230+
file.contents = new Buffer(JSON.stringify(moduleDoc), enc);
231+
this.push(file);
232+
cb();
233+
});
234+
}
235+
236+
var pkg = getPkgInfo();
237+
238+
// Create moduleList
239+
var moduleList = [];
240+
241+
// Create behaviorsMap
242+
var behaviors = glob.sync(SRC+'shared/behaviors/*.json');
243+
var behaviorsMap = {};
244+
behaviors.forEach(function(behavior) {
245+
var behaviorKey = behavior.replace(SRC+'shared/behaviors/','')
246+
.replace('.json','')
247+
.toLowerCase();
248+
behaviorsMap[behaviorKey] = JSON.parse(fs.readFileSync(behavior));
249+
});
250+
251+
// Create articleList and articleMap
252+
var articleList = [];
253+
var articleMap = {};
254+
255+
// Finally open the stream
256+
gulp.src(SRC+'mm-*/doc.json')
257+
.pipe(injectBehaviorDocs(behaviorsMap))
258+
.pipe(injectDocsMeta(pkg, moduleList, articleList, articleMap))
259+
.pipe(tap(function(file, t) {
260+
var moduleDoc = JSON.parse(file.contents);
261+
var curried = wrap.bind(
262+
{src:"./docs/component_template.html"},
263+
moduleDoc,
264+
{engine:"hogan"}
265+
);
266+
return t.through(curried, []);
267+
}))
268+
.pipe(rename(function(path) {
269+
path.basename = path.dirname;
270+
path.dirname = '';
271+
path.extname = '.html';
272+
}))
273+
.pipe(gulp.dest(BUILD_DOCS))
274+
.on('error',console.log);
275+
});
276+
277+
gulp.task('gh-pages', function() {
278+
var pkg = getPkgInfo();
279+
return gulp.src(BUILD_DOCS+'**/*')
280+
.pipe(ghPages({
281+
message: 'docs updates v'+pkg.version
282+
}));
283+
});
284+
164285
/** LIVE **/
165286

166287
gulp.task('watch', function () {

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"gulp-changed": "^1.3.0",
2525
"gulp-conventional-changelog": "^1.0.1",
2626
"gulp-debug": "^2.1.2",
27+
"gulp-gh-pages": "^0.5.4",
2728
"gulp-git": "^1.7.0",
2829
"gulp-inline-assets": "^0.1.1",
2930
"gulp-marked": "^1.0.0",
@@ -33,12 +34,14 @@
3334
"gulp-rename": "^1.2.2",
3435
"gulp-sass": "^2.1.0",
3536
"gulp-tag-version": "^1.3.0",
37+
"gulp-tap": "^0.1.3",
3638
"gulp-util": "^3.0.7",
3739
"gulp-vulcanize": "^6.0.1",
3840
"gulp-wrap": "^0.11.0",
3941
"hogan.js": "^3.0.2",
4042
"marked": "^0.3.3",
4143
"merge-stream": "^1.0.0",
42-
"run-sequence": "^1.1.4"
44+
"run-sequence": "^1.1.4",
45+
"through2": "^2.0.1"
4346
}
4447
}

0 commit comments

Comments
 (0)