@fcostarodrigo/walk
Advanced tools
Comparing version 4.0.2 to 4.1.0
{ | ||
"name": "@fcostarodrigo/walk", | ||
"version": "4.0.2", | ||
"version": "4.1.0", | ||
"description": "Transverse files recursively", | ||
"main": "src/index.js", | ||
"main": "src/walk.js", | ||
"scripts": { | ||
"test": "jest unit int --coverage", | ||
"pretest": "eslint src", | ||
"unit-test": "jest unit --watch", | ||
"format": "pretty-quick" | ||
"format": "prettier --write ." | ||
}, | ||
@@ -33,18 +32,23 @@ "repository": { | ||
"devDependencies": { | ||
"eslint": "^6.1.0", | ||
"eslint-config-airbnb-base": "^13.2.0", | ||
"eslint-config-prettier": "^6.0.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-jest": "^22.14.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"husky": "^3.0.1", | ||
"jest": "^24.8.0", | ||
"prettier": "^1.18.2", | ||
"pretty-quick": "^1.11.1" | ||
"@types/jest": "^25.2.1", | ||
"@types/node": "^13.13.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.1.0", | ||
"eslint-config-prettier": "^6.10.1", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-jest": "^23.8.2", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"eslint-plugin-tsc": "^1.2.0", | ||
"husky": "^4.2.5", | ||
"jest": "^25.3.0", | ||
"lint-staged": "^10.1.3", | ||
"prettier": "^2.0.4", | ||
"typescript": "^3.8.3" | ||
}, | ||
"dependencies": {}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged && npm test" | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# WALK | ||
# Walk | ||
@@ -31,31 +31,18 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/57550a6a14ac4915a5502e0277654c0f)](https://app.codacy.com/app/fcostarodrigo/walk?utm_source=github.com&utm_medium=referral&utm_content=fcostarodrigo/walk&utm_campaign=Badge_Grade_Dashboard) | ||
```typescript | ||
function walk( | ||
root?: string, | ||
includeFolders?: boolean, | ||
): AsyncIterableIterator<string>; | ||
```javascript | ||
walk(root, lisFolders, walkFolder, readdir); | ||
``` | ||
`root`: Path to where the search starts. Defaults to `.`. | ||
`root`: Optional folder to transverse. Defaults to `.`. | ||
`includeFolders`: If paths of folders should be returned. Defaults to `false`. | ||
`includeFolders`: Optional flag to list folders. Defaults to `false`. | ||
The function is an async generator that yields the paths of the files recursively. | ||
`walkFolder`: Optional callback to decide if a folder is going to be transversed. | ||
## Development | ||
`readdir`: Optional node function override. | ||
Full tests with coverage | ||
The function is an async generator that yields the paths of the files recursively. | ||
```bash | ||
npm test | ||
``` | ||
Unit tests and watch for changes | ||
```bash | ||
npm run unit-test | ||
``` | ||
## License | ||
[MIT License](http://www.opensource.org/licenses/mit-license.php) |
const path = require("path"); | ||
const walk = require("./walk"); | ||
const testPath = path.join(__dirname, "test"); | ||
describe("walk", () => { | ||
beforeAll(() => { | ||
process.chdir(path.join(__dirname, "test")); | ||
}); | ||
describe("walk", () => { | ||
it("should list files recursively", async () => { | ||
const files = []; | ||
for await (const file of walk()) { | ||
files.push(file); | ||
} | ||
const expectedFiles = [ | ||
path.resolve(testPath, "folderA", "folderB", "fileB"), | ||
path.resolve(testPath, "folderA", "fileC"), | ||
path.resolve(testPath, "folderA", "fileA"), | ||
]; | ||
path.join("foods", "egg"), | ||
path.join("foods", "pizza"), | ||
path.join("foods", "fruits", "banana"), | ||
].sort(); | ||
for await (const file of walk(testPath)) { | ||
expect(file).toBe(expectedFiles.pop()); | ||
} | ||
expect(files.sort()).toEqual(expectedFiles); | ||
}); | ||
it("should list files with folders recursively", async () => { | ||
const files = []; | ||
for await (const file of walk(undefined, true)) { | ||
files.push(file); | ||
} | ||
const expectedFiles = [ | ||
path.resolve(testPath, "folderA", "folderB", "fileB"), | ||
path.resolve(testPath, "folderA", "folderB"), | ||
path.resolve(testPath, "folderA", "fileC"), | ||
path.resolve(testPath, "folderA", "fileA"), | ||
path.resolve(testPath, "folderA"), | ||
path.resolve(testPath), | ||
]; | ||
".", | ||
path.join("foods"), | ||
path.join("foods", "egg"), | ||
path.join("foods", "pizza"), | ||
path.join("foods", "fruits"), | ||
path.join("foods", "fruits", "banana"), | ||
].sort(); | ||
for await (const file of walk(testPath, true)) { | ||
expect(file).toBe(expectedFiles.pop()); | ||
expect(files.sort()).toEqual(expectedFiles); | ||
}); | ||
it("should not transverse some folders", async () => { | ||
const walkFolder = (folderPath) => | ||
folderPath !== path.join("foods", "fruits"); | ||
const files = []; | ||
for await (const file of walk(undefined, undefined, walkFolder)) { | ||
files.push(file); | ||
} | ||
const expectedFiles = [ | ||
path.join("foods", "egg"), | ||
path.join("foods", "pizza"), | ||
].sort(); | ||
expect(files.sort()).toEqual(expectedFiles); | ||
}); | ||
it("should yield the first argument when it is a file", async () => { | ||
const filePath = path.resolve(testPath, "folderA", "fileA"); | ||
const filePath = path.join("foods", "egg"); | ||
const files = []; | ||
for await (const file of walk(filePath, true)) { | ||
expect(file).toBe(filePath); | ||
files.push(file); | ||
} | ||
expect(files).toEqual([filePath]); | ||
}); | ||
}); |
@@ -8,15 +8,24 @@ const fs = require("fs"); | ||
* @param {string} root | ||
* @param {boolean} includeFolders | ||
* @param {boolean} listFolders | ||
* @param {(folderPath: string) => boolean} walkFolder | ||
* @param {typeof fs.promises.readdir} readdir | ||
* @returns {AsyncIterableIterator<string>} | ||
*/ | ||
async function* walk(root = ".", includeFolders = false) { | ||
async function* walk( | ||
root = ".", | ||
listFolders = false, | ||
walkFolder = () => true, | ||
readdir = fs.promises.readdir, | ||
) { | ||
try { | ||
const files = await fs.promises.readdir(root); | ||
const files = await readdir(root); | ||
if (includeFolders) { | ||
if (listFolders) { | ||
yield root; | ||
} | ||
for (const file of files) { | ||
yield* walk(path.join(root, file), includeFolders); | ||
if (walkFolder(root)) { | ||
for (const file of files) { | ||
yield* walk(path.join(root, file), listFolders, walkFolder, readdir); | ||
} | ||
} | ||
@@ -23,0 +32,0 @@ } catch (error) { |
@@ -1,13 +0,10 @@ | ||
const fs = require("fs"); | ||
const walk = require("./walk"); | ||
jest.mock("fs", () => ({ promises: { readdir: jest.fn() } })); | ||
describe("walk", () => { | ||
it("should throw errors thrown by the file system", () => { | ||
const error = new Error(); | ||
fs.promises.readdir.mockRejectedValueOnce(error); | ||
const iterator = walk(); | ||
const readdir = jest.fn().mockRejectedValueOnce(error); | ||
const iterator = walk(undefined, undefined, undefined, readdir); | ||
return expect(iterator.next()).rejects.toBe(error); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6176
16
112
1
14
48