🌳
pt0/deployF/entrypointsF/virtualImportsAI.mts
6import * as _ from 'lodash-es'
7import fs from 'fs'
8import path from 'path'
13const findCopyToDockerDirs = (basePath: string) => {
14 const results: string[] = []
15 const absBasePath = pathDownJoin(ptDir, basePath)
16 if (!fs.existsSync(absBasePath)) return results
18 const walk = (dir: string) => {
19 try {
20 const entries = fs.readdirSync(dir, {withFileTypes: true})
21 for (const entry of entries) {
22 if (!entry.isDirectory()) continue
23 const fullPath = path.join(dir, entry.name)
24 if (entry.name === 'copyToDocker') {
25 results.push(fullPath)
26 } else if (!entry.name.startsWith('.') && entry.name !== 'node_modules') {
27 walk(fullPath)
28 }
29 }
30 } catch {}
31 }
32 walk(absBasePath)
33 return results
36export const getVirtualImportsForDeployScript = async (regEpPath: string) => {
37 const headGitSha = await getHeadGitSha()
38 const cacheKeyA = [headGitSha, regEpPath, 'virtualImports-v1']
39 return await runMemoTempfile({cacheKeyA}, async () => {
40 const appPath = await getMonorSubdirForRegEp(regEpPath)
41 const epType = getEpType(regEpPath)
42 const epTypeRuntimeDirsH: Record<string, string[]> = {
43 eptNextjsApp: ['pages', 'app', 'public', 'server'],
44 eptEnsStaticSite: ['public'],
45 eptEnsGallerySite: ['public'],
46 }
47 const dirsToScan = (epType && epTypeRuntimeDirsH[epType]) || []
48 const virtualImportsA: string[] = []
50 if (appPath && dirsToScan.length) {
51 for (const subdir of dirsToScan) {
52 const dirPath = [ptDir, appPath, subdir].join('/')
53 try {
54 if (!fs.existsSync(dirPath)) continue
55 const filesA = await lsFilePathsRec(dirPath)
56 const monorPathsA = _.map(filesA, absPathToPtPath)
57 virtualImportsA.push(...monorPathsA)
58 } catch { /* dir may not exist or be readable */ }
59 }
61 if (epType === 'eptNextjsApp') {
62 const serverFilesA = ['runnext.mjs', 'next.config.mjs', 'tsconfig.json']
63 for (const filename of serverFilesA) {
64 const filePath = [appPath, filename].join('/')
65 const absPath = [ptDir, filePath].join('/')
66 if (fs.existsSync(absPath)) {
67 virtualImportsA.push(filePath)
68 }
69 }
70 }
71 }
73 if (appPath) {
74 const copyToDockerDirs = findCopyToDockerDirs(appPath)
75 for (const copyDir of copyToDockerDirs) {
76 const filesA = await lsFilePathsRec(copyDir)
77 virtualImportsA.push(...filesA.map(absPathToPtPath))
78 }
79 } else {
80 const madgeH = await ptMadge([regEpPath])
81 const importTreePaths = _.uniq([regEpPath, ..._.keys(madgeH), ..._.flatten(_.values(madgeH))])
82 const dirsInTree = _.uniq(importTreePaths.map(p => path.dirname(p)))
83 for (const dir of dirsInTree) {
84 const copyToDockerPath = pathDownJoin(ptDir, dir, 'copyToDocker')
85 if (fs.existsSync(copyToDockerPath)) {
86 const filesA = await lsFilePathsRec(copyToDockerPath)
87 virtualImportsA.push(...filesA.map(absPathToPtPath))
88 }
89 }
90 }
92 return virtualImportsA
93 })
96export const getDeployScriptsIncludingFile = async (filePath: string) => {
97 const registry = getRegistrySync()
98 const resultA: string[] = []
99 for (const { regEpPath } of registry) {
100 const virtualImports = await getVirtualImportsForDeployScript(regEpPath)
101 if (virtualImports.includes(filePath)) {
102 resultA.push(regEpPath)
103 }
104 }
105 return resultA