forked from claabs/epicgames-freegames-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.ts
111 lines (105 loc) · 3.09 KB
/
commands.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { usage } from 'yargs';
import AccountManager from './test/util/account';
import { newCookieJar } from './src/common/request';
import FreeGames from './src/free-games';
import Purchase from './src/purchase';
interface ReleaseArgs {
[x: string]: unknown;
u: string;
p: string;
_: string[];
$0: string;
}
interface RedeemArgs {
[x: string]: unknown;
u?: string;
p?: string;
t?: string;
_: string[];
$0: string;
}
const createAccount = async (): Promise<void> => {
const account = new AccountManager();
await account.init();
};
const releaseAccount = async (args: ReleaseArgs): Promise<void> => {
const account = new AccountManager(args.u, args.p);
await account.login();
await account.changeEmail();
};
const redeemGames = async (args: RedeemArgs): Promise<void> => {
const user = process.env.TEST_USER || args.u;
const pass = process.env.TEST_PASSWORD || args.p;
const totp = process.env.TEST_TOTP || args.t;
if (!user || !pass || !totp) throw new Error('Missing username, password, or TOTP');
const account = new AccountManager(user, pass, totp);
await account.login();
const requestClient = newCookieJar(user);
const freeGames = new FreeGames(requestClient, account.permMailAddress);
const purchase = new Purchase(requestClient, account.permMailAddress);
const offers = await freeGames.getAllFreeGames(); // Get purchasable offers
await purchase.purchaseGames(offers); // Purchase games;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { argv } = usage('$0 <command> [option]')
.command(
['create-account', 'create'],
'Create a fresh account',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(yargs: any) => {
return yargs
.usage('$0 create-account')
.usage('$0 create')
.help();
},
createAccount
)
.command(
['release-account', 'remove', 'release'],
'Remove an account by its login',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(yargs: any) => {
return yargs
.usage('$0 release --username <username> --pasword <password>')
.option('u', {
alias: ['user', 'username'],
type: 'string',
demandOption: true,
})
.option('p', {
alias: ['pass', 'password'],
type: 'string',
demandOption: true,
})
.help();
},
releaseAccount
)
.command(
['redeem', 'free-games'],
'Redeem all free games for a user',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(yargs: any) => {
return yargs
.usage('$0 release --username <username> --pasword <password>')
.option('u', {
alias: ['user', 'username'],
type: 'string',
demandOption: false,
})
.option('p', {
alias: ['pass', 'password'],
type: 'string',
demandOption: false,
})
.option('t', {
alias: ['totp', 'mfa'],
type: 'string',
demandOption: false,
})
.help();
},
redeemGames
)
.help()
.demandCommand();