forked from ne1ro/siteshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siteshot.js
executable file
·97 lines (82 loc) · 2.99 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
#!/usr/bin/env node
// Generated by CoffeeScript 1.6.3
(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,
_this = this;
if (process.argv.indexOf('config') !== -1) {
this.config();
} else {
config = require("" + (process.cwd()) + "/siteshot.json");
parseString(fs.readFileSync(config.sitemap), function(err, result) {
var routes;
if (err != null) {
throw err;
} else {
routes = _.flatten(_.pluck(result.urlset.url, 'loc'));
return phantom.create(function(ph) {
var pageLoad;
pageLoad = function(route, callback) {
return ph.createPage(function(page) {
return page.open(route, function(status) {
if (status === 'success') {
return page.evaluate((function() {
return document.documentElement.outerHTML;
}), 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();
});
});
});
}
});
});
};
return async.eachSeries(routes, pageLoad, function(err) {
if (err != null) {
throw err;
}
console.log('----------------------------');
console.log('Finish snapshots');
return ph.exit();
});
});
}
});
}
}
SiteShot.prototype.config = function() {
var example;
example = {
snapshotDir: "" + (process.cwd()) + "/snapshots",
sitemap: "" + (process.cwd()) + "/sitemap.xml"
};
return fs.writeFileSync('siteshot.json', JSON.stringify(example, null, 2));
};
return SiteShot;
})();
module.exports = SiteShot;
new SiteShot;
}).call(this);