-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalogueItems.js
164 lines (154 loc) · 3.88 KB
/
catalogueItems.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
const { Select } = require("enquirer");
const {
OUTPUT_FOLDER,
OUTPUT_FILE_NAME,
CATA_ID_FILE_NAME,
CATA_NAME_FILE_NAME,
CATA_RARITY_FILE_NAME,
CURRENCY_OUTPUT_FILE,
CATA_CURRENCY_ID_FILE_NAME,
CATA_CURRENCY_NAME_FILE_NAME,
} = require("./config.json");
const { Log, readFile, createJSONFile } = require("./utils");
const catalogueByID = async (itemData) => {
Log.pending("Cataloging items by ID");
const { items } = itemData;
const ids = {};
items.forEach((item) => {
const { id, ...rest } = item;
ids[id] = { ...rest };
});
try {
await createJSONFile(`./${OUTPUT_FOLDER}/${CATA_ID_FILE_NAME}.json`, ids);
Log.success("ID catalogue success");
} catch (err) {
Log.failure("ID catalogue failed");
console.error(err);
}
};
const catalogueByName = async (itemData) => {
Log.pending("Cataloging items by name");
const { items } = itemData;
const names = {};
items.forEach((item) => {
const { name, ...rest } = item;
names[name] = { ...rest };
});
try {
await createJSONFile(
`./${OUTPUT_FOLDER}/${CATA_NAME_FILE_NAME}.json`,
names
);
Log.success("Name catalogue success");
} catch (err) {
Log.failure("Name catalogue failed");
console.error(err);
}
};
const catalogueByRarity = async (itemData) => {
Log.pending("Cataloging items by rarity");
const { items } = itemData;
const rarities = {};
items.forEach((item) => {
const { rarity, ...rest } = item;
if (rarities[rarity]) {
rarities[rarity].push({ ...rest });
} else {
rarities[rarity] = [{ ...rest }];
}
});
try {
await createJSONFile(
`./${OUTPUT_FOLDER}/${CATA_RARITY_FILE_NAME}.json`,
rarities
);
Log.success("Rarity catalogue success");
} catch (err) {
Log.failure("Rarity catalogue failed");
console.error(err);
}
};
const catalogueCurrById = async (currData) => {
Log.pending("Cataloging currencies by ID");
const ids = {};
currData.forEach((currency) => {
const { id, ...rest } = currency;
ids[id] = { ...rest };
});
try {
await createJSONFile(
`./${OUTPUT_FOLDER}/${CATA_CURRENCY_ID_FILE_NAME}.json`,
ids
);
Log.success("Currency ID catalogue success");
} catch (err) {
Log.failure("Currency ID catalogue failed");
console.error(err);
}
};
const catalogueCurrByName = async (currData) => {
Log.pending("Cataloging currencies by name");
const names = {};
currData.forEach((currency) => {
const { name, ...rest } = currency;
names[name] = { ...rest };
});
try {
await createJSONFile(
`./${OUTPUT_FOLDER}/${CATA_CURRENCY_NAME_FILE_NAME}.json`,
names
);
Log.success("Currency name catalogue success");
} catch (err) {
Log.failure("Currency name catalogue failed");
console.error(err);
}
};
const choices = {
ids: {
m: "By IDs (object)",
file: OUTPUT_FILE_NAME,
f: catalogueByID,
},
name: {
m: "By name (object)",
file: OUTPUT_FILE_NAME,
f: catalogueByName,
},
rarity: {
m: "By rarity (arrays)",
file: OUTPUT_FILE_NAME,
f: catalogueByRarity,
},
"curr-ids": {
m: "Currencies by IDs (object)",
file: CURRENCY_OUTPUT_FILE,
f: catalogueCurrById,
},
"curr-name": {
m: "Currencies by name (object)",
file: CURRENCY_OUTPUT_FILE,
f: catalogueCurrByName,
},
};
const prompt = new Select({
name: "cataction",
message: "How would you like to catalogue the data?",
choices: Object.entries(choices).map(([id, value]) => ({
message: value.m,
name: id,
})),
});
const catalogue = async () => {
try {
const answer = await prompt.run();
const { file, f } = choices[answer];
readFile(`./${OUTPUT_FOLDER}/${file}.json`, async (itemData) => {
f(itemData);
});
} catch (err) {
Log.failure(`cannot find "./${OUTPUT_FOLDER}/${OUTPUT_FILE_NAME}.json"`);
console.error(err);
}
};
module.exports = catalogue;