Skip to content

Commit

Permalink
feat: Added auto-configuration process
Browse files Browse the repository at this point in the history
Now it helps you set up credentials on first start/misconfiguration
  • Loading branch information
rafaelpernil2 committed Mar 25, 2022
1 parent ffafe35 commit 5069eb5
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 41 deletions.
6 changes: 3 additions & 3 deletions config/tokens.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"accessToken": "",
"refreshToken": "",
"scope": [
"channel:manage:redemptions",
"channel:read:redemptions",
"chat:read",
"chat:edit",
"chat:read"
"channel:read:redemptions",
"channel:manage:redemptions"
],
"expiresIn": 0,
"obtainmentTimestamp": 0
Expand Down
85 changes: 73 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twitch-midi-bot",
"version": "0.1.4",
"version": "0.1.5",
"description": "",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -27,11 +27,12 @@
"@twurple/chat": "^5.0.18",
"@twurple/eventsub": "^5.0.18",
"@twurple/pubsub": "^5.0.18",
"chalk": "^4.1.2",
"dotenv": "^16.0.0",
"harmonics": "^1.1.0",
"jzz": "^1.4.5",
"nanotimer": "^0.3.15",
"webmidi": "^3.0.15",
"jzz": "^1.4.5"
"webmidi": "^3.0.15"
},
"pkg": {
"assets": [
Expand All @@ -47,6 +48,7 @@
},
"devDependencies": {
"@types/nanotimer": "^0.3.0",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"@vercel/ncc": "^0.33.3",
Expand All @@ -63,7 +65,8 @@
"printWidth": 180,
"trailingComma": "none",
"singleQuote": true,
"tabWidth": 4
"tabWidth": 4,
"endOfLine": "auto"
},
"bin": "dist/index.js",
"preferGlobal": true
Expand Down
10 changes: 8 additions & 2 deletions src/configuration/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const ERROR_MSG = {
INSUFFICIENT_PERMISSIONS: "You don't have enough permissions to use this command, ask me or a mod to launch this command or subscribe to this channel",
INVALID_SWEEP_RANGE: 'Invalid sweep range',
TWITCH_API: 'Could not connect to Twitch',
INVALID_REWARD: 'Invalid MIDI command from reward, please review the configuration of this bot'
INVALID_REWARD: 'Invalid MIDI command from reward, please review the configuration of this bot',
BAD_SETUP_PROCESS: 'Bad setup, try again'
};

export const GLOBAL = {
Expand All @@ -33,8 +34,13 @@ export const CONFIG = {
BOT_TOKENS_PATH: './config/bot-tokens.json',
BROADCASTER_TOKENS_PATH: './config/broadcaster-tokens.json',
REWARDS_PATH: './config/rewards.json',
DOT_ENV_PATH: '.env',
DEFAULT_VOLUME: 0.8,
DEFAULT_TEMPO: 120
DEFAULT_TEMPO: 120,
LOCAL_SERVER_HOST: 'localhost',
LOCAL_SERVER_PORT: 8000,
REDIRECT_URI: 'http://localhost:8000',
TWITCH_BASE_AUTH_URL: 'https://id.twitch.tv/oauth2/'
};

export const COMMANDS = {
Expand Down
49 changes: 32 additions & 17 deletions src/configuration/env-loader.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { EnvObject } from '../types/env-object-type';
import { ERROR_MSG, GLOBAL } from './constants';

function getLoadedEnvVariables(): EnvObject {
const variablesToLoad: Partial<EnvObject> = {
CLIENT_ID: undefined,
CLIENT_SECRET: undefined,
BOT_ACCESS_TOKEN: undefined,
BOT_REFRESH_TOKEN: undefined,
BROADCASTER_ACCESS_TOKEN: undefined,
BROADCASTER_REFRESH_TOKEN: undefined,
TARGET_CHANNEL: undefined,
TARGET_MIDI_NAME: undefined,
TARGET_MIDI_CHANNEL: undefined,
REWARDS_MODE: undefined
};
const loadedVariables = Object.fromEntries(Object.entries(variablesToLoad).map(([key]) => [key, process.env[key]])) as EnvObject;
areVariablesValid(loadedVariables);
return loadedVariables;
async function getLoadedEnvVariables(altSetupProcess?: () => Promise<EnvObject>): Promise<EnvObject> {
function getVariables() {
const variablesToLoad: Partial<EnvObject> = {
CLIENT_ID: undefined,
CLIENT_SECRET: undefined,
BOT_ACCESS_TOKEN: undefined,
BOT_REFRESH_TOKEN: undefined,
BROADCASTER_ACCESS_TOKEN: undefined,
BROADCASTER_REFRESH_TOKEN: undefined,
TARGET_CHANNEL: undefined,
TARGET_MIDI_NAME: undefined,
TARGET_MIDI_CHANNEL: undefined,
REWARDS_MODE: undefined
};
const loadedVariables = Object.fromEntries(Object.entries(variablesToLoad).map(([key]) => [key, process.env[key]])) as EnvObject;
areVariablesValid(loadedVariables);
return loadedVariables;
}

try {
return getVariables();
} catch (error) {
const loadedVariables = await altSetupProcess?.();
if (loadedVariables == null) {
throw new Error(ERROR_MSG.BAD_SETUP_PROCESS);
}
areVariablesValid(loadedVariables);
// Let's try again
return loadedVariables;
}
}

function areVariablesValid(loadedVariables: Record<string, string | undefined>): loadedVariables is EnvObject {
const invalidVariables = Object.entries(loadedVariables).filter(([, value]) => value == null);
const invalidVariables = Object.entries(loadedVariables).filter(([, value]) => value == null || value === GLOBAL.EMPTY_MESSAGE);
for (const [key] of invalidVariables) {
throw new Error(`This app cannot be executed, make sure you set a valid value for ${key} inside the .env file`);
}
Expand Down
Loading

0 comments on commit 5069eb5

Please sign in to comment.