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 - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

5

package.json
{
"name": "@jsenv/filesystem",
"version": "2.0.0",
"version": "2.1.0",
"description": "Collection of functions to interact with filesystem in Node.js",

@@ -19,4 +19,3 @@ "license": "MIT",

"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"access": "public"
},

@@ -23,0 +22,0 @@ "type": "module",

23

README.md

@@ -11,8 +11,4 @@ # Jsenv filesystem

`@jsenv/filesystem` provides functions needed to work with files. It has no external dependency.
`@jsenv/filesystem` provides functions needed to work with files.
# Examples
The whole list of functions exported by `@jsenv/filesystem` are documented in [./docs/API.md](./docs/API.md). This section demonstrates how `@jsenv/filesystem` can be used in common use cases.
## Get a relative url

@@ -25,3 +21,4 @@

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

@@ -59,6 +56,6 @@

added: () => {
packageJSON = JSON.parse(String(readFileSync(filePath)))
packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)))
},
updated: () => {
packageJSON = JSON.parse(String(readFileSync(filePath)))
packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)))
},

@@ -78,3 +75,3 @@ removed: () => {

const contentMap = {}
const directoryContentDescription = {}
const unregister = registerDirectoryLifecycle("file:///directory/", {

@@ -86,6 +83,6 @@ watchDescription: {

added: ({ relativeUrl, type }) => {
contentMap[relativeUrl] = type
directoryContentDescription[relativeUrl] = type
},
removed: ({ relativeUrl }) => {
delete contentMap[relativeUrl]
delete directoryContentDescription[relativeUrl]
},

@@ -96,2 +93,6 @@ })

# API
[docs/API.md](./docs/API.md)
# Installation

@@ -98,0 +99,0 @@

import { promises } from "node:fs"
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
import { fileSystemPathToUrl } from "./fileSystemPathToUrl.js"
import { urlToFileSystemPath } from "./urlToFileSystemPath.js"

@@ -9,2 +10,4 @@ import { resolveUrl } from "./resolveUrl.js"

import { readFileSystemNodeStat } from "./readFileSystemNodeStat.js"
import { readSymbolicLink } from "./readSymbolicLink.js"
import { removeFileSystemNode } from "./removeFileSystemNode.js"

@@ -15,30 +18,24 @@ // https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_symlink_target_path_type

export const writeSymbolicLink = async ({ from, to, type }) => {
/**
* Writes a symbolic link pointing from a filesystem node to an other
* @param {string} from Where symlink is written
* @param {string} to The symlink target
* @param {"file" | "dir"} [type] Symlink type if you know it before hand
* @param {boolean} [allowUseless=false] Prevent error when symlink already exists with the same target
* @param {boolean} [allowOverwrite=false] Will replace any existing symlink
*/
export const writeSymbolicLink = async ({
from,
to,
type,
allowUseless = false,
allowOverwrite = false,
}) => {
const fromUrl = assertAndNormalizeFileUrl(from)
const toUrl = getToAsUrl(to, fromUrl)
const toPath = urlToFileSystemPath(toUrl)
let toValue
if (typeof to === "string") {
// absolute filesystem path
if (isFileSystemPath(to)) {
toValue = to
}
// relative url
else if (to.startsWith("./") || to.startsWith("../")) {
toValue = to
}
// absolute url
else {
const toUrl = String(new URL(to, fromUrl))
toValue = urlToFileSystemPath(toUrl)
}
} else if (to instanceof URL) {
toValue = urlToFileSystemPath(to)
} else {
throw new TypeError(`symbolic link to must be a string or an url, received ${to}`)
}
if (isWindows && typeof type === "undefined") {
// without this if you write a symbolic link without specifying the type on windows
// you later get EPERM when doing stat on the symlink
const toUrl = resolveUrl(toValue, fromUrl)
const toStats = await readFileSystemNodeStat(toUrl, { nullIfNotFound: true })

@@ -50,11 +47,48 @@ type = toStats && toStats.isDirectory() ? "dir" : "file"

try {
await symlink(toValue, symbolicLinkPath, type)
await symlink(toPath, symbolicLinkPath, type)
} catch (error) {
if (error.code === "ENOENT") {
await ensureParentDirectories(fromUrl)
await symlink(toValue, symbolicLinkPath, type)
await symlink(toPath, symbolicLinkPath, type)
return
}
if (error.code === "EEXIST") {
if (allowUseless) {
const existingSymbolicLinkUrl = await readSymbolicLink(fromUrl)
if (existingSymbolicLinkUrl === toUrl) {
return
}
}
if (allowOverwrite) {
await removeFileSystemNode(fromUrl)
await symlink(toPath, symbolicLinkPath, type)
return
}
}
throw error
}
}
const getToAsUrl = (to, fromUrl) => {
if (typeof to === "string") {
// absolute filesystem path
if (isFileSystemPath(to)) {
return fileSystemPathToUrl(to)
}
// relative url
if (to.startsWith("./") || to.startsWith("../")) {
return resolveUrl(to, fromUrl)
}
// absolute url
return resolveUrl(to, fromUrl)
}
if (to instanceof URL) {
return String(to)
}
throw new TypeError(`symbolic link to must be a string or an url, received ${to}`)
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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