-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgulp.js
151 lines (126 loc) · 4.1 KB
/
gulp.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var http = require('http');
var _ = require('lodash');
var ecstatic = require('ecstatic');
var gulp = require('gulp');
var path = require('path');
var Q = require('q');
var watch = require('gulp-watch');
var ignore = require('gulp-ignore');
var rimraf = require('gulp-rimraf');
// polyfill
require("./src/ext/starts_with");
var cli = require('./src/cli');
var Configuration = require('./src/configuration');
// Simple options parsing
function parseOptions() {
try {
var res = cli.parse();
var options = res.options;
// supports gulpfiles using src_dir insteaf of srcDir from baked.config
Object.defineProperty(options, "src_dir", {
get: function () { return options.srcDir; },
set: function (value) { options.srcDir = value; }
});
Object.defineProperty(options, "dst_dir", {
get: function () { return options.dstDir; },
set: function (value) { options.dstDir = value; }
});
return options;
} catch (e) {
console.error(e.message);
process.exit(1);
}
}
var initialized = false;
var config = {};
function init(cfg) {
initialized = true;
if (!cfg) cfg = {};
var argOptions = _.defaults({}, cfg.options, {
srcDir: "to_generate",
dstDir: "generated"
});
// supports gulpfiles providing src_dir insteaf of srcDir to init()
if (!argOptions.srcDir) { argOptions.srcDir = argOptions.src_dir; }
if (!argOptions.dstDir) { argOptions.dstDir = argOptions.dst_dir; }
var cliOptions = parseOptions();
var configuration = Configuration.readFromFileSync(_.assign(argOptions, cliOptions, {
ignore: _.union(argOptions.ignore, cliOptions.ignore)
}));
// supports gulpfiles using src_dir insteaf of srcDir from baked.config
Object.defineProperty(configuration, "src_dir", {
get: function () { return configuration.srcDir; },
set: function (value) { configuration.srcDir = value; }
});
Object.defineProperty(configuration, "dst_dir", {
get: function () { return configuration.dstDir; },
set: function (value) { configuration.dstDir = value; }
});
_.assign(config, {
options: configuration,
buildDir: cfg.buildDir || pathTo('build'),
baked: cfg.baked || require('./src/server')
});
return config;
}
var root = __dirname;
function pathTo(localPath) {
return path.join(root, localPath);
}
/* tasks */
function defineTasks(gulp) {
gulp.task('baked:init', function() {
if (!initialized) {
init();
}
});
gulp.task('baked:generate', ['baked:init'], function () {
return Q.fcall(config.beforeGenerate || _.identity)
.then(function () {
return config.baked.generate(config.options);
})
.then(config.afterGenerate || _.identity)
.then(
function (failures) {
if (!_.isEmpty(failures)) {
console.error("" + failures.length + " errors:\n");
_.each(failures, function (failure) {
console.error(failure.src + ": ", failure.error);
console.error(failure.dst, "args:", failure.args||{}, "\n");
});
}
console.info("Ne mangez pas trop vite");
},
function (err) { console.error(err.stack); throw err; }
);
});
gulp.task('baked:server', function () {
var port = 8282;
http.createServer(ecstatic({
root: config.options.dstDir,
baseDir: '/',
cache: 1,
showDir: false,
autoIndex: true,
defaultExt: 'html'
})).listen(port);
console.info("Listen connection on http://127.0.0.1:" + port);
});
gulp.task('baked:watch', ['baked:init'], function () {
gulp.watch(config.options.srcDir + '/**/*', ['baked:generate']);
});
gulp.task('baked:clean', ['baked:init'], function() {
return gulp.src(config.options.dstDir + '/**/*', { read: false })
.pipe(ignore(config.options.dstDir + ' /.gitkeep'))
.pipe(rimraf());
});
/* default tasks */
gulp.task('baked:serve', ['baked:generate', 'baked:server', 'baked:watch']);
gulp.task('baked:default', ['baked:generate']);
}
defineTasks(gulp);
/* exports */
exports.config = config;
exports.init = init;
exports.parseOptions = parseOptions;
exports.defineTasks = defineTasks;