feat(grpc): add npm package, update Node.js & TypeScript gRPC code and gen scripts

This commit is contained in:
Nex 2020-11-17 00:43:10 +08:00
parent 0845a71aba
commit a6d6428393
4 changed files with 2903 additions and 59 deletions

2799
grpc/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
grpc/package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "@bdledger/api-grpc",
"version": "0.0.1-alpha.0",
"description": "BDLedger gRPC API for Node.js",
"private": true,
"repository": {
"type": "git",
"url": "ssh://vcs@phabricator.internetapi.cn/source/bdledger-apis.git"
},
"keywords": [
"BDLedger",
"API",
"gRPC",
"Node.js"
],
"devDependencies": {
"grpc-tools": "^1.9.1",
"grpc_tools_node_protoc_ts": "^5.0.1"
}
}

View File

@ -3,48 +3,54 @@ setlocal EnableDelayedExpansion
cd /d "%~dp0..\pb"
where /q protoc || echo Missing protoc && exit /b
set gen=..\gen
set pbs=bdware/bdledger/api/*.proto
set exec=protoc
set gen_dir=..\gen
set pb_files=bdware/bdledger/api/*.proto
set emptypb_file=google/protobuf/empty.proto
set cmd=protoc
for %%A in (%*) do (
if "%%A"=="-a" set exec=start /b protoc
if "%%A"=="--async" set exec=start /b protoc
if "%%A"=="-a" set cmd=start /b !cmd!
if "%%A"=="--async" set cmd=start /b !cmd!
if "%%A"=="go" (
set plugin=protoc-gen-go
where /q !plugin! || echo Missing !plugin! && exit /b
set vplugin=protoc-gen-govalidators
where /q !vplugin! || echo Missing !vplugin! && exit /b
set out=%gen%\go
set out=%gen_dir%\go
if not exist !out! mkdir !out!
echo Generating Go code
!exec! --go_out=!out! --go-grpc_out=!out! --govalidators_out=!out! %pbs%
!cmd! ^
--go_out=!out! ^
--go-grpc_out=!out! ^
--govalidators_out=!out! ^
%pb_files%
)
if "%%A"=="gohttp" (
set plugin=protoc-gen-grpc-gateway
where /q !plugin! || echo Missing !plugin! && exit /b
set out=%gen%\go
set out=%gen_dir%\go
if not exist !out! mkdir !out!
echo Generating Go HTTP code
!exec! --grpc-gateway_out=grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml,logtostderr=true:!out! %pbs%
!cmd! ^
--grpc-gateway_out=grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml,logtostderr=true:!out! ^
%pb_files%
)
if "%%A"=="nodejs" (
set plugin=grpc_tools_node_protoc_plugin
where /q !plugin! || echo missing !plugin! && exit /b
for /f %%i in ('where !plugin!') do set pluginPath=%%i
set out=%gen%\nodejs
for /f %%i in ('npm bin') do set npmBinPath=%%i
set jsCmd=!npmBinPath!\grpc_tools_node_protoc
if "%%A"=="-a" set jsCmd=start /b !cmd!
if "%%A"=="--async" set jsCmd=start /b !cmd!
set pluginPath=!npmBinPath!\protoc-gen-ts.cmd
set out=%gen_dir%\nodejs
if not exist !out! mkdir !out!
echo Generating Node.js code
!exec! --js_out=import_style=commonjs,binary:!out! --grpc_out=!out! --plugin=protoc-gen-grpc=!pluginPath! %pbs% google/protobuf/empty.proto
)
if "%%A"=="ts" (
set plugin=protoc-gen-ts
where /q !plugin! || echo missing !plugin! && exit /B
for /f %%i in ('where !plugin!') do set pluginPath=%%i
set out=%gen%\nodejs
if not exist !out! mkdir !out!
echo Generating TypeScript definitions
!exec! --ts_out=!out! --plugin=protoc-gen-ts=!pluginPath! %pbs% google/protobuf/empty.proto
echo Generating Node.js code ^& TypeScript definitions
!jsCmd! ^
--plugin=protoc-gen-ts=!pluginPath! ^
--js_out=import_style=commonjs,binary:!out! ^
--grpc_out=grpc_js:!out! ^
--ts_out=grpc_js:!out! ^
%pb_files% %emptypb_file%
@echo off
)
if "%%A"=="docs" (
set plugin=protoc-gen-ts
@ -52,8 +58,17 @@ for %%A in (%*) do (
set out=..\..\docs
if not exist !out! mkdir !out!
echo Generating documentation
!exec! --doc_out=!out! --doc_opt=html,apis.html %pbs% google/protobuf/empty.proto
start /b protoc --doc_out=!out! --doc_opt=markdown,apis.md %pbs% google/protobuf/empty.proto
start /b protoc --doc_out=!out! --doc_opt=json,apis.json %pbs% google/protobuf/empty.proto
!cmd! ^
--doc_out=!out! ^
--doc_opt=html,apis.html ^
%pb_files% %emptypb_file%
start /b protoc ^
--doc_out=!out! ^
--doc_opt=markdown,apis.md ^
%pb_files% %emptypb_file%
start /b protoc ^
--doc_out=!out! ^
--doc_opt=json,apis.json ^
%pb_files% %emptypb_file%
)
)

View File

@ -16,9 +16,9 @@ fi
gen_dir="$pb_dir/../gen"
pb_files="bdware/bdledger/api/*.proto"
empty_file="google/protobuf/empty.proto"
emptypb_file="google/protobuf/empty.proto"
exec=protoc
cmd=protoc
async_tag=""
# check all arguments
@ -46,7 +46,11 @@ do
mkdir -p $out
fi
echo "Generating Go code"
$exec --go_out=$out --go-grpc_out=$out --govalidators_out=$out $pb_files $async_tag
$cmd \
--go_out=$out \
--go-grpc_out=$out \
$pb_files \
$async_tag
fi
if [ $aug == "gohttp" ]; then
plugin=protoc-gen-grpc-gateway
@ -59,33 +63,27 @@ do
mkdir -p $out
fi
echo "Generating Go HTTP code"
$exec --grpc-gateway_out=grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml,logtostderr=true:$out $pb_files $async_tag
$cmd \
--grpc-gateway_out=grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml,logtostderr=true:$out \
$pb_files \
$async_tag
fi
if [ $aug == "nodejs" ]; then
plugin=grpc_tools_node_protoc_plugin
plugin_path=$(which $plugin)
if [ $? -ne 0 ];then
echo "missing plugin: $plugin" && exit
fi
out="$gen_dir/nodejs"
if [ ! -d $out ]; then
mkdir -p $out
fi
echo "Generating Node.js code"
$exec --js_out=import_style=commonjs,binary:$out --grpc_out=$out --plugin=protoc-gen-grpc=$plugin_path $pb_files $empty_file $async_tag
fi
if [ $aug == "ts" ]; then
plugin=protoc-gen-ts
plugin_path=$(which $plugin)
if [ $? -ne 0 ];then
echo "missing plugin: $plugin" && exit
fi
out="$gen_dir/nodejs"
if [ ! -d $out ]; then
mkdir -p $out
fi
echo "Generating TypeScript code"
$exec --ts_out=$out -plugin=protoc-gen-ts=$plugin_path $pb_files $empty_file $async_tag
npmBinPath=$(npm bin)
jsCmd=$npmBinPath/grpc_tools_node_protoc
pluginPath=$npmBinPath/protoc-gen-ts
out="$gen_dir/nodejs"
if [ ! -d $out ]; then
mkdir -p $out
fi
echo "Generating Node.js code & TypeScript definitions"
$jsCmd \
--plugin=protoc-gen-ts=$pluginPath \
--js_out=import_style=commonjs,binary:$out \
--grpc_out=grpc_js:$out \
--ts_out=grpc_js:$out \
$pb_files $emptypb_file \
$async_tag
fi
if [ $aug == "docs" ]; then
plugin=protoc-gen-ts
@ -98,8 +96,20 @@ do
mkdir -p $out
fi
echo "Generating documentations"
$exec --doc_out=$out --doc_opt=html,apis.html $pb_files $empty_file $async_tag
$exec --doc_out=$out --doc_opt=markdown,apis.md $pb_files $empty_file $async_tag
$exec --doc_out=$out --doc_opt=json,apis.json $pb_files $empty_file $async_tag
$cmd \
--doc_out=$out \
--doc_opt=html,apis.html \
$pb_files $emptypb_file \
$async_tag
$cmd \
--doc_out=$out \
--doc_opt=markdown,apis.md \
$pb_files $emptypb_file \
$async_tag
$cmd \
--doc_out=$out \
--doc_opt=json,apis.json \
$pb_files $emptypb_file \
$async_tag
fi
done
done