1 | const prompts = require('prompts');
|
2 | const { l10n, print, Database, Subscription } = require('../..');
|
3 |
|
4 | exports.command = 'add [subscribable...]';
|
5 |
|
6 | exports.aliases = [];
|
7 |
|
8 | exports.desc = l10n('CMD_ADD_DESC');
|
9 |
|
10 | exports.builder = (yargs) => {
|
11 | yargs
|
12 | .usage(l10n('CMD_ADD_USAGE'))
|
13 | .options({
|
14 | 'i': {
|
15 | alias: 'interactive',
|
16 | describe: l10n('CMD_ADD_OPT_I'),
|
17 | type: 'boolean',
|
18 | },
|
19 | 'y': {
|
20 | alias: 'yes',
|
21 | describe: l10n('CMD_ADD_OPT_Y'),
|
22 | type: 'boolean',
|
23 | },
|
24 | 'n': {
|
25 | alias: 'no',
|
26 | describe: l10n('CMD_ADD_OPT_N'),
|
27 | type: 'boolean',
|
28 | },
|
29 | })
|
30 | .check((argv) => {
|
31 | if (argv.yes && argv.no) {
|
32 | throw new Error(l10n('CMD_ADD_OPT_YN_ERR'));
|
33 | }
|
34 | if (!argv.subscribable && !argv.interactive) {
|
35 | throw new Error(l10n('CMD_ADD_OPT_NO_I_NO_SUBS'));
|
36 | }
|
37 | return true;
|
38 | })
|
39 | .fail((msg, err) => {
|
40 | yargs.showHelp();
|
41 | console.log();
|
42 | print.error(msg);
|
43 | process.exit(1);
|
44 | })
|
45 | .example('dmhy add "搖曳露營,萌喵,繁體,~1080p~"', l10n('CMD_ADD_EXAMPLE1_DESC'))
|
46 | .example('dmhy add "./camp.yml"', l10n('CMD_ADD_EXAMPLE2_DESC'))
|
47 | .example('dmhy add -i', l10n('CMD_ADD_EXAMPLE3_DESC'));
|
48 | };
|
49 |
|
50 | exports.handler = async (argv) => {
|
51 | const db = new Database();
|
52 |
|
53 | const safeAdd = async (sub) => {
|
54 | const existed = db.find(sub);
|
55 | if (existed) {
|
56 | if (argv.yes) {
|
57 | db.add(sub);
|
58 | print.success(l10n('CMD_ADD_SUCCESS', { title: sub.title }));
|
59 | } else if (!argv.yes && !argv.no) {
|
60 |
|
61 | const answer = await prompts({
|
62 | type: 'toggle',
|
63 | name: 'add',
|
64 | message: l10n('CMD_ADD_PROMPTS_CONFIRM', { title: sub.title }),
|
65 | initial: false,
|
66 | active: 'Yes',
|
67 | inactive: 'No',
|
68 | });
|
69 | if (answer.add) {
|
70 | db.add(sub);
|
71 | print.success(l10n('CMD_ADD_SUCCESS', { title: sub.title }));
|
72 | }
|
73 | }
|
74 | } else {
|
75 | db.add(sub);
|
76 | print.success(l10n('CMD_ADD_SUCCESS', { title: sub.title }));
|
77 | }
|
78 | };
|
79 |
|
80 | if (argv.interactive) {
|
81 | print.info(l10n('CMD_ADD_INTERACTIVE_INFO'));
|
82 | const iSubscriptionLike = {};
|
83 | do {
|
84 |
|
85 | const answer = await prompts({
|
86 | type: 'text',
|
87 | name: 'title',
|
88 | message: l10n('CMD_ADD_INTERACTIVE_TITLE'),
|
89 |
|
90 | });
|
91 | if (!answer.title.trim()) {
|
92 | print.error(l10n('CMD_ADD_INTERACTIVE_TITLE_ERR'));
|
93 | } else {
|
94 | iSubscriptionLike.title = answer.title;
|
95 | break;
|
96 | }
|
97 | } while (true);
|
98 |
|
99 | const answer = await prompts([{
|
100 | type: 'list',
|
101 | name: 'keywords',
|
102 | message: l10n('CMD_ADD_INTERACTIVE_KEYWORDS'),
|
103 | separator: ',',
|
104 | }, {
|
105 | type: 'list',
|
106 | name: 'unkeywords',
|
107 | message: l10n('CMD_ADD_INTERACTIVE_UNKEYWORDS'),
|
108 | separator: ',',
|
109 | }, {
|
110 | type: 'text',
|
111 | name: 'episodeParser',
|
112 | message: l10n('CMD_ADD_INTERACTIVE_EPISODEPARSER'),
|
113 | initial: '',
|
114 | }]);
|
115 | answer.keywords = answer.keywords.filter(Boolean);
|
116 | answer.unkeywords = answer.unkeywords.filter(Boolean);
|
117 | Object.assign(iSubscriptionLike, answer);
|
118 | const sub = Subscription.from(iSubscriptionLike);
|
119 | await safeAdd(sub);
|
120 | } else {
|
121 | for (const subscribable of argv.subscribable) {
|
122 | const sub = new Subscription(subscribable);
|
123 | await safeAdd(sub);
|
124 | }
|
125 | }
|
126 |
|
127 | db.save();
|
128 | process.exit(0);
|
129 | };
|