@runbook-docs/mcp-server
Version:
Runbook Model Context Protocol Server
157 lines (156 loc) • 5.15 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class Config {
config;
configFilePath;
constructor() {
this.configFilePath = path.join(__dirname, 'env', 'config.json');
this.config = {
baseUrl: 'https://sample.runbook.jp',
apiToken: ''
};
this.loadConfig();
}
get baseUrl() {
return this.config.baseUrl;
}
get apiToken() {
return this.config.apiToken;
}
get prefix() {
return this.config.prefix;
}
saveConfig(newConfig) {
if (!fs.existsSync(path.dirname(this.configFilePath))) {
fs.mkdirSync(path.dirname(this.configFilePath), { recursive: true });
}
fs.writeFileSync(this.configFilePath, JSON.stringify(newConfig, null, 2));
this.config = { ...this.config, ...newConfig };
}
clearConfig() {
if (fs.existsSync(this.configFilePath)) {
fs.unlinkSync(this.configFilePath);
}
this.config = {
baseUrl: 'https://sample.runbook.jp',
apiToken: ''
};
}
upperSnakeToCamel(upperSnake) {
const words = upperSnake.toLowerCase().split('_');
return (words[0] +
words
.slice(1)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(''));
}
kebabToCamel(kebab) {
const words = kebab.toLowerCase().split('-');
return (words[0] +
words
.slice(1)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(''));
}
setConfigFile() {
if (fs.existsSync(this.configFilePath)) {
try {
const fileContent = fs.readFileSync(this.configFilePath, 'utf-8');
const fileConfig = JSON.parse(fileContent);
this.config = { ...this.config, ...fileConfig };
}
catch {
throw new Error('Failed to parse the config file.');
}
}
}
setEnvironmentVariables() {
const envVars = ['BASE_URL', 'API_TOKEN', 'PREFIX'];
for (const envVar of envVars) {
const envName = `RUNBOOK_${envVar}`;
if (process.env[envName]) {
this.config = {
...this.config,
[this.upperSnakeToCamel(envVar)]: process.env[envName]
};
}
}
}
setCommandlineArguments() {
const { values } = (0, util_1.parseArgs)({
args: process.argv.slice(2),
allowPositionals: true,
options: {
'api-token': {
type: 'string',
short: 'n',
multiple: false
},
'base-url': {
type: 'string',
short: 'h',
multiple: false
},
prefix: {
type: 'string',
short: 'p',
multiple: false
}
}
});
for (const key of Object.keys(values)) {
const k = key;
const value = values[k];
if (key && value) {
this.config = { ...this.config, [this.kebabToCamel(key)]: value };
}
}
}
loadConfig() {
this.setConfigFile();
this.setEnvironmentVariables();
try {
this.setCommandlineArguments();
}
catch {
// Ignore errors
}
}
}
exports.default = Config;