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

14
package-lock.json generated
View File

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

View File

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