-
-
Save jaabiri/ad616da0a30ffb39efae3e39b7f9196e to your computer and use it in GitHub Desktop.
File walker for Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getStaticProps(context) { | |
const pages = await walkFiles(path.resolve(process.cwd(), 'src/pages')); | |
return { | |
props: { | |
pages, | |
}, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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