Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@npmcli/fs

npm62.9mISC4.0.0

filesystem utilities for the npm cli

npm, oss

readme

@npmcli/fs

polyfills, and extensions, of the core fs module.

Features

  • fs.cp polyfill for node < 16.7.0
  • fs.withTempDir added
  • fs.readdirScoped added
  • fs.moveFile added

fs.withTempDir(root, fn, options) -> Promise

Parameters

  • root: the directory in which to create the temporary directory
  • fn: a function that will be called with the path to the temporary directory
  • options
    • tmpPrefix: a prefix to be used in the generated directory name

Usage

The 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()

fs.readdirScoped(root) -> Promise

Parameters

  • root: the directory to read

Usage

Like fs.readdir but handling @org/module dirs as if they were a single entry.

const { readdirScoped } = require('@npmcli/fs')
const entries = await readdirScoped('node_modules')
// entries will be something like: ['a', '@org/foo', '@org/bar']

fs.moveFile(source, dest, options) -> Promise

A fork of move-file with support for Common JS.

Highlights

  • Promise API.
  • Supports moving a file across partitions and devices.
  • Optionally prevent overwriting an existing file.
  • Creates non-existent destination directories for you.
  • Automatically recurses when source is a directory.

Parameters

  • source: File, or directory, you want to move.
  • dest: Where you want the file or directory moved.
  • options
    • overwrite (boolean, default: true): Overwrite existing destination file(s).

Usage

The built-in fs.rename() is just a JavaScript wrapper for the C rename(2) function, which doesn't support moving files across partitions or devices. This module is what you would have expected fs.rename() to be.

const { moveFile } = require('@npmcli/fs');

(async () => {
    await moveFile('source/unicorn.png', 'destination/unicorn.png');
    console.log('The file has been moved');
})();

changelog

Changelog

4.0.0 (2024-09-11)

⚠️ BREAKING CHANGES

  • @npmcli/fs now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • 618ca23 #121 align to npm 10 node engine range (@hashtagchris)

    Chores

  • 1c67142 #123 enable auto publish (@hashtagchris)
  • 83f4580 #121 run template-oss-apply (@hashtagchris)
  • 65bff4e #119 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • 6dd91fc #120 postinstall for dependabot template-oss PR (@hashtagchris)
  • 72176f8 #120 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])

3.1.1 (2024-05-03)

Documentation

  • 2cb7474 #91 fix the example for readdirScoped (#91) (@aminya)

Chores

  • 237e7c6 #106 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • 456d343 #106 postinstall for dependabot template-oss PR (@lukekarrys)
  • 5b0f7c6 #105 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

3.1.0 (2022-11-03)

Features

  • ecbb150 #63 port @npmcli/move-file (#63) (@lukekarrys)
  • 1268710 #62 add readdirScoped method (@lukekarrys)

3.0.0 (2022-10-11)

⚠️ BREAKING CHANGES

  • this package no longer exports the full set of core fs functions
  • this removes the owner option from all methods that previously supported it, as well as the withOwner and withOwnerSync methods
  • @npmcli/fs is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • b788931 #54 remove custom promisification in favor of fs/promises (#54) (@nlf)
  • e666309 #53 remove owner related code as well as stale polyfills (#53) (@nlf)
  • 895d205 #46 postinstall for dependabot template-oss PR (@lukekarrys)

2.1.2 (2022-08-15)

Bug Fixes

2.1.1 (2022-07-20)

Bug Fixes

  • remove polyfills which are out of range of our engines (#35) (be1e7b2)

2.1.0 (2022-03-21)

Features

  • add withOwner and withOwnerSync methods (#21) (40ee281)

2.0.1 (2022-02-23)

Bug Fixes

2.0.0 (2022-02-23)

⚠ BREAKING CHANGES

  • This removes support for node 10 and non-LTS versions of 12 and 14

Bug Fixes

Dependencies

  • update @gar/promisify requirement from ^1.0.1 to ^1.1.3 (#14) (e24e318)