IMP-23: New thrift-ts (#6)

This commit is contained in:
Rinat Arsaev 2022-03-04 16:37:29 +03:00 committed by GitHub
parent 5b63735b07
commit d5dc8ef9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 39 deletions

View File

@ -13,7 +13,7 @@ async function execWithLog(cmd, cwd = process.cwd()) {
},
(error, stdout, stderr) => {
if (error === null) {
console.log(stderr);
console.log(stdout);
res(stdout);
} else {
// console.error(error);
@ -25,24 +25,39 @@ async function execWithLog(cmd, cwd = process.cwd()) {
);
}
async function codegen(protoPath, depsPaths, outputPath, onlyModel) {
await execWithLog(`npx thrift-ts ${protoPath} -o ${outputPath} -d false`);
if (!onlyModel) {
const name = path.parse(protoPath).name;
const namedPath = path.join(outputPath, name);
fs.mkdirSync(namedPath, { recursive: true });
await execWithLog(
`thrift -r -gen js:node -o ${namedPath} ${depsPaths
.map((dep) => `-I ${dep}`)
.join(' ')} ${protoPath}`
);
await execWithLog(
`npx thrift-ts ${protoPath} -o ${path.join(
namedPath,
'metadata.json'
)} --json --pack --prettify`
);
}
async function codegen(protoPath, depsPaths, outputPath) {
const name = path.parse(protoPath).name;
const namedPath = path.join(outputPath, name);
fs.mkdirSync(namedPath, { recursive: true });
await execWithLog(
`thrift -r -gen js:node -o ${namedPath} ${depsPaths
.map((dep) => `-I ${dep}`)
.join(' ')} ${protoPath}`
);
const protos = fs
.readdirSync(path.join(namedPath, 'gen-nodejs'))
.map((file) => file.split('_types.js'))
.filter((parts) => parts[1] === '')
.map(([name]) => name);
fs.writeFileSync(
path.join(namedPath, 'context.js'),
`module.exports = {${protos
.map((proto) => `${proto}: require('./gen-nodejs/${proto}_types.js')`)
.join(',')}}`
);
}
async function genModel(paths, outputPath) {
await execWithLog(`thrift-ts ${paths.join(' ')} -o ${outputPath} -d false`);
}
async function genMetadata(paths, outputPath) {
await execWithLog(
`thrift-ts ${paths.join(' ')} -o ${path.join(
outputPath,
'metadata.json'
)} --json --pack --prettify`
);
}
function isThriftFile(file) {
@ -84,22 +99,15 @@ async function codegenAll() {
const protos = fs.readdirSync(PROTO_PATH).filter((proto) => isThriftFile(proto));
const depsCodegens = [];
for (const depPath of depsPaths) {
const depsProtos = fs
.readdirSync(path.join(depPath, DEPS_PROTO_DIR))
.filter((proto) => isThriftFile(proto));
const protosPaths = depsProtos.map((proto) => path.join(depPath, DEPS_PROTO_DIR, proto));
for (const protoPath of protosPaths) {
depsCodegens.push(codegen(protoPath, depsPaths, DIST_PATH));
}
}
await Promise.all(depsCodegens);
const codegens = [];
for (const protoPath of protos.map((proto) => path.join(PROTO_PATH, proto))) {
codegens.push(codegen(protoPath, depsPaths, DIST_PATH));
}
await Promise.all(codegens);
await Promise.all([
...codegens,
genModel([PROTO_PATH, ...depsPaths], DIST_PATH),
genMetadata([PROTO_PATH, ...depsPaths], DIST_PATH),
]);
}
module.exports = codegenAll;

14
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "0.1.0",
"license": "Apache-2.0",
"dependencies": {
"@vality/thrift-ts": "^2.2.1",
"@vality/thrift-ts": "^2.2.2-e258b9a.0",
"yargs": "^17.3.1"
},
"bin": {
@ -20,9 +20,9 @@
}
},
"node_modules/@vality/thrift-ts": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/@vality/thrift-ts/-/thrift-ts-2.2.1.tgz",
"integrity": "sha512-1oKRDo9A7nkO+IbKkATo+qP+4LP/tyHTrqvEOK7NIKdjvrv5vLAsR3yTIUaY7buzqYkNx7omhnpi0TBv5Fxz2A==",
"version": "2.2.2-e258b9a.0",
"resolved": "https://registry.npmjs.org/@vality/thrift-ts/-/thrift-ts-2.2.2-e258b9a.0.tgz",
"integrity": "sha512-4gAgkmMwE4Rr4RXeyboaeV0Iv5BcWf4xFZSxiIhbjREjo3tT0V8TYzTyjIPOmdylt/WnvVct2e1slQc1IfpoZA==",
"dependencies": {
"buffer": "^5.2.1",
"glob": "^7.1.3",
@ -868,9 +868,9 @@
},
"dependencies": {
"@vality/thrift-ts": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/@vality/thrift-ts/-/thrift-ts-2.2.1.tgz",
"integrity": "sha512-1oKRDo9A7nkO+IbKkATo+qP+4LP/tyHTrqvEOK7NIKdjvrv5vLAsR3yTIUaY7buzqYkNx7omhnpi0TBv5Fxz2A==",
"version": "2.2.2-e258b9a.0",
"resolved": "https://registry.npmjs.org/@vality/thrift-ts/-/thrift-ts-2.2.2-e258b9a.0.tgz",
"integrity": "sha512-4gAgkmMwE4Rr4RXeyboaeV0Iv5BcWf4xFZSxiIhbjREjo3tT0V8TYzTyjIPOmdylt/WnvVct2e1slQc1IfpoZA==",
"requires": {
"buffer": "^5.2.1",
"glob": "^7.1.3",

View File

@ -16,7 +16,7 @@
"thrift-codegen": "bin/index.js"
},
"dependencies": {
"@vality/thrift-ts": "^2.2.1",
"@vality/thrift-ts": "^2.2.2-e258b9a.0",
"yargs": "^17.3.1"
},
"devDependencies": {