1 | const yaml = require('js-yaml');
|
2 | const Table = require('easy-table');
|
3 | const { l10n, Database, TheEpisode, print } = require('../..');
|
4 |
|
5 | exports.command = 'list [SID...]';
|
6 |
|
7 | exports.aliases = ['ls'];
|
8 |
|
9 | exports.desc = l10n('CMD_LS_DESC');
|
10 |
|
11 | exports.builder = (yargs) => {
|
12 | yargs
|
13 | .usage(l10n('CMD_LS_USAGE'))
|
14 | .options({
|
15 | 'ss': {
|
16 | alias: 'subscribable-string',
|
17 | describe: l10n('CMD_LS_OPT_SS'),
|
18 | type: 'boolean',
|
19 | },
|
20 | 'sy': {
|
21 | alias: 'subscribable-yaml',
|
22 | describe: l10n('CMD_LS_OPT_SY'),
|
23 | type: 'boolean',
|
24 | },
|
25 | })
|
26 | .check((argv) => {
|
27 | if (argv.ss && argv.sy) {
|
28 | throw new Error(l10n('CMD_LS_OPT_SYSS_ERR'));
|
29 | }
|
30 | return true;
|
31 | })
|
32 | .fail((msg, err) => {
|
33 | yargs.showHelp();
|
34 | console.log();
|
35 | print.error(msg);
|
36 | process.exit(1);
|
37 | })
|
38 | .example('dmhy ls AAA', l10n('CMD_LS_EXAMPLE1_DESC'))
|
39 | .example('dmhy ls -ss', l10n('CMD_LS_EXAMPLE2_DESC'));
|
40 | };
|
41 |
|
42 | exports.handler = (argv) => {
|
43 | const db = new Database();
|
44 | db.sort();
|
45 |
|
46 | if (argv.ss) {
|
47 | const sids = argv.SID || db.subscriptions.map((sub) => sub.sid);
|
48 | sids.forEach((sid) => {
|
49 | const sub = db.find({ sid });
|
50 | console.log([sub.title, ...sub.keywords, ...sub.unkeywords.map((k) => `~${k}~`)].join(','));
|
51 | });
|
52 | process.exit(0);
|
53 | }
|
54 |
|
55 | if (argv.sy) {
|
56 | const sids = argv.SID || db.subscriptions.map((sub) => sub.sid);
|
57 | sids.forEach((sid) => {
|
58 | const sub = db.find({ sid });
|
59 | const { title, keywords, unkeywords, episodeParser } = sub;
|
60 | console.log(yaml.safeDump({ title,
|
61 | keywords,
|
62 | unkeywords,
|
63 | episodeParser: `${episodeParser}` }));
|
64 | });
|
65 | process.exit(0);
|
66 | }
|
67 |
|
68 |
|
69 | if (argv.SID) {
|
70 |
|
71 | argv.SID.forEach((sid) => {
|
72 | const sub = db.find({ sid });
|
73 | if (!sub) {
|
74 | print.error(l10n('CMD_LS_SID_NOT_FOUND', { sid }));
|
75 | process.exit(1);
|
76 | }
|
77 | const { title, keywords, unkeywords, episodeParser } = sub;
|
78 | const t = {};
|
79 | t[l10n('CMD_LS_CELL_SID')] = sid;
|
80 | t[l10n('CMD_LS_CELL_TITLE')] = title;
|
81 | t[l10n('CMD_LS_CELL_KEYWORDS')] = keywords.join(', ');
|
82 | t[l10n('CMD_LS_CELL_UNKEYWORDS')] = unkeywords.join(', ');
|
83 | t[l10n('CMD_LS_CELL_EPISODEPARSER')] = `${episodeParser}`;
|
84 | console.log(Table.print(t).trim());
|
85 | console.log();
|
86 |
|
87 | const tht = new Table();
|
88 | sub.threads.forEach((th) => {
|
89 | tht.cell(l10n('CMD_LS_CELL_THREAD_EPISODE'), th.episode.toString(TheEpisode.ascendCompare));
|
90 | tht.cell(l10n('CMD_LS_CELL_THREAD_TITLE'), th.title);
|
91 | tht.newRow();
|
92 | });
|
93 | console.log(tht.toString().trim());
|
94 | console.log();
|
95 | });
|
96 | } else {
|
97 |
|
98 | const t = new Table();
|
99 | db.subscriptions.forEach((sub) => {
|
100 | t.cell(l10n('CMD_LS_CELL_SID'), sub.sid);
|
101 | t.cell(l10n('CMD_LS_CELL_LATEST'), sub.latest < 0 ? '--' : sub.latest.toString().padStart(2, '0'));
|
102 | t.cell(l10n('CMD_LS_CELL_TITLE'), sub.title);
|
103 | t.newRow();
|
104 | });
|
105 | console.log(t.toString().trim());
|
106 | }
|
107 | process.exit(0);
|
108 | };
|