UNPKG

pg-proto-parser

Version:
71 lines (70 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stripExtension = exports.writeFileToDisk = exports.getHeader = exports.getFieldName = exports.cloneAndNameNode = exports.hasUndefinedInitialValue = exports.toSpecialCamelCase = exports.getUndefinedKey = void 0; const fs_1 = require("fs"); const path_1 = require("path"); const package_1 = require("../package"); const pkg = (0, package_1.readAndParsePackageJson)(); const getUndefinedKey = (enumName) => { // Split the name into parts where a lowercase letter is followed by an uppercase letter const parts = enumName.split(/(?<=[a-z])(?=[A-Z])/); const processedParts = parts.map(part => { // For parts that are all uppercase and longer than 1 character, only the first character should remain uppercase if (part === part.toUpperCase() && part.length > 1) { return part.charAt(0) + part.slice(1).toLowerCase(); } return part; }); const upperSnakeCase = processedParts.join('_').toUpperCase(); return `${upperSnakeCase}_UNDEFINED`; }; exports.getUndefinedKey = getUndefinedKey; const toSpecialCamelCase = (s) => { return s .replace(/_+/g, '') // Remove all underscores .replace(/([A-Z]+)([A-Z][a-z]|$)/g, (match, p1, p2) => p1.toLowerCase() + p2 // Lowercase all but the last letter of consecutive caps ) .replace(/^./, (match) => match.toLowerCase()); // Ensure the first character is lowercase }; exports.toSpecialCamelCase = toSpecialCamelCase; const hasUndefinedInitialValue = (enumData) => { const entries = Object.entries(enumData.values); if (entries.length === 0) return false; const undefinedKey = (0, exports.getUndefinedKey)(enumData.name); const firstEntry = entries[0]; return firstEntry[0] === undefinedKey && firstEntry[1] === 0; }; exports.hasUndefinedInitialValue = hasUndefinedInitialValue; const cloneAndNameNode = (node, name) => { const clone = JSON.parse(JSON.stringify(node)); return { name, ...clone }; }; exports.cloneAndNameNode = cloneAndNameNode; const getFieldName = (field, fallbackName) => { return field.options?.json_name ? field.options.json_name : fallbackName; }; exports.getFieldName = getFieldName; const getHeader = () => { const version = process.env.NODE_ENV === 'test' ? 'latest' : pkg.version; return `/** * This file was automatically generated by pg-proto-parser@${version}. * DO NOT MODIFY IT BY HAND. Instead, modify the source proto file, * and run the pg-proto-parser generate command to regenerate this file. */ `; }; exports.getHeader = getHeader; const writeFileToDisk = (path, contents, options) => { const c = (options.includeHeader && (0, path_1.extname)(path) === '.ts') ? `${(0, exports.getHeader)()}${contents}` : contents; (0, fs_1.writeFileSync)(path, c); }; exports.writeFileToDisk = writeFileToDisk; const stripExtension = (filename) => { const extension = (0, path_1.extname)(filename); return (0, path_1.basename)(filename, extension); }; exports.stripExtension = stripExtension;