🌳
pt0/nextF/deployF/nextAppPathsF.mts
1import * as _ from 'lodash-es'
7import fs from 'fs'
9const isBuildArtifact = (path: string) => {
10 const {prebuildEnvConfH} = getAppCfg() || {}
11 if (prebuildEnvConfH?.builtWorkerFilename && path.endsWith(`/${prebuildEnvConfH.builtWorkerFilename}`)) return true
12 if (prebuildEnvConfH?.builtSwFilename && path.endsWith(`/${prebuildEnvConfH.builtSwFilename}`)) return true
13 return false
16const isServerOnlyPath = (path: string) => {
17 if (_.includes(path, '/pages/api/')) return true
18 if (_.endsWith(path, '/route.js') || _.endsWith(path, '/route.ts')) return true
19 if (_.endsWith(path, '/runnext.mjs')) return true
20 if (_.endsWith(path, '/next.config.mjs')) return true
21 return false
24export const isNextJsRoutePath = (p: string): boolean =>
25 p.includes('/pages/') || p.includes('/app/')
27const routeHandlerExtA = ptNextPageExtA
28export const isNextRouteHandlerFile = (p: string): boolean => {
29 if (!p.includes('/app/')) return false
30 for (const ext of routeHandlerExtA) {
31 if (p.endsWith('/route.' + ext)) return true
32 }
33 return false
36export const nextAppPaths = async ({inclClient=true, inclServer, feOnly=false}: {inclClient?: boolean, inclServer?: boolean, feOnly?: boolean}={}) => {
37 const ctx = getAppCfg()
38 let {inclPtPathA=[], appPath, inclServer: ctxInclServer=true} = ctx
39 if (!appPath) return inclPtPathA
40 inclServer = inclServer ?? ctxInclServer
42 // feOnly: frontend-only mode - include pages/app/public but exclude server-only paths
43 if (feOnly) {
44 const pagesPathsA = _.map(await lsFilePathsRec([ptDir, appPath, 'pages'].join('/')), absPathToPtPath)
45 const appPathsA = _.map(await lsFilePathsRec([ptDir, appPath, 'app'].join('/')), absPathToPtPath)
46 const publicPathsA = _.reject(_.map(await lsFilePathsRec([ptDir, appPath, 'public'].join('/')), absPathToPtPath), isBuildArtifact)
47 inclPtPathA = [
48 ...inclPtPathA,
49 ..._.reject([...pagesPathsA, ...appPathsA], isServerOnlyPath),
50 ...publicPathsA,
51 ]
52 return inclPtPathA
53 }
55 // Legacy behavior for inclServer/inclClient
56 if (inclServer) {
57 inclPtPathA = [
58 ...inclPtPathA,
59 ..._.map([
60 ...await lsFilePathsRec([ptDir, appPath, 'pages'].join('/')),
61 ...await lsFilePathsRec([ptDir, appPath, 'app'].join('/')),
62 ], absPathToPtPath)
63 ]
64 }
66 if (inclClient) {
67 inclPtPathA = [
68 ...inclPtPathA,
69 ..._.reject(_.map(await lsFilePathsRec([ptDir, appPath, 'public'].join('/')), absPathToPtPath), isBuildArtifact)
70 ]
71 } else {
72 inclPtPathA = _.reject(inclPtPathA, path => _.includes(path, '/pages/') && !_.includes(path, '/api/'))
73 }
75 const serverAddA = ['runnext.mjs', 'next.config.mjs']
76 if (inclServer) {
77 _.each(serverAddA, (filename) => {
78 const ptPath = [appPath, filename].join('/')
79 if (fs.existsSync([ptDir, ptPath].join('/'))) {
80 inclPtPathA.push(ptPath)
81 }
82 })
83 } else {
84 inclPtPathA = _.reject(inclPtPathA, (path: string) => _.includes(path, '/api/') || _.find(serverAddA, file => _.endsWith(path, `/${file}`))) as string[]
85 }
87 return inclPtPathA