Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mxiao-cll committed Nov 28, 2024
1 parent 0b2167f commit c13996b
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
path_to_generator="$GITHUB_WORKSPACE/dist/src/generator-adapter"
npm install -g yo@4.3.1
# The command bellow will generate new EA with default name 'example-adapter' in specified directory 'examples'
yo "$path_to_generator" examples
yo "$path_to_generator" examples --ignore-version-check
cd examples/example-adapter
yarn build
yarn test
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"prom-client": "13.2.0",
"redlock": "5.0.0-beta.2",
"ws": "8.18.0",
"yeoman-generator": "7.3.3"
"yeoman-generator": "7.3.3",
"@yeoman/types": "1.5.0",
"mem-fs": "4.1.1",
"@yeoman/adapter": "1.6.0"
},
"scripts": {
"build": "mkdir -p ./dist/src && cp package.json dist/src && cp README.md dist/src && tsc && yarn build-generator",
Expand All @@ -39,7 +42,6 @@
"@types/node": "22.10.1",
"@types/sinonjs__fake-timers": "8.1.5",
"@types/ws": "8.5.13",
"@types/yeoman-generator": "5.2.14",
"@typescript-eslint/eslint-plugin": "8.16.0",
"@typescript-eslint/parser": "8.16.0",
"ava": "6.2.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/adapter-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { execSync } from 'child_process'
const pathArg = process.argv[2] || ''

const generatorPath = resolve(__dirname, './generator-adapter')
const generatorCommand = `yo ${generatorPath} ${pathArg} `
const generatorCommand = `yo ${generatorPath} ${pathArg} --ignore-version-check`

execSync(generatorCommand, { stdio: 'inherit' })
47 changes: 25 additions & 22 deletions scripts/generator-adapter/generators/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as Generator from 'yeoman-generator'
import { exec } from 'child_process'

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

interface InputTransport {
type: string
Expand All @@ -18,8 +22,7 @@ interface GeneratorEndpointContext {
normalizedEndpointNameCap: string
}


module.exports = class extends Generator<{rootPath: string}> {
export default class extends Generator.default {
props: {
// Current ea-framework version in package.json
frameworkVersion: string
Expand All @@ -43,7 +46,7 @@ module.exports = class extends Generator<{rootPath: string}> {
standalone = process.env.EXTERNAL_ADAPTER_GENERATOR_STANDALONE === 'true'

constructor(args, opts) {
super(args, opts)
super(args, opts, { customInstallTask: true })
this.argument('rootPath', {
type: String,
required: false,
Expand Down Expand Up @@ -85,7 +88,7 @@ module.exports = class extends Generator<{rootPath: string}> {
this.props = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
frameworkVersion: (await import('../../../package.json')).version,
frameworkVersion: (await require('../../../package.json')).version,
adapterName,
endpoints,
endpointNames,
Expand Down Expand Up @@ -117,22 +120,22 @@ module.exports = class extends Generator<{rootPath: string}> {
baseFiles.forEach(fileName => {
this.fs.copyTpl(
this.templatePath(fileName),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/${fileName}`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/${fileName}`),
{...this.props, standalone: this.standalone}
)
})

// copy main index.ts file
this.fs.copyTpl(
this.templatePath(`src/index.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/index.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/index.ts`),
this.props
)

// Copy overrides
this.fs.copyTpl(
this.templatePath('src/config/overrides.json'),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/config/overrides.json`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/config/overrides.json`),
this.props,
)

Expand All @@ -142,7 +145,7 @@ module.exports = class extends Generator<{rootPath: string}> {
// Router endpoints
this.fs.copyTpl(
this.templatePath('src/endpoint/endpoint-router.ts.ejs'),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/endpoint/${inputEndpointName}.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/endpoint/${inputEndpointName}.ts`),
{
inputEndpointName,
inputTransports,
Expand All @@ -155,15 +158,15 @@ module.exports = class extends Generator<{rootPath: string}> {
inputTransports.forEach(transport => {
this.fs.copyTpl(
this.templatePath(`src/transport/${transport.type}.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/transport/${inputEndpointName}-${transport.type}.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/transport/${inputEndpointName}-${transport.type}.ts`),
{ inputEndpointName, includeComments: this.props.includeComments },
)
})
} else {
// Single transport endpoints
this.fs.copyTpl(
this.templatePath('src/endpoint/endpoint.ts.ejs'),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/endpoint/${inputEndpointName}.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/endpoint/${inputEndpointName}.ts`),
{
inputEndpointName,
inputTransports,
Expand All @@ -175,7 +178,7 @@ module.exports = class extends Generator<{rootPath: string}> {

this.fs.copyTpl(
this.templatePath(`src/transport/${inputTransports[0].type}.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/transport/${inputEndpointName}.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/transport/${inputEndpointName}.ts`),
{ inputEndpointName, includeComments: this.props.includeComments },
)
}
Expand All @@ -184,7 +187,7 @@ module.exports = class extends Generator<{rootPath: string}> {
// Create endpoint barrel file
this.fs.copyTpl(
this.templatePath(`src/endpoint/index.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/endpoint/index.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/endpoint/index.ts`),
{ endpoints: Object.values(this.props.endpoints) },
)

Expand All @@ -196,7 +199,7 @@ module.exports = class extends Generator<{rootPath: string}> {
// Copy config
this.fs.copyTpl(
this.templatePath('src/config/index.ts.ejs'),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/src/config/index.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/src/config/index.ts`),
{setBgExecuteMsEnv: customBgEndpoints.length}
)

Expand All @@ -205,7 +208,7 @@ module.exports = class extends Generator<{rootPath: string}> {
if (httpEndpoints.length) {
this.fs.copyTpl(
this.templatePath(`test/adapter.test.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/test/integration/adapter.test.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/test/integration/adapter.test.ts`),
{ endpoints: httpEndpoints, transportName: 'rest', setBgExecuteMsEnv: false },
)
}
Expand All @@ -218,7 +221,7 @@ module.exports = class extends Generator<{rootPath: string}> {
}
this.fs.copyTpl(
this.templatePath(`test/adapter-ws.test.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/test/integration/${fileName}`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/test/integration/${fileName}`),
{ endpoints: wsEndpoints },
)
}
Expand All @@ -234,7 +237,7 @@ module.exports = class extends Generator<{rootPath: string}> {
}
this.fs.copyTpl(
this.templatePath(`test/adapter.test.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/test/integration/${fileName}`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/test/integration/${fileName}`),
{ endpoints: customFgEndpoints, transportName: 'customfg', setBgExecuteMsEnv: false },
)
}
Expand All @@ -245,15 +248,15 @@ module.exports = class extends Generator<{rootPath: string}> {
}
this.fs.copyTpl(
this.templatePath(`test/adapter.test.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/test/integration/${fileName}`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/test/integration/${fileName}`),
{ endpoints: customBgEndpoints, transportName: 'custombg', setBgExecuteMsEnv: true },
)
}

// Copy test fixtures
this.fs.copyTpl(
this.templatePath(`test/fixtures.ts.ejs`),
this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/test/integration/fixtures.ts`),
this.destinationPath(`${this.args[0]}/${this.props.adapterName}/test/integration/fixtures.ts`),
{
includeWsFixtures: wsEndpoints.length > 0,
includeHttpFixtures: httpEndpoints.length > 0 || customBgEndpoints.length > 0 || customFgEndpoints.length > 0,
Expand Down Expand Up @@ -290,17 +293,17 @@ module.exports = class extends Generator<{rootPath: string}> {
pkgJson.scripts['test'] = 'EA_PORT=0 METRICS_ENABLED=false jest --updateSnapshot'
}

this.fs.extendJSON(this.destinationPath(`${this.options.rootPath}/${this.props.adapterName}/package.json`), pkgJson)
this.fs.extendJSON(this.destinationPath(`${this.args[0]}/${this.props.adapterName}/package.json`), pkgJson)
}

// install stage is used to run npm or yarn install scripts
install() {
this.yarnInstall([], {cwd: `${this.options.rootPath}/${this.props.adapterName}`})
async install() {
await exec(`yarn install --cwd ${this.args[0]}/${this.props.adapterName}`);
}

// end is the last stage. can be used for messages or cleanup
end() {
this.log(`🚀 Adapter '${this.props.adapterName}' was successfully created. 📍${this.options.rootPath}/${this.props.adapterName}`)
this.log(`🚀 Adapter '${this.props.adapterName}' was successfully created. 📍${this.args[0]}/${this.props.adapterName}`)
}

private async _promptAdapterName(): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import overrides from '../config/overrides.json'
import { <%= inputTransports[i].name %> } from '../transport/<%= inputEndpointName %>-<%= inputTransports[i].type %>' <% }
%>
<%- include ./base.ts.ejs %>
<%- include ('base.ts.ejs') %>
export const endpoint = new AdapterEndpoint({
<% if (includeComments) { -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { config } from '../config'
import overrides from '../config/overrides.json'
import { <%= inputTransports[0].name %> } from '../transport/<%= inputEndpointName %>'

<%- include ./base.ts.ejs %>
<%- include ('base.ts.ejs') %>

export const endpoint = new AdapterEndpoint({
<% if (includeComments) { -%>
Expand Down
1 change: 1 addition & 0 deletions scripts/generator-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"files": [
"generators"
],
"type": "module",
"main": "generators/app/index.js",
"keywords": [
"yeoman-generator",
Expand Down
6 changes: 3 additions & 3 deletions scripts/generator-adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"compilerOptions": {
"rootDir": "./../",
"outDir": "../../dist/src",
"target": "es2020",
"target": "es2022",
"downlevelIteration": true,
"esModuleInterop": false,
"esModuleInterop": true,
"moduleResolution": "node",
"module": "CommonJS",
"module": "es2020",
"resolveJsonModule": true
},
"files": [
Expand Down
Loading

0 comments on commit c13996b

Please sign in to comment.