manidlou / node-klaw-sync Goto Github PK
View Code? Open in Web Editor NEWNode.js recursive synchronous fast file system walker
License: MIT License
Node.js recursive synchronous fast file system walker
License: MIT License
I am using version 6.0.0 and it seems that noDir
is not working.
const path = require('path');
const klawSync = require('klaw-sync');
const tmplDir = path.join(__dirname, 'generators/react/app/templates/');
// get all templates (no directories)
const templates = klawSync(tmplDir, { noDir: true });
templates.forEach((template) => {
const tmplPath = template.path;
console.log(template.stats.isFile() ? 'f' : 'd', tmplPath);
});
Output shows both files and directories. Am I doing something wrong?
const klawSync = require('klaw-sync');
klawSync(srcPath, {
nodir: true,
filter: (item) => path.extname(item.path) === '.md'
})
like directory structure:
https://github.com/cipchk/delon/tree/master/packages/abc
just only get:
./README.md
should be includes like this:
./avatar-list/index.md
...
./README.md
work in 3.0.0
.
I just updated to version 4
(was on version 2).
I noticed that with the option nodir: true
, the library was actually looking only at the files, which is expected, but unfortunately it does so only at depth 0, and never goes inside of the directories.
It is a bit of a problem, because if one wants to find specific files (using a filter
function), you will need instead to do something like:
const path = require('path');
const klawSync = require('klaw-sync');
function filterFn(item) {
return item.stats.isDirectory() || /[some regex]/.test(path.basename(item.path));
}
const paths = klawSync([FOLDER_PATH], { filter: filterFn });
and then you will need to loop on the paths
one more time to filter the directories out yourself.
const files = paths.filter(function(item) {
return item.stats.isFile();
});
It would great if the default with nodir: true
was to traverse all the directories, and call the filter
function only for the files.
Right now, the code uses statSync
, but this attempts to read through symlinks, and will throw ENOENT
for any broken symlinks.
IMO, this call should be lstatSync
as it gives users more control (and also doesn't throw exceptions if the link is broken).
I found this will using a library that depends on klawSync, I wasn't aware that there was a broken symlink in the directory, but nonetheless I don't think the library should assume that users always want to read through symlinks. It would be better to let the user library break and they can deal with it accordingly.
The typescript typings for version 2.x do not exist. It looks like they only exist for version 1.1. I'm not sure if you're maintaining these, but I just wanted to make you aware if you are doing that.
https://www.npmjs.com/package/@types/klaw-sync
https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/klaw-sync
After switching to klaw, I seem to be having some issue loading only files with a certain name.
Say I want to recursively load only files named package.json
. It seems like I could do something like:
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: '!**/package.json'})
I have also tried several other variations of the ignore pattern but nothing seems to match properly.
I am probably missing something obvious... Any ideas?
With the latest version of node (10), It is not able to get files from inner dirs. It collect files only from the given dir.
Package version: 6.0.0
The README reads:
traverseAll
<Boolean>
default:true
- traverse all subdirectories, regardless of
filter
option. (When set totrue
,traverseAll
produces similar behavior to the default behavior prior to v4.0.0. The current default oftraverseAll: false
is equivalent to the oldnoRecurseOnFailedFilter: true
).
First, it says it's true
by default.
Then it says "The current default of traverseAll: false
" implicating it's false
by default.
In the code it is in fact undefined
, which is equivalent to false
in this case.
If the parameter is not present, klaw-sync
behaves as if it was false
.
What is the intended default value of this parameter, true
or false
?
true
seems more reasonable to me, but regardless, I believe default value should be indicated clearly in the docs and correspond to the actual behavior.
I also find it confusing that sister module node-klaw
does not have this option at all.
OS: Windows 10.
Unfortunetly klaw-sync treat .asar folders like foders and list's all files that are inside.
this is very problematic, because thouse files are not accesible, example:
electron.asar\browser\api\app.js
electron.asar\browser\api\auto-updater\auto-updater-native.js
When the path is D:\ or C:\ on Windows, had the error which operation not permitted.
Maybe should to have options for ignore the error and keep to walking directory.
{ Error: EPERM: operation not permitted, stat 'D:\\System Volume Information'
at Object.statSync (fs.js:850:3)
at Object.statSync (D:\...\node_modules\graceful-fs\polyfills.js:295:24)
at klawSync (D:\...\node_modules\klaw-sync\klaw-sync.js:16:24)
....
}
Hi! ๐
Firstly, thanks for your work on this project! ๐
Today I used patch-package to patch [email protected]
for the project I'm working on.
This fixes #17.
Here is the diff that solved my problem:
diff --git a/node_modules/klaw-sync/klaw-sync.js b/node_modules/klaw-sync/klaw-sync.js
index 74b87c6..6e7689a 100644
--- a/node_modules/klaw-sync/klaw-sync.js
+++ b/node_modules/klaw-sync/klaw-sync.js
@@ -13,7 +13,7 @@ function klawSync (dir, opts, ls) {
const paths = opts.fs.readdirSync(dir).map(p => dir + path.sep + p)
for (var i = 0; i < paths.length; i += 1) {
const pi = paths[i]
- const st = opts.fs.statSync(pi)
+ const st = opts.fs.lstatSync(pi)
const item = {path: pi, stats: st}
const isUnderDepthLimit = (!opts.rootDepth || pi.split(path.sep).length - opts.rootDepth < opts.depthLimit)
const filterResult = opts.filter ? opts.filter(item) : true
This issue body was partially generated by patch-package.
Hi,
When using this options, I can only access the level 0 (default) of the directory been read.
I want to match any "/folder/subfolder" that match my regexp (link: https://regex101.com/r/EYiftm/1).
The regexp seem to be correct, but when I walk the root folder with the option "deepthLimit:1" (so to get the folder and subfolder names in the path) and the filter option, I only get the folder in deep 0.
If I disable the filteroption, I get the folder/subfolder as expected.
depthLimit: 1
get ignored when using with filter
option.function filter_yearFolder(item){
return /\/\d{4}\/.+$/.test(item.path);
}
let dirs = klawSync("/Users/user1/work", {nofile: true, depthLimit: 1, filter: filter_yearFolder})
console.log(dirs);
filter
option let depthLimit:1
work as expected (but sans any filtering)let dirs = klawSync("/Users/user1/work", {nofile: true, depthLimit: 1})
console.log(dirs);
Best regards!!
example:
const files = klawSync(os.homedir()+'/Documents', {nodir: true, depthLimit: 0});
with above line i get the file and directory names. But dir names are just truncated file path in children.
result-> file.txt, dirname, dirname2
directory names are actually pointing to the files in child directories but truncated.
i.e -> dirname/file-in-child.txt
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.