corde
Version:
A simple library for Discord bot tests
108 lines (85 loc) • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.init = void 0;
const tslib_1 = require("tslib");
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
const path_1 = (0, tslib_1.__importDefault)(require("path"));
const consts_1 = require("../consts");
const errors_1 = require("../errors");
const utils_1 = require("../utils");
function init(fileType = "json") {
if (!fileType) {
fileType = "json";
}
const fileContent = getFileFromType(fileType);
if (!fileContent) {
console.log(
` - ${chalk_1.default.bold(fileType)} is not a valid type. Use '${chalk_1.default.bold(
"init --help",
)}' to check valid types`,
);
return;
}
try {
const fileName = `corde.config.${fileType}`;
const filePath = path_1.default.resolve(process.cwd(), fileName);
fs_1.default.writeFileSync(filePath, fileContent);
console.log(
`- ${chalk_1.default.green("Successfully")} generated corde config in ${chalk_1.default.bold(
filePath,
)}`,
);
console.log(fileContent);
} catch (error) {
throw new errors_1.FileError(
" - Fail in config file creation. Check if you have permission to create files in this directory.",
);
}
}
exports.init = init;
function getFileFromType(type) {
if (type === "json") {
return convertObjectToFileType(true);
} else if (type === "js") {
return `module.exports = ${convertObjectToFileType(false)}`;
} else if (type === "ts") {
return `export = ${convertObjectToFileType(false)}`;
}
return undefined;
}
function convertObjectToFileType(isJson) {
const strType = isJson ? '"' : "";
const DOUBLE_SPACE = " ";
let response = isJson
? `{\n${DOUBLE_SPACE}"$schema": "./node_modules/corde/schema/corde.schema.json",\n`
: "{\n";
const keys = (0, utils_1.keysOf)(consts_1.DEFAULT_CONFIG);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let value = consts_1.DEFAULT_CONFIG[key];
if ((0, utils_1.typeOf)(value) === "string") {
value = '""';
}
if (Array.isArray(value)) {
let newValue = "[";
for (let i = 0; i < value.length; i++) {
if ((0, utils_1.typeOf)(value[i]) === "string") {
newValue += `"${value[i]}"`;
} else {
newValue += `${value[i]}`;
}
if (i != value.length - 1) {
newValue += ", ";
}
}
value = newValue + "]";
}
const comma = i === keys.length - 1 ? "" : ",";
response += DOUBLE_SPACE + `${strType}${key}${strType}: ${value}${comma}\n`;
}
response += "}";
return response;
}