Socket
Socket
Sign inDemoInstall

@jsenv/filesystem

Package Overview
Dependencies
Maintainers
2
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsenv/filesystem

Collection of functions to interact with filesystem in Node.js


Version published
Weekly downloads
149
decreased by-46.21%
Maintainers
2
Weekly downloads
 
Created
Source

Jsenv filesystem

Collection of functions to interact with filesystem in Node.js.

npm package github main codecov coverage

Presentation

@jsenv/filesystem provides functions needed to work with files. It has no external dependency.

Examples

The whole list of functions exported by @jsenv/filesystem are documented in ./docs/API.md. This section demonstrates how @jsenv/filesystem can be used in common use cases.

Get a relative url

import { urlToRelativeUrl } from "@jsenv/filesystem"

const projectDirectoryUrl = "file:///project/"
const jsFileUrl = "file:///project/src/file.js"
const relativeUrl = urlToRelativeUrl(jsFileUrl, projectDirectoryUrl) // "src/file.js"

Collect files using pattern matching

import { collectFiles } from "@jsenv/filesystem"

const jsAndSourceMapFiles = await collectFiles({
  directoryUrl: "file:///project/directory/",
  structuredMetaMap: {
    isJsFile: {
      "./**/*.js": true,
    },
    isSourceMapFile: {
      "./**/*.js.map": true,
    },
  },
  predicate: ({ isJsFile, isSourceMapFile }) => isJsFile || isSourceMapFile,
})
console.log(jsAndSourceMapFiles.length)

Watch a specific file change

import { readFileSync } from "node:fs"
import { registerFileLifecycle } from "@jsenv/filesystem"

const packageJSONFileUrl = new URL("./package.json", import.meta.url)
let packageJSON = null
const unregister = registerFileLifecycle(packageJSONFileUrl, {
  added: () => {
    packageJSON = JSON.parse(String(readFileSync(filePath)))
  },
  updated: () => {
    packageJSON = JSON.parse(String(readFileSync(filePath)))
  },
  removed: () => {
    packageJSON = null
  },
  notifyExistent: true,
})
unregister() // stop watching the file changes

Watch many file change

import { registerDirectoryLifecycle } from "@jsenv/filesystem"

const contentMap = {}
const unregister = registerDirectoryLifecycle("file:///directory/", {
  watchDescription: {
    "./**/*": true,
    "./node_modules/": false,
  },
  added: ({ relativeUrl, type }) => {
    contentMap[relativeUrl] = type
  },
  removed: ({ relativeUrl }) => {
    delete contentMap[relativeUrl]
  },
})
unregister() // stop watching the directory changes

Installation

npm install @jsenv/filesystem

Development

If you are part or want to be part of the developpers of this package, check docs/development.md

FAQs

Package last updated on 15 Aug 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc