-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
106 lines (89 loc) · 2.94 KB
/
generate.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
var stylus = require('stylus'),
nib = require('nib'),
jade = require('jade'),
walk = require('walk'),
fs = require('fs'),
path = require('path'),
ncp = require('ncp').ncp;
// http://www.geedew.com/2012/10/24/remove-a-directory-that-is-not-empty-in-nodejs/
var rmdirSync = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
rmdirSync(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
rmdirSync('./output');
fs.mkdirSync('./output');
fs.mkdirSync('./output/stylesheets');
fs.mkdirSync('./output/javascripts');
var stylesheets = walk.walk('./src/stylesheets', { followLinks: false });
stylesheets.on('file', function (root, fileStats, next) {
var file = path.join(root, fileStats.name);
if(['_', '.'].indexOf(fileStats.name.charAt(0)) != -1){
console.log('Skipped ' + file);
next();
return;
}
fs.readFile(file, 'utf-8', function(err, data) {
if (err) {
console.error('Could not read ' + file, err);
return;
}
stylus(data)
.set('filename', fileStats.name)
.set('compress', true)
.set('paths', [root])
.use(nib())
.render(function(err, css) {
if (err) {
console.error('Could not process ' + file, err);
return;
}
fs.writeFile('./output/stylesheets/' + fileStats.name.replace(/\.styl$/i, '.css'), css, function(err){
if (err) {
console.error('Could not write ' + file, err);
return;
}
console.log('Processed ' + file);
});
});
});
next();
});
var html = walk.walk('./src/pages', { followLinks: false });
html.on('file', function (root, fileStats, next) {
var file = path.join(root, fileStats.name);
if(['_', '.'].indexOf(fileStats.name.charAt(0)) != -1){
console.log('Skipped ' + file);
next();
return;
}
var html = jade.renderFile(file, { filename: fileStats.name, page: fileStats.name.replace(/\.jade$/, '') });
if (!html) {
console.error('Could not process ' + file, err);
next()
return;
}
fs.writeFile('./output/' + fileStats.name.replace(/\.jade$/i, '.html'), html, function(err){
if (err) {
console.error('Could not write ' + file, err);
return;
}
console.log('Processed ' + file);
});
next();
});
ncp('./static', './output', function (err) {
if (err) {
console.error('Could not copy static/*: ', err);
return;
}
console.log('Processed static/*');
});