-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm.js
More file actions
175 lines (152 loc) · 5.49 KB
/
Copy pathsm.js
File metadata and controls
175 lines (152 loc) · 5.49 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const {SitemapStream} = require('sitemap');
const {createWriteStream} = require('fs');
const {resolve} = require('path');
const axios = require('axios');
const {set, keys, forEach, bindAll} = require('lodash');
const rateLimit = require('axios-rate-limit');
// const { execSync } = require('child_process');
class SiteBuilder {
constructor() {
this.api = rateLimit(axios.create(), {maxRequests: 75, perMilliseconds: 1000});
// const currBranch = execSync(`git rev-parse --abbrev-ref HEAD`);
const currBranch = process.env.GIT_BRANCH;
console.log(`==== branch: ${currBranch} ====`);
if (currBranch === 'master') {
this.baseURL = 'https://api.allclear.app';
this.appUrl = 'https://go.allclear.app';
} else if (currBranch === 'staging') {
this.baseURL = 'https://api-staging.allclear.app';
this.appUrl = 'https://app-staging.allclear.app';
} else if (currBranch === 'dev') {
this.baseURL = 'https://api-dev.allclear.app';
this.appUrl = 'https://app-dev.allclear.app';
} else {
this.baseURL = 'https://api.allclear.app';
this.appUrl = 'https://go.allclear.app';
}
console.log('--- Creating Sitemap With These URLs: ---');
console.log('baseURL', this.baseURL);
console.log('appUrl', this.appUrl);
this.failures = {};
bindAll(this, ['getStates', 'getCities', 'build', 'populateCities', 'populateAllSitesInCity']);
if (currBranch === 'staging' || currBranch === 'master' || currBranch === 'dev') {
console.log(`Building on branch: ${currBranch}`);
this.build();
} else {
console.log(`No local branch, using prod: ${currBranch}`);
this.build();
}
}
async getLocations(body) {
return this.api({
method: 'POST',
headers: {'content-type': 'application/json'},
url: `${this.baseURL}/facilities/search`,
data: {
...body,
pageSize: 500, // todo: maybe this is not enough in the future
},
});
}
async getStates() {
return this.api({
method: 'GET',
headers: {'content-type': 'application/json'},
url: `${this.baseURL}/facilities/states`,
});
}
async getCities(state) {
return this.api({
method: 'GET',
url: `${this.baseURL}/facilities/cities`,
params: {state},
});
}
populateAllSitesInCity() {
let allPromises = [];
// loop thru states
forEach(this.stateMap, async (e, state) => {
// loop thru cities
forEach(this.stateMap[state].cities, (e, city) => {
// call a search for all facilities within a city
const currPromise = this.getLocations({city, state})
.then(({data}) => {
const {records} = data;
// const dashedCity = dashStr(city);
// const dashedState = dashStr(state);
set(this, `stateMap.${state}.cities.${city}`, {...e, records});
console.log(`==SETTING locations for: ${city}, ${state}`);
// finally, write out this facility route to the sitemap
forEach(records, (facility) => {
// const dashedName = dashStr(facility.name);
const {id, name} = facility;
const urlSuffix = (name) ? name : id;
// todo: comment this in for both name and id
// this.smStream.write({url: `/locations/${dashedState}/${dashedCity}/${dashedName}`});
this.smStream.write({url: `/locations/${state}/${city}/${urlSuffix}`});
});
})
.catch((err) => {
this.failures[city] = {...err, location: `${state},${city}`};
});
allPromises.push(currPromise);
});
});
Promise.all(allPromises).then(() => {
console.log('-------------------');
console.log('Sitemap Completed!');
console.log('-------------------');
console.log(`${keys(this.failures).length} Failures ⬇`);
forEach(this.failures, (e) => {
console.log(e.location);
});
this.smStream.end();
this.smStream.pipe(createWriteStream(resolve('./public/sitemap.xml')));
});
}
populateCities() {
let allPromises = [];
forEach(this.stateMap, async (e, state) => {
const currPromise = this.getCities(state).then(({data}) => {
forEach(data, (city) => {
const cityName = city.name;
// const dashedCity = dashStr(cityName);
// const dashedState = dashStr(state);
set(this, `stateMap.${state}.cities.${cityName}`, {total: city.total});
this.smStream.write({url: `/locations/${state}/${cityName}`});
});
});
allPromises.push(currPromise);
});
Promise.all(allPromises).then(() => {
console.log('=== Populating Centers Per City . Please Hold... ===');
this.populateAllSitesInCity();
});
}
async build() {
try {
this.smStream = new SitemapStream({hostname: this.appUrl});
this.smStream.write({url: '/map'});
this.smStream.write({url: '/contact-tracing'});
this.smStream.write({url: '/create-account'});
this.smStream.write({url: '/state-list'});
this.stateMap = {};
console.log('Populating States');
await this.getStates()
.then((resp) => {
forEach(resp.data, (e2) => {
this.stateMap[e2.name] = {};
});
console.log('Populating Cities');
this.populateCities();
})
.catch((e, i) => {});
} catch (e) {
console.error(e);
}
}
}
// function dashStr(str) {
// return str.replace(/ /g, '-');
// }
new SiteBuilder();