TD-237: Add i64 to number flag & compile services (#4)

This commit is contained in:
Rinat Arsaev 2022-03-24 11:14:54 +03:00 committed by GitHub
parent e258b9ab2d
commit 1d124bbe09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 62 deletions

View File

@ -1,29 +1,19 @@
name: Master
name: Publish
on:
push:
branches:
- 'master'
- 'main'
push:
branches: ["master", "main"]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Init NodeJS
uses: actions/setup-node@v2
with:
node-version: '16.13.2'
cache: 'npm'
registry-url: 'https://registry.npmjs.org'
- name: Install Packages
run: npm ci
- name: Build
run: npm run build
- name: Version Up
run: npm version prerelease --preid ${GITHUB_SHA::7} --no-git-tag-version
- name: Publish NPM
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
publish:
name: Publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: valitydev/action-frontend/setup@v0.1
- run: npm ci
- name: Build
run: npm run build
- uses: valitydev/action-frontend/publish@v0.1
with:
npm-token: ${{ secrets.NPM_TOKEN }}

View File

@ -1,20 +1,16 @@
name: PR
on:
pull_request:
branches: [ '*' ]
pull_request:
branches: ["*"]
jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Init NodeJS
uses: actions/setup-node@v2
with:
node-version: '16.13.2'
cache: 'npm'
- name: Install Packages
run: npm ci
- name: Check
run: npm run build
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: valitydev/action-frontend/setup@v0.1
- run: npm ci
- name: Check
run: npm run build

View File

@ -30,6 +30,7 @@ export default class BaseCompiler {
buffer: string[] = [];
filename: string;
int64AsString: boolean = false;
int64AsNumber: boolean = false;
camelCase: boolean = false;
definition: boolean = true;
@ -49,6 +50,10 @@ export default class BaseCompiler {
this.int64AsString = options.int64AsString;
}
if (typeof options.int64AsNumber !== "undefined") {
this.int64AsNumber = options.int64AsNumber;
}
if (typeof options.camelCase !== "undefined") {
this.camelCase = options.camelCase;
}
@ -91,6 +96,9 @@ export default class BaseCompiler {
case "i8":
return "number";
case "i64":
if (this.int64AsNumber) {
return "number";
}
return "Int64";
case "string":
case "binary":
@ -464,18 +472,19 @@ export default class BaseCompiler {
}
wMethod(method: Method) {
this.wIntend();
this.write(method.name);
this.wBrackets(() => {
const args = method.args;
this.wMethodArgs(args, {
returnType: method.type,
expections: method.throws
});
});
this.write(":", SPACE, "void");
this.write(";");
this.write("\n");
// Callback
// this.wIntend();
// this.write(method.name);
// this.wBrackets(() => {
// const args = method.args;
// this.wMethodArgs(args, {
// returnType: method.type,
// expections: method.throws
// });
// });
// this.write(":", SPACE, "void");
// this.write(";");
// this.write("\n");
this.wIntend();
this.write(method.name);
@ -518,9 +527,9 @@ export default class BaseCompiler {
return argLike;
}
wService(service: Service, basename: string) {
wService(service: Service, basename: string, serviceName: string) {
this.wIntend();
this.write("class", SPACE, "Client", SPACE);
this.write("interface", SPACE, "Client", SPACE);
this.wBlock(false, () => {
this.increaseIntend();
Object.values(service.functions).forEach((method, index, array) => {
@ -536,5 +545,14 @@ export default class BaseCompiler {
});
this.decreaseIntend(false);
});
this.write(
`export const config = {
functions: [${Object.keys(service.functions)
.map(k => `'${k}'`)
.join(",")}],
name: '${basename}',
serviceName: '${serviceName}'
}`
);
}
}

View File

@ -20,10 +20,14 @@ export default class ServiceCompiler extends BaseCompiler {
}
this.writeCallbackTypeDeclare();
this.writeCommonType();
this.wExport(() => this.wService(this.service, this.basename));
this.wExport(() =>
this.wService(this.service, this.basename, this.name)
);
return {
filename: `${path.basename(this.name, ".thrift")}.d.ts`,
filename: `${this.basename}-${path.basename(this.name, ".thrift")}${
this.definition ? ".d" : ""
}.ts`,
content: this.buffer.join("")
};
}

View File

@ -32,6 +32,12 @@ export default () => {
default: false,
type: "boolean"
})
.options("num", {
alias: "int64AsNumber",
describe: "treat type int64 as type number",
default: false,
type: "boolean"
})
.options("d", {
alias: "definition",
describe: "generate definition type",
@ -109,6 +115,7 @@ export default () => {
tabSize: argv.t,
spaceAsTab: argv.s,
int64AsString: argv.i,
int64AsNumber: argv.num,
definition: argv.d,
camelCase: argv.c,
json: argv.j,

View File

@ -20,7 +20,7 @@ class Compile extends BaseCompiler {
this.filename = file.filename;
this.ast = thriftPraser(file.content);
if (this.ast.service && this.definition) {
if (this.ast.service) {
const services = this.ast.service;
const basename = path.basename(this.filename, ".thrift");
const include = Object.assign({}, this.ast.include, {

View File

@ -13,6 +13,7 @@ export type CompileOptions = {
tabSize?: number;
spaceAsTab?: boolean;
int64AsString?: boolean;
int64AsNumber?: boolean;
definition?: boolean;
camelCase?: boolean;
json?: boolean;

View File

@ -1,3 +1,3 @@
{
"extends": ["tslint:recomended", "tslint-config-prettier"]
"extends": ["tslint:recommended", "tslint-config-prettier"]
}