From 6db121b8d132e66d637dd143cfa2732fd0769bb9 Mon Sep 17 00:00:00 2001 From: Boris Novikov Date: Wed, 28 Jun 2023 20:15:18 +0400 Subject: [PATCH 1/3] feat: ignore directories during scan based on redwood.toml configuration --- packages/internal/src/files.ts | 6 ++++-- packages/project-config/src/config.ts | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/internal/src/files.ts b/packages/internal/src/files.ts index e88a136a157d..3f20bf0aac0b 100644 --- a/packages/internal/src/files.ts +++ b/packages/internal/src/files.ts @@ -2,7 +2,7 @@ import path from 'path' import fg from 'fast-glob' -import { getPaths } from '@redwoodjs/project-config' +import { getConfig, getPaths } from '@redwoodjs/project-config' import { getNamedExports, hasDefaultExport, fileToAst } from './ast' @@ -26,10 +26,12 @@ export const findPages = (cwd: string = getPaths().web.pages) => { } export const findDirectoryNamedModules = (cwd: string = getPaths().base) => { + const config = getConfig() + const modules = fg.sync('**/src/**/*.{ts,js,jsx,tsx}', { cwd, absolute: true, - ignore: ['node_modules'], + ignore: config.cli.ignoreScanDirs, }) // Cell's also follow use the directory-named-module pattern, diff --git a/packages/project-config/src/config.ts b/packages/project-config/src/config.ts index 29fddc4a9f76..6f99f9eb3af3 100644 --- a/packages/project-config/src/config.ts +++ b/packages/project-config/src/config.ts @@ -105,6 +105,7 @@ export interface Config { } useSDLCodeGenForGraphQLTypes: boolean } + cli: { ignoreScanDirs: string[] } } export interface CLIPlugin { @@ -178,6 +179,9 @@ const DEFAULT_CONFIG: Config = { }, useSDLCodeGenForGraphQLTypes: false, }, + cli: { + ignoreScanDirs: ['node_modules'], + }, } /** From 0239db95cec7e31bc5c80c91ccbed8a74eb54080 Mon Sep 17 00:00:00 2001 From: Boris Novikov Date: Sat, 29 Jul 2023 21:59:10 +0400 Subject: [PATCH 2/3] feat: narrow glob instead of introducing cli option --- packages/internal/src/files.ts | 42 ++++++++++++++++++++------- packages/project-config/src/config.ts | 4 --- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/packages/internal/src/files.ts b/packages/internal/src/files.ts index a1052420ee0c..a8da26578673 100644 --- a/packages/internal/src/files.ts +++ b/packages/internal/src/files.ts @@ -2,7 +2,7 @@ import path from 'path' import fg from 'fast-glob' -import { getConfig, getPaths } from '@redwoodjs/project-config' +import { getPaths } from '@redwoodjs/project-config' import { getNamedExports, hasDefaultExport, fileToAst } from './ast' @@ -25,17 +25,26 @@ export const findPages = (cwd: string = getPaths().web.pages) => { return modules.filter(isPageFile) } +/** + * This function finds all modules in the 'web' and 'api' directories excluding 'node_modules' and Cell files. + * Cell files are also directory named modules but they have their special type mirror file, so they are ignored. + * + * @param {string} cwd - The directory path to start searching from. By default, it is the base path of the project. + * @returns {Array} modules - An array of absolute paths for the found modules. + * + * @example + * // Assuming the base directory of your project is '/Users/user/myproject' + * findDirectoryNamedModules('/Users/user/myproject'); + * // This will return an array with the absolute paths of all matching files, e.g.: + * // ['/Users/user/myproject/web/src/components/Author/Author.tsx', '/Users/user/myproject/web/src/pages/AboutPage/AboutPage.tsx'] + */ export const findDirectoryNamedModules = (cwd: string = getPaths().base) => { - const config = getConfig() - - const modules = fg.sync('**/src/**/*.{ts,js,jsx,tsx}', { + const modules = fg.sync('(api|web)/src/**/*.{ts,js,jsx,tsx}', { cwd, absolute: true, - ignore: config.cli.ignoreScanDirs, + ignore: ['node_modules'], }) - // Cell's also follow use the directory-named-module pattern, - // but they get their own special type mirror file, so ignore them. return modules .filter(isDirectoryNamedModuleFile) .filter((p) => !isCellFile(p)) @@ -105,8 +114,8 @@ export const findRouteHooksSrc = (cwd: string = getPaths().web.src) => { }) } -export const findPrerenderedHtml = (cwd = getPaths().web.dist) => - fg.sync('**/*.html', { cwd, ignore: ['200.html', '404.html'] }) +// export const findPrerenderedHtml = (cwd = getPaths().web.dist) => +// fg.sync('**/*.html', { cwd, ignore: ['200.html', '404.html'] }) export const isCellFile = (p: string) => { const { dir, name } = path.parse(p) @@ -168,7 +177,20 @@ export const isPageFile = (p: string) => { return true } - +/** + * This function checks if the given path belongs to a directory named module. + * A directory named module is where the filename (without extension) is the same as the directory it is in. + * + * @param {string} p - The absolute path of the file. + * @returns {boolean} - Returns true if the path belongs to a directory named module, false otherwise. + * + * @example + * isDirectoryNamedModuleFile('/Users/user/myproject/web/src/components/Author/Author.tsx'); + * // Returns: true + * + * isDirectoryNamedModuleFile('/Users/user/myproject/web/src/components/Author/AuthorInfo.tsx'); + * // Returns: false + */ export const isDirectoryNamedModuleFile = (p: string) => { const { dir, name } = path.parse(p) return dir.endsWith(name) diff --git a/packages/project-config/src/config.ts b/packages/project-config/src/config.ts index b599e6e86f78..308b213a88a8 100644 --- a/packages/project-config/src/config.ts +++ b/packages/project-config/src/config.ts @@ -111,7 +111,6 @@ export interface Config { enabled: boolean } } - cli: { ignoreScanDirs: string[] } } export interface CLIPlugin { @@ -191,9 +190,6 @@ const DEFAULT_CONFIG: Config = { enabled: false, }, }, - cli: { - ignoreScanDirs: ['node_modules'], - }, } /** From d0a2709b1580fb8da26f6341e31e66e250ec5ce9 Mon Sep 17 00:00:00 2001 From: Boris Novikov Date: Mon, 28 Aug 2023 11:20:09 +0400 Subject: [PATCH 3/3] refactor: remove commented function as it is not used --- packages/internal/src/files.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/internal/src/files.ts b/packages/internal/src/files.ts index a8da26578673..a53816dfc17c 100644 --- a/packages/internal/src/files.ts +++ b/packages/internal/src/files.ts @@ -114,9 +114,6 @@ export const findRouteHooksSrc = (cwd: string = getPaths().web.src) => { }) } -// export const findPrerenderedHtml = (cwd = getPaths().web.dist) => -// fg.sync('**/*.html', { cwd, ignore: ['200.html', '404.html'] }) - export const isCellFile = (p: string) => { const { dir, name } = path.parse(p)