Skip to content

Commit

Permalink
feat: allow passing --keep-case and --force-number to compileProtos (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster authored Feb 13, 2024
1 parent 157ec0d commit 004d112
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
47 changes: 39 additions & 8 deletions tools/src/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ async function buildListOfProtos(
return result;
}

interface CompileProtosOptions {
skipJson?: boolean;
esm?: boolean;
keepCase?: boolean;
forceNumber?: boolean;
}

/**
* Runs `pbjs` to compile the given proto files, placing the result into
* `./protos/protos.json`. No support for changing output filename for now
Expand All @@ -249,10 +256,16 @@ async function buildListOfProtos(
async function compileProtos(
rootName: string,
protos: string[],
skipJson = false,
esm = false
options: CompileProtosOptions
): Promise<void> {
if (!skipJson) {
const extraArgs = [];
if (options.keepCase) {
extraArgs.push('--keep-case');
}
if (options.forceNumber) {
extraArgs.push('--force-number');
}
if (!options.skipJson) {
// generate protos.json file from proto list
const jsonOutput = path.join('protos', 'protos.json');
if (protos.length === 0) {
Expand All @@ -263,6 +276,7 @@ async function compileProtos(
const pbjsArgs4JSON = [
'--target',
'json',
...extraArgs,
'-p',
'protos',
'-p',
Expand All @@ -275,14 +289,15 @@ async function compileProtos(
}

// generate protos/protos.js from protos.json
const jsOutput = esm
const jsOutput = options.esm
? path.join('protos', 'protos.cjs')
: path.join('protos', 'protos.js');
const pbjsArgs4js = [
'-r',
rootName,
'--target',
'static-module',
...extraArgs,
'-p',
'protos',
'-p',
Expand All @@ -298,13 +313,14 @@ async function compileProtos(
await writeFile(jsOutput, jsResult);

let jsOutputEsm;
if (esm) {
if (options.esm) {
jsOutputEsm = path.join('protos', 'protos.js');
const pbjsArgs4jsEsm = [
'-r',
rootName,
'--target',
'static-module',
...extraArgs,
'-p',
'protos',
'-p',
Expand All @@ -324,7 +340,7 @@ async function compileProtos(

// generate protos/protos.d.ts
const tsOutput = path.join('protos', 'protos.d.ts');
const pbjsArgs4ts = [esm ? jsOutputEsm! : jsOutput, '-o', tsOutput];
const pbjsArgs4ts = [options.esm ? jsOutputEsm! : jsOutput, '-o', tsOutput];
await pbtsMain(pbjsArgs4ts);

let tsResult = (await readFile(tsOutput)).toString();
Expand Down Expand Up @@ -372,6 +388,8 @@ export async function main(parameters: string[]): Promise<void> {
const protoJsonFiles: string[] = [];
let skipJson = false;
let esm = false;
let keepCase = false;
let forceNumber = false;
const directories: string[] = [];
for (const parameter of parameters) {
if (parameter === '--skip-json') {
Expand All @@ -382,6 +400,14 @@ export async function main(parameters: string[]): Promise<void> {
esm = true;
continue;
}
if (parameter === '--keep-case') {
keepCase = true;
continue;
}
if (parameter === '--force-number') {
forceNumber = true;
continue;
}
// it's not an option so it's a directory
const directory = parameter;
directories.push(directory);
Expand All @@ -390,10 +416,15 @@ export async function main(parameters: string[]): Promise<void> {
const rootName = await generateRootName(directories);
if (esm) {
const esmProtos = await buildListOfProtos(protoJsonFiles, esm);
await compileProtos(rootName, esmProtos, skipJson, esm);
await compileProtos(rootName, esmProtos, {
skipJson,
esm,
keepCase,
forceNumber,
});
}
const protos = await buildListOfProtos(protoJsonFiles, esm);
await compileProtos(rootName, protos, skipJson, esm);
await compileProtos(rootName, protos, {skipJson, esm, keepCase, forceNumber});
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tools/test/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,48 @@ describe('compileProtos tool', () => {
assert.equal(js.toString().includes(link), false);
}
});

it('converts names to camelCase and uses Long by default', async function () {
this.timeout(20000);
const dirName = path.join(testDir, 'protoLists', 'parameters');
await compileProtos.main([dirName]);
const jsonBuf = await readFile(expectedJsonResultFile);
const json = JSON.parse(jsonBuf.toString());
const js = await readFile(expectedJSResultFile);
assert(json.nested.test.nested.Test.fields.snakeCaseField);
assert(js.includes('@property {number|Long|null} [checkForceNumber]'));
});

it('understands --keep-case', async function () {
this.timeout(20000);
const dirName = path.join(testDir, 'protoLists', 'parameters');
await compileProtos.main(['--keep-case', dirName]);
const jsonBuf = await readFile(expectedJsonResultFile);
const json = JSON.parse(jsonBuf.toString());
const js = await readFile(expectedJSResultFile);
assert(json.nested.test.nested.Test.fields.snake_case_field);
assert(js.includes('@property {number|Long|null} [check_force_number]'));
});

it('understands --force-number', async function () {
this.timeout(20000);
const dirName = path.join(testDir, 'protoLists', 'parameters');
await compileProtos.main(['--force-number', dirName]);
const jsonBuf = await readFile(expectedJsonResultFile);
const json = JSON.parse(jsonBuf.toString());
const js = await readFile(expectedJSResultFile);
assert(json.nested.test.nested.Test.fields.snakeCaseField);
assert(js.includes('@property {number|null} [checkForceNumber]'));
});

it('understands both --keep-case and --force-number', async function () {
this.timeout(20000);
const dirName = path.join(testDir, 'protoLists', 'parameters');
await compileProtos.main(['--keep-case', '--force-number', dirName]);
const jsonBuf = await readFile(expectedJsonResultFile);
const json = JSON.parse(jsonBuf.toString());
const js = await readFile(expectedJSResultFile);
assert(json.nested.test.nested.Test.fields.snake_case_field);
assert(js.includes('@property {number|null} [check_force_number]'));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"../../protos/google/example/library/v1/parameters.proto"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package test;

message Test {
string snake_case_field = 1;
uint64 check_force_number = 2;
}

0 comments on commit 004d112

Please sign in to comment.