-
Notifications
You must be signed in to change notification settings - Fork 9
/
fromDataBlue.js
158 lines (150 loc) · 4.98 KB
/
fromDataBlue.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
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
const fs = require('fs');
const axios = require('axios');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
var getRepoInfo = require('git-repo-info');
// as per https://github.com/water-fountains/proximap/pull/338#issuecomment-609027058
// run with
// ~/git/proximap $ npm run sync_datablue
//
// to run it just for one of the three, do
// ~/git/proximap $ npm run sync_datablue for=locations
// API URL.
const apiUrlBeta = 'https://api.beta.water-fountains.org/';
const apiUrlStable = 'https://api.water-fountains.org/';
const apiUrlLocal = 'http://localhost:3000/'; // use in development.
const callAPI = function (branch, filename, url) {
const apiUrl = branch === 'stable' ? apiUrlStable : apiUrlBeta;
const metadataUrl = `${apiUrl}api/v1/metadata/${url}`;
// Get fountain_properties data from server and create file.
axios
.get(metadataUrl)
.then(function (response) {
// handle success
const data = JSON.stringify(response.data, null, 4);
if (!fs.existsSync(filename)) {
fs.writeFileSync(filename, '', { encoding: 'utf8' });
}
fs.writeFileSync(filename, data, { encoding: 'utf8' });
console.log(`${metadataUrl} sync ${branch} DONE to ` + filename + ' ' + new Date().toISOString());
})
.catch(function (error) {
// handle error
console.log(error + ' ' + new Date().toISOString());
});
};
const workOnRelevantArgs = function (args) {
for (let arg of args) {
switch (arg.trim().toLowerCase()) {
case 'fountains':
createSharedFile(`./src/assets/fountain_properties.json`, 'fountain_properties')
.then(resp => {})
.catch(error => {
console.error(error);
});
break;
case 'locations':
createSharedFile(`./src/assets/locations.json`, 'locations')
.then(resp => {})
.catch(error => {
console.error(error);
});
break;
case 'constants':
createSharedFile(`./src/assets/shared-constants.json`, 'shared-constants')
.then(resp => {})
.catch(error => {
console.error(error);
});
break;
default:
console.log(
'for= "' + arg + '" unknown - so doing all 3 "fountains,locations,constants" ' + new Date().toISOString()
);
createSharedFile(`./src/assets/fountain_properties.json`, 'fountain_properties')
.then(resp => {})
.catch(error => {
console.error(error);
});
createSharedFile(`./src/assets/locations.json`, 'locations')
.then(resp => {})
.catch(error => {
console.error(error);
});
createSharedFile(`./src/assets/shared-constants.json`, 'shared-constants')
.then(resp => {})
.catch(error => {
console.error(error);
});
}
}
};
if (process.argv.length > 2) {
let i = -1;
process.argv.forEach(function (val, index, array) {
i++;
if (val.indexOf('for') !== -1) {
const argsWoEqual = val.split('=');
if (2 != argsWoEqual.length) {
console.log(
'val "' +
val +
'" should be followed by a "=" and then "fountains|locations|constants" (if multiple separated by comma) ' +
new Date().toISOString()
);
} else {
const args = argsWoEqual[1].split(',');
workOnRelevantArgs(args);
}
} else {
if (1 < i) {
const valTrLc = val.trim().toLowerCase();
if (2 == i && 8 < valTrLc.length) {
const args = [valTrLc];
workOnRelevantArgs(args);
if (process.argv.length > 3) {
console.log('ignoring further arguments " ' + new Date().toISOString());
}
return;
} else {
console.log(
i +
': unknown argument "' +
val +
'" - either none or "fountains|locations|constants" (if multiple separated by comma)" ' +
new Date().toISOString()
);
}
}
}
});
} else {
createSharedFile(`./src/assets/fountain_properties.json`, 'fountain_properties')
.then(resp => {})
.catch(error => {
console.error(error);
});
createSharedFile(`./src/assets/locations.json`, 'locations')
.then(resp => {})
.catch(error => {
console.error(error);
});
createSharedFile(`./src/assets/shared-constants.json`, 'shared-constants')
.then(resp => {})
.catch(error => {
console.error(error);
});
}
async function createSharedFile(filename, url) {
//console.log(`starting "${url}" to "`+filename+'" '+new Date().toISOString());
const branch = await exec('git rev-parse --abbrev-ref HEAD', (err, stdout, stderr) => {
if (err) {
var info = getRepoInfo();
callAPI(info.branch, filename, url);
} else if (stdout) {
const branch = stdout.toString().trim();
callAPI(branch, filename, url);
}
});
callAPI(branch, filename, url);
}