-
Notifications
You must be signed in to change notification settings - Fork 35
/
action.js
executable file
·47 lines (41 loc) · 1.03 KB
/
action.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const randomUser = require('random-user');
// for you to change easily
const dataFolder = '/data';
const now = new Date();
const pathToData = path.join(__dirname, dataFolder, fileString(now)) + '.json';
// read data, if needed
let data = [];
if (fs.existsSync(pathToData)) {
data = JSON.parse(fs.readFileSync(pathToData));
}
// scrape data, possibly using prior data
async function getData() {
const user = await randomUser('simple');
user.invokedAt = now;
data.push(user);
}
// execute and persist data
getData() // no top level await... yet
.then(() => {
// persist data
fs.writeFileSync(path.resolve(pathToData), JSON.stringify(data, null, 2));
});
/**
*
* utils
*
*/
function fileString(ts) {
const year = ts.getUTCFullYear();
const month = (ts.getUTCMonth() + 1).toString().padStart(2, '0');
const day = ts
.getUTCDate()
.toString()
.toString()
.padStart(2, '0');
const name = `${year}-${month}-${day}`;
return name;
}