UNPKG

@conjecture-dev/g-std

Version:

A collection of TypeScript utility functions for common programming tasks

51 lines 1.73 kB
"use strict"; import * as fs from "fs"; import path from "path"; import hash from 'object-hash'; export const Hash = (x) => hash(x); export const iterateFiles = function* (directory) { const files = fs.readdirSync(directory); for (const file of files) { const fullPath = path.join(directory, file); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { // Recursively iterate subdirectories yield* iterateFiles(fullPath); } else { // Yield file path yield fullPath; } } }; export const getFiletreeDefaults = async (directoryPath) => { const results = []; async function traverse(currentPath) { const entries = fs.readdirSync(currentPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(currentPath, entry.name); if (entry.isDirectory()) { await traverse(fullPath); } else if (entry.isFile() && (entry.name.endsWith(".ts") || entry.name.endsWith(".tsx"))) { try { // @ts-ignore const module = await import(fullPath); if (module.default) { results.push({ path: fullPath, defaultExport: module.default, }); } } catch (error) { console.error(`Error importing ${fullPath}:`, error); } } } } await traverse(directoryPath); return results; }; //# sourceMappingURL=index.js.map