Socket
Socket
Sign inDemoInstall

@jsenv/filesystem

Package Overview
Dependencies
2
Maintainers
2
Versions
62
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsenv/filesystem


Version published
Maintainers
2
Created

Readme

Source

Jsenv filesystem npm package github main codecov coverage

Collection of functions to interact with filesystem in Node.js

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)
console.log(relativeUrl) // "src/file.js"

List files using pattern matching

import { listFilesMatching } from "@jsenv/filesystem"

const jsFiles = await listFilesMatching(
  "./**/*.js",
  new URL("./", import.meta.url),
)
[
  'file:///Users/dmail/docs/demo/a.js',
  'file:///Users/dmail/docs/demo/b.js'
]

Watch a specific file changes

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(packageJSONFileUrl)))
  },
  updated: () => {
    packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)))
  },
  removed: () => {
    packageJSON = null
  },
  notifyExistent: true,
})
unregister() // stop watching the file changes

Watch many files changes

import { registerDirectoryLifecycle } from "@jsenv/filesystem"

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

API

docs/API.md

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

Last updated on 04 Nov 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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc