Skip to content

Commit

Permalink
minor refactoring (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored Jan 31, 2025
1 parent 20ec17b commit 0be4e5d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
objectAssign,
unique,
assertPosixPath,
} from '../../../utils'
import { type ExportList, getExportList } from '../../../transformer/getExportList'
} from '../../utils'
import { type ExportList, getExportList } from '../getExportList'
import fs from 'node:fs'
import path from 'node:path'
import pc from '@brillout/picocolors'
Expand All @@ -26,7 +26,7 @@ type GeneratedShield = {
project: Project & { tsConfigFilePath: null | string }
}

assertModuleScope('codegen/generateShield.ts')
assertModuleScope('generateShield/generateShield.ts')
const generatedShields: GeneratedShield[] = []
let resutlAlreayLogged = false
const projects: Record<string, Project> = {}
Expand Down Expand Up @@ -121,19 +121,20 @@ function generate({
telefuncFileSource: SourceFile
shieldGenSource: SourceFile
telefuncFilePath: string
// All exports of `.telefunc.js` files must be functions, thus we generate a shield() for each export.
// If an export isn't a function then the error message is a bit ugly: https://github.com/brillout/telefunc/issues/142
exportList: ExportList
}): string {
shieldGenSource.addImportDeclaration({
moduleSpecifier: getTelefuncFileImportPath(telefuncFilePath),
namedImports: exportList.map((e) => e.exportName),
})

// assign the template literal type to a string
// then diagnostics are used to get the value of the template literal type
for (const exportedFunction of exportList) {
// Assign the template literal type to a string, then diagnostics are used to get the value of the template literal type.
for (const e of exportList) {
shieldGenSource.addTypeAlias({
name: getShieldName(exportedFunction.exportName),
type: `ShieldArrStr<Parameters<typeof ${exportedFunction.exportName}>>`,
name: getShieldName(e.exportName),
type: `ShieldArrStr<Parameters<typeof ${e.exportName}>>`,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { transformTelefuncFileServerSide }

import { ExportList, getExportList } from './getExportList'
import { assertPosixPath } from './utils'
import { generateShield } from '../server/shield/codegen/generateShield'
import { generateShield } from './generateShield/generateShield'
import { getServerConfig } from '../server/serverConfig'

async function transformTelefuncFileServerSide(
Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/vite/plugins/printShieldGenResult.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { printShieldGenResult }

import type { Plugin, ResolvedConfig } from 'vite'
import { logResult } from '../../server/shield/codegen/generateShield'
import { logResult } from '../../transformer/generateShield/generateShield'
import { projectInfo } from '../utils'
import pc from '@brillout/picocolors'

Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/webpack/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { install }

import { resolve } from 'node:path'
import { logResult } from '../server/shield/codegen/generateShield'
import { logResult } from '../transformer/generateShield/generateShield'
import { getRoot } from './getInfo'
import type { Compiler } from './types'
const dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import
Expand Down

0 comments on commit 0be4e5d

Please sign in to comment.