Skip to content

Commit

Permalink
deps: @npmcli/fs@3.1.0
Browse files Browse the repository at this point in the history
This also removes `readdir-scoped-modules` and `@npmcli/fs` since those
are now a part of `@npmcli/fs`
  • Loading branch information
lukekarrys committed Nov 3, 2022
1 parent b8292d8 commit 8c2424f
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 34 deletions.
5 changes: 2 additions & 3 deletions lib/utils/completion/installed-shallow.js
@@ -1,8 +1,7 @@
const { promisify } = require('util')
const readdir = promisify(require('readdir-scoped-modules'))
const { readdirScoped } = require('@npmcli/fs')

const installedShallow = async (npm, opts) => {
const names = global => readdir(global ? npm.globalDir : npm.localDir)
const names = global => readdirScoped(global ? npm.globalDir : npm.localDir)
const { conf: { argv: { remain } } } = opts
if (remain.length > 3) {
return null
Expand Down
4 changes: 4 additions & 0 deletions node_modules/@npmcli/fs/lib/index.js
Expand Up @@ -2,8 +2,12 @@

const cp = require('./cp/index.js')
const withTempDir = require('./with-temp-dir.js')
const readdirScoped = require('./readdir-scoped.js')
const moveFile = require('./move-file.js')

module.exports = {
cp,
withTempDir,
readdirScoped,
moveFile,
}
78 changes: 78 additions & 0 deletions node_modules/@npmcli/fs/lib/move-file.js
@@ -0,0 +1,78 @@
const { dirname, join, resolve, relative, isAbsolute } = require('path')
const fs = require('fs/promises')

const pathExists = async path => {
try {
await fs.access(path)
return true
} catch (er) {
return er.code !== 'ENOENT'
}
}

const moveFile = async (source, destination, options = {}, root = true, symlinks = []) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required')
}

options = {
overwrite: true,
...options,
}

if (!options.overwrite && await pathExists(destination)) {
throw new Error(`The destination file exists: ${destination}`)
}

await fs.mkdir(dirname(destination), { recursive: true })

try {
await fs.rename(source, destination)
} catch (error) {
if (error.code === 'EXDEV' || error.code === 'EPERM') {
const sourceStat = await fs.lstat(source)
if (sourceStat.isDirectory()) {
const files = await fs.readdir(source)
await Promise.all(files.map((file) =>
moveFile(join(source, file), join(destination, file), options, false, symlinks)
))
} else if (sourceStat.isSymbolicLink()) {
symlinks.push({ source, destination })
} else {
await fs.copyFile(source, destination)
}
} else {
throw error
}
}

if (root) {
await Promise.all(symlinks.map(async ({ source: symSource, destination: symDestination }) => {
let target = await fs.readlink(symSource)
// junction symlinks in windows will be absolute paths, so we need to
// make sure they point to the symlink destination
if (isAbsolute(target)) {
target = resolve(symDestination, relative(symSource, target))
}
// try to determine what the actual file is so we can create the correct
// type of symlink in windows
let targetStat = 'file'
try {
targetStat = await fs.stat(resolve(dirname(symSource), target))
if (targetStat.isDirectory()) {
targetStat = 'junction'
}
} catch {
// targetStat remains 'file'
}
await fs.symlink(
target,
symDestination,
targetStat
)
}))
await fs.rm(source, { recursive: true, force: true })
}
}

module.exports = moveFile
20 changes: 20 additions & 0 deletions node_modules/@npmcli/fs/lib/readdir-scoped.js
@@ -0,0 +1,20 @@
const { readdir } = require('fs/promises')
const { join } = require('path')

const readdirScoped = async (dir) => {
const results = []

for (const item of await readdir(dir)) {
if (item.startsWith('@')) {
for (const scopedItem of await readdir(join(dir, item))) {
results.push(join(item, scopedItem))
}
} else {
results.push(item)
}
}

return results
}

module.exports = readdirScoped
8 changes: 4 additions & 4 deletions node_modules/@npmcli/fs/package.json
@@ -1,6 +1,6 @@
{
"name": "@npmcli/fs",
"version": "3.0.0",
"version": "3.1.0",
"description": "filesystem utilities for the npm cli",
"main": "lib/index.js",
"files": [
Expand Down Expand Up @@ -29,8 +29,8 @@
"author": "GitHub Inc.",
"license": "ISC",
"devDependencies": {
"@npmcli/eslint-config": "^3.0.1",
"@npmcli/template-oss": "4.5.1",
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/template-oss": "4.8.0",
"tap": "^16.0.1"
},
"dependencies": {
Expand All @@ -41,7 +41,7 @@
},
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "4.5.1"
"version": "4.8.0"
},
"tap": {
"nyc-arg": [
Expand Down
21 changes: 9 additions & 12 deletions package-lock.json
Expand Up @@ -66,7 +66,6 @@
"read",
"read-package-json",
"read-package-json-fast",
"readdir-scoped-modules",
"rimraf",
"semver",
"ssri",
Expand Down Expand Up @@ -143,7 +142,6 @@
"read": "~1.0.7",
"read-package-json": "^6.0.0",
"read-package-json-fast": "^3.0.1",
"readdir-scoped-modules": "^1.1.0",
"rimraf": "^3.0.2",
"semver": "^7.3.8",
"ssri": "^10.0.0",
Expand All @@ -162,7 +160,7 @@
"devDependencies": {
"@npmcli/docs": "^1.0.0",
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/fs": "^3.0.0",
"@npmcli/fs": "^3.1.0",
"@npmcli/git": "^4.0.1",
"@npmcli/promise-spawn": "^6.0.1",
"@npmcli/template-oss": "4.8.0",
Expand Down Expand Up @@ -2106,9 +2104,9 @@
}
},
"node_modules/@npmcli/fs": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-3.0.0.tgz",
"integrity": "sha512-GdeVD+dnBxzMslTFvnctLX5yIqV4ZNZBWNbo1OejQ++bZpnFNQ1AjOn9Sboi+LzheQbCBU1ts1mhEVduHrcZOQ==",
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-3.1.0.tgz",
"integrity": "sha512-7kZUAaLscfgbwBQRbvdMYaZOWyMEcPTH/tJjnyAWJ/dvvs9Ef+CERx/qJb9GExJpl1qipaDGn7KqHnFGGixd0w==",
"inBundle": true,
"dependencies": {
"semver": "^7.3.5"
Expand Down Expand Up @@ -2874,7 +2872,7 @@
},
"node_modules/asap": {
"version": "2.0.6",
"inBundle": true,
"dev": true,
"license": "MIT"
},
"node_modules/async-hook-domain": {
Expand Down Expand Up @@ -3874,7 +3872,7 @@
},
"node_modules/debuglog": {
"version": "1.0.1",
"inBundle": true,
"dev": true,
"license": "MIT",
"engines": {
"node": "*"
Expand Down Expand Up @@ -4035,7 +4033,7 @@
},
"node_modules/dezalgo": {
"version": "1.0.4",
"inBundle": true,
"dev": true,
"license": "ISC",
"dependencies": {
"asap": "^2.0.0",
Expand Down Expand Up @@ -10049,7 +10047,7 @@
},
"node_modules/readdir-scoped-modules": {
"version": "1.1.0",
"inBundle": true,
"dev": true,
"license": "ISC",
"dependencies": {
"debuglog": "^1.0.1",
Expand Down Expand Up @@ -14007,10 +14005,10 @@
"license": "ISC",
"dependencies": {
"@isaacs/string-locale-compare": "^1.1.0",
"@npmcli/fs": "^3.1.0",
"@npmcli/installed-package-contents": "^2.0.0",
"@npmcli/map-workspaces": "^3.0.0",
"@npmcli/metavuln-calculator": "^5.0.0",
"@npmcli/move-file": "^3.0.0",
"@npmcli/name-from-folder": "^1.0.1",
"@npmcli/node-gyp": "^3.0.0",
"@npmcli/package-json": "^3.0.0",
Expand All @@ -14035,7 +14033,6 @@
"promise-all-reject-late": "^1.0.0",
"promise-call-limit": "^1.0.1",
"read-package-json-fast": "^3.0.1",
"readdir-scoped-modules": "^1.1.0",
"semver": "^7.3.7",
"ssri": "^10.0.0",
"treeverse": "^3.0.0",
Expand Down
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -113,7 +113,6 @@
"read": "~1.0.7",
"read-package-json": "^6.0.0",
"read-package-json-fast": "^3.0.1",
"readdir-scoped-modules": "^1.1.0",
"rimraf": "^3.0.2",
"semver": "^7.3.8",
"ssri": "^10.0.0",
Expand Down Expand Up @@ -184,7 +183,6 @@
"read",
"read-package-json",
"read-package-json-fast",
"readdir-scoped-modules",
"rimraf",
"semver",
"ssri",
Expand All @@ -199,7 +197,7 @@
"devDependencies": {
"@npmcli/docs": "^1.0.0",
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/fs": "^3.0.0",
"@npmcli/fs": "^3.1.0",
"@npmcli/git": "^4.0.1",
"@npmcli/promise-spawn": "^6.0.1",
"@npmcli/template-oss": "4.8.0",
Expand Down
5 changes: 2 additions & 3 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Expand Up @@ -7,9 +7,8 @@ const cacache = require('cacache')
const promiseCallLimit = require('promise-call-limit')
const realpath = require('../../lib/realpath.js')
const { resolve, dirname } = require('path')
const { promisify } = require('util')
const treeCheck = require('../tree-check.js')
const readdir = promisify(require('readdir-scoped-modules'))
const { readdirScoped } = require('@npmcli/fs')
const { lstat, readlink } = require('fs/promises')
const { depth } = require('treeverse')
const log = require('proc-log')
Expand Down Expand Up @@ -447,7 +446,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
const globalExplicitUpdateNames = []
if (this[_global] && (this[_updateAll] || this[_updateNames].length)) {
const nm = resolve(this.path, 'node_modules')
for (const name of await readdir(nm).catch(() => [])) {
for (const name of await readdirScoped(nm).catch(() => [])) {
tree.package.dependencies = tree.package.dependencies || {}
const updateName = this[_updateNames].includes(name)
if (this[_updateAll] || updateName) {
Expand Down
7 changes: 3 additions & 4 deletions workspaces/arborist/lib/arborist/load-actual.js
Expand Up @@ -3,8 +3,7 @@
const { relative, dirname, resolve, join, normalize } = require('path')

const rpj = require('read-package-json-fast')
const { promisify } = require('util')
const readdir = promisify(require('readdir-scoped-modules'))
const { readdirScoped } = require('@npmcli/fs')
const walkUp = require('walk-up-path')
const ancestorPath = require('common-ancestor-path')
const treeCheck = require('../tree-check.js')
Expand Down Expand Up @@ -362,7 +361,7 @@ module.exports = cls => class ActualLoader extends cls {
async [_loadFSChildren] (node) {
const nm = resolve(node.realpath, 'node_modules')
try {
const kids = await readdir(nm)
const kids = await readdirScoped(nm)
return Promise.all(
// ignore . dirs and retired scoped package folders
kids.filter(kid => !/^(@[^/]+\/)?\./.test(kid))
Expand Down Expand Up @@ -412,7 +411,7 @@ module.exports = cls => class ActualLoader extends cls {
}

const entries = nmContents.get(p) ||
await readdir(p + '/node_modules').catch(() => [])
await readdirScoped(p + '/node_modules').catch(() => [])
nmContents.set(p, entries)
if (!entries.includes(name)) {
continue
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/arborist/reify.js
Expand Up @@ -19,7 +19,7 @@ const {
rm,
symlink,
} = require('fs/promises')
const moveFile = require('@npmcli/move-file')
const { moveFile } = require('@npmcli/fs')
const PackageJson = require('@npmcli/package-json')
const packageContents = require('@npmcli/installed-package-contents')
const runScript = require('@npmcli/run-script')
Expand Down
3 changes: 1 addition & 2 deletions workspaces/arborist/package.json
Expand Up @@ -4,10 +4,10 @@
"description": "Manage node_modules trees",
"dependencies": {
"@isaacs/string-locale-compare": "^1.1.0",
"@npmcli/fs": "^3.1.0",
"@npmcli/installed-package-contents": "^2.0.0",
"@npmcli/map-workspaces": "^3.0.0",
"@npmcli/metavuln-calculator": "^5.0.0",
"@npmcli/move-file": "^3.0.0",
"@npmcli/name-from-folder": "^1.0.1",
"@npmcli/node-gyp": "^3.0.0",
"@npmcli/package-json": "^3.0.0",
Expand All @@ -32,7 +32,6 @@
"promise-all-reject-late": "^1.0.0",
"promise-call-limit": "^1.0.1",
"read-package-json-fast": "^3.0.1",
"readdir-scoped-modules": "^1.1.0",
"semver": "^7.3.7",
"ssri": "^10.0.0",
"treeverse": "^3.0.0",
Expand Down
15 changes: 13 additions & 2 deletions workspaces/arborist/test/arborist/reify.js
Expand Up @@ -52,6 +52,17 @@ const mocks = {

return fs.promises.mkdir(...args)
},
rename: async (...args) => {
if (failRename) {
throw failRename
} else if (failRenameOnce) {
const er = failRenameOnce
failRenameOnce = null
throw er
} else {
return fs.promises.rename(...args)
}
},
rm: async (...args) => {
if (failRm) {
throw new Error('rm fail')
Expand All @@ -74,8 +85,8 @@ This is a one-time fix-up, please be patient...
]

// need this to be injected so that it doesn't pull from main cache
const moveFile = t.mock('@npmcli/move-file', { fs: fsMock })
mocks['@npmcli/move-file'] = moveFile
const { moveFile } = t.mock('@npmcli/fs', { 'fs/promises': mocks['fs/promises'] })
mocks['@npmcli/fs'] = { moveFile }

// track the warnings that are emitted. returns a function that removes
// the listener and provides the list of what it saw.
Expand Down

0 comments on commit 8c2424f

Please sign in to comment.