UNPKG

dce-dev-wizard

Version:

Wizard for managing development apps at Harvard DCE.

236 lines 10 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var fs_1 = __importDefault(require("fs")); var path_1 = __importDefault(require("path")); // Error code regex var errorCodeRegex = /new ErrorWithCode\(\s*(.+),\s*(.+)\s*\)(;|,)/; // Get the project directory var pwd = path_1.default.join(process.env.PWD || process.env.CWD || __dirname, '../..'); /*------------------------------------------------------------------------*/ /* ------------------------------- Helpers ------------------------------ */ /*------------------------------------------------------------------------*/ /** * Get all paths to the error code types within a folder * @author Gabe Abrams * @param parentFolderPath - the path to the parent folder * @returns an array of paths to the error code types */ var getErrorCodesPaths = function (parentFolderPath) { // Get all files in the folder var files = fs_1.default.readdirSync(parentFolderPath); // For each item in the folder, recurse if folder, analyze if file var paths = []; files.forEach(function (file) { // Get the full path var fullPath = path_1.default.join(parentFolderPath, file); // If folder, recurse var isFolder = (fs_1.default .lstatSync(fullPath) .isDirectory()); if (isFolder) { // Skip if the "from-server" folder if (file !== 'from-server') { // Recurse paths.push.apply(paths, getErrorCodesPaths(fullPath)); } } else { // Not folder. Analyze if (file.includes('ErrorCode') && (file.endsWith('.ts') || file.endsWith('.tsx'))) { // Found an error code file paths.push(fullPath); } } }); // Return all accumulated paths return paths; }; /** * Read an error code file and return the error codes within it * @author Gabe Abrams * @param filePath - the path to the error code file * @returns an array of error codes */ var readErrorCodes = function (filePath) { // Read the file var fileContents = fs_1.default.readFileSync(filePath, 'utf8'); // Get the name of the error code file var parent = ((filePath.split('/').pop() || 'UnknownErrorCodeParent') .replace('.tsx', '') .replace('.ts', '')); // Split the file into lines var lines = fileContents.split('\n'); // Find all error codes var errorCodes = []; lines.forEach(function (line, lineNumber) { // Format of an error code: // DescriptionOfErrorCode = 'CODE', // Check if this line is an error code var matches = line.trim().match(/(\w+) = '(\w+)',/); if (matches) { // Found an error code var name_1 = matches[1]; var code = matches[2]; // Add to error codes errorCodes.push({ parent: parent, code: code, found: false, prop: name_1, appearances: [], }); } }); // Return all error codes return errorCodes; }; /*------------------------------------------------------------------------*/ /* -------------------------------- Main -------------------------------- */ /*------------------------------------------------------------------------*/ /*----------------------------------------*/ /* ---------- Parse Error Codes --------- */ /*----------------------------------------*/ console.log('\nUpdating error docs...'); // Get error code file paths var serverPaths = getErrorCodesPaths(path_1.default.join(pwd, '/server/src')); var clientPaths = getErrorCodesPaths(path_1.default.join(pwd, '/client/src')); var errorCodePaths = serverPaths.concat(clientPaths); // Read each and process the enums var errorCodes = []; errorCodePaths.forEach(function (filePath) { errorCodes.push.apply(errorCodes, readErrorCodes(filePath)); }); /*----------------------------------------*/ /* -------- Validate Error Codes -------- */ /*----------------------------------------*/ // Display fatal error if duplicate error codes exist var errorCodeSet = new Set(); errorCodes.forEach(function (errorCode) { if (errorCodeSet.has(errorCode.code)) { console.error("Duplicate error code defined: ".concat(errorCode.code, " in ").concat(errorCode.parent, ". Fatal error. Exiting!")); process.exit(1); } }); /*----------------------------------------*/ /* ---------- Find Appearances ---------- */ /*----------------------------------------*/ /** * Recursively look for appearances of error codes * @author Gabe Abrams * @param folderPath - the path to the folder to search */ var findAppearances = function (folderPath) { // Format of an error appearance: // new ErrorWithCode( // `Message template with ${templateVariables} in it perhaps`, // ErrorCodeParent.ErrorCodePropName, // ); // Get all files in the folder var files = fs_1.default.readdirSync(folderPath); // For each item in the folder, recurse if folder, analyze if file files.forEach(function (file) { // Get the full path var fullPath = path_1.default.join(folderPath, file); // If folder, recurse var isFolder = (fs_1.default .lstatSync(fullPath) .isDirectory()); if (isFolder) { // Recurse findAppearances(fullPath); } else { // Not folder. Analyze if ((file.endsWith('.ts') || file.endsWith('.tsx')) && !errorCodePaths.includes(fullPath)) { // Read the file var fileContents_1 = fs_1.default.readFileSync(fullPath, 'utf8'); // Check if found errorCodes.find(function (errorCode) { if (fileContents_1.includes("".concat(errorCode.parent, ".").concat(errorCode.prop, ",")) || fileContents_1.includes("".concat(errorCode.parent, ".").concat(errorCode.prop, ";")) || fileContents_1.includes("".concat(errorCode.parent, ".").concat(errorCode.prop, " ")) || fileContents_1.includes("".concat(errorCode.parent, ".").concat(errorCode.prop, ")")) || fileContents_1.includes("".concat(errorCode.parent, ".").concat(errorCode.prop, "\n"))) { errorCode.found = true; } }); // Check if this line is an error appearance var matches = fileContents_1.trim().match(errorCodeRegex); var _loop_1 = function () { // Found an error appearance var messageTemplate = matches[1]; var parentAndProp = matches[2]; // Find the error code var errorCode = errorCodes.find(function (errorCode) { return ("".concat(errorCode.parent, ".").concat(errorCode.prop, ",") === parentAndProp.trim()); }); if (errorCode) { // Add the appearance errorCode.appearances.push({ messageTemplate: messageTemplate, filePath: fullPath, }); } // Remove this match fileContents_1 = fileContents_1.replace(matches[0], ''); matches = fileContents_1.trim().match(errorCodeRegex); }; while (matches) { _loop_1(); } } } }); }; // Add appearances in each folder findAppearances(path_1.default.join(pwd, '/server/src')); findAppearances(path_1.default.join(pwd, '/client/src')); /*------------------------------------------------------------------------*/ /* ----------------------------- Build Docs ----------------------------- */ /*------------------------------------------------------------------------*/ var docsMD = ''; /*----------------------------------------*/ /* -------------- Metadata -------------- */ /*----------------------------------------*/ var pkg = require(path_1.default.join(pwd, 'package.json')); if (!pkg) { console.log('package.json not found'); process.exit(1); } var projectName = pkg.name || 'This Project'; /*----------------------------------------*/ /* ---------------- Intro --------------- */ /*----------------------------------------*/ docsMD = "# Error Cheatsheet for ".concat(projectName, "\n"); docsMD += 'This is an auto-generated document that outlines the friendly error codes that users may encounter when using this app.\n\n'; /*----------------------------------------*/ /* ---------------- Table --------------- */ /*----------------------------------------*/ // Header docsMD += '\n| Code | Internal Name | Associated Message |\n'; docsMD += '| -------- | -------- | -------- |\n'; // Error codes errorCodes.forEach(function (err) { // Skip ones that are not in use if (!err.found) { return; } var associatedMessage = (err .appearances .map(function (appearance) { return "<div>".concat(appearance.messageTemplate.replace('`', '\''), "<div style=\"font-size: 80%\">").concat(appearance.filePath.replace(pwd, ''), "</div></div>"); }) .join('<br>')); if (err.appearances.length === 0) { associatedMessage = 'No additional information'; } docsMD += "| ".concat(err.code, " | ").concat(err.prop, " | ").concat(associatedMessage, " |\n"); }); // Finish fs_1.default.writeFileSync(path_1.default.join(pwd, 'docs/ErrorCodeDocs.md'), docsMD, 'utf-8'); console.log('Success! Written to docs/ErrorCodeDocs.md'); //# sourceMappingURL=buildErrorDocs.js.map