-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.js
252 lines (193 loc) · 7.31 KB
/
processor.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'use strict';
var fs = require('fs-extra');
var path = require('path');
var Q = require('q');
var gulp = require('gulp');
var gutil = require('gulp-util');
var jade = require('jade');
var sass = require('gulp-sass');
var csso = require('gulp-csso');
var tap = require('gulp-tap');
var _ = require('lodash');
var templateUtils = require('./template_utils');
var ext = require('gulp-util').replaceExtension;
var vinylFS = require('vinyl-fs');
function Processor(branches) {
if (!(this instanceof Processor)) {
return new Processor(branches);
}
this.branches = branches;
this.customTasks = {};
}
module.exports = Processor;
Processor.prototype.setRepo = function(repo) {
this.repo = repo;
var split = repo.path.split('/');
this.reponame = split[split.length-1];
this.username = split[split.length-2];
this.inputPath = path.resolve('./.tmp/input/' + this.username + '/' + this.reponame + '/' + (new Date().getTime()));
this.outputPath = path.resolve('./.tmp/output/' + this.username + '/' + this.reponame + '/' + (new Date().getTime()));
};
/*
* Create temp input and output folders
*/
Processor.prototype.readyRepo = function() {
var self = this;
// default tasks
this._registerGulpTasks();
// user generated tasks
_.each(this.customTasks, function(val, key) {
gulp.task(key, val(self.repo.path, self.outputPath));
});
return Q.ninvoke(self.repo, 'checkout', this.branches.inputBranch)
.then(function() {
return Q.nfcall(fs.mkdirs, self.inputPath);
})
.then(function() {
return Q.nfcall(fs.copy, self.repo.path, self.inputPath);
})
.then(function() {
return Q.nfcall(fs.mkdirs, self.outputPath);
});
};
Processor.prototype.processRepo = function() {
return Q.ninvoke(this.repo, 'checkout', this.branches.buildBranch)
.then(function() {
return Q.ninvoke(gulp, 'start', 'static');
}).then(function() {
return Q.ninvoke(gulp, 'start', 'templates');
}).then(function() {
return Q.ninvoke(gulp, 'start', 'js');
});
};
Processor.prototype.publishRepo = function() {
var self = this;
return Q.ninvoke(this.repo, 'checkout', this.branches.outputBranch)
.then(function() {
return Q.ninvoke(self.repo, 'remove', self.repo.path + '/*', {
r: true
});
})
.then(function() {
return Q.nfcall(fs.copy, self.outputPath, self.repo.path);
})
.then(function() {
return Q.ninvoke(self.repo, 'add', self.repo.path + '/*');
})
.then(function() {
return Q.ninvoke(self.repo, 'commit', '[Autopages] auto-creating new github pages.');
})
.then(function() {
return Q.ninvoke(self.repo, 'remote_push', 'origin');
});
};
Processor.prototype._getInputPath = function(path) {
var inputPath = path.replace(this.repo.path, this.inputPath).replace('/templates', '');
return inputPath.substr(0, inputPath.lastIndexOf('/'));
};
Processor.prototype._getInputFilenames = function(path) {
var inputPath = this._getInputPath(path);
var files;
try {
files = fs.readdirSync(inputPath);
} catch(err) {
files = [];
}
return files;
};
Processor.prototype._registerGulpTasks = function() {
var self = this;
gulp.task('css', function() {
return gulp
.src(self.repo.path + '/stylesheets/app.scss')
.pipe(
sass({
includePaths: [self.repo.path + '/stylesheets'],
errLogToConsole: true
}))
.pipe( csso() )
.pipe( gulp.dest(self.outputPath + '/css/') );
});
gulp.task('images', function() {
return gulp
.src(self.repo.path + '/images/**/*')
.pipe( gulp.dest(self.outputPath + '/images/') );
});
gulp.task('fonts', function() {
return gulp
.src(self.repo.path + '/fonts/**/*.{otf,svg,ttf,woff,eot}')
.pipe( gulp.dest(self.outputPath + '/fonts/') );
});
gulp.task('js', function() {
return gulp
.src(self.repo.path + '/js/**/*')
.pipe(gulp.dest(self.outputPath + '/js/'));
});
gulp.task('copy', function() {
return gulp
.src(self.repo.path + '/copy/**/*')
.pipe(gulp.dest(self.outputPath + '/'));
});
gulp.task('static', ['images', 'fonts', 'css', 'copy']);
gulp.task('list-templates', function() {
// this is really ugly so far...
// takes a file like /posts/_index.jade
//
// and creates
//
// /posts/my-post-1/index.html
// /posts/my-post-2/index.html
// ...
// etc
//
vinylFS
.src(self.repo.path + '/templates/**/_index.jade')
.pipe(tap(function(indexFile) {
var contentFiles = self._getInputFilenames(indexFile.path);
_.each(contentFiles, function(contentFile) {
gulp.src(indexFile.path)
.pipe(tap(function(f) {
var compile = jade.compile(String(f.contents), {
filename: f.path,
pretty: false
});
var compiled = compile({
_: _,
inputPath: self._getInputPath(f.path),
file: contentFile,
autopages: templateUtils(self._getInputPath(f.path))
});
f.contents = new Buffer(compiled);
f.base = indexFile.base;
f.path = f.path.replace('_index.jade', '') + ext(contentFile, '') + '/index.html';
}))
.pipe(gulp.dest(self.outputPath + '/'));
});
}));
});
gulp.task('static-templates', function() {
return gulp
.src(self.repo.path + '/templates/**/index*.jade')
.pipe(tap(function(file) {
var compile = jade.compile(String(file.contents), {
filename: file.path,
pretty: false
});
var compiled = compile({
_: _,
inputPath: self._getInputPath(file.path),
files: self._getInputFilenames(file.path),
autopages: templateUtils(self._getInputPath(file.path))
});
file.contents = new Buffer(compiled);
file.path = ext(file.path, '.html');
}))
.on('error', gutil.log)
.pipe(gulp.dest(self.outputPath + '/'));
});
gulp.task('templates', ['static-templates', 'list-templates']);
};
Processor.prototype.use = function(taskObj) {
this.customTasks = _.extend(this.customTasks, taskObj);
return this;
};