forked from jakearchibald/jank-invaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-static.js
103 lines (89 loc) · 2.45 KB
/
build-static.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
// All the files needed for the static version
var manifest = [
'/',
'/static/css/all.css',
'/static/css/fonts/akashi.ttf',
'/static/css/imgs/metal.jpg',
'/static/css/imgs/nebula.jpg',
'/static/css/imgs/stars.png',
'/static/js/all.js',
'/static/js/all.js.map',
'/static/js/q.js',
'/static/js/EventEmitter.js',
'/static/js/ji/index.js',
'/static/js/ji/utils.js',
'/static/js/ji/Ship.js',
'/static/js/ji/Flash.js',
'/static/js/ji/Shot.js',
'/static/js/ji/Explosion.js',
'/static/js/ji/Level.js',
'/static/js/ji/Intro.js',
'/static/js/ji/Summary.js',
'/static/js/main.js',
'/static/imgs/particle-sprites.png',
'/static/imgs/ships.png'
];
var server = 'http://localhost:3000';
var fs = require('fs');
var path = require('path');
var http = require('http');
var Q = require('q');
function mkdirDeep(pathToMake) {
return pathToMake.split('/').reduce(function(pathSoFar, nextPathPart) {
pathSoFar = path.join(pathSoFar, nextPathPart);
if (!fs.existsSync(pathSoFar)) {
fs.mkdirSync(pathSoFar, 0755);
}
return pathSoFar;
}, __dirname);
}
function deleteDir(path) {
if(fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteDir(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
module.exports = function(done) {
// remove current dir
fs.readdirSync('build').forEach(function(file) {
if (file[0] == '.') { return; }
file = 'build/' + file;
if (fs.statSync(file).isDirectory()) {
deleteDir(file);
}
else {
fs.unlinkSync(file);
}
});
var promises = manifest.map(function(urlPath) {
var pathParts = urlPath.split('/').slice(1);
var fileName = pathParts[pathParts.length - 1] || 'index.html';
var dir = pathParts.slice(0, -1).join('/');
var deferred = new Q.defer();
if (dir) {
mkdirDeep('build/' + dir);
}
var staticFile = fs.createWriteStream(path.join('build', dir, fileName), {
flags: 'w',
mode: 0644
});
staticFile.on('error', function(err) {
deferred.reject(err);
});
staticFile.on('close', deferred.resolve.bind(deferred));
http.get(server + urlPath, function(res) {
res.pipe(staticFile);
});
return deferred.promise;
});
Q.all(promises).then(done, function(err) {
throw err;
});
};