phecda-server 5.0.0-beta.20 → 5.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,10 @@
1
1
  import { fileURLToPath, pathToFileURL } from 'url'
2
- import { existsSync } from 'fs'
3
2
  import { writeFile } from 'fs/promises'
4
3
  import { extname, isAbsolute, relative } from 'path'
5
4
  import ts from 'typescript'
6
5
  import chokidar from 'chokidar'
7
6
  import { PS_FILE_RE, log } from '../dist/index.mjs'
8
- import { compile, genUnImportRet } from './utils.mjs'
7
+ import { compile, genUnImportRet, handleClassTypes } from './utils.mjs'
9
8
  let port
10
9
  const isLowVersion = parseFloat(process.version.slice(1)) < 18.18
11
10
  // this part is important or not?
@@ -35,15 +34,14 @@ export async function initialize(data) {
35
34
  if (data)
36
35
  port = data.port
37
36
 
38
- if (process.env.PS_NO_DTS)
37
+ if (process.env.PS_UNIMPORT_BAN)
39
38
  return
40
39
 
41
40
  unimportRet = await genUnImportRet()
42
41
 
43
42
  if (unimportRet) {
44
43
  log('auto import...')
45
- if (!existsSync(dtsPath))
46
- writeFile(dtsPath, await unimportRet.generateTypeDeclarations())
44
+ writeFile(dtsPath, handleClassTypes(await unimportRet.generateTypeDeclarations()))
47
45
  }
48
46
  }
49
47
 
@@ -1,3 +1,4 @@
1
+ import { isAbsolute } from 'path'
1
2
  import { transform } from '@swc-node/core'
2
3
  const injectInlineSourceMap = ({ code, map }) => {
3
4
  if (map) {
@@ -18,7 +19,6 @@ export async function compile(sourcecode, filename) {
18
19
  emitDecoratorMetadata: true,
19
20
  experimentalDecorators: true,
20
21
  esModuleInterop: false,
21
-
22
22
  })
23
23
 
24
24
  return injectInlineSourceMap({ code, map })
@@ -26,16 +26,57 @@ export async function compile(sourcecode, filename) {
26
26
 
27
27
  export async function genUnImportRet() {
28
28
  try {
29
- const allExports = Object.keys(await import('../dist/index.mjs'))
29
+ const psExports = Object.keys(await import('../dist/index.mjs'))
30
30
  const { createUnimport } = await import('unimport')
31
31
 
32
+ const workspaceExports = await findWorkspaceExports()
32
33
  return createUnimport({
33
- imports: allExports.map((k) => {
34
- return { name: k, from: 'phecda-server' }
35
- }),
34
+ imports: psExports
35
+ .map((k) => {
36
+ return { name: k, from: 'phecda-server' }
37
+ })
38
+ .concat(workspaceExports),
36
39
  })
37
40
  }
38
41
  catch (e) {
39
42
  return false
40
43
  }
41
44
  }
45
+
46
+ function slash(str) {
47
+ return str.replace(/\\/g, '/')
48
+ }
49
+
50
+ async function findWorkspaceExports() {
51
+ try {
52
+ const { default: fg } = await import('fast-glob')
53
+ const { scanExports } = await import('unimport')
54
+ const result = await fg(
55
+ '**/*.@(controller|service|module|extension|ext|guard|interceptor|plugin|filter|pipe|edge).ts',
56
+ {
57
+ ignore: ['node_modules'],
58
+ absolute: true,
59
+ cwd: process.cwd(),
60
+ onlyFiles: true,
61
+ followSymbolicLinks: true,
62
+ },
63
+ )
64
+
65
+ const files = Array.from(new Set(result.flat())).map(slash)
66
+
67
+ return (await Promise.all(files.map(i => scanExports(i, false))))
68
+ .flat()
69
+ }
70
+ catch (e) {
71
+ return []
72
+ }
73
+ }
74
+
75
+ export function handleClassTypes(input) {
76
+ return input.replace(/const\s+(\w+)\s*:\s*typeof\s+import\(['"](.+)['"]\)\['(\w+)'\]/g, (_, n, p, e) => {
77
+ if (isAbsolute(p))
78
+ return `${_}\n type ${n} = InstanceType<typeof import('${p}')['${e}']>`
79
+
80
+ return _
81
+ })
82
+ }