|
| 1 | +const https = require('https'); |
| 2 | +const fs = require('fs'); |
| 3 | +const { exec } = require('child_process'); |
| 4 | + |
| 5 | +if (typeof process.argv[2] !== 'string') { |
| 6 | + throw new Error('branchName not provided'); |
| 7 | +} |
| 8 | + |
| 9 | +// Configuration Variables |
| 10 | +const branchName = process.argv[2]; |
| 11 | +const protoUrl = `https://raw.githubusercontent.com/pganalyze/libpg_query/${branchName}/protobuf/pg_query.proto`; |
| 12 | +const protoFilePath = 'libpg_query/protobuf/pg_query.proto'; |
| 13 | +const protoJSFilePath = 'proto.js'; |
| 14 | +const originalPackageName = 'protobufjs/minimal'; |
| 15 | +const newPackageName = '@pgsql/protobufjs/minimal'; |
| 16 | +const pbjsCommand = ['pbjs', '--keep-case', '-t', 'static-module', '-o', protoJSFilePath, protoFilePath]; |
| 17 | + |
| 18 | +// Step 1: Download the .proto file |
| 19 | +function downloadProtoFile(callback) { |
| 20 | + https.get(protoUrl, (response) => { |
| 21 | + if (response.statusCode !== 200) { |
| 22 | + console.error(`Failed to download file: Status Code: ${response.statusCode}`); |
| 23 | + response.resume(); // consume response data to free up memory |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const fileStream = fs.createWriteStream(protoFilePath); |
| 28 | + response.pipe(fileStream); |
| 29 | + fileStream.on('finish', () => { |
| 30 | + fileStream.close(); |
| 31 | + console.log('Downloaded proto file.'); |
| 32 | + callback(); |
| 33 | + }); |
| 34 | + }).on('error', (err) => { |
| 35 | + console.error(`Error downloading the file: ${err.message}`); |
| 36 | + fs.unlink(protoFilePath, () => {}); // Delete the file async. (No need to check error here) |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +// Step 2: Generate proto.js using pbjs (Assuming pbjs is installed and accessible) |
| 41 | +function generateProtoJS(callback) { |
| 42 | + exec(pbjsCommand.join(' '), (error, stdout, stderr) => { |
| 43 | + if (error) { |
| 44 | + console.error(`Error during code generation: ${error.message}`); |
| 45 | + return; |
| 46 | + } |
| 47 | + console.log('Generated proto.js from proto file.'); |
| 48 | + callback(); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +// Step 3: Replace text in proto.js |
| 53 | +function replaceTextInProtoJS() { |
| 54 | + fs.readFile(protoJSFilePath, 'utf8', (err, data) => { |
| 55 | + if (err) { |
| 56 | + console.error(`Error reading proto.js: ${err.message}`); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const result = data.replace(new RegExp(originalPackageName, 'g'), newPackageName); |
| 61 | + |
| 62 | + fs.writeFile(protoJSFilePath, result, 'utf8', (err) => { |
| 63 | + if (err) { |
| 64 | + console.error(`Error writing back to proto.js: ${err.message}`); |
| 65 | + return; |
| 66 | + } |
| 67 | + console.log('Replaced text in proto.js successfully.'); |
| 68 | + }); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +// Execute all steps |
| 73 | +downloadProtoFile(() => { |
| 74 | + generateProtoJS(() => { |
| 75 | + replaceTextInProtoJS(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments