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.
@npmcli/fs
Advanced tools
The @npmcli/fs package is a file system utility library that provides a set of asynchronous file system methods. It is designed to offer more convenient and higher-level operations for interacting with the file system in Node.js applications. This package is particularly useful for tasks like reading, writing, and manipulating files and directories in a way that's compatible with the rest of the npm CLI ecosystem.
Reading files
This feature allows you to asynchronously read the contents of a file. The code sample demonstrates how to read a file and handle the promise returned by the readFile method.
const { readFile } = require('@npmcli/fs');
readFile('path/to/file.txt', 'utf8').then(contents => {
console.log(contents);
}).catch(error => {
console.error('Error reading file:', error);
});
Writing files
This feature enables you to write data to a file asynchronously. The code sample shows how to write a string to a file and handle the promise to catch any errors.
const { writeFile } = require('@npmcli/fs');
const content = 'Hello, world!';
writeFile('path/to/file.txt', content, 'utf8').then(() => {
console.log('File written successfully');
}).catch(error => {
console.error('Error writing file:', error);
});
Removing files
This feature provides a way to asynchronously remove a file. The code sample illustrates how to delete a file and handle the promise to catch errors.
const { unlink } = require('@npmcli/fs');
unlink('path/to/file.txt').then(() => {
console.log('File removed successfully');
}).catch(error => {
console.error('Error removing file:', error);
});
fs-extra is a popular npm package that extends the built-in Node.js fs module. It adds file system methods that aren't included in the native fs module, such as copying directories and files, removing directories, and ensuring directories exist. Compared to @npmcli/fs, fs-extra offers a broader set of file system operations and is widely used for its convenience methods.
rimraf is a Node.js package that provides a simple way to remove files and directories, mimicking the UNIX command `rm -rf`. It's specifically focused on deletion operations, making it more specialized compared to @npmcli/fs, which offers a variety of file system operations including reading, writing, and removing files.
polyfills, and extensions, of the core fs
module.
fs.rm
polyfill for node versions < 14.14.0fs.mkdir
polyfill adding support for the recursive
and force
options in node versions < 10.12.0fs.copyFile
extended to accept an owner
optionfs.mkdir
extended to accept an owner
optionfs.mkdtemp
extended to accept an owner
optionfs.writeFile
extended to accept an owner
optionfs.withTempDir
addedowner
optionThe copyFile
, mkdir
, mkdtemp
, writeFile
, and withTempDir
functions
all accept a new owner
property in their options. It can be used in two ways:
{ owner: { uid: 100, gid: 100 } }
- set the uid
and gid
explicitly{ owner: 100 }
- use one value, will set both uid
and gid
the sameThe special string 'inherit'
may be passed instead of a number, which will
cause this module to automatically determine the correct uid
and/or gid
from the nearest existing parent directory of the target.
fs.withTempDir(root, fn, options) -> Promise
root
: the directory in which to create the temporary directoryfn
: a function that will be called with the path to the temporary directoryoptions
tmpPrefix
: a prefix to be used in the generated directory nameThe withTempDir
function creates a temporary directory, runs the provided
function (fn
), then removes the temporary directory and resolves or rejects
based on the result of fn
.
const fs = require('@npmcli/fs')
const os = require('os')
// this function will be called with the full path to the temporary directory
// it is called with `await` behind the scenes, so can be async if desired.
const myFunction = async (tempPath) => {
return 'done!'
}
const main = async () => {
const result = await fs.withTempDir(os.tmpdir(), myFunction)
// result === 'done!'
}
main()
FAQs
filesystem utilities for the npm cli
The npm package @npmcli/fs receives a total of 6,826,340 weekly downloads. As such, @npmcli/fs popularity was classified as popular.
We found that @npmcli/fs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers 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.