diff --git a/ONE.Interfaces.ProtocolBuffers b/ONE.Interfaces.ProtocolBuffers index e695d6c..9045e32 160000 --- a/ONE.Interfaces.ProtocolBuffers +++ b/ONE.Interfaces.ProtocolBuffers @@ -1 +1 @@ -Subproject commit e695d6c3404675257dd6529f3033bcc93f3545cc +Subproject commit 9045e325df3e557db1521593f53dba8ce3e8bf21 diff --git a/generation/generateJS.sh b/generation/generateJS.sh index 47222c1..c3a26e1 100755 --- a/generation/generateJS.sh +++ b/generation/generateJS.sh @@ -14,11 +14,22 @@ npm install PROTOPATHS=`find ../one.interfaces.protocolbuffers -type d -print | xargs -n 1 -I {} echo "-p {}" | xargs echo` # == compile with protobufjs == -for P in `find ../one.interfaces.protocolbuffers/proto/one -name "*.proto"` +for P in `find ../one.interfaces.protocolbuffers/proto/flat -name "*.proto"` do PROTO=`basename -s .proto ${P}` echo "=== ${P} :: (${PROTO})" - node_modules/protobufjs/cli/bin/pbjs -t static-module -w commonjs ${PROTOPATHS} -o ./out/js/${PROTO}.js ${P} + # Special handling for apiresponse to include all enum files + if [ "$PROTO" = "apiresponse" ]; then + echo "=== Including all enum files for apiresponse ===" + ENUM_FILES=`find ../one.interfaces.protocolbuffers/proto/flat -name "enum_*.proto" | xargs echo` + node_modules/protobufjs/cli/bin/pbjs -t static-module -w commonjs ${PROTOPATHS} -o ./out/js/${PROTO}.js ${P} ${ENUM_FILES} + else + node_modules/protobufjs/cli/bin/pbjs -t static-module -w commonjs ${PROTOPATHS} -o ./out/js/${PROTO}.js ${P} + fi done + +# == post-process files to move enums to bottom == +echo "=== Post-processing files to move enums to bottom ===" +node reorganize-enums.js diff --git a/generation/reorganize-enums.js b/generation/reorganize-enums.js new file mode 100644 index 0000000..94a073c --- /dev/null +++ b/generation/reorganize-enums.js @@ -0,0 +1,181 @@ +const fs = require('fs'); +const path = require('path'); + +console.log('=== Enum Reorganization Script ==='); + +const outputDir = './out/js'; +if (!fs.existsSync(outputDir)) { + console.log('Output directory does not exist. Run the full generation first.'); + process.exit(1); +} + +const files = fs.readdirSync(outputDir).filter(f => f.endsWith('.js')); +console.log(`Found ${files.length} JS files to process`); + +files.forEach(filename => { + const filepath = path.join(outputDir, filename); + const content = fs.readFileSync(filepath, 'utf8'); + + console.log(`Processing ${filename}...`); + + // Check if file has already been processed + if (content.includes('// === ENUMS SECTION - All enums moved to bottom ===')) { + console.log(` - Already processed, skipping ${filename}`); + return; + } + + const lines = content.split('\n'); + const cleanLines = []; + const enumBlocks = []; + let i = 0; + + // First pass: Find all enum definitions and their JSDoc comments + while (i < lines.length) { + const line = lines[i]; + + // Check if this is an enum definition + if (line.match(/^\$root\.Enum.*= \(function\(\)/)) { + const enumNameMatch = line.match(/^\$root\.(Enum\w+)/); + const enumName = enumNameMatch ? enumNameMatch[1] : null; + + console.log(` Found enum: ${enumName}`); + + // Find the end of the enum definition + let enumLines = [line]; + let braceCount = 1; + let j = i + 1; + + while (j < lines.length && braceCount > 0) { + const nextLine = lines[j]; + enumLines.push(nextLine); + + // Count braces + const openBraces = (nextLine.match(/\(/g) || []).length; + const closeBraces = (nextLine.match(/\)/g) || []).length; + braceCount += openBraces - closeBraces; + + if (braceCount === 0 && nextLine.match(/^\}\)\(\);/)) { + break; + } + j++; + } + + // Look for JSDoc comment for this enum (search backwards from current position first, then forwards) + let enumComment = null; + + // Search backwards first (more likely to be nearby) + for (let k = i - 1; k >= 0; k--) { + if (lines[k] && lines[k].includes(`@exports ${enumName}`)) { + // Found the JSDoc comment, find its boundaries + let commentStart = k; + while (commentStart > 0 && lines[commentStart] && !lines[commentStart].match(/^\s*\/\*\*/)) { + commentStart--; + } + + let commentEnd = k; + while (commentEnd < lines.length && lines[commentEnd] && !lines[commentEnd].match(/^\s*\*\//)) { + commentEnd++; + } + + if (commentStart >= 0 && commentEnd < lines.length && lines[commentStart] && lines[commentEnd]) { + enumComment = lines.slice(commentStart, commentEnd + 1).join('\n'); + console.log(` Found JSDoc comment for ${enumName} (backward search)`); + } + break; + } + } + + // If not found backwards, search forwards + if (!enumComment) { + for (let k = j + 1; k < lines.length; k++) { + if (lines[k] && lines[k].includes(`@exports ${enumName}`)) { + // Found the JSDoc comment, find its boundaries + let commentStart = k; + while (commentStart > 0 && lines[commentStart] && !lines[commentStart].match(/^\s*\/\*\*/)) { + commentStart--; + } + + let commentEnd = k; + while (commentEnd < lines.length && lines[commentEnd] && !lines[commentEnd].match(/^\s*\*\//)) { + commentEnd++; + } + + if (commentStart >= 0 && commentEnd < lines.length && lines[commentStart] && lines[commentEnd]) { + enumComment = lines.slice(commentStart, commentEnd + 1).join('\n'); + console.log(` Found JSDoc comment for ${enumName} (forward search)`); + } + break; + } + } + } + + // Create the complete enum block + let enumBlock = ''; + if (enumComment) { + enumBlock = enumComment + '\n' + enumLines.join('\n'); + } else { + enumBlock = enumLines.join('\n'); + console.log(` No JSDoc comment found for ${enumName}`); + } + + enumBlocks.push(enumBlock); + + // Skip past this enum definition + i = j + 1; + continue; + } + + // Check if this line is part of an enum JSDoc comment (to be removed) + if (line.match(/^\s*\/\*\*/) || line.match(/^\s*\*\s*@exports\s+Enum/)) { + // This might be an enum JSDoc comment, check if it's for an enum + let isEnumComment = false; + let commentEnd = i; + + // Find the end of this comment block + while (commentEnd < lines.length && !lines[commentEnd].match(/^\s*\*\//)) { + if (lines[commentEnd].match(/^\s*\*\s*@exports\s+Enum/)) { + isEnumComment = true; + } + commentEnd++; + } + + if (isEnumComment) { + console.log(` Removing orphaned enum JSDoc comment at line ${i + 1}`); + // Skip this entire comment block + i = commentEnd + 1; + continue; + } + } + + // This is a regular line, keep it + cleanLines.push(line); + i++; + } + + console.log(` Found ${enumBlocks.length} enum blocks`); + + // Reconstruct file with enums at bottom + if (enumBlocks.length > 0) { + // Remove the last module.exports line if it exists + while (cleanLines.length > 0 && (cleanLines[cleanLines.length - 1].trim() === '' || cleanLines[cleanLines.length - 1].includes('module.exports'))) { + cleanLines.pop(); + } + + const newContent = [ + ...cleanLines, + '', + '// === ENUMS SECTION - All enums moved to bottom ===', + '', + ...enumBlocks, + '', + 'module.exports = $root;' + ].join('\n'); + + fs.writeFileSync(filepath, newContent); + console.log(` ✓ Moved ${enumBlocks.length} enum(s) to bottom in ${filename}`); + } else { + console.log(` - No enums found in ${filename}`); + } +}); + +console.log('=== Enum reorganization complete ==='); diff --git a/lib/package-lock.json b/lib/package-lock.json index 845c163..17c0a3f 100644 --- a/lib/package-lock.json +++ b/lib/package-lock.json @@ -1,12 +1,12 @@ { "name": "@aquaticinformatics/claros_protobuf", - "version": "15.2.0", + "version": "15.8.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@aquaticinformatics/claros_protobuf", - "version": "15.2.0", + "version": "15.8.0", "license": "UNLICENSED", "dependencies": { "protobufjs": "^6.9.0" diff --git a/lib/package.json b/lib/package.json index 0225a70..9ab4cd7 100644 --- a/lib/package.json +++ b/lib/package.json @@ -1,7 +1,7 @@ { "name": "@aquaticinformatics/claros_protobuf", - "version": "15.2.0", - "description": "Claros Protocol Buffers for JavaScript (& TypeScript). Version 6.13.0 of ONE.Interfaces.ProtocolBuffers", + "version": "15.8.0", + "description": "Claros Protocol Buffers for JavaScript (& TypeScript). Version 7.13.0 of ONE.Interfaces.ProtocolBuffers", "license": "UNLICENSED", "main": "src/apiresponse.js", "types": "types.d.ts",