Skip to content

Commit ed041ac

Browse files
committed
use protobufjs with modifications for [json_name]
1 parent 9b6a671 commit ed041ac

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
"scripts": {
33-
"proto": "node_modules/protobufjs-cli/bin/pbjs --keep-case -t static-module -o proto.js libpg_query/protobuf/pg_query.proto",
33+
"protogen": "node ./script/codegen.js 16-latest",
3434
"clean": "rimraf build",
3535
"configure": "node-pre-gyp configure",
3636
"install": "node-pre-gyp install --fallback-to-build --loglevel verbose",
@@ -56,12 +56,12 @@
5656
"emnapi": "^0.43.1",
5757
"lodash": "^4.17.15",
5858
"mocha": "^5.2.0",
59-
"protobufjs-cli": "https://gitpkg.now.sh/michaelbgreen/protobuf.js/cli?6254efb1bd9263978e9a90da3b05c4ccbcd6564a"
59+
"@pgsql/protobufjs-cli": "1.1.5"
6060
},
6161
"dependencies": {
6262
"@emnapi/runtime": "^0.43.1",
6363
"@mapbox/node-pre-gyp": "^1.0.8",
64-
"protobufjs": "git+https://github.com/michaelbgreen/protobuf.js.git#c0b68ac5c8fbb1ded261c0924bbce0538370333e"
64+
"@pgsql/protobufjs": "7.2.6",
6565
"node-addon-api": "^7.0.0",
6666
"node-gyp": "^10.0.1"
6767
},

script/codegen.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)