Skip to content
Permalink
Browse files

feat(enums): generate static enums for use without grpc

  • Loading branch information...
kritzware committed Mar 6, 2019
1 parent 5317f14 commit 008cddaf15bc1623176b87936fa2291f50fdbd36
Showing with 2,817 additions and 4 deletions.
  1. +2 −1 .npmignore
  2. +13 −1 Makefile
  3. +1 −0 package.json
  4. +48 −0 scripts/generate-enums.js
  5. +1 −1 src/index.ts
  6. +2,751 −0 src/lib/enums.ts
  7. +1 −1 yarn.lock
@@ -12,4 +12,5 @@ build/docs
coverage
.nyc_output
*.log
googleapis
googleapis
scripts
@@ -9,15 +9,27 @@ PROTOC_GEN_GRPC_PATH=./node_modules/grpc-tools/bin/grpc_node_plugin
PROTO_ROOT_DIR=googleapis/
PROTO_SRC_DIR=google/ads/googleads/$(ADS_VERSION)/**/*.proto
PROTO_SRC_DEPENDENCIES=google/**/*.proto
PROTO_ENUMS_ONLY=$(PROTO_ROOT_DIR)/google/ads/googleads/$(ADS_VERSION)/enums/*.proto

# Directory to write generated code to (.js and .d.ts files)
OUT_DIR=src/protos
# Proto compiled enum filepath
OUT_COMPILED_ENUMS=compiled-enums.json
# Static enum filepath (ts)
OUT_STATIC_TS_ENUMS=src/lib/enums.ts

.SILENT: protos

protos: clean compile-protos
$(MAKE) enums
echo "finished all"

enums:
pbjs -t json $(PROTO_ENUMS_ONLY) > ./scripts/$(OUT_COMPILED_ENUMS)
node ./scripts/generate-enums.js $(OUT_COMPILED_ENUMS) $(ADS_VERSION) $(OUT_STATIC_TS_ENUMS)
rm ./scripts/$(OUT_COMPILED_ENUMS)

# TODO: These proto compilation steps could be cleaned up and moved to a bash script
compile-protos:
# Compile depedency protos
# Temporary workaround for https://github.com/protocolbuffers/protobuf/issues/5318
@@ -61,4 +73,4 @@ clean:
rm -rf $(OUT_DIR)/*
mkdir -p $(OUT_DIR)

.PHONY: protos
.PHONY: protos enums
@@ -44,6 +44,7 @@
"npm-run-all": "^4.1.5",
"nyc": "^13.1.0",
"prettier": "^1.15.2",
"protobufjs": "^6.8.8",
"standard-version": "^4.4.0",
"trash-cli": "^1.4.0",
"ts-jest": "^23.10.5",
@@ -0,0 +1,48 @@
const fs = require("fs");

const COMPILED_ENUMS_FILEPATH = process.argv[2];
const API_VERSION = process.argv[3];
const OUT_FILE = process.argv[4];

const file = require(__dirname + `/${COMPILED_ENUMS_FILEPATH}`);
const enums = file.nested.google.nested.ads.nested.googleads.nested.v1.nested.enums.nested;
const stream = fs.createWriteStream(OUT_FILE);

/*
Here we manually build Typescript enums that have no dependencies e.g. grpc messages, objects etc.
This means these specific enums can be imported in web or non-node environments.
The enum import name is also flattened, to reduce verbosity
e.g. "AccountBudgetProposalTypeEnum.AccountBudgetProposalType"
becomes just "AccountBudgetProposalType"
*/

stream.write("/* Autogenerated File! Do Not Edit. */\n");
const allNames = [];

for (const name in enums) {
const enumDef = enums[name].nested[name.replace("Enum", "")].values;
allNames.push(name.replace("Enum", ""));

let e = `\n`;

for (const key in enumDef) {
const value = enumDef[key];
e += `"${key}" = ${value},\n`;
}

stream.write(`\n
/**
* @name ${name}.${name.replace("Enum", "")}
*/
enum ${name.replace("Enum", "")} { ${e} }
\n`);
}

const allEnumsSingleExport = `
/* Additionally, all enums are exported in a single object called "enums" */
export const StaticEnums = {\n${allNames.map(name => `${name}\n`)}\n}`;

stream.write(allEnumsSingleExport);

stream.end();
console.log(`Finished writing enums to ${OUT_FILE}`);
@@ -1,3 +1,3 @@
export * from "./lib/client";
export * from "./lib/types";
export { formatCallResults } from "./lib/utils";
export * from "./lib/enums";

0 comments on commit 008cdda

Please sign in to comment.
You can’t perform that action at this time.