UNPKG

@averagehelper/corde

Version:

A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)

81 lines (80 loc) 2.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = void 0; const chalk_1 = __importDefault(require("chalk")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const prettier_1 = require("prettier"); const errors_1 = require("../errors"); const jsonFile = { botPrefix: "", botTestId: "", channelId: "", cordeTestToken: "", guildId: "", testFiles: [""], botTestToken: "", timeOut: 5000, }; const jsFile = ` module.exports = ${JSON.stringify(jsonFile)} `; const tsFile = ` module.exports = ${JSON.stringify(jsonFile)} `; /** * Initialize a config file with all available options. * Formatted using **prettier** * * @version 1.0 * * @param fileType Possible type of file * * @throws Error if could not create the config file */ function init(fileType = "json") { let fileContent = ""; // No declaration of fileType is considered 'json' if (!fileType) { fileType = "json"; } if (fileType === "json") { fileContent = JSON.stringify(jsonFile); } else if (fileType === "js") { fileContent = jsFile; } else if (fileType === "ts") { fileContent = tsFile; } else { console.log(` - ${chalk_1.default.bold(fileType)} is not a valid type. Use '${chalk_1.default.bold("init --help")}' to check valid types`); } try { const fileName = `corde.${fileType}`; const filePath = path_1.default.resolve(process.cwd(), fileName); fileContent = formatFile(fileContent, fileType); fs_1.default.writeFileSync(filePath, fileContent); console.log(`- ${chalk_1.default.green("Successfully")} generated corde config in ${chalk_1.default.bold(filePath)}`); } 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 formatFile(file, type) { let fileParser = "babel"; // Attempt to format a json with babel parse results in error if (type === "json") { fileParser = "json"; } return prettier_1.format(file, { printWidth: 100, singleQuote: true, trailingComma: "all", parser: fileParser, }); }