forked from pure-css/pure-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
102 lines (92 loc) · 2.7 KB
/
build.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
var fs = require('fs');
var path = require('path');
var child = require('child_process');
var app = require('./app');
var dst = process.argv[2] || '../pure-site-html';
var request = require('request');
var pages = app.locals.pages;
var prefix = process.argv.length > 3 ? process.argv[3] : 'pure-site';
if( prefix && prefix.indexOf('/') !== prefix.length-1 )
prefix = prefix + '/';
/**
* create server to get rendered page.
*/
var http = require('http');
var server = http.createServer(app).listen(app.locals.settings.port, function () {
});
/**
* from node-mkdirp
* @sa https://github.com/substack/node-mkdirp
*/
function mkdir(p) {
p = path.resolve(p);
try {
fs.mkdirSync(p, '0755');
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
mkdir(path.dirname(p));
mkdir(p);
break;
default:
var stat;
try { stat = fs.statSync(p); }
catch (err1) { throw err0; }
if (!stat.isDirectory()) throw err0;
break;
}
}
};
if( !fs.existsSync(dst) )
mkdir(dst);
var regexp = {};
var file = {};
for( var name in pages ) {
var link = pages[name];
if( link === '/' )
file[name] = link + 'index.html';
else
file[name] = link.substring(0, link.length-1) + '.html';
regexp[name] = new RegExp('"' + link + '"', 'g');
}
var host = 'http://localhost:' + app.locals.settings.port;
var timeout = null;
for( var name in pages ) {
(function(name) {
request(host + pages[name], function(err, res, body) {
for( var n in pages )
body = body.replace(regexp[n], '"' + file[n] + '"');
body = body.replace( /"\//g, '"/' + prefix);
var splitted = pages[name].split('/');
if( splitted.length > 3 ) {
splitted.pop(); splitted.pop();
mkdir(dst + splitted.join('/'));
}
fs.writeFileSync(dst + file[name], body);
console.log( 'Completed to write ' + (dst + file[name]) + ' ' + (body.length/8) + 'bytes');
if(timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout( function(){ server.close(); }, 1000 );
});
})(name);
}
var src = './public';
(function cp(dir) {
var files = fs.readdirSync(src+dir);
for( var i=0 ; i<files.length ; i++ ) {
var file = files[i];
if( !file.indexOf('.') ) continue;
if( fs.statSync(src+dir+file).isDirectory() ) {
if( !fs.existsSync(dst+'/'+dir+file) )
fs.mkdirSync(dst+'/'+dir+file, '0755');
cp(dir+file+'/');
} else {
var buff = fs.readFileSync(src+dir+file);
fs.writeFileSync(dst+'/'+dir+file, buff);
console.log('Completed to copy ' + src+dir+file);
}
}
})('/');