diff --git a/run.js b/run.js index 1f83c50..8372f30 100755 --- a/run.js +++ b/run.js @@ -19,17 +19,51 @@ const standardLibrary = resolve(fileURLToPath(dirname(import.meta.url)), 'lib'); (async () => { // TODO simplify this line gaddam - log('reading', systemLocation, '...'); + const ignoreDeps = (path) => parse(path).name !== 'node_modules'; + const files = [ - ...readdirSync(systemLocation).map(v => resolve(systemLocation, v)), - ...readdirSync(standardLibrary).map(v => resolve(standardLibrary, v)) + ...walkdirSync(systemLocation, ignoreDeps), + ...walkdirSync(standardLibrary, ignoreDeps) ]; const fullpaths = files .filter(v => lstatSync(v).isFile()) .filter(v => parse(v).ext === '.v'); - for(const path of fullpaths) log(path); + log('included modules'); + log(files); + log('parsing modules...'); const modules = await Promise.all(fullpaths.map(loc => Module.create(loc, systemLocation))); const sys = new System(modules, systemLocation); })() + +function walkdirSync(root, filter = () => true) { + log('reading', root, '...'); + const paths = readdirSync(root).map(v => resolve(root, v)); + const [ files, dirs ] = sift(paths.filter(filter), (v) => lstatSync(v).isFile()); + log(`files: ${files.length} | dirs: ${dirs.length}`); + const rfiles = dirs.map(v => walkdirSync(v, filter)).reduce((a, v) => [...a, ...v], []); + + return [ + ...files, + ...rfiles + ]; +} + +/** + * + * @param {T[]} a + * @param {(v: T, i: number, a: T[]) => string} fn + * + * @returns {Object} + */ +function sift(a, fn) { + let left = [], right = []; + for(let i = 0; i < a.length; i ++) { + const v = a[i] + const lr = !!fn(v, i, a); + if(lr) left = [...left, v]; + else right = [...right, v]; + } + return [left, right]; +} \ No newline at end of file