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" cd /d "%~dp0..\pb"
where /q protoc || echo Missing protoc && exit /b where /q protoc || echo Missing protoc && exit /b
set gen=..\gen set gen_dir=..\gen
set pbs=bdware/bdledger/api/*.proto set pb_files=bdware/bdledger/api/*.proto
set exec=protoc set emptypb_file=google/protobuf/empty.proto
set cmd=protoc
for %%A in (%*) do ( for %%A in (%*) do (
if "%%A"=="-a" set exec=start /b protoc if "%%A"=="-a" set cmd=start /b !cmd!
if "%%A"=="--async" set exec=start /b protoc if "%%A"=="--async" set cmd=start /b !cmd!
if "%%A"=="go" ( if "%%A"=="go" (
set plugin=protoc-gen-go set plugin=protoc-gen-go
where /q !plugin! || echo Missing !plugin! && exit /b where /q !plugin! || echo Missing !plugin! && exit /b
set vplugin=protoc-gen-govalidators set vplugin=protoc-gen-govalidators
where /q !vplugin! || echo Missing !vplugin! && exit /b where /q !vplugin! || echo Missing !vplugin! && exit /b
set out=%gen%\go set out=%gen_dir%\go
if not exist !out! mkdir !out! if not exist !out! mkdir !out!
echo Generating Go code 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" ( if "%%A"=="gohttp" (
set plugin=protoc-gen-grpc-gateway set plugin=protoc-gen-grpc-gateway
where /q !plugin! || echo Missing !plugin! && exit /b where /q !plugin! || echo Missing !plugin! && exit /b
set out=%gen%\go set out=%gen_dir%\go
if not exist !out! mkdir !out! if not exist !out! mkdir !out!
echo Generating Go HTTP code 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" ( if "%%A"=="nodejs" (
set plugin=grpc_tools_node_protoc_plugin for /f %%i in ('npm bin') do set npmBinPath=%%i
where /q !plugin! || echo missing !plugin! && exit /b set jsCmd=!npmBinPath!\grpc_tools_node_protoc
for /f %%i in ('where !plugin!') do set pluginPath=%%i if "%%A"=="-a" set jsCmd=start /b !cmd!
set out=%gen%\nodejs 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! if not exist !out! mkdir !out!
echo Generating Node.js code echo Generating Node.js code ^& TypeScript definitions
!exec! --js_out=import_style=commonjs,binary:!out! --grpc_out=!out! --plugin=protoc-gen-grpc=!pluginPath! %pbs% google/protobuf/empty.proto !jsCmd! ^
) --plugin=protoc-gen-ts=!pluginPath! ^
if "%%A"=="ts" ( --js_out=import_style=commonjs,binary:!out! ^
set plugin=protoc-gen-ts --grpc_out=grpc_js:!out! ^
where /q !plugin! || echo missing !plugin! && exit /B --ts_out=grpc_js:!out! ^
for /f %%i in ('where !plugin!') do set pluginPath=%%i %pb_files% %emptypb_file%
set out=%gen%\nodejs @echo off
if not exist !out! mkdir !out!
echo Generating TypeScript definitions
!exec! --ts_out=!out! --plugin=protoc-gen-ts=!pluginPath! %pbs% google/protobuf/empty.proto
) )
if "%%A"=="docs" ( if "%%A"=="docs" (
set plugin=protoc-gen-ts set plugin=protoc-gen-ts
@ -52,8 +58,17 @@ for %%A in (%*) do (
set out=..\..\docs set out=..\..\docs
if not exist !out! mkdir !out! if not exist !out! mkdir !out!
echo Generating documentation echo Generating documentation
!exec! --doc_out=!out! --doc_opt=html,apis.html %pbs% google/protobuf/empty.proto !cmd! ^
start /b protoc --doc_out=!out! --doc_opt=markdown,apis.md %pbs% google/protobuf/empty.proto --doc_out=!out! ^
start /b protoc --doc_out=!out! --doc_opt=json,apis.json %pbs% google/protobuf/empty.proto --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" gen_dir="$pb_dir/../gen"
pb_files="bdware/bdledger/api/*.proto" pb_files="bdware/bdledger/api/*.proto"
empty_file="google/protobuf/empty.proto" emptypb_file="google/protobuf/empty.proto"
exec=protoc cmd=protoc
async_tag="" async_tag=""
# check all arguments # check all arguments
@ -46,7 +46,11 @@ do
mkdir -p $out mkdir -p $out
fi fi
echo "Generating Go code" 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 fi
if [ $aug == "gohttp" ]; then if [ $aug == "gohttp" ]; then
plugin=protoc-gen-grpc-gateway plugin=protoc-gen-grpc-gateway
@ -59,33 +63,27 @@ do
mkdir -p $out mkdir -p $out
fi fi
echo "Generating Go HTTP code" 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 fi
if [ $aug == "nodejs" ]; then if [ $aug == "nodejs" ]; then
plugin=grpc_tools_node_protoc_plugin npmBinPath=$(npm bin)
plugin_path=$(which $plugin) jsCmd=$npmBinPath/grpc_tools_node_protoc
if [ $? -ne 0 ];then pluginPath=$npmBinPath/protoc-gen-ts
echo "missing plugin: $plugin" && exit out="$gen_dir/nodejs"
fi if [ ! -d $out ]; then
out="$gen_dir/nodejs" mkdir -p $out
if [ ! -d $out ]; then fi
mkdir -p $out echo "Generating Node.js code & TypeScript definitions"
fi $jsCmd \
echo "Generating Node.js code" --plugin=protoc-gen-ts=$pluginPath \
$exec --js_out=import_style=commonjs,binary:$out --grpc_out=$out --plugin=protoc-gen-grpc=$plugin_path $pb_files $empty_file $async_tag --js_out=import_style=commonjs,binary:$out \
fi --grpc_out=grpc_js:$out \
if [ $aug == "ts" ]; then --ts_out=grpc_js:$out \
plugin=protoc-gen-ts $pb_files $emptypb_file \
plugin_path=$(which $plugin) $async_tag
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
fi fi
if [ $aug == "docs" ]; then if [ $aug == "docs" ]; then
plugin=protoc-gen-ts plugin=protoc-gen-ts
@ -98,8 +96,20 @@ do
mkdir -p $out mkdir -p $out
fi fi
echo "Generating documentations" echo "Generating documentations"
$exec --doc_out=$out --doc_opt=html,apis.html $pb_files $empty_file $async_tag $cmd \
$exec --doc_out=$out --doc_opt=markdown,apis.md $pb_files $empty_file $async_tag --doc_out=$out \
$exec --doc_out=$out --doc_opt=json,apis.json $pb_files $empty_file $async_tag --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 fi
done done