-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·64 lines (53 loc) · 1.46 KB
/
index.mjs
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
#!/usr/bin/env node
import assert, { AssertionError } from 'node:assert';
import parseDate from '@nrk/simple-date-parse';
import { EX_OK, EX_SYNC_FAILED, EX_USAGE } from './consts/exitCodes.mjs';
import { mubiFetchViewLog } from './lib/mubi.mjs';
import { traktSyncViewLog } from './lib/trakt.mjs';
import { configFileInit } from './lib/configFile.mjs';
import args, { argsInit, printHelp } from './lib/args.mjs';
import config from './lib/config.mjs';
import { printErr } from './lib/log.mjs';
/**
* @typedef {import('./types').ViewLog} ViewLog
*/
await Promise.all([
configFileInit(),
argsInit(),
]);
if (args.help)
{
printHelp();
process.exit(EX_OK);
}
try
{
const now = new Date();
const since = parseDate(config.since, now);
assert.ok(!Number.isNaN(since), `Invalid since date: ${config.since}`);
assert.ok(since.toISOString() !== now.toISOString(), `Failed to parse since date: ${config.since}`);
/** @type {ViewLog} */
const viewLog = await mubiFetchViewLog({ since });
if (viewLog.length)
{
const success = await traktSyncViewLog(viewLog);
if (!success)
{
printErr('Failed to save view log on trakt.tv');
process.exit(EX_SYNC_FAILED);
}
}
}
catch (error)
{
if (error instanceof AssertionError && !error.generatedMessage)
{
printErr(error.message);
process.exit(EX_USAGE);
}
else
{
throw error;
}
}
process.exit(EX_OK);