forked from paularmstrong/normalizr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (193 loc) · 5.49 KB
/
index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as Action from './src/redux/actions';
import * as Selector from './src/redux/selectors';
import inquirer from 'inquirer';
import store from './src/redux';
const REPO = 'paularmstrong/normalizr';
const start = () => {
inquirer
.prompt([
{
type: 'input',
name: 'repo',
message: 'What is the slug of the repo you wish to browseMain?',
default: REPO,
validate: (input) => {
if (!/^[a-zA-Z0-9]+\/[a-zA-Z0-9]+/.test(input)) {
return 'Repo slug must be in the form "user/project"';
}
return true;
}
}
])
.then(({ repo }) => {
store.dispatch(Action.setRepo(repo));
main();
});
};
const main = () => {
return inquirer
.prompt([
{
type: 'list',
name: 'action',
message: 'What would you like to do?',
choices: ['Browse current state', 'Get new data', new inquirer.Separator(), 'Quit']
}
])
.then(({ action }) => {
switch (action) {
case 'Browse current state':
return browseMain();
case 'Get new data':
return pull();
default:
return process.exit();
}
});
};
const browseMain = () => {
return inquirer
.prompt([
{
type: 'list',
name: 'browseMainAction',
message: 'What would you like to do?',
choices: () => {
return [
{ value: 'print', name: 'Print the entire state tree' },
new inquirer.Separator(),
...Object.keys(store.getState()).map((value) => ({ value, name: `Browse ${value}` })),
new inquirer.Separator(),
{ value: 'main', name: 'Go Back to Main Menu' }
];
}
}
])
.then((answers) => {
switch (answers.browseMainAction) {
case 'main':
return main();
case 'print':
console.log(JSON.stringify(store.getState(), null, 2));
return browseMain();
default:
return browse(answers.browseMainAction);
}
});
};
const browse = (stateKey) => {
return inquirer
.prompt([
{
type: 'list',
name: 'action',
message: `Browse ${stateKey}`,
choices: [
{ value: 'count', name: 'Show # of Objects' },
{ value: 'keys', name: 'List All Keys' },
{ value: 'view', name: 'View by Key' },
{ value: 'all', name: 'View All' },
{ value: 'denormalize', name: 'Denormalize' },
new inquirer.Separator(),
{ value: 'browseMain', name: 'Go Back to Browse Menu' },
{ value: 'main', name: 'Go Back to Main Menu' }
]
},
{
type: 'list',
name: 'list',
message: `Select the ${stateKey} to view:`,
choices: Object.keys(store.getState()[stateKey]),
when: ({ action }) => action === 'view'
}
])
.then(({ action, list }) => {
const state = store.getState()[stateKey];
if (list) {
console.log(JSON.stringify(state[list], null, 2));
}
switch (action) {
case 'count':
console.log(`-> ${Object.keys(state).length} items.`);
return browse(stateKey);
case 'keys':
Object.keys(state).map((key) => console.log(key));
return browse(stateKey);
case 'all':
console.log(JSON.stringify(state, null, 2));
return browse(stateKey);
case 'denormalize':
return browseDenormalized(stateKey);
case 'browseMain':
return browseMain();
case 'main':
return main();
default:
return browse(stateKey);
}
});
};
const browseDenormalized = (stateKey) => {
return inquirer
.prompt([
{
type: 'list',
name: 'selector',
message: `Denormalize a/and ${stateKey} entity`,
choices: [
...Object.keys(store.getState()[stateKey]),
new inquirer.Separator(),
{ value: 'browse', name: 'Go Back to Browse Menu' },
{ value: 'main', name: 'Go Back to Main Menu' }
]
}
])
.then(({ selector }) => {
switch (selector) {
case 'browse':
return browse(stateKey);
case 'main':
return main();
default: {
const data = Selector[`select${stateKey.replace(/s$/, '')}`](store.getState(), selector);
console.log(JSON.stringify(data, null, 2));
return browseDenormalized(stateKey);
}
}
});
};
const pull = () => {
return inquirer
.prompt([
{
type: 'list',
name: 'pullAction',
message: 'What data would you like to fetch?',
choices: () => {
return [
...Object.keys(store.getState()).map((value) => ({ value, name: value })),
new inquirer.Separator(),
{ value: 'main', name: 'Go Back to Main Menu' }
];
}
}
])
.then((answers) => {
switch (answers.pullAction) {
case 'commits':
return store.dispatch(Action.getCommits()).then(pull);
case 'issues':
return store.dispatch(Action.getIssues()).then(pull);
case 'labels':
return store.dispatch(Action.getLabels()).then(pull);
case 'milestones':
return store.dispatch(Action.getMilestones()).then(pull);
case 'pullRequests':
return store.dispatch(Action.getPullRequests()).then(pull);
case 'main':
default:
return main();
}
});
};
start();