Skip to content

Instantly share code, notes, and snippets.

@jaabiri
Forked from souporserious/example.js
Created May 12, 2021 04:06
Show Gist options
  • Save jaabiri/ad616da0a30ffb39efae3e39b7f9196e to your computer and use it in GitHub Desktop.
Save jaabiri/ad616da0a30ffb39efae3e39b7f9196e to your computer and use it in GitHub Desktop.
File walker for Node
export async function getStaticProps(context) {
const pages = await walkFiles(path.resolve(process.cwd(), 'src/pages'));
return {
props: {
pages,
},
};
}
async function walkFiles(filePath, isDirectory = null) {
const extension = path.extname(filePath);
const name = path.basename(filePath, extension);
let children = [];
if (isDirectory !== false) {
const files = await fs.readdir(filePath, { withFileTypes: true });
children = await Promise.all(
files.map((file) =>
walkFiles(`${filePath}/${file.name}`, file.isDirectory())
)
);
files.forEach((file) => {
console.log(file.name, file.isDirectory());
});
}
return {
name,
children,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment