-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (75 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var gulp = require('gulp');
var path = require('path');
var fs = require('fs');
var gulpif = require('gulp-if');
var pug = require('gulp-pug');
var htmlMin = require('gulp-htmlmin');
var yargs = require('yargs').argv;
var _ = require('lodash');
var cwd = process.cwd();
var default_config = {
src: [
'!' + path.join(cwd, 'src', 'templates', 'client', '**'),
'!' + path.join(cwd, 'src', 'templates', '**', '_*.pug'),
path.join(cwd, 'src', 'templates', '**', '*.pug')
],
dest: path.join(cwd, 'dist')
};
var handler = {
env: '',
argv: yargs,
src_path: cwd,
static_path: 'static/',
getFile: getFile,
getJSON: getJSON,
static_url: function(path, base_path) {
return staticURL.call(this, path, base_path || this.static_path);
}
};
var self = {
config: default_config,
handler: handler,
set: function(config) {
this.config = _.assign(this.config, config);
},
run: function(config) {
config = config || this.config;
config.handler = _.assign(this.handler, config.handler);
return templates(config);
}
};
function getFile(path) {
return fs.readFileSync(path, {
encoding: 'utf-8'
});
}
function getJSON(path) {
var str = getFile(path);
if (str) {
return JSON.parse(str);
}
return {};
}
function staticURL(path, base_path) {
if (this.env === 'jinja') {
return "{{ handler.static_url('" + path + "') }}";
}
return base_path + path;
}
function templates(config) {
return gulp.src(config.src)
.pipe(
pug({
locals: {
handler: config.handler || {}
},
pretty: (yargs.prod) ? false : true
})
)
.pipe(gulpif(yargs.prod, htmlMin()));
}
gulp.task('templates', function() {
self.run()
.pipe(gulp.dest(self.config.dest));
});
module.exports = self;