-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathnewspapers.ts
73 lines (62 loc) · 2.27 KB
/
newspapers.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
import Table from '../../util/table.js';
import type { Arguments as ParentArguments } from './index.js';
import createDebug from '../../util/debug.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:newspapers');
export const command = 'newspapers';
export const desc = 'List all newspaper issues';
export function builder(yargs: Argv<ParentArguments>) {
return yargs.option('user', {
describe: 'Nintendo Account ID',
type: 'string',
}).option('token', {
describe: 'Nintendo Account session token',
type: 'string',
}).option('islander', {
describe: 'NookLink user ID',
type: 'string',
}).option('json', {
describe: 'Output raw JSON',
type: 'boolean',
}).option('json-pretty-print', {
describe: 'Output pretty-printed JSON',
type: 'boolean',
});
}
type Arguments = YargsArguments<ReturnType<typeof builder>>;
export async function handler(argv: ArgumentsCamelCase<Arguments>) {
const storage = await initStorage(argv.dataPath);
const usernsid = argv.user ?? await storage.getItem('SelectedUser');
const token: string = argv.token ||
await storage.getItem('NintendoAccountToken.' + usernsid);
const {nooklinkuser, data} = await getUserToken(storage, token, argv.islander, argv.zncProxyUrl, argv.autoUpdateSession);
const latest = await nooklinkuser.getLatestNewspaper();
const newspapers = await nooklinkuser.getNewspapers();
if (argv.jsonPrettyPrint) {
console.log(JSON.stringify(newspapers, null, 4));
return;
}
if (argv.json) {
console.log(JSON.stringify(newspapers));
return;
}
const table = new Table({
head: [
'ID',
'Type',
'Start date',
'End date',
],
});
for (const newspaper of newspapers.newspapers) {
table.push([
newspaper.findKey + (newspaper.findKey === latest.findKey ? ' *' : ''),
newspaper.type,
newspaper.beginDate,
newspaper.endDate,
]);
}
console.log(table.toString());
}