mirror of
https://github.com/valitydev/frontend-thrift-codegen.git
synced 2024-11-06 02:15:17 +00:00
feat: IMP-23: Init (#1)
This commit is contained in:
parent
cd6094f6d9
commit
b0b9b71cc6
25
.github/workflows/master.yaml
vendored
Normal file
25
.github/workflows/master.yaml
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
name: Master
|
||||
on:
|
||||
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: Publish NPM
|
||||
run: npm publish
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
23
.github/workflows/pr-close.yaml
vendored
Normal file
23
.github/workflows/pr-close.yaml
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
name: Version Up & Update Changelog
|
||||
on:
|
||||
pull_request:
|
||||
types: [closed]
|
||||
jobs:
|
||||
merge_job:
|
||||
name: Version Up & Update Changelog
|
||||
if: github.event.pull_request.merged == true
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: Conventional Changelog
|
||||
uses: TriPSs/conventional-changelog-action@v3
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
close_job:
|
||||
name: Just Close
|
||||
if: github.event.pull_request.merged == false
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: |
|
||||
echo PR #${{ github.event.number }} has been closed without being merged
|
21
.github/workflows/pr-title.yaml
vendored
Normal file
21
.github/workflows/pr-title.yaml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
name: Check PR Title
|
||||
on:
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- reopened
|
||||
- edited
|
||||
- synchronize
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check PR Title
|
||||
uses: aslafy-z/conventional-pr-title-action@v2.2.5
|
||||
with:
|
||||
success-state: Title follows the specification.
|
||||
failure-state: Title does not follow the specification.
|
||||
context-name: conventional-pr-title
|
||||
preset: conventional-changelog-angular@latest
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
20
.github/workflows/pr.yaml
vendored
Normal file
20
.github/workflows/pr.yaml
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
name: PR
|
||||
on:
|
||||
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 check
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
5
.idea/.gitignore
vendored
Normal file
5
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
12
.idea/frontend-thrift-codegen.iml
Normal file
12
.idea/frontend-thrift-codegen.iml
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/jsLibraryMappings.xml
Normal file
6
.idea/jsLibraryMappings.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptLibraryMappings">
|
||||
<includedPredefinedLibrary name="Node.js Core" />
|
||||
</component>
|
||||
</project>
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ThriftCompiler">
|
||||
<compilers />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/frontend-thrift-codegen.iml" filepath="$PROJECT_DIR$/.idea/frontend-thrift-codegen.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
8
.idea/prettier.xml
Normal file
8
.idea/prettier.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PrettierConfiguration">
|
||||
<option name="myRunOnSave" value="true" />
|
||||
<option name="myRunOnReformat" value="true" />
|
||||
<option name="myFilesPattern" value="{**/*,*}.{js,ts,jsx,tsx,yml,yaml,md}" />
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
3
.prettierignore
Normal file
3
.prettierignore
Normal file
@ -0,0 +1,3 @@
|
||||
package.json
|
||||
package-lock.json
|
||||
node_modules
|
5
.prettierrc
Normal file
5
.prettierrc
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"printWidth": 100,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 4
|
||||
}
|
10
README.md
10
README.md
@ -1 +1,9 @@
|
||||
# Frontend Thrift Codegen Utils
|
||||
# Frontend Thrift Codegen CLI
|
||||
|
||||
Generate NodeJS/JS code, create models and metadata by Thrift services.
|
||||
|
||||
## Usage
|
||||
|
||||
```shell
|
||||
thirft-codegen
|
||||
```
|
||||
|
3
bin/index.js
Executable file
3
bin/index.js
Executable file
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
require('../lib/cli').default();
|
96
lib/cli.js
Normal file
96
lib/cli.js
Normal file
@ -0,0 +1,96 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { exec } = require('child_process');
|
||||
const yargs = require('yargs');
|
||||
|
||||
async function execWithLog(cmd, cwd = process.cwd()) {
|
||||
console.log(`> ${cmd}`);
|
||||
return await new Promise((res, rej) =>
|
||||
exec(
|
||||
cmd,
|
||||
{
|
||||
cwd,
|
||||
},
|
||||
(error, stdout, stderr) => {
|
||||
if (error === null) {
|
||||
console.log(stderr);
|
||||
res(stdout);
|
||||
} else {
|
||||
// console.error(error);
|
||||
console.error(stderr);
|
||||
rej(error);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async function codegen(protoPath, depsPaths, outputPath, onlyModel) {
|
||||
const thriftTs = path.join(__dirname, '../node_modules/.bin/thrift-ts');
|
||||
await execWithLog(`${thriftTs} ${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(
|
||||
`${thriftTs} ${protoPath} -o ${path.join(
|
||||
namedPath,
|
||||
'metadata.json'
|
||||
)} --json --pack --prettify`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function codegenAll() {
|
||||
const argv = yargs.command('gen').argv;
|
||||
|
||||
const PROTO_DIR = argv._[0] || './proto';
|
||||
const DEPS_DIRS = ['./node_modules', './node_modules/@vality'];
|
||||
const PROJECT_PATH = process.cwd();
|
||||
|
||||
const PROTO_PATH = path.join(PROJECT_PATH, PROTO_DIR);
|
||||
const DIST_PATH = path.join(PROJECT_PATH, './dist');
|
||||
|
||||
const depsPaths = DEPS_DIRS.map((depsDir) =>
|
||||
fs
|
||||
.readdirSync(path.join(PROJECT_PATH, depsDir))
|
||||
.map((dep) => path.join(PROJECT_PATH, depsDir, dep))
|
||||
)
|
||||
.flat()
|
||||
.filter((depPath) => {
|
||||
try {
|
||||
return fs.lstatSync(path.join(depPath, 'proto')).isDirectory();
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
const protos = fs
|
||||
.readdirSync(PROTO_PATH)
|
||||
.filter((proto) => path.parse(proto).ext === '.thrift');
|
||||
|
||||
const depsCodegens = [];
|
||||
for (const depPath of depsPaths) {
|
||||
const depsProtos = fs
|
||||
.readdirSync(path.join(depPath, 'proto'))
|
||||
.filter((proto) => path.parse(proto).ext === '.thrift');
|
||||
const protosPaths = depsProtos.map((proto) => path.join(depPath, 'proto', 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);
|
||||
}
|
||||
|
||||
module.exports = codegenAll;
|
||||
module.exports.default = codegenAll;
|
1487
package-lock.json
generated
Normal file
1487
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
Normal file
25
package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "@vality/thrift-codegen",
|
||||
"version": "0.1.0",
|
||||
"description": "",
|
||||
"main": "./lib/codegen.js",
|
||||
"scripts": {
|
||||
"check": "prettier \"**/*.{html,js,ts,css,scss,md,json,prettierrc,svg,huskyrc,yml,yaml}\" --list-different"
|
||||
},
|
||||
"author": "Vality",
|
||||
"license": "Apache-2.0",
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"bin": {
|
||||
"thrift-codegen": "bin/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vality/thrift-ts": "^2.2.1",
|
||||
"yargs": "^17.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^2.5.1"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user