-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
198 lines (180 loc) · 5.93 KB
/
main.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
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
import { writeFileSync } from "fs";
import { prompt } from "inquirer";
import { nanoid } from "nanoid";
import {
HepsiburadaMainPrompts,
HepsiburadaPromptsWrapper,
} from "./companies/hepsiburada/prompts";
import {
TrendyolMainPrompts,
TrendyolPromptsWrapper,
} from "./companies/trendyol/prompts";
import { logger } from "./lib/logger";
import { productMainPrompt } from "./lib/prompts";
import {
ArrayOfLiterals,
Companies,
Product,
ProductCategories,
} from "./lib/types";
import {
configPrompt,
lengthValidator,
returnDataFile,
writeToExcel,
} from "./lib/utils";
import { companies, productCategories } from "./lib/variables";
import { notionCreateProduct } from "./scripts/notion";
export async function mainPrompt() {
const { companies: selectedCompanies, productCategory } = await prompt<{
companies: ArrayOfLiterals<Companies>;
productCategory: keyof ProductCategories[Companies[number]];
}>([
{
name: "companies",
type: "search-checkbox",
choices: companies,
message: "Şirket seçiniz",
validate: (input: string[]) => lengthValidator(input),
suffix: ":",
},
{
type: "search-list",
name: "productCategory",
message: "Ürün kategorisi seçiniz",
choices: (answers) => {
// 1 Company
if (answers.companies.length <= 1) {
const company = answers.companies[0];
if (company) return Object.values(productCategories[company]);
}
// 2 or more companies
if (answers.companies.length > 1) {
const categories = answers.companies
.map((company) => Object.values(productCategories[company]))
.flat();
return categories.filter(
(item, index) => !(categories.indexOf(item) === index)
);
}
throw new Error("Company doesn't exists!");
},
filter: (
input: ProductCategories[Companies[number]][keyof ProductCategories[Companies[number]]],
answers
) => {
// If 1 company exists it will get it. If there are more than one that means that the choices will get narrowed. So, typing only the first company of the array will do it.
const company = answers.companies[0];
if (company)
// Gets the key from the value
return Object.keys(productCategories[company]).find((_key) => {
const key = _key as keyof ProductCategories[Companies[number]];
return productCategories[company][key] === input;
}) as keyof ProductCategories[Companies[number]];
throw new Error("Company doesn't exists!");
},
validate: (input: string[]) => {
return lengthValidator(input);
},
suffix: ":",
},
]);
return { selectedCompanies, productCategory };
}
export async function main(productMainOptionsParam?: Product) {
const { productCategory, selectedCompanies } = await mainPrompt();
const { productMainOptions, productExists } = await productMainPrompt(
productMainOptionsParam
);
const configData = await configPrompt();
const nanoId = nanoid(12);
if (selectedCompanies.includes("hepsiburada")) {
const companyMainOptions = await HepsiburadaMainPrompts();
const result = await HepsiburadaPromptsWrapper[productCategory](
productMainOptions,
companyMainOptions
);
if (!result.products[0]) throw new Error("Products array is empty!");
let brand: string;
if ("Uyumlu Marka" in result.products[0]) {
brand = result.products[0]?.["Uyumlu Marka"];
} else if (
"productKnownBrandName" in result &&
result.productKnownBrandName !== undefined
) {
brand = result.productKnownBrandName;
} else {
brand = "UNKNOWN";
}
await writeToExcel({
company: "hepsiburada",
category: result.category,
caseBrand: brand,
trademark: result.products[0]?.Marka ?? "",
outPath: configData.path,
data: result.products,
mainModalCode: productMainOptions.productCode,
nanoId,
});
}
if (selectedCompanies.includes("trendyol")) {
const companyMainOptions = await TrendyolMainPrompts();
const result = await TrendyolPromptsWrapper[productCategory](
productMainOptions,
companyMainOptions
);
if (!result.products[0]) throw new Error("Products array is empty!");
let brand: string;
if ("Uyumlu Marka" in result.products[0]) {
brand = result.products[0]?.["Uyumlu Marka"];
} else if (
"productKnownBrandName" in result &&
result.productKnownBrandName !== undefined
) {
brand = result.productKnownBrandName;
} else {
brand = "UNKNOWN";
}
await writeToExcel({
company: "trendyol",
category: result.category,
caseBrand: brand,
trademark: result.products[0]?.Marka ?? "",
outPath: configData.path,
data: result.products,
mainModalCode: productMainOptions.productCode,
nanoId,
});
}
// If product doesn't exists and config set to run database
if (!productExists && configData.runDatabase) {
const product = {
id: nanoId,
productTitle: productMainOptions.productTitle,
productCode: productMainOptions.productCode,
productDescription: productMainOptions.productDescription,
price: productMainOptions.price,
stockAmount: productMainOptions.stockAmount,
};
const project = returnDataFile("project");
if (project.database === "JSON") {
const products = returnDataFile("products");
writeFileSync(
"./data/products.json",
JSON.stringify([...products, product])
);
} else if (project.database === "Notion") {
try {
await notionCreateProduct(product);
} catch (error) {
logger.error(error);
// When fails writes it to notion.json
const notion = returnDataFile("notion");
writeFileSync(
"./data/notion.json",
JSON.stringify([...notion, product])
);
}
}
}
}