-
Notifications
You must be signed in to change notification settings - Fork 200
/
main.ts
69 lines (62 loc) · 2.4 KB
/
main.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
import * as yargs from "yargs";
import { pathExists, writeFile, remove } from "fs-extra";
import { getSecret, Secret } from "./lib/secrets";
import { Fetcher, loggerWithErrors, tryReadJson } from "@definitelytyped/utils";
import { currentTimeStamp } from "./util/util";
import full from "./full";
import { getFunctionTimeoutSeconds, lockFilePath } from "./lib/settings";
export default function main() {
return withFileLock(lockFilePath, async () => {
const githubAccessToken = await getSecret(Secret.GITHUB_ACCESS_TOKEN);
const dry = !!(yargs.argv.dry || process.env.WEBHOOK_FORCE_DRY);
if (!githubAccessToken) {
console.log("The environment variable GITHUB_ACCESS_TOKEN must be set.");
} else {
console.log(`=== ${dry ? "DRY" : "PRODUCTION"} RUN ===`);
const fetcher = new Fetcher();
const log = loggerWithErrors()[0];
log.info("");
log.info("");
log.info(`# ${currentTimeStamp()}`);
log.info("");
log.info("Starting full...");
await full(
dry,
githubAccessToken,
fetcher,
{
definitelyTypedPath: undefined,
parseInParallel: false,
progress: false,
},
log
);
}
});
}
type LockFileResult = { triggered: true } | { triggered: false; timestamp: string };
export async function withFileLock(lockFilePath: string, cb: () => Promise<void>): Promise<LockFileResult> {
console.log("Checking for lock file...");
if (await pathExists(lockFilePath)) {
const lastRunStartTimestamp = (await tryReadJson(lockFilePath, isLockfileFormat))?.timestamp || currentTimeStamp();
const elapsedSeconds = (Date.now() - Date.parse(lastRunStartTimestamp)) / 1000;
if (elapsedSeconds < getFunctionTimeoutSeconds()) {
console.log("Lock file exists; new run not triggered.");
return { triggered: false, timestamp: lastRunStartTimestamp };
}
}
console.log("Lock file does not exist; writing lock file and running.");
await writeFile(lockFilePath, JSON.stringify({ timestamp: currentTimeStamp() }));
cb().then(
() => remove(lockFilePath),
async (error) => {
console.error(error?.stack || error?.message || error);
await remove(lockFilePath);
process.exit(1);
}
);
return { triggered: true };
}
function isLockfileFormat(parsed: any): parsed is { timestamp: string } {
return parsed && typeof parsed === "object" && "timestamp" in parsed;
}