Skip to content

Commit

Permalink
refactor: recursive option을 사용하여 getPagesFiles 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
d0422 committed Jun 5, 2024
1 parent e46c745 commit b849a39
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 27 deletions.
1 change: 0 additions & 1 deletion apps/miniNext/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const files = fs
.readdirSync('public', { recursive: true })
.filter((fileName) => fileName.match(/.tsx/));

console.log(files);
const createConfigFile = (fullFileName) => {
const [fileName] = fullFileName.split('.tsx');
return {
Expand Down
4 changes: 2 additions & 2 deletions apps/miniNext/src/core/createClientFiles.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync, mkdirSync, writeFile } from 'fs';
import path from 'path';
import { _getPagesFiles } from './getPagesFiles';
import { getPagesFiles } from './getPagesFiles';

export const createClientFiles = async () => {
const files = await _getPagesFiles('src/pages');
const files = await getPagesFiles('src/pages');
if (!existsSync(path.resolve(__dirname, `../../public`))) {
mkdirSync(path.resolve(__dirname, `../../public`));
}
Expand Down
27 changes: 5 additions & 22 deletions apps/miniNext/src/core/getPagesFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@ import { Dirent, readdir } from 'fs';

const PAGES_PATH = 'dist/pages';

const fileProcess = async (pagesPath: string, file: Dirent) => {
if (file.isDirectory())
return await _getPagesFiles(`${pagesPath}/${file.name}`);

return `${pagesPath}/${file.name}`;
};

export const _getPagesFiles = async (pagesPath = PAGES_PATH) => {
const files: Dirent[] = await new Promise((resolve, reject) => {
readdir(pagesPath, { withFileTypes: true }, (err, files) => {
if (err) reject();
resolve(files);
export const getPagesFiles = async (path = PAGES_PATH) => {
return await new Promise<string[]>((resolve, reject) => {
readdir(path, { recursive: true }, (err, files) => {
if (err) reject(err);
resolve(files as string[]);
});
});

const fileList = await Promise.all(
files.map(async (file) => await fileProcess(pagesPath, file))
);

return [...fileList.flat()];
};

export const getPagesFiles = async () => {
return await _getPagesFiles();
};
4 changes: 2 additions & 2 deletions apps/miniNext/src/core/getServerSidePropsFunction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'path';
import { _getPagesFiles } from './getPagesFiles';
import { getPagesFiles } from './getPagesFiles';

type ServerSideFunction = Record<string, Function>;

export const getServerSidePropsFunction = async () => {
const result: ServerSideFunction = {};

const files = await _getPagesFiles();
const files = await getPagesFiles();
files.map((file) => {
const { getServerSideProps } = require(path.resolve(file));
result[file] = getServerSideProps;
Expand Down
1 change: 1 addition & 0 deletions apps/miniNext/src/core/routeMapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getRoutingPath = (fileName: string) => {

export const routeMapper = async (app: Express) => {
const files = await getPagesFiles();

files.forEach((fullFileName) => {
const fileName = fullFileName
.replace(/dist\/pages\//, '')
Expand Down

0 comments on commit b849a39

Please sign in to comment.