-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.mjs
139 lines (129 loc) · 3.27 KB
/
variables.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as dotenv from 'dotenv';
import { cards } from 'fab-cards';
import inquirer from 'inquirer';
export async function setupVariables(options, heroFromDecklist) {
process.env = {};
dotenv.config();
const { NAME, PRONOUN, GEM_ID, EVENT, LOCALE } = process.env;
if (NAME) {
console.log(`Oh hi ${NAME}`);
}
const now = new Date();
let variables = {
name: NAME ? NAME : 'Nobody',
pronoun: PRONOUN ? PRONOUN : 'it',
gem_id: GEM_ID ? GEM_ID : 'Unknown',
event: EVENT ? EVENT : 'Kitchen table',
date: now.toLocaleDateString(LOCALE ? LOCALE : 'fi-FI'), // "localize it!" -- Peter Tosh, probably
hero: heroFromDecklist ? heroFromDecklist : '',
};
if (options['skip-interaction']) {
return variables;
}
await inquirer
.prompt([
{
name: 'name',
message: 'What is your full name?',
default: variables.name,
},
{
name: 'pronoun',
message: 'What pronouns you use?',
default: variables.pronoun,
},
{
name: 'gem_id',
message: 'What is your GEM ID?',
default: variables.gem_id,
},
{
name: 'event',
message: 'Name of the event the deck is for?',
default: variables.event,
},
{
name: 'date',
message: 'What is the event date?',
default: variables.date,
},
])
.then((answers) => {
variables = { hero: variables.hero, ...answers };
})
.catch((error) => {
if (error.isTtyError) {
throw new Error(`Current environment does not accept interactive input`);
} else {
throw error;
}
});
if (!variables.hero) {
variables.hero = await heroPicker(variables, cards);
}
return variables;
}
async function heroPicker(variables, cards) {
let heronames;
await inquirer
.prompt([
{
// TODO: I'm sure this selection UX could be improved somehow
type: 'expand',
name: 'format',
message: 'Is the format using adult (Classic Constructed) or young heroes (Blitz)?',
choices: [
{
key: 'a',
value: 'adult',
},
{
key: 'y',
value: 'young',
},
{
key: 'b',
value: 'both',
},
],
},
])
.then((answers) => {
heronames = cards
.filter((c) => c.hero)
.filter((c) => {
return answers.format == 'both' || (answers.format == 'adult' && !c.young) || (answers.format == 'young' && c.young);
})
.map((h) => {
return h.name;
});
})
.catch((error) => {
if (error.isTtyError) {
throw new Error(`Current environment does not accept interactive input`);
} else {
throw error;
}
});
await inquirer
.prompt([
{
type: 'list',
name: 'hero',
message: 'Which hero are you using?',
default: variables.hero,
choices: heronames,
},
])
.then((answers) => {
variables.hero = answers.hero;
})
.catch((error) => {
if (error.isTtyError) {
throw new Error(`Current environment does not accept interactive input`);
} else {
throw error;
}
});
return variables.hero;
}