Comparing version 0.0.1 to 0.0.2
55
index.js
@@ -8,5 +8,34 @@ 'use strict'; | ||
var path = require('path'); | ||
var gulp = require('gulp'); | ||
module.exports = function (data, opts) { | ||
// 不使用 gulp.dest 存盘因为 | ||
// 1. gulp.dest 获取的文件路径依赖 file.relative | ||
// 2. gulp.watch 时 gulp.src(event.path) 获取的 file.relative 仅有文件名 | ||
// 3. gulp.src 通过通配符匹配的文件,获取的 file.relative 是相对 gulp 启动目录的 | ||
module.exports.savefile = function (opts) { | ||
return through.obj(function (file, enc, cb) { | ||
if (file.isNull()) { | ||
this.push(file); | ||
return cb(); | ||
} | ||
if (file.isStream()) { | ||
this.emit('error', new gutil.PluginError('gulp-tpl.save', 'Streaming not supported')); | ||
return cb(); | ||
} | ||
try { | ||
fs.writeFileSync(file.path, file.contents || ''); | ||
} catch (err) { | ||
this.emit('error', new gutil.PluginError('gulp-tpl.save', err)); | ||
} | ||
this.push(file); | ||
cb(); | ||
}); | ||
} | ||
// 组合 Handlebars 和 yaml 自动生成网页 | ||
module.exports.html = function (opts) { | ||
var options = opts || {}; | ||
@@ -26,3 +55,2 @@ //Go through a partials object | ||
return through.obj(function (file, enc, cb) { | ||
@@ -35,12 +63,27 @@ if (file.isNull()) { | ||
if (file.isStream()) { | ||
this.emit('error', new gutil.PluginError('gulp-compile-handlebars', 'Streaming not supported')); | ||
this.emit('error', new gutil.PluginError('gulp-tpl.html', 'Streaming not supported')); | ||
return cb(); | ||
} | ||
var filepath = file.path; | ||
var dirname = path.dirname(filepath); | ||
var basename = path.basename(filepath); | ||
var name = path.basename(filepath, path.extname(basename)); | ||
var tplfile = path.join(dirname, name + ".hbs"); | ||
var datafile = path.join(dirname, name + ".yaml"); | ||
var htmlfile = path.join(dirname, name + ".html"); | ||
var tpl = fs.readFileSync(tplfile, 'utf8'); | ||
var data = {}; | ||
if (fs.existsSync(datafile)) { | ||
data = yaml.safeLoad(fs.readFileSync(datafile, 'utf8')); | ||
} | ||
try { | ||
console.dir(file); | ||
var template = Handlebars.compile(file.contents.toString()); | ||
var template = Handlebars.compile(tpl); | ||
file.contents = new Buffer(template(data)); | ||
file.path = htmlfile; | ||
} catch (err) { | ||
this.emit('error', new gutil.PluginError('gulp-compile-handlebars', err)); | ||
this.emit('error', new gutil.PluginError('gulp-tpl.html', err)); | ||
} | ||
@@ -47,0 +90,0 @@ |
{ | ||
"name": "gulp-tpl", | ||
"version": "0.0.1", | ||
"description": "gulp-tpl\r ========", | ||
"version": "0.0.2", | ||
"description": "handlebars + yaml -> html", | ||
"author": "changkong <changkong012@gmail.com>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -1,2 +0,4 @@ | ||
gulp_tpl | ||
gulp-tpl | ||
======== | ||
handlebars + yaml -> html |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3444
78
4