FRONTEND-484: New thrift gen config (#254)

This commit is contained in:
Ildar Galeev 2021-03-31 14:05:35 +03:00 committed by GitHub
parent d5b90f6c1d
commit fd7dc44b32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 572 additions and 101 deletions

2
.gitignore vendored
View File

@ -39,7 +39,9 @@ testem.log
Thumbs.db
/src/assets/meta-*.json
/src/assets/api-meta/
/src/app/thrift-services/**/gen-*/
/src/app/api/**/gen-*/
# Sonar
.scannerwork

View File

@ -44,6 +44,7 @@ init:
compile:
npm run compile-thrift
npm run compile-thrift-depricated
build: check lint compile
npm run build

330
package-lock.json generated
View File

@ -1298,6 +1298,19 @@
"@babel/helper-validator-identifier": "^7.10.4",
"chalk": "^2.0.0",
"js-tokens": "^4.0.0"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
"convert-source-map": {
@ -1500,6 +1513,19 @@
"@babel/helper-validator-identifier": "^7.10.4",
"chalk": "^2.0.0",
"js-tokens": "^4.0.0"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
"@babel/template": {
@ -1685,6 +1711,17 @@
"js-tokens": "^4.0.0"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
},
"js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
@ -2443,6 +2480,19 @@
"@babel/helper-validator-identifier": "^7.10.4",
"chalk": "^2.0.0",
"js-tokens": "^4.0.0"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
"js-tokens": {
@ -2499,6 +2549,19 @@
"@babel/helper-validator-identifier": "^7.10.4",
"chalk": "^2.0.0",
"js-tokens": "^4.0.0"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
"debug": {
@ -5670,13 +5733,54 @@
"dev": true
},
"chalk": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
"integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==",
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz",
"integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"dependencies": {
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dev": true,
"requires": {
"color-convert": "^2.0.1"
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"requires": {
"color-name": "~1.1.4"
}
},
"color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"dev": true
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true,
"requires": {
"has-flag": "^4.0.0"
}
}
}
},
"chardet": {
@ -5896,6 +6000,17 @@
"resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.4.tgz",
"integrity": "sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==",
"dev": true
},
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
@ -8047,24 +8162,38 @@
"dev": true
},
"eslint-plugin-react": {
"version": "7.22.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-react/-/eslint-plugin-react-7.22.0.tgz",
"integrity": "sha512-p30tuX3VS+NWv9nQot9xIGAHBXR0+xJVaZriEsHoJrASGCJZDJ8JLNM0YqKqI0AKm6Uxaa1VUHoNEibxRCMQHA==",
"version": "7.23.1",
"resolved": "https://registry.npmjs.org/eslint-plugin-react/-/eslint-plugin-react-7.23.1.tgz",
"integrity": "sha512-MvFGhZjI8Z4HusajmSw0ougGrq3Gs4vT/0WgwksZgf5RrLrRa2oYAw56okU4tZJl8+j7IYNuTM+2RnFEuTSdRQ==",
"dev": true,
"requires": {
"array-includes": "^3.1.1",
"array.prototype.flatmap": "^1.2.3",
"array-includes": "^3.1.3",
"array.prototype.flatmap": "^1.2.4",
"doctrine": "^2.1.0",
"has": "^1.0.3",
"jsx-ast-utils": "^2.4.1 || ^3.0.0",
"object.entries": "^1.1.2",
"object.fromentries": "^2.0.2",
"object.values": "^1.1.1",
"minimatch": "^3.0.4",
"object.entries": "^1.1.3",
"object.fromentries": "^2.0.4",
"object.values": "^1.1.3",
"prop-types": "^15.7.2",
"resolve": "^1.18.1",
"string.prototype.matchall": "^4.0.2"
"resolve": "^2.0.0-next.3",
"string.prototype.matchall": "^4.0.4"
},
"dependencies": {
"array-includes": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.3.tgz",
"integrity": "sha512-gcem1KlBU7c9rB+Rq8/3PPKsK2kjqeEBa3bD5kkQo4nYlOHQCJqIJFqBXDEfwaRuYTT4E+FxA9xez7Gf/e3Q7A==",
"dev": true,
"requires": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.3",
"es-abstract": "^1.18.0-next.2",
"get-intrinsic": "^1.1.1",
"is-string": "^1.0.5"
}
},
"doctrine": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz",
@ -8074,15 +8203,122 @@
"esutils": "^2.0.2"
}
},
"es-abstract": {
"version": "1.18.0",
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0.tgz",
"integrity": "sha512-LJzK7MrQa8TS0ja2w3YNLzUgJCGPdPOV1yVvezjNnS89D+VR08+Szt2mz3YB2Dck/+w5tfIq/RoUAFqJJGM2yw==",
"dev": true,
"requires": {
"call-bind": "^1.0.2",
"es-to-primitive": "^1.2.1",
"function-bind": "^1.1.1",
"get-intrinsic": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.2",
"is-callable": "^1.2.3",
"is-negative-zero": "^2.0.1",
"is-regex": "^1.1.2",
"is-string": "^1.0.5",
"object-inspect": "^1.9.0",
"object-keys": "^1.1.1",
"object.assign": "^4.1.2",
"string.prototype.trimend": "^1.0.4",
"string.prototype.trimstart": "^1.0.4",
"unbox-primitive": "^1.0.0"
}
},
"get-intrinsic": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.1.tgz",
"integrity": "sha512-kWZrnVM42QCiEA2Ig1bG8zjoIMOgxWwYCEeNdwY6Tv/cOSeGpcoX4pXHfKUxNKVoArnrEr2e9srnAxxGIraS9Q==",
"dev": true,
"requires": {
"function-bind": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.1"
}
},
"has-symbols": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.2.tgz",
"integrity": "sha512-chXa79rL/UC2KlX17jo3vRGz0azaWEx5tGqZg5pO3NUyEJVB17dMruQlzCCOfUvElghKcm5194+BCRvi2Rv/Gw==",
"dev": true
},
"is-callable": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.3.tgz",
"integrity": "sha512-J1DcMe8UYTBSrKezuIUTUwjXsho29693unXM2YhJUTR2txK/eG47bvNa/wipPFmZFgr/N6f1GA66dv0mEyTIyQ==",
"dev": true
},
"is-regex": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.2.tgz",
"integrity": "sha512-axvdhb5pdhEVThqJzYXwMlVuZwC+FF2DpcOhTS+y/8jVq4trxyPgfcwIxIKiyeuLlSQYKkmUaPQJ8ZE4yNKXDg==",
"dev": true,
"requires": {
"call-bind": "^1.0.2",
"has-symbols": "^1.0.1"
}
},
"object-inspect": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.9.0.tgz",
"integrity": "sha512-i3Bp9iTqwhaLZBxGkRfo5ZbE07BQRT7MGu8+nNgwW9ItGp1TzCTw2DLEoWwjClxBjOFI/hWljTAmYGCEwmtnOw==",
"dev": true
},
"object.assign": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.2.tgz",
"integrity": "sha512-ixT2L5THXsApyiUPYKmW+2EHpXXe5Ii3M+f4e+aJFAHao5amFRW6J0OO6c/LU8Be47utCx2GL89hxGB6XSmKuQ==",
"dev": true,
"requires": {
"call-bind": "^1.0.0",
"define-properties": "^1.1.3",
"has-symbols": "^1.0.1",
"object-keys": "^1.1.1"
}
},
"object.values": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/object.values/-/object.values-1.1.3.tgz",
"integrity": "sha512-nkF6PfDB9alkOUxpf1HNm/QlkeW3SReqL5WXeBLpEJJnlPSvRaDQpW3gQTksTN3fgJX4hL42RzKyOin6ff3tyw==",
"dev": true,
"requires": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.3",
"es-abstract": "^1.18.0-next.2",
"has": "^1.0.3"
}
},
"resolve": {
"version": "1.20.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz",
"integrity": "sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==",
"version": "2.0.0-next.3",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-2.0.0-next.3.tgz",
"integrity": "sha512-W8LucSynKUIDu9ylraa7ueVZ7hc0uAgJBxVsQSKOXOyle8a93qXhcz+XAXZ8bIq2d6i4Ehddn6Evt+0/UwKk6Q==",
"dev": true,
"requires": {
"is-core-module": "^2.2.0",
"path-parse": "^1.0.6"
}
},
"string.prototype.trimend": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.4.tgz",
"integrity": "sha512-y9xCjw1P23Awk8EvTpcyL2NIr1j7wJ39f+k6lvRnSMz+mz9CGz9NYPelDk42kOz6+ql8xjfK8oYzy3jAP5QU5A==",
"dev": true,
"requires": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.3"
}
},
"string.prototype.trimstart": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.4.tgz",
"integrity": "sha512-jh6e984OBfvxS50tdY2nRZnoC5/mLFKOREQfw8t5yytkoUsJRNxvI/E39qu1sD0OtWI3OC0XgKSmcWwziwYuZw==",
"dev": true,
"requires": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.3"
}
}
}
},
@ -16707,6 +16943,19 @@
"dev": true,
"requires": {
"chalk": "^2.0.1"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
"split-string": {
@ -17384,6 +17633,19 @@
"stable": "^0.1.8",
"unquote": "~1.1.1",
"util.promisify": "~1.0.0"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
}
}
},
"symbol-observable": {
@ -18472,15 +18734,23 @@
}
},
"unbox-primitive": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.0.tgz",
"integrity": "sha512-P/51NX+JXyxK/aigg1/ZgyccdAxm5K1+n8+tvqSntjOivPt19gvm1VC49RWYetsiub8WViUchdxl/KWHHB0kzA==",
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz",
"integrity": "sha512-tZU/3NqK3dA5gpE1KtyiJUrEB0lxnGkMFHptJ7q6ewdZ8s12QrODwNbhIJStmJkd1QDXa1NRA8aF2A1zk/Ypyw==",
"dev": true,
"requires": {
"function-bind": "^1.1.1",
"has-bigints": "^1.0.0",
"has-symbols": "^1.0.0",
"which-boxed-primitive": "^1.0.1"
"has-bigints": "^1.0.1",
"has-symbols": "^1.0.2",
"which-boxed-primitive": "^1.0.2"
},
"dependencies": {
"has-symbols": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.2.tgz",
"integrity": "sha512-chXa79rL/UC2KlX17jo3vRGz0azaWEx5tGqZg5pO3NUyEJVB17dMruQlzCCOfUvElghKcm5194+BCRvi2Rv/Gw==",
"dev": true
}
}
},
"unc-path-regex": {
@ -19292,6 +19562,16 @@
"yargs": "^12.0.1"
},
"dependencies": {
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
},
"find-up": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz",

View File

@ -12,6 +12,7 @@
"prettier:fix": "npm run prettier-preset -- --write",
"fix": "npm run lint:fix; npm run prettier:fix",
"e2e": "ng e2e",
"compile-thrift-depricated": "ts-node --project tools/tsconfig.json tools/compile-thrift-depricated",
"compile-thrift": "ts-node --project tools/tsconfig.json tools/compile-thrift",
"postinstall": "ngcc"
},
@ -83,6 +84,7 @@
"@types/uuid": "~3.4.3",
"@typescript-eslint/eslint-plugin": "4.3.0",
"@typescript-eslint/parser": "4.3.0",
"chalk": "^4.1.0",
"codelyzer": "^6.0.0",
"del": "^5.1.0",
"eslint": "^7.6.0",

0
src/app/api/.gitkeep Normal file
View File

View File

@ -8,5 +8,31 @@
"ank": "./node_modules/ank-proto/proto",
"skipper": { "path": "./node_modules/skipper-proto/proto", "meta": true },
"deanonimus": { "path": "./node_modules/deanonimus-proto/proto", "meta": true }
},
"config": [
{
"namespace": "fistful",
"baseUrl": "./node_modules/fistful-proto/proto",
"services": [
{
"outputFolder": "wallet",
"definitionFile": "wallet.thrift"
},
{
"outputFolder": "deposit",
"definitionFile": "deposit.thrift"
}
]
},
{
"namespace": "damsel",
"baseUrl": "./node_modules/damsel/proto",
"services": [
{
"outputFolder": "domain-config",
"definitionFile": "domain_config.thrift"
}
]
}
]
}

View File

@ -0,0 +1,103 @@
import { exec } from 'child_process';
import * as del from 'del';
import { glob } from 'glob';
import * as path from 'path';
import * as config from '../thrift-config.json';
const ROOT_DIR = path.join(__dirname, '..');
const THRIFT_PATH = 'woody_js/dist/thrift';
const OUTPUT_PATH = './src/app/thrift-services';
const GEN_MODEL_DIR = 'gen-model';
const GEN_CLIENT_DIR = 'gen-nodejs';
const META_PATH = 'src/assets';
async function execWithLog(cmd: string) {
console.log(`> ${cmd}`);
return await new Promise<string>((res, rej) =>
exec(
cmd,
{
cwd: ROOT_DIR,
},
(error, stdout, stderr) => {
if (error === null) {
console.log(stderr);
res(stdout);
} else {
console.error(error);
console.error(stderr);
rej(error);
}
}
)
);
}
async function genClient(name: string, thriftPath: string) {
const out = path.join(OUTPUT_PATH, name);
await del([path.join(OUTPUT_PATH, GEN_CLIENT_DIR)]);
return await execWithLog(
`thrift -r -gen js:node,runtime_package=${THRIFT_PATH} -o ${out} ${thriftPath};`
);
}
async function genModel(name: string, protoPath: string) {
const out = path.join(OUTPUT_PATH, name, GEN_MODEL_DIR);
await del([path.join(OUTPUT_PATH, GEN_MODEL_DIR)]);
return await execWithLog(`thrift-ts ${protoPath} -o ${out} -d false`);
}
async function genMeta(name: string, protoPath: string) {
const out = path.join(META_PATH, `meta-${name}.json`);
await del([out]);
return await execWithLog(`thrift-ts ${protoPath} -o ${out} --json --pack --prettify`);
}
async function compileProto(protoName: string, proto: string | { path: string; meta: boolean }) {
let protoPath: string;
let withMeta = false;
if (typeof proto === 'object') {
protoPath = proto.path;
withMeta = proto.meta;
} else {
protoPath = proto;
}
const globPattern = path.join(`${protoPath}/**/*.thrift`);
const thriftFiles = await new Promise<string[]>((res, rej) =>
glob(globPattern, {}, (err, files) => {
if (err) {
rej(err);
}
res(files);
})
);
console.log(`Compile ${protoName}: ${protoPath}`);
const genList: Promise<any>[] = [];
genList.push(genModel(protoName, protoPath));
if (withMeta) {
genList.push(genMeta(protoName, protoPath));
}
for (const f of thriftFiles) {
genList.push(genClient(protoName, f));
}
await Promise.all(genList);
}
async function compileProtos() {
try {
await Promise.all(
Object.entries(config.proto).map(([protoName, proto]) => compileProto(protoName, proto))
);
console.log('Generated');
} catch (e) {
console.error(e);
console.error('Not generated');
throw e;
}
}
compileProtos();

View File

@ -1,103 +1,160 @@
import { exec } from 'child_process';
import * as del from 'del';
import { glob } from 'glob';
import * as path from 'path';
import * as fs from 'fs';
import * as chalk from 'chalk';
import * as del from 'del';
import * as config from '../thrift-config.json';
import * as configDefinition from '../thrift-config.json';
const ROOT_DIR = path.join(__dirname, '..');
const THRIFT_PATH = 'woody_js/dist/thrift';
const log = console.log;
const error = console.error;
const OUTPUT_PATH = './src/app/thrift-services';
const GEN_MODEL_DIR = 'gen-model';
const GEN_CLIENT_DIR = 'gen-nodejs';
const META_PATH = 'src/assets';
interface ServiceDefinition {
definitionFile: string;
outputFolder: string;
}
async function execWithLog(cmd: string) {
console.log(`> ${cmd}`);
interface NamespaceDefinition {
namespace: string;
baseUrl: string;
services: ServiceDefinition[];
}
interface PathsConfig {
outputNamespacePath: string;
model: {
definitionsFolder: string;
outputFolder: string;
};
services: {
definitionFile: string;
outputFolder: string;
}[];
meta: {
definitionsFolder: string;
outputFile: string;
};
}
async function execute(cmd: string, cwd = path.join(__dirname, '..')) {
return await new Promise<string>((res, rej) =>
exec(
cmd,
{
cwd: ROOT_DIR,
cwd,
},
(error, stdout, stderr) => {
if (error === null) {
console.log(stderr);
res(stdout);
} else {
console.error(error);
console.error(stderr);
rej(error);
rej(stderr);
}
}
)
);
}
async function genClient(name: string, thriftPath: string) {
const out = path.join(OUTPUT_PATH, name);
await del([path.join(OUTPUT_PATH, GEN_CLIENT_DIR)]);
return await execWithLog(
`thrift -r -gen js:node,runtime_package=${THRIFT_PATH} -o ${out} ${thriftPath};`
);
function mkdirIfNotExist(path: string) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
}
async function genModel(name: string, protoPath: string) {
const out = path.join(OUTPUT_PATH, name, GEN_MODEL_DIR);
await del([path.join(OUTPUT_PATH, GEN_MODEL_DIR)]);
return await execWithLog(`thrift-ts ${protoPath} -o ${out} -d false`);
}
async function genMeta(name: string, protoPath: string) {
const out = path.join(META_PATH, `meta-${name}.json`);
await del([out]);
return await execWithLog(`thrift-ts ${protoPath} -o ${out} --json --pack --prettify`);
}
async function compileProto(protoName: string, proto: string | { path: string; meta: boolean }) {
let protoPath: string;
let withMeta = false;
if (typeof proto === 'object') {
protoPath = proto.path;
withMeta = proto.meta;
} else {
protoPath = proto;
}
const globPattern = path.join(`${protoPath}/**/*.thrift`);
const thriftFiles = await new Promise<string[]>((res, rej) =>
glob(globPattern, {}, (err, files) => {
if (err) {
rej(err);
}
res(files);
})
);
console.log(`Compile ${protoName}: ${protoPath}`);
const genList: Promise<any>[] = [];
genList.push(genModel(protoName, protoPath));
if (withMeta) {
genList.push(genMeta(protoName, protoPath));
}
for (const f of thriftFiles) {
genList.push(genClient(protoName, f));
}
await Promise.all(genList);
}
async function compileProtos() {
async function compileTsDefinitions(definitionsPath: string, outputPath: string) {
try {
await Promise.all(
Object.entries(config.proto).map(([protoName, proto]) => compileProto(protoName, proto))
);
console.log('Generated');
} catch (e) {
console.error(e);
console.error('Not generated');
throw e;
log('Compiling typescript definition');
await execute(`thrift-ts ${definitionsPath} -o ${outputPath} -d false`);
} catch (err) {
log(`Typescript definition ${chalk.red('compilation failed')}`);
throw err;
}
}
compileProtos();
async function compileJsonMetadata(definitionsPath: string, outputFilePath: string) {
try {
log('Compiling JSON metadata');
await execute(`thrift-ts ${definitionsPath} -o ${outputFilePath} --json --pack --prettify`);
} catch (err) {
log(`JSON metadata ${chalk.red('compilation failed')}`);
throw err;
}
}
async function compileService(definitionFilePath: string, outputPath: string) {
try {
log(`Compiling service: ${definitionFilePath}`);
await execute(
`thrift -r -gen js:node,runtime_package=woody_js/dist/thrift -o ${outputPath} ${definitionFilePath};`
);
} catch (err) {
log(`Service: ${definitionFilePath} ${chalk.red('compilation failed')}`);
throw err;
}
}
function toPathConfig(
{ baseUrl, namespace, services }: NamespaceDefinition,
baseOutputPath = 'src/app/api',
outputModelDirName = 'gen-model',
baseOutputMetaPath = 'src/assets/api-meta'
): PathsConfig {
const outputNamespacePath = path.join(baseOutputPath, namespace);
return {
outputNamespacePath,
model: {
definitionsFolder: baseUrl,
outputFolder: path.join(outputNamespacePath, outputModelDirName),
},
services: services.map(({ definitionFile, outputFolder }) => ({
definitionFile: path.join(baseUrl, definitionFile),
outputFolder: path.join(outputNamespacePath, outputFolder),
})),
meta: {
definitionsFolder: baseUrl,
outputFile: path.join(baseOutputMetaPath, `${namespace}.json`),
},
};
}
async function clear({ model, meta, services }: PathsConfig) {
await del([model.outputFolder, ...services.map((s) => s.outputFolder), meta.outputFile]);
}
function prepareOutputDirs({ services, outputNamespacePath }: PathsConfig) {
mkdirIfNotExist(outputNamespacePath);
for (const service of services) {
mkdirIfNotExist(service.outputFolder);
}
}
async function compileNamespace(namespaceDefinition: NamespaceDefinition) {
log(
chalk.cyan(
`Namespace ${chalk.bold.cyan(namespaceDefinition.namespace)} compilation started`
)
);
const pathsConfig = toPathConfig(namespaceDefinition);
await clear(pathsConfig);
prepareOutputDirs(pathsConfig);
await compileTsDefinitions(pathsConfig.model.definitionsFolder, pathsConfig.model.outputFolder);
for (const config of pathsConfig.services) {
await compileService(config.definitionFile, config.outputFolder);
}
await compileJsonMetadata(pathsConfig.meta.definitionsFolder, pathsConfig.meta.outputFile);
}
async function compile(definitions: NamespaceDefinition[]) {
try {
log(chalk.bold.magenta('Thrift services compilation started'));
for (const def of definitions) {
await compileNamespace(def);
}
log(chalk.bold.green('Thrift services compilation finished'));
} catch (e) {
error(e);
error(chalk.bold.red('Thrift services compilation failed'));
process.exit(1);
}
}
compile(configDefinition.config);