-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
58 lines (54 loc) · 1.63 KB
/
index.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
var { getMetadata, getSheet } = require("./sheets");
var { upload } = require("./s3");
var fs = require("fs").promises;
// actually do the work
var punch = async function (workbook, slug) {
console.log(`Pulling sheet: ${workbook}`);
// check metadata -- only republish NPR sheets
try {
var meta = await getMetadata(workbook);
} catch (err) {
return {
error:
"Unable to access this sheet - is it shared with the service account?"
};
}
if (!meta.owners.some((owner) => owner.emailAddress.match(/@npr.org$/))) {
return { error: "This sheet is not owned by an NPR user." };
}
// get sheet from request input
var data = await getSheet(workbook);
// upload to S3
var json = JSON.stringify(data, null, 2);
await upload("apps.npr.org", `dailygraphics/data/sheets/${slug}.json`, json);
return {
status: "success",
href: `https://apps.npr.org/dailygraphics/data/sheets/${slug}.json`
};
};
// run on Lambda
exports.handler = async function (event, context, callback) {
var { sheet, slug } = event.queryStringParameters;
var result = await punch(sheet, slug);
return {
statusCode: "error" in result ? 500 : 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
body: JSON.stringify(result)
};
};
// run locally
var local = async function () {
var minimist = require("minimist");
var { sheet, slug } = minimist(process.argv);
if (!sheet || !slug) {
return console.log("Please provide a sheet and a slug.");
}
var result = await punch(sheet, slug);
console.log(result);
};
if (!process.env.LAMBDA_TASK_ROOT) {
local();
}