Skip to content

Commit

Permalink
修复openapi的react sdk无法构建的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed May 7, 2024
1 parent 77319c0 commit 509e5a4
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/spicy-dolls-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@opendoc/backend": patch
"@opendoc/sdk": patch
---

修复 openapi 的 react sdk 无法构建的问题
12 changes: 9 additions & 3 deletions app/backend/src/modules/sdk/compiler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class CompilerService {
const compileDir = path.join(dir, 'compiling')

try {
await exec('npm install --production=false && npm run build', { cwd: compileDir })
await exec('npm install --production=false --legacy-peer-deps && npm run build', { cwd: compileDir })
} catch (e) {
if (e instanceof Error) {
if ('stdout' in e) {
Expand Down Expand Up @@ -160,7 +160,10 @@ export class CompilerService {
name: sdk.fullName,
version: sdk.version,
dependencies: {
core: `@${sdk.scope}/${apiDocument.code}`,
core: {
name: `@${sdk.scope}/${apiDocument.code}`,
version: sdk.version,
},
},
},
})
Expand Down Expand Up @@ -194,7 +197,10 @@ export class CompilerService {
name: sdk.fullName,
version: sdk.version,
dependencies: {
core: `@${sdk.scope}/${apiDocument.code}`,
core: {
name: `@${sdk.scope}/${apiDocument.code}`,
version: sdk.version,
},
},
},
})
Expand Down
2 changes: 2 additions & 0 deletions app/sdk/src/compile-asyncapi-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function compile(options: CompileAsyncapiOptions): CompileResult[] {
name: options.project.name,
outdir: options.outdir,
version: options.project.version,
dependencies: options.project.dependencies,
registry: options.project.registry,
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/sdk/src/compile-openapi-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ function compile(options: CompileOpenapiOptions): CompileResult[] {
name: options.project.name,
outdir: options.outdir,
version: options.project.version,
dependencies: options.project.dependencies,
registry: options.project.registry,
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/sdk/src/compile-openapi-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function compile(options: CompileOpenapiOptions): CompileResult[] {
outdir: options.outdir,
version: options.project.version,
dependencies: options.project.dependencies,
registry: options.project.registry,
}
}

Expand All @@ -65,7 +66,7 @@ function compile(options: CompileOpenapiOptions): CompileResult[] {
}

{
const fileContent = templates.t_hook({ ...context })
const fileContent = templates.t_hook(R.clone(context))
const filename = formatFilename(getSafeOperationName(pathname, method, operation))
const filepath = path.join(output, 'hooks', `${filename}.ts`)

Expand Down
32 changes: 24 additions & 8 deletions app/sdk/src/compile-project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import * as R from 'ramda'
import { readAndCompileTemplate } from './utils/read-and-compile-template'

import './handlebar/register-helper.js'
Expand All @@ -15,6 +16,7 @@ const templates = {
t_build_sh: readAndCompileTemplate('project/build_sh'),
t__gitignore: readAndCompileTemplate('project/_gitignore'),
t__npmignore: readAndCompileTemplate('project/_npmignore'),
t__npmrc: readAndCompileTemplate('project/_npmrc'),
}

export async function compileProject(options: CompileProjectOptions): Promise<void> {
Expand All @@ -24,27 +26,40 @@ export async function compileProject(options: CompileProjectOptions): Promise<vo
throw TypeError(message)
}

const { outdir, name, version } = Value.Default(CompileProjectOptions, Value.Clone(options)) as Required<CompileProjectOptions>
const context = { name, version }
const opts = Value.Default(CompileProjectOptions, Value.Clone(options)) as Required<CompileProjectOptions>

const { outdir, name, version, dependencies, registry } = opts
const context = {
package: {
name,
outdir,
version,
registry,
dependencies,
},
}


const tsconfigFilepath = path.join(outdir, 'tsconfig.json')
const tsconfigFileContent = templates.t_tsconfig_json(context)
const tsconfigFileContent = templates.t_tsconfig_json(R.clone(context))

const tsconfigEsFilepath = path.join(outdir, 'tsconfig.es.json')
const tsconfigEsFileContent = templates.t_tsconfig_es_json(context)
const tsconfigEsFileContent = templates.t_tsconfig_es_json(R.clone(context))

const tsconfigUmdFilepath = path.join(outdir, 'tsconfig.umd.json')
const tsconfigUmdFileContent = templates.t_tsconfig_umd_json(context)
const tsconfigUmdFileContent = templates.t_tsconfig_umd_json(R.clone(context))

const buildShFilepath = path.join(outdir, 'build.sh')
const buildShFileContent = templates.t_build_sh(context)
const buildShFileContent = templates.t_build_sh(R.clone(context))

const gitignoreFilepath = path.join(outdir, '.gitignore')
const gitignoreFileContent = templates.t__gitignore(context)
const gitignoreFileContent = templates.t__gitignore(R.clone(context))

const npmignoreFilepath = path.join(outdir, '.npmignore')
const npmignoreFileContent = templates.t__npmignore(context)
const npmignoreFileContent = templates.t__npmignore(R.clone(context))

const npmrcFilepath = path.join(outdir, '.npmrc')
const npmrcFileContent = templates.t__npmrc(R.clone(context))

await Promise.allSettled([
fs.outputFile(tsconfigFilepath, tsconfigFileContent),
Expand All @@ -53,6 +68,7 @@ export async function compileProject(options: CompileProjectOptions): Promise<vo
fs.outputFile(buildShFilepath, buildShFileContent),
fs.outputFile(gitignoreFilepath, gitignoreFileContent),
fs.outputFile(npmignoreFilepath, npmignoreFileContent),
fs.outputFile(npmrcFilepath, npmrcFileContent),
])

await fs.chmod(buildShFilepath, 0o755)
Expand Down
10 changes: 9 additions & 1 deletion app/sdk/src/interface/compile-project-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ export const CompileProjectOptions = Type.Object({
name: Type.String(),
version: Type.Optional(Type.String({ default: '0.0.1' })),

dependencies: Type.Optional(Type.Record(Type.String(), Type.String())),
dependencies: Type.Optional(Type.Record(
Type.String(),
Type.Object({
name: Type.String(),
version: Type.String(),
}),
{ default: {} }
)),
registry: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })),
})

// eslint-disable-next-line @typescript-eslint/no-redeclare
Expand Down
4 changes: 2 additions & 2 deletions app/sdk/src/templates/openapi-react/hook.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-nocheck
import { useEffect, useState } from "react"
import type { RequestParameters, ResponseMap } from "${{package.dependencies.core}}/types/{{h__change-case @root.fileNamingStyle (h__get-safe-operation-name pathname method operation)}}.js"
import { {{h__get-safe-operation-name pathname method operation}} } from "{{package.dependencies.core}}/operations/{{h__change-case @root.fileNamingStyle (h__get-safe-operation-name pathname method operation)}}.js"
import type { RequestParameters, ResponseMap } from "${{package.dependencies.core.name}}/types/{{h__change-case @root.fileNamingStyle (h__get-safe-operation-name pathname method operation)}}.js"
import { {{h__get-safe-operation-name pathname method operation}} } from "{{package.dependencies.core.name}}/operations/{{h__change-case @root.fileNamingStyle (h__get-safe-operation-name pathname method operation)}}.js"


export interface Use{{h__change-case "pascalCase" (h__get-safe-operation-name pathname method operation)}}<STATUS extends keyof ResponseMap> {
Expand Down
2 changes: 1 addition & 1 deletion app/sdk/src/templates/openapi-react/package_json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},

"peerDependencies": {
"{{package.dependencies.core}}": "{{package.version}}",
"{{package.dependencies.core.name}}": "{{package.dependencies.core.version}}",
"react": ">= 16"
}
}
5 changes: 5 additions & 0 deletions app/sdk/src/templates/project/_npmrc.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
auto-install-peers=false

{{#each package.registry as |registry scope|}}
{{scope}}={{registry}}
{{/each}}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "pnpm -r exec rm -rf node_modules && rm -rf node_modules",
"build": "turbo build --filter @opendoc/*",
"dev": "turbo dev --filter @opendoc/*"
"build": "turbo build --filter=@opendoc/* --filter=!@opendoc/frontend --filter=!@opendoc/backend",
"dev": "turbo dev --filter=@opendoc/* --filter=!@opendoc/frontend --filter=!@opendoc/backend"
},
"keywords": [
"opendoc"
Expand Down

0 comments on commit 509e5a4

Please sign in to comment.