-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-check.js
More file actions
77 lines (66 loc) · 3.42 KB
/
pre-check.js
File metadata and controls
77 lines (66 loc) · 3.42 KB
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
const { existsSync } = require('fs');
const { resolve } = require("node:path");
// pre-check: check if user transpiled typescript
if (!existsSync('./build/main.js')) {
console.error('\x1b[41mFailed to locate application core!\x1b[0m');
console.error('Check if you have run \x1b[33m"npm run build"\x1b[0m.');
console.error('This is required to application to work correctly.');
process.exit(1);
}
// define the application path
global.devMode = !!process.env.DEV_MODE;
global.appRoot = resolve(__dirname);
// skip extra check and if user asked for slash command actions
if (!!process.env.COMMAND_ACTION) {
const { register, unregister } = require('./build/modules/discord/command/CommandRegister');
if (process.env.COMMAND_ACTION === 'register') register();
else if (process.env.COMMAND_ACTION === 'unregister') unregister();
else {
console.error(`\x1b[41mUnknwon command action:\x1b[0m ${process.env.COMMAND_ACTION}`);
console.error('Please check documentation about command actions.');
process.exit(1);
}
return;
}
// load additional modules which requires app build
const config = require('./build/modules/config/ConfigLoader').default;
// some call requires asynchronous call, warp code with async function
async function preCheck() {
// pre-check: check if settings file exists
if (!existsSync('./configs/general.json') || !existsSync('./configs/tts.json')) {
console.error('\x1b[41mFailed to locate settings configuration!\x1b[0m');
console.error('You can refer \x1b[33m"configs/general.json.example"\x1b[0m and \x1b[33m"configs/tts.json.example"\x1b[0m to create new one.');
process.exit(1);
}
// pre-check: check if authorization method available for GCP-TTS (when uses it)
if (config.tts.provider === "GoogleCloud")
{
const { getClient, authenticate } = require('./build/modules/tts/provider/googleCloud/CredentialsManager');
// check if Workload Identity Federation available
if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
console.log('\x1b[33mGOOGLE_APPLICATION_CREDENTIALS\x1b[0m provided, pre-check might take longer.');
try {
// try to authenticate with provided credentials
const client = await getClient();
// verify if credentials working correctly
await authenticate(client);
} catch (err) {
console.error(err);
// credentials not working, show error and exit
console.error('\x1b[41mFailed to authenticate with Workload Identity Federation credentials!\x1b[0m');
console.error('Please check if \x1b[33mGOOGLE_APPLICATION_CREDENTIALS\x1b[0m environment variable is correctly set,');
console.error('or there is any error inside credentials file.');
process.exit(1);
}
}
// check if Service Account Key available instead
else if (!existsSync('./configs/gcp-credentials.json'))
{
console.error('\x1b[41mFailed to locate Google Cloud Platform credentials!\x1b[0m');
console.error('Download your Google Cloud Platform credentials, rename to \x1b[33m"gcp-credentials.json"\x1b[0m and put it into configs directory.');
process.exit(1);
}
}
}
// pre-check done. configure app and start main application
preCheck().then(() => require('./build/main'));