Skip to content

Commit e161ae8

Browse files
author
Alistair Crook
authored
Typescript compilation (#78)
## What is the goal of this PR? Compile protobuf files to typescript rather than raw javascript in the nodejs target. ## What are the changes implemented in this PR? Create genrule calling protoc compiler directly with the appropriate plugins to output both javascript and typescript type definition files when built.
1 parent e462d6b commit e161ae8

File tree

10 files changed

+3386
-16
lines changed

10 files changed

+3386
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ logs/
6666

6767
# Python cache #
6868
__pycache__
69+
70+
# Node Modules #
71+
node_modules

.grabl/automation.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ build:
2222
correctness:
2323
build:
2424
machine: graknlabs-ubuntu-20.04
25+
#NPM packages can't be built under RBE
2526
script: |
26-
bazel build --config=rbe //...
27+
bazel build //...
2728
bazel run @graknlabs_dependencies//tool/checkstyle:test-coverage
2829
bazel test --config=rbe $(bazel query 'kind(checkstyle_test, //...)')
2930
build-dependency:
@@ -42,6 +43,21 @@ build:
4243
export DEPLOY_MAVEN_USERNAME=$REPO_GRAKN_USERNAME
4344
export DEPLOY_MAVEN_PASSWORD=$REPO_GRAKN_PASSWORD
4445
bazel run --define version=$(git rev-parse HEAD) //grpc/java:deploy-maven -- snapshot
46+
deploy-npm-snapshot:
47+
filter:
48+
owner: graknlabs
49+
branch: master
50+
machine: graknlabs-ubuntu-20.04
51+
script: |
52+
curl https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
53+
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
54+
sudo apt update -y
55+
sudo apt install -y expect
56+
export DEPLOY_NPM_USERNAME=$REPO_GRAKN_USERNAME
57+
export DEPLOY_NPM_PASSWORD=$REPO_GRAKN_PASSWORD
58+
export DEPLOY_NPM_EMAIL=$REPO_GRAKN_EMAIL
59+
bazel run --define version=$(git rev-parse HEAD) //grpc/nodejs:deploy-npm -- snapshot
60+
dependencies: [build, build-dependency]
4561

4662
release:
4763
filter:

BUILD

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package(default_visibility = ["//visibility:public"])
1919

20-
exports_files(["VERSION"])
20+
exports_files(["VERSION", "package.json"])
2121
load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
2222
load("@graknlabs_bazel_distribution//github:rules.bzl", "deploy_github")
2323
load("//:deployment.bzl", "deployment")
@@ -28,6 +28,10 @@ checkstyle_test(
2828
"*",
2929
".grabl/automation.yml",
3030
]),
31+
exclude = [
32+
"package-lock.json",
33+
"package.json",
34+
],
3135
license_type = "agpl",
3236
size = "small",
3337
)

WORKSPACE

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
#
1717

18-
workspace(name = "graknlabs_protocol")
18+
workspace(
19+
name = "graknlabs_protocol",
20+
managed_directories = {"@npm": ["node_modules"]},
21+
)
1922

2023
################################
2124
# Load @graknlabs_dependencies #
@@ -109,4 +112,21 @@ maven(artifacts)
109112
load("@graknlabs_bazel_distribution//common:rules.bzl", "workspace_refs")
110113
workspace_refs(
111114
name = "graknlabs_protocol_workspace_refs"
112-
)
115+
)
116+
117+
#########################
118+
# Load NPM dependencies #
119+
#########################
120+
121+
load("@graknlabs_dependencies//builder/nodejs:deps.bzl", nodejs_deps = "deps")
122+
nodejs_deps(["@graknlabs_dependencies//builder/nodejs:remove-node-patches.patch"])
123+
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install")
124+
125+
node_repositories(
126+
preserve_symlinks = False,
127+
)
128+
npm_install(
129+
name = "npm",
130+
package_json = "//:package.json",
131+
package_lock_json = "//:package-lock.json",
132+
)

dependencies/graknlabs/repositories.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def graknlabs_dependencies():
2121
git_repository(
2222
name = "graknlabs_dependencies",
2323
remote = "https://github.com/graknlabs/dependencies",
24-
commit = "f647768040014285e75849feed37d67a2b29260e", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_dependencies
24+
commit = "fb1457c0d7db3a3b2e342e358cc612681410045a", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_dependencies
2525
)

grpc/nodejs/BUILD

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,106 @@
1616
#
1717

1818
package(default_visibility = ["//visibility:public"])
19-
load("@stackb_rules_proto//node:node_grpc_compile.bzl", "node_grpc_compile")
2019
load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
20+
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm", "nodejs_binary")
21+
load("@graknlabs_bazel_distribution//npm:rules.bzl", "assemble_npm", "deploy_npm")
22+
load("@graknlabs_dependencies//distribution:deployment.bzl", "deployment")
23+
load("//:deployment.bzl", github_deployment = "deployment")
2124

22-
node_grpc_compile(
25+
26+
nodejs_binary(
27+
name = "grpc_tools_node_protoc",
28+
data = ["@npm//grpc-tools:grpc-tools"],
29+
entry_point = "@npm//:node_modules/grpc-tools/bin/protoc.js",
30+
templated_args = ["--nobazel_node_patches"],
31+
)
32+
33+
nodejs_binary(
34+
name = "protoc-gen-ts",
35+
data = ["@npm//grpc_tools_node_protoc_ts:grpc_tools_node_protoc_ts"],
36+
entry_point = "@npm//:node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts",
37+
templated_args = ["--nobazel_node_patches"],
38+
)
39+
40+
41+
genrule(
2342
name = "protocol",
43+
outs = [
44+
"protobuf/answer_pb.d.ts",
45+
"protobuf/answer_pb.js",
46+
"protobuf/concept_pb.d.ts",
47+
"protobuf/concept_pb.js",
48+
"protobuf/database_pb.d.ts",
49+
"protobuf/database_pb.js",
50+
"protobuf/grakn_pb.d.ts",
51+
"protobuf/grakn_pb.js",
52+
"protobuf/grakn_grpc_pb.js",
53+
"protobuf/grakn_grpc_pb.d.ts",
54+
"protobuf/options_pb.d.ts",
55+
"protobuf/options_pb.js",
56+
"protobuf/query_pb.d.ts",
57+
"protobuf/query_pb.js",
58+
"protobuf/session_pb.d.ts",
59+
"protobuf/session_pb.js",
60+
"protobuf/transaction_pb.d.ts",
61+
"protobuf/transaction_pb.js",
62+
],
63+
# The below command performs a protoc compilation of our proto files into typescript and javascript. Line by line:
64+
# Symlink the node modules required by the typescript plugin into the directory the command will be run from
65+
# Run the node.js protoc (protocol compiler) with the following flags:
66+
# Use the gen-ts plugin to generate typescript declaration files in addition to javascript
67+
# Output javascript with commonjs style exports, to the genrule output directory.
68+
# Output services to the genrule output directory (without this line, grakn_grpc_pb is omitted)
69+
# Output typescript to (you guessed it) the genrule output directory
70+
# Set the .proto file relative path to the WORKSPACE this command is being run from
71+
# Use the .proto files found in the :proto-raw-buffers filegroup as inputs.
72+
cmd = "ln -s $(execpath //grpc/nodejs:protoc-gen-ts).runfiles/npm/node_modules ./node_modules ;\
73+
$(execpath //grpc/nodejs:grpc_tools_node_protoc) \
74+
--plugin='protoc-gen-ts=$(rootpath @npm//:node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts)' \
75+
--js_out='import_style=commonjs,binary:./$(@D)/' \
76+
--grpc_out='grpc_js:./$(@D)/' \
77+
--ts_out='grpc_js:./$(@D)/' \
78+
--proto_path=`dirname $(execpath //:WORKSPACE)` \
79+
$(execpaths //protobuf:proto-raw-buffers);",
80+
# Dependencies for the above command. the //:WORKSPACE label is required to locate the relative path for the .proto files so they can import each other.
81+
tools = [
82+
"//grpc/nodejs:grpc_tools_node_protoc",
83+
"//grpc/nodejs:protoc-gen-ts",
84+
"@npm//:node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts",
85+
"@npm//grpc_tools_node_protoc_ts",
86+
"@npm//google-protobuf",
87+
"//protobuf:proto-raw-buffers",
88+
"//:WORKSPACE"
89+
],
90+
)
91+
92+
pkg_npm(
93+
name = "protocol-package",
94+
package_name = "graknlabs_protocol",
95+
srcs = glob([
96+
"package.json",
97+
]),
2498
deps = [
25-
"//protobuf:answer-proto",
26-
"//protobuf:concept-proto",
27-
"//protobuf:database-proto",
28-
"//protobuf:grakn-proto",
29-
"//protobuf:options-proto",
30-
"//protobuf:query-proto",
31-
"//protobuf:session-proto",
32-
"//protobuf:transaction-proto",
33-
]
99+
":protocol",
100+
],
101+
)
102+
103+
assemble_npm(
104+
name = "assemble-npm",
105+
target = ":protocol-package",
106+
)
107+
108+
deploy_npm(
109+
name = "deploy-npm",
110+
target = ":assemble-npm",
111+
snapshot = deployment["npm.snapshot"],
112+
release = deployment["npm.release"],
34113
)
35114

36115
checkstyle_test(
37116
name = "checkstyle",
38117
include = glob(["*"]),
118+
exclude = ["package.json"],
39119
license_type = "agpl",
40120
size = "small",
41121
)

grpc/nodejs/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "graknlabs-grpc-protocol",
3+
"version": "2.0.0-alpha",
4+
"description": "GRPC Protocol for Grakn Client",
5+
"author": "Grakn Labs",
6+
"license": "Apache-2.0",
7+
"homepage": "https://grakn.ai",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/graknlabs/protocol"
11+
},
12+
"dependencies": {
13+
"@grpc/grpc-js": "1.2.1"
14+
},
15+
"devDependencies": {
16+
"@bazel/typescript": "2.3.1",
17+
"grpc-tools": "1.10.0",
18+
"grpc_tools_node_protoc_ts": "5.0.1",
19+
"typescript": "3.9.7"
20+
}
21+
}

0 commit comments

Comments
 (0)