Skip to content

Commit

Permalink
Make processPagesDir use glob & remove recursion.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterp committed Jan 17, 2021
1 parent cc9db40 commit fdade5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 50 deletions.
1 change: 1 addition & 0 deletions packages/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"glob": "7.1.6",
"deepmerge": "^4.2.2",
"findup-sync": "^4.0.0",
"kill-port": "^1.6.1",
Expand Down
65 changes: 16 additions & 49 deletions packages/internal/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'

import findUp from 'findup-sync'
import { glob } from 'glob'

import { getConfig } from './config'

Expand Down Expand Up @@ -152,60 +153,26 @@ export const getPaths = (BASE_DIR: string = getBaseDir()): Paths => {
}

/**
* Recursively process the pages directory and return information useful for
* automated imports.
* Process the pages directory and return information useful for automated imports.
*/
export const processPagesDir = (
webPagesDir: string = getPaths().web.pages,
prefix: Array<string> = []
webPagesDir: string = getPaths().web.pages
): Array<PagesDependency> => {
const deps: Array<PagesDependency> = []
const entries = fs.readdirSync(webPagesDir, { withFileTypes: true })

// Iterate over a dir's entries, recursing as necessary into
// subdirectories.
entries.forEach((entry) => {
if (entry.isDirectory()) {
try {
// Actual page js or tsx files reside in a directory of the same
// name (supported by: directory-named-webpack-plugin), so let's
// construct the filename of the actual Page file.

const importPath = path.join(webPagesDir, entry.name, entry.name)
const resolvedImport = resolveFile(importPath)
// Note: I'm a bit concerned about throwing here, since this might cause an infinite loop in some scenarios.
if (resolvedImport === null) {
throw new Error('A Page import could not be found.')
}
// If the Page exists, then construct the dependency object and push it
// onto the deps array.
const basename = path.posix.basename(entry.name)
const importName = prefix.join() + basename
// `src/pages/<PageName>`
const importFile = ['src', 'pages', ...prefix, basename].join('/')
deps.push({
importName,
importPath,
const: importName,
path: path.join(webPagesDir, entry.name),
importStatement: `const ${importName
.split(',')
.join('')} = { name: '${importName
.split(',')
.join('')}', loader: () => import('${importFile}') }`,
})
} catch (e) {
// If the Page doesn't exist then we are in a directory of Page
// directories, so let's recurse into it and do the whole thing over
// again.
const newPrefix = [...prefix, entry.name]
deps.push(
...processPagesDir(path.join(webPagesDir, entry.name), newPrefix)
)
}
const pagePaths = glob.sync('**/**/*Page.{js,jsx,tsx}', { cwd: webPagesDir })
return pagePaths.map((pagePath) => {
const p = path.parse(pagePath)
// converts `admin/DeleteUserPage` -> `adminDeleteUserPage`
const importName = p.dir.replace(path.sep, '')
const importPath = path.join(webPagesDir, p.dir, p.name)
const importStatement = `const ${importName} = { name: '${importName}', loader: import('${importPath}') }`
return {
importName,
const: importName,
importPath,
path: importPath,
importStatement,
}
})
return deps
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10088,7 +10088,7 @@ glob-to-regexp@^0.3.0:
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab"
integrity sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs=

glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
glob@7.1.6, glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
Expand Down

0 comments on commit fdade5f

Please sign in to comment.