@runbook-docs/mcp-server
Version:
Runbook Model Context Protocol Server
131 lines (130 loc) • 4.16 kB
JavaScript
;
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 });
exports.saveConfig = saveConfig;
exports.loadConfig = loadConfig;
const util_1 = require("util");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
let config = {
baseUrl: 'https://sample.runbook.jp',
apiToken: ''
};
function saveConfig(newConfig) {
// create directory if it doesn't exist
if (!fs.existsSync(path.dirname(configFilePath))) {
fs.mkdirSync(path.dirname(configFilePath), { recursive: true });
}
fs.writeFileSync(configFilePath, JSON.stringify(newConfig, null, 2));
}
function upperSnakeToCamel(upperSnake) {
const words = upperSnake.toLowerCase().split('_');
return (words[0] +
words
.slice(1)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(''));
}
function kebabToCamel(kebab) {
const words = kebab.toLowerCase().split('-');
return (words[0] +
words
.slice(1)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(''));
}
function setConfigFile() {
if (fs.existsSync(configFilePath)) {
try {
const fileContent = fs.readFileSync(configFilePath, 'utf-8');
const fileConfig = JSON.parse(fileContent);
config = { ...config, ...fileConfig };
}
catch {
throw new Error('Failed to parse the config file.');
}
}
}
function setEnvironmentVariables() {
const envVars = ['BASE_URL', 'API_TOKEN'];
for (const envVar of envVars) {
const envName = `RUNBOOK_${envVar}`;
if (process.env[envName]) {
config = {
...config,
[upperSnakeToCamel(envVar)]: process.env[envName]
};
}
}
}
function 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
}
}
});
for (const key of Object.keys(values)) {
const k = key;
const value = values[k];
if (key && value) {
config = { ...config, [kebabToCamel(key)]: value };
}
}
}
const configFilePath = path.join(__dirname, 'env', 'config.json');
function loadConfig() {
setConfigFile();
setEnvironmentVariables();
try {
setCommandlineArguments();
}
catch {
// Ignore errors
}
return config;
}
loadConfig();
exports.default = config;