Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The klaw-sync npm package is a Node.js module that allows users to recursively walk ('klaw') through the file system synchronously. It is useful for tasks such as reading all files in a directory and its subdirectories, filtering files by certain criteria, and obtaining file stats in a synchronous manner.
Recursive file listing
This feature allows you to list all files and directories within a given directory recursively. The example code lists all paths within '/some/directory'.
const klawSync = require('klaw-sync');
const paths = klawSync('/some/directory');
console.log(paths);
Filtering files
This feature allows you to filter the files and directories based on a custom function. In the example, only '.txt' files are listed.
const klawSync = require('klaw-sync');
const path = require('path');
const filterFn = item => path.extname(item.path) === '.txt';
const txtFiles = klawSync('/some/directory', { filter: filterFn });
console.log(txtFiles);
Including file stats
This feature allows you to include file stats in the output. The example code lists directories (excluding files) within '/some/directory' and includes their stats.
const klawSync = require('klaw-sync');
const pathsWithStats = klawSync('/some/directory', { nofile: true, stats: true });
console.log(pathsWithStats);
The 'glob' package provides similar functionality for matching files using the patterns known as 'globs'. Unlike klaw-sync, which provides a list of files by walking the directory tree, glob applies pattern matching to select files. It can be used synchronously or asynchronously.
The 'readdirp' package is another Node.js module that reads directories recursively. It streams entry information and can be a more memory-efficient way to handle large directories. It is similar to klaw-sync but is built around a streaming interface.
The 'node-dir' package provides a range of directory and file reading utilities. It can read files recursively and synchronously like klaw-sync, but it also offers additional utilities for reading files asynchronously, reading the contents of files, and more.
klaw-sync
is a Node.js recursive file system walker, which is the synchronous counterpart of klaw. It lists all files and directories inside a directory recursively and returns an array of objects that each object has two properties: path
and stats
. path
is the full path of the file or directory and stats
is an instance of fs.Stats.
npm i klaw-sync
directory
{String}
options
{Object}
optional (all options are false
by default)
ignore
{String | Array<String>}
any paths or micromatch patterns to ignore (can be string or an array of strings)
nodir
{Boolean}
return only files (ignore directories)
nofile
{Boolean}
return only directories (ignore files)
return: {Array<Object>}
[{path: '', stats: {}}]
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir')
// paths = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/file1', stats: {}}]
catch error
var klawSync = require('klaw-sync')
var paths
try {
paths = klawSync('/some/dir')
} catch (er) {
console.error(er)
}
console.dir(paths)
files only
var klawSync = require('klaw-sync')
var files = klawSync('/some/dir', {nodir: true})
// files = [{path: '/some/dir/file1', stats: {}}, {path: '/some/dir/file2', stats: {}}]
directories only
var klawSync = require('klaw-sync')
var dirs = klawSync('/some/dir', {nofile: true})
// dirs = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/dir2', stats: {}}]
ignore node_modules
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: 'node_modules'})
ignore node_modules
and .git
using micromatch patterns
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: '{node_modules,.git}'})
ignore node_modules
, .git
and all *.js
files using micromatch patterns
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: ['{node_modules,.git}', '*.js']})
lint: npm run lint
unit test: npm run unit
lint & unit: npm test
The bm.js
runs some basic benchmark tests for two cases, without --ignore
(basic usage) and with --ignore
, on these modules:
Just for fun, it turned out (as of January 25, 2017) for the most cases klaw-sync
is faster than other modules!
#####run benchmark
To run benchmark, just specify the root --dir=
. To ignore paths or patterns, use -i
flag.
npm run benchmark -- --dir=/some/dir
npm run benchmark -- --dir=/some/dir -i "node_modules"
npm run benchmark -- --dir=/some/dir -i "node_modules" -i "*.js"
Special thanks to:
for their contribution and support.
Licensed under MIT
FAQs
Recursive, synchronous, and fast file system walker
The npm package klaw-sync receives a total of 1,191,083 weekly downloads. As such, klaw-sync popularity was classified as popular.
We found that klaw-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.