-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate-static.ts
147 lines (116 loc) · 3.8 KB
/
generate-static.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const fs = require('fs');
const request = require('request');
const mkdirp = require('mkdirp');
const path = require('path');
const rimraf = require('rimraf');
const copydir = require('copy-dir');
const moment = require('moment');
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
export let handleRoutesArray = (arr, resultArr, base = '/') => {
arr.forEach((route: any) => {
if (route.path !== '**') {
resultArr.push(base + route.path);
if (route.children && route.children.length > 0) {
handleRoutesArray(route.children, resultArr, base + route.path + '/');
}
}
});
};
export function generateStaticWebsite(baseHost, port, routesArray, outputLocation, assetsDirectory) {
let siteMapArr = [];
function generateSiteMap(pathArr: string[]) {
console.log('Generating sitemap.xml file');
return new Promise((resolve) => {
let base = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
pathArr.forEach((p: any) => {
let path = p;
if (path === 'index') {
path = '/';
}
let priority;
let depth = (path.split('/') || []).length;
if (depth === 0 || depth === 1) {
priority = '1.0';
} else if (depth === 2) {
priority = '0.8';
} else {
priority = '0.5';
}
base += `
<url>
<loc>http://angular-meteor.com${path}</loc>
<lastmod>${ moment().format('YYYY-MM-DD') }</lastmod>
<changefreq>weekly</changefreq>
<priority>${ priority }</priority>
</url>`;
});
base += `</urlset>`;
let fullFilePath = path.join(outputLocation, 'sitemap.xml');
fs.writeFileSync(fullFilePath, base);
resolve();
});
}
function generateStaticForUrl(fullBaseUrl, urlPath): Promise<string> {
let url = fullBaseUrl + urlPath;
return new Promise((resolve, reject) => {
request(url, (err, res) => {
if (err) {
return reject(err);
}
console.log('Done loading HTML content for ' + url);
resolve({
html: res.body,
url: urlPath
});
});
});
}
rimraf.sync(outputLocation);
copydir.sync(assetsDirectory, path.join(outputLocation, path.basename(assetsDirectory)), function (stat, filepath, filename) {
if (stat === 'file' && (path.extname(filepath) === '.md' || path.extname(filepath) === '.patch')) {
return false;
}
return true;
});
let fullBaseUrl = 'http://' + baseHost + ':' + port;
let urlsToLoad = ['/'];
handleRoutesArray(routesArray, urlsToLoad);
let r;
let donePromise = new Promise((resolve) => {
r = resolve;
});
function runNext(arr, i = 0) {
if (arr[i]) {
generateStaticForUrl(fullBaseUrl, arr[i]).then((result: any) => {
let filePath = result.url;
if (filePath === '/') {
filePath = 'index';
}
siteMapArr.push(filePath);
let filePathIndex = filePath + '/index.html';
filePath = filePath + '.html';
let fullFilePath = path.join(outputLocation, filePath);
let fullFilePathIndex = path.join(outputLocation, filePathIndex);
mkdirp.sync(path.dirname(fullFilePath));
mkdirp.sync(path.dirname(fullFilePathIndex));
fs.writeFileSync(fullFilePath, result.html);
fs.writeFileSync(fullFilePathIndex, result.html);
console.log('Done writing to files:' + [fullFilePath, fullFilePathIndex].join(', '));
runNext(arr, i + 1);
});
} else {
generateSiteMap(siteMapArr).then(() => {
r();
});
}
return donePromise;
}
return runNext(urlsToLoad);
}