Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does ncc support dynamic required(path) of fs.readdir ? #1190

Open
seepine opened this issue May 9, 2024 · 0 comments
Open

Does ncc support dynamic required(path) of fs.readdir ? #1190

seepine opened this issue May 9, 2024 · 0 comments

Comments

@seepine
Copy link

seepine commented May 9, 2024

Project Structure

- functions
  - user
    - info.ts
    - list.ts
  - health.ts
- util.ts
- index.ts

Use fs read functions dir and dynamic required all ts(health.ts)

util.ts

export function getFilesAndFoldersInDir(dir) {
  const basePath = path.join(process.cwd(), `${dir}`)
  const filesList = [];
  readFile(basePath, basePath, filesList);
  return filesList;
}
export function readFile(basePath, path, filesList) {
  const files = fs.readdirSync(path);
  files.forEach(walk);
  function walk(file) {
    const states = fs.statSync(path + "/" + file);
    if (states.isDirectory()) {
      readFile(basePath, path + "/" + file, filesList);
    } else {
      const obj: any = {};
      obj.size = states.size;
      obj.name = file;
      obj.path = path + "/" + file;
      obj.relativePath = obj.path.replace(basePath, "");
      obj.url = obj.relativePath.replace(".ts", "").replace(".js", "");
      filesList.push(obj);
    }
  }
}

index.ts

  let apiDir = "src/functions";
  let list = getFilesAndFoldersInDir(apiDir);
  list.forEach(async (obj: any) => {
    if (!obj.name.endsWith(".ts") && !obj.name.endsWith(".js")) {
      return
    }
    let handler = await import(obj.path);
    // TODO something...

  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant