-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
138 lines (122 loc) · 3.92 KB
/
index.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
#! /usr/bin/env node
import Airtable from "airtable";
import {
getAirtableEvents,
getAirtableSpeakers,
getAirtableSponsors,
} from "./src/repos/airtable.js";
import {
getWebsiteEvents,
getWebsiteSpeakers,
getWebsiteSponsors,
getWebsiteTalks,
} from "./src/repos/website.js";
import {
confirmUpdate,
getTargetEvent,
promptForApiToken,
promptForSeattlejsProjectPath,
promptPhotoResize,
} from "./src/repos/user-input.js";
import {
mapAirtableEventsToWebsiteEvents,
makeWebsiteEvent,
reconcileEvents,
} from "./src/events.js";
import { reconcileSpeakers } from "./src/speakers.js";
import { reconcileSponsors } from "./src/sponsors.js";
import { reconcileTalks } from "./src/talks.js";
import { exportImages, exportData } from "./src/repos/website.js";
import {
AirtableMetadata,
getAirtableMetadata,
} from "./src/repos/airtable-metadata.js";
import { getApiToken, saveApiToken } from "./src/auth.js";
import { loadConfig, saveConfig } from "./src/config.js";
console.log("welcome to the seattlejs-airtable-cli!");
let token = await getApiToken();
if (!token) {
token = await promptForApiToken();
await saveApiToken(token);
}
const config = await loadConfig();
if (!config.seattlejsProjectPath) {
const projectPath = await promptForSeattlejsProjectPath();
config.seattlejsProjectPath = projectPath;
await saveConfig(config);
}
const airtableMetadata: AirtableMetadata = await getAirtableMetadata(token);
Airtable.configure({
apiKey: token,
endpointUrl: "https://api.airtable.com",
});
console.log("querying airtable...");
const airtableBase = Airtable.base(airtableMetadata.baseId);
// load the airtable data we'll need
const airtableEvents = await getAirtableEvents(
airtableBase,
airtableMetadata.eventsId,
);
const airtableSpeakers = await getAirtableSpeakers(
airtableBase,
airtableMetadata.speakersId,
);
const airtableSponsors = await getAirtableSponsors(
airtableBase,
airtableMetadata.sponsorsId,
);
console.log("gathering existing website data...");
const websiteEvents = await getWebsiteEvents(config.seattlejsProjectPath);
const websiteSpeakers = await getWebsiteSpeakers(config.seattlejsProjectPath);
const websiteTalks = await getWebsiteTalks(config.seattlejsProjectPath);
const websiteSponsors = await getWebsiteSponsors(config.seattlejsProjectPath);
// associate all the airtable events with the website events
// this is kind of bad because it will miss events that are in
// the website but not in airtable. It's not all that bad because
// the eventmap isn't used for writing the actual json output, it's
// only used to prompt the user for which event they want to modify
const eventMap = mapAirtableEventsToWebsiteEvents(
airtableEvents,
websiteEvents,
);
const targetEvent = await getTargetEvent(eventMap);
if (!targetEvent.website) {
targetEvent.website = makeWebsiteEvent(targetEvent.airtable);
}
const { newPhotos, updatedSpeakers } = reconcileSpeakers(
targetEvent,
airtableSpeakers,
websiteSpeakers,
);
const { updatedTalks, removedTalks } = reconcileTalks(
targetEvent,
airtableSpeakers,
websiteTalks,
);
const { newLogos, updatedSponsors } = reconcileSponsors(
targetEvent,
airtableSponsors,
websiteSponsors,
);
reconcileEvents(targetEvent, websiteEvents);
const confirmation = await confirmUpdate(
updatedSpeakers,
updatedTalks,
removedTalks,
updatedSponsors,
);
if (confirmation) {
await exportData(websiteSpeakers, "speakers", config.seattlejsProjectPath);
const existingPhotos = await exportImages(
newPhotos,
"speakers",
config.seattlejsProjectPath,
);
await exportData(websiteTalks, "talks", config.seattlejsProjectPath);
await exportData(websiteSponsors, "sponsors", config.seattlejsProjectPath);
await exportImages(newLogos, "sponsors", config.seattlejsProjectPath);
await exportData(websiteEvents, "events", config.seattlejsProjectPath);
promptPhotoResize(existingPhotos);
} else {
console.log("aborting update, have a nice day :)");
}