Skip to content

Commit

Permalink
feat: implement dir walkjs
Browse files Browse the repository at this point in the history
  • Loading branch information
HaidarJbeily7 committed Nov 24, 2024
1 parent fe442b3 commit eef3c25
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
42 changes: 36 additions & 6 deletions eo2js-runtime/src/objects/org/eolang/fs/dir$walk.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const object = require('../../../../runtime/object')
const {LAMBDA} = require('../../../../runtime/attribute/specials');
const {LAMBDA, RHO} = require('../../../../runtime/attribute/specials');
const at_void = require('../../../../runtime/attribute/at-void');
const data = require('../../../../runtime/data');
const glob = require('glob');
const dataized = require('../../../../runtime/dataized');
const fs = require('fs');
const path = require('path');

/**
* Dir.walk recursively walks a directory and returns files matching a glob pattern.
Expand All @@ -11,10 +13,38 @@ const glob = require('glob');
const dir$walk = function() {
const obj = object('dir$walk')
obj.attrs['glob'] = at_void('glob')
obj.assets[LAMBDA] = function(_) {
const pattern = obj.attrs['glob'].data;
const files = glob.sync(pattern);
return data.toObject(files.join('\n'));
obj.assets[LAMBDA] = function(self) {
const dirPath = dataized(self.take(RHO).take('file').take('path')).toString();
const pattern = dataized(self.take('glob')).toString();
const absPath = path.resolve(dirPath);
const phi = require('../../../../runtime/phi');
const eolang = phi.take('org.eolang');
const fs_file = eolang.take('fs.file');

const walkSync = function(dir, pattern) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(function(file) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
results = results.concat(walkSync(filePath, pattern));
} else {
const relativePath = path.relative(absPath, filePath);
if (require('minimatch')(relativePath, pattern)) {
results.push(fs_file.copy().with({0: data.toObject(relativePath)}));
}
}
});
return results;
}

try {
const files = walkSync(absPath, pattern);
return data.toTuple(files);
} catch (err) {
throw new Error(`Can't walk at ${absPath}: ${err.message}`);
}
}
return obj
}
Expand Down
23 changes: 15 additions & 8 deletions eo2js-runtime/test/objects/org/eolang/fs/dir$walk.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const assert = require('assert');
const dir$walk = require('../../../../../temp/objects/org/eolang/fs/dir$walk');
const {STRING} = require('../../../../../temp/runtime/types');
const dataized = require('../../../../../temp/runtime/dataized');
const data = require('../../../../../temp/runtime/data');
const dir$walk = require('../../../../../src/objects/org/eolang/fs/dir$walk');
const {STRING} = require('../../../../../src/runtime/types');
const dataized = require('../../../../../src/runtime/dataized');
const data = require('../../../../../src/runtime/data');
const fs = require('fs');
const path = require('path');
const object = require('../../../../../src/runtime/object');
const {RHO} = require('../../../../../src/runtime/attribute/specials');

describe('dir$walk', function() {
const directory = path.resolve(__dirname, '../../../../../temp/test-dir');
const directory = path.resolve(__dirname, '../test-dir');

beforeEach(function() {
if (fs.existsSync(directory)) {
Expand All @@ -25,11 +27,16 @@ describe('dir$walk', function() {
});

it('should find files matching glob pattern', function() {
const dir = object('dir');
dir.attrs['file'] = object('file');
dir.attrs['file'].attrs['path'] = data.toObject(directory);
const walk = dir$walk();
walk.attrs['glob'] = data.toObject('**/*.txt');
const result = dataized(walk, STRING).split('\n').sort();
walk.attrs[RHO] = dir;
walk.attrs['glob'] = data.toObject('**/*.txt');
const files = dataized(walk);
const paths = files.map(file => dataized(file.take('path'), STRING));
assert.deepEqual(
result,
paths.sort(),
[
'file1.txt',
'subdir/file3.txt'
Expand Down

0 comments on commit eef3c25

Please sign in to comment.