-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (122 loc) · 4 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
import readline from 'readline';
import fs from 'fs';
import express from 'express';
import ngrok from 'ngrok';
import { createManagementClient } from '@kentico/kontent-management';
function getHostAddress(config) {
const port = config.port ?? 8000; // Port value or default to 8000
const addr = config.addr ?? port;
if (typeof addr === 'function') {
return addr(port);
}
return addr;
}
async function startNgrok(config) {
// start and wait for Ngrok
const tunnel = getHostAddress(config);
const ngrokArg = typeof tunnel === 'string' ? { addr: tunnel } : tunnel;
console.log(`Starting ngrok with argument '${tunnel}'`)
const url = await ngrok.connect(ngrokArg);
return url;
}
function waitForInput() {
function askQuestion(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}))
}
return askQuestion("Press a key to exit");
}
async function createOrUpdateKontent(ngrokUrl, config) {
// Get elementCodename or default from config
const elementCodename = config.elementCodename || 'custom_element';
// create and/or update custom element type with new properties
console.log(`Creating or updating custom element test model '${config.kontent.modelCodename}'`);
const client = createManagementClient({ projectId: config.kontent.projectId, apiKey: config.kontent.apiKey });
const types = await client.listContentTypes().toPromise();
if (!types.data.items.some((type) => type.codename === config.kontent.modelCodename)) {
await client.addContentType()
.withData((builder) => {
return {
name: 'Custom Element Tester',
codename: config.kontent.modelCodename,
elements: [
builder.customElement({
name: 'Custom Element',
type: 'custom',
source_url: ngrokUrl,
json_parameters: "",
codename: elementCodename,
}),
],
};
})
.toPromise();
} else {
await client.modifyContentType()
.byTypeCodename(config.kontent.modelCodename)
.withData([
{
op: 'replace',
path: `/elements/codename:${elementCodename}/source_url`,
value: ngrokUrl,
},
])
.toPromise();
}
console.log(`Kontent URL: https://app.kontent.ai/${config.kontent.projectId}/`);
}
function startExpressServer(config) {
const expressPort = config.port ?? 8000;
console.log(`Starting Express server on port ${expressPort} with static files folder '${config.express.staticFilesPath}'.`);
const app = express();
app.use(express.static(config.express.staticFilesPath));
app.listen(config.port, () => {
console.log(`Custom Open Search element page listening on port ${expressPort}`);
});
// TODO: Listen on app exit?
// Disconnect Ngrok
// await ngrok.disconnect();
}
function getConfigPath() {
const configArg = process.argv.find(arg => arg.includes('--config='));
const defaultConfigPath = './kcet.config.js';
if (configArg) {
return configArg
.substring(configArg.indexOf('=') + 1)
.trim(`"`)
.trim(`'`) || defaultConfigPath;
}
return defaultConfigPath;
}
function validateConfigFile(filePath) {
if (!fs.existsSync(path)) {
console.log(`Config file ${filePath} does not exist.`)
return false;
}
return true;
}
async function main(config) {
// TODO: Validate conig...
const ngrokUrl = await startNgrok(config);
await createOrUpdateKontent(ngrokUrl, config);
// TODO:
// - open browser and navigate to custom element instance
if (config.express && config.express.staticFilesPath) {
startExpressServer(config);
} else {
await waitForInput();
// Disconnect Ngrok
await ngrok.disconnect();
}
}
const configPath = getConfigPath();
if (validateConfigFile(configPath)) {
const { default: config } = await import(configPath);
await main(config);
}