Skip to content

Commit d6c6067

Browse files
committed
feature: @putout/operator-filesystem: readDirectory: add
1 parent 4c02cd5 commit d6c6067

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

packages/operator-filesystem/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ const [dirPath] = findFile(ast, 'hello');
3737
const newDirectoryPath = createDirectory(dirPath, 'world');
3838
```
3939

40+
### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`
41+
42+
```js
43+
const {operator} = require('putout');
44+
const {
45+
finedFiles,
46+
findFile,
47+
readDirectory,
48+
} = operator;
49+
50+
const [dirPath] = findFile(ast, '/bin');
51+
readDirectory(dirPath);
52+
// returns list of files
53+
[];
54+
```
55+
4056
### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
4157

4258
```js

packages/operator-filesystem/lib/filesystem.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module.exports.moveFile = (filePath, dirPath) => {
169169

170170
const dirname = getFilename(dirPath);
171171
const filename = getFilename(filePath);
172-
const dirPathFiles = getProperty(dirPath, 'files');
172+
const dirPathFiles = getFiles(dirPath);
173173
const filenamePath = getProperty(filePath, 'filename');
174174

175175
const basename = filename
@@ -274,6 +274,15 @@ module.exports.createFile = (dirPath, name, content) => {
274274

275275
const getFiles = (dirPath) => getProperty(dirPath, 'files');
276276

277+
module.exports.readDirectory = (dirPath) => {
278+
const fileType = getFileType(dirPath);
279+
280+
if (fileType !== 'directory')
281+
return [];
282+
283+
return getFiles(dirPath).get('value.elements');
284+
};
285+
277286
module.exports.createDirectory = (dirPath, name) => {
278287
const dirPathFiles = getFiles(dirPath);
279288
const parentFilename = getFilename(dirPath);

packages/operator-filesystem/lib/filesystem.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
getFileContent,
2525
createFile,
2626
createDirectory,
27+
readDirectory,
2728
getParentDirectory,
2829
readFileContent,
2930
writeFileContent,
@@ -1098,6 +1099,38 @@ test('putout: operator: filesystem: findFile: directory', (t) => {
10981099
t.end();
10991100
});
11001101

1102+
test('putout: operator: filesystem: readDirectory: file', (t) => {
1103+
const ast = parseFilesystem(['/hello/', '/hello/world/', '/hello/world.txt']);
1104+
1105+
const [helloDir] = findFile(ast, '/hello/world.txt');
1106+
const files = readDirectory(helloDir).map(getFilename);
1107+
1108+
const expected = [];
1109+
1110+
t.deepEqual(files, expected);
1111+
t.end();
1112+
});
1113+
1114+
test('putout: operator: filesystem: readDirectory', (t) => {
1115+
const ast = parseFilesystem([
1116+
'/hello/',
1117+
'/hello/world/',
1118+
'/hello/world/abc',
1119+
'/hello/world.txt',
1120+
]);
1121+
1122+
const [helloDir] = findFile(ast, '/hello');
1123+
const files = readDirectory(helloDir).map(getFilename);
1124+
1125+
const expected = [
1126+
'/hello/world',
1127+
'/hello/world.txt',
1128+
];
1129+
1130+
t.deepEqual(files, expected);
1131+
t.end();
1132+
});
1133+
11011134
test('putout: operator: filesystem: moveFile: sameDirectory', (t) => {
11021135
const ast = parseFilesystem(['/', '/hello/', '/hello/world.txt']);
11031136
const [dirPath] = findFile(ast, '/');

0 commit comments

Comments
 (0)