-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsiteshot.js
executable file
·118 lines (102 loc) · 4.18 KB
/
siteshot.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
#!/usr/bin/env node
// Generated by CoffeeScript 1.10.0
(function() {
var SiteShot, _, async, fs, mkdirp, parseString, path, phantom, url;
fs = require('fs');
url = require('url');
path = require('path');
_ = require('lodash');
parseString = require('xml2js').parseString;
phantom = require('phantom');
async = require('async');
mkdirp = require('mkdirp');
SiteShot = (function() {
function SiteShot() {
var config;
if (process.argv.indexOf('config') !== -1) {
this.config();
} else {
config = require((process.cwd()) + "/config.js");
parseString(fs.readFileSync(config.sitemap), (function(_this) {
return function(err, result) {
var routes;
if (err != null) {
throw err;
} else {
routes = _.flatten(_.pluck(result.urlset.url, 'loc'));
return phantom.create('--ignore-ssl-errors=yes', '--ssl-protocol=any', function(ph) {
var pageLoad;
pageLoad = function(route, callback) {
return ph.createPage(function(page) {
return page.open(route, function(status) {
if (status === 'success') {
return setTimeout((function() {
var generateHTML;
generateHTML = function() {
return page.evaluate((function() {
var doctype, html;
doctype = (new XMLSerializer).serializeToString(document.doctype);
html = document.documentElement.outerHTML;
return doctype + html;
}), (function(_this) {
return function(res) {
var snapPath, snapPrefix;
snapPrefix = url.parse(route).path === '/' ? '/index' : url.parse(route).path;
snapPath = "" + config.snapshotDir + snapPrefix + ".html";
return mkdirp(path.dirname(snapPath), function(err) {
if (err != null) {
throw err;
}
return fs.writeFile(snapPath, res, function(err) {
if (err != null) {
throw err;
}
page.close();
console.log("Finish loading " + route + " and save it in " + snapPath);
return callback();
});
});
};
})(this));
};
if (typeof config.pageModifier === 'function') {
return config.pageModifier(page, function() {
return generateHTML();
});
} else {
return generateHTML();
}
}), config.delay || 0);
}
});
});
};
return async.eachSeries(routes, pageLoad, function(err) {
if (err != null) {
throw err;
}
console.log('----------------------------');
console.log('Finish snapshots');
return ph.exit();
});
});
}
};
})(this));
}
}
SiteShot.prototype.config = function() {
var example;
example = {
snapshotDir: (process.cwd()) + "/snapshots",
sitemap: (process.cwd()) + "/sitemap.xml",
delay: 500,
pageModifier: null
};
return fs.writeFileSync('config.js', "module.exports = " + (JSON.stringify(example, null, 2)) + ";");
};
return SiteShot;
})();
module.exports = SiteShot;
new SiteShot;
}).call(this);