From 1a483fa80178a772af3ce7bbdc7c58969e5e7c51 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 19 Oct 2023 11:35:27 -0600 Subject: [PATCH] chore: update fs and path imports --- src/commands/plugins/inspect.ts | 16 ++++++++-------- src/plugins.ts | 29 ++++++++++++++--------------- src/yarn.ts | 6 +++--- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/commands/plugins/inspect.ts b/src/commands/plugins/inspect.ts index 2cdfd45e..27a6e521 100644 --- a/src/commands/plugins/inspect.ts +++ b/src/commands/plugins/inspect.ts @@ -1,13 +1,13 @@ import {Args, Command, Flags, Plugin, ux} from '@oclif/core' import chalk from 'chalk' import {readFile} from 'node:fs/promises' -import * as path from 'node:path' +import {dirname, join, sep} from 'node:path' import Plugins from '../../plugins.js' import {sortBy} from '../../util.js' function trimUntil(fsPath: string, part: string): string { - const parts = fsPath.split(path.sep) + const parts = fsPath.split(sep) // eslint-disable-next-line unicorn/no-array-reduce const indices = parts.reduce( (result, current, index) => (current === part ? [...result, index] : result), @@ -15,7 +15,7 @@ function trimUntil(fsPath: string, part: string): string { ) const partIndex = Math.max(...indices) if (partIndex === -1) return fsPath - return parts.slice(0, partIndex + 1).join(path.sep) + return parts.slice(0, partIndex + 1).join(sep) } type Dependencies = Record @@ -50,19 +50,19 @@ export default class PluginsInspect extends Command { // In this case we want these operations to happen // sequentially so the `no-await-in-loop` rule is ignored async findDep(plugin: Plugin, dependency: string): Promise<{pkgPath: null | string; version: null | string}> { - const dependencyPath = path.join(...dependency.split('/')) - let start = path.join(plugin.root, 'node_modules') + const dependencyPath = join(...dependency.split('/')) + let start = join(plugin.root, 'node_modules') const paths = [start] while ((start.match(/node_modules/g) ?? []).length > 1) { - start = trimUntil(path.dirname(start), 'node_modules') + start = trimUntil(dirname(start), 'node_modules') paths.push(start) } // TODO: use promise.any to check the paths in parallel // requires node >= 16 for (const p of paths) { - const fullPath = path.join(p, dependencyPath) - const pkgJsonPath = path.join(fullPath, 'package.json') + const fullPath = join(p, dependencyPath) + const pkgJsonPath = join(fullPath, 'package.json') try { // eslint-disable-next-line no-await-in-loop const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf8')) diff --git a/src/plugins.ts b/src/plugins.ts index 78babab8..136b6f66 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -1,8 +1,7 @@ import {Config, Errors, Interfaces, ux} from '@oclif/core' import makeDebug from 'debug' -import * as fs from 'node:fs' -import {readFile} from 'node:fs/promises' -import * as path from 'node:path' +import {access, mkdir, readFile, rename, writeFile} from 'node:fs/promises' +import {dirname, join, resolve} from 'node:path' import {gt, valid, validRange} from 'semver' import {findNode, findNpm, uniq, uniqWith} from './util.js' @@ -25,7 +24,7 @@ const initPJSON: UserPJSON = { async function fileExists(filePath: string): Promise { try { - await fs.promises.access(filePath) + await access(filePath) return true } catch { return false @@ -68,7 +67,7 @@ export default class Plugins { const list = await this.list() const friendly = list.find((p) => this.friendlyName(p.name) === this.friendlyName(name)) const unfriendly = list.find((p) => this.unfriendlyName(p.name) === this.unfriendlyName(name)) - const link = list.find((p) => p.type === 'link' && path.resolve(p.root) === path.resolve(name)) + const link = list.find((p) => p.type === 'link' && resolve(p.root) === resolve(name)) return (friendly ?? unfriendly ?? link ?? false) as | Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.User @@ -91,7 +90,7 @@ export default class Plugins { plugin = await Config.load({ devPlugins: false, name, - root: path.join(this.config.dataDir, 'node_modules', name), + root: join(this.config.dataDir, 'node_modules', name), userPlugins: false, }) await this.refresh({all: true, prod: true}, plugin.root) @@ -124,7 +123,7 @@ export default class Plugins { plugin = await Config.load({ devPlugins: false, name, - root: path.join(this.config.dataDir, 'node_modules', name), + root: join(this.config.dataDir, 'node_modules', name), userPlugins: false, }) this.debug(`finished loading plugin ${name} at root ${plugin.root}`) @@ -153,7 +152,7 @@ export default class Plugins { } async link(p: string, {install}: {install: boolean}): Promise { - const c = await Config.load(path.resolve(p)) + const c = await Config.load(resolve(p)) ux.action.start(`${this.config.name}: linking plugin ${c.name}`) this.isValidPlugin(c) @@ -231,12 +230,12 @@ export default class Plugins { const deduped = [...new Set(pluginRoots)] await Promise.all( deduped.map(async (r) => { - if (await fileExists(path.join(r, 'yarn.lock'))) { + if (await fileExists(join(r, 'yarn.lock'))) { this.debug(`yarn.lock exists at ${r}. Installing prod dependencies`) await doRefresh(r) - } else if (await fileExists(path.join(r, 'oclif.lock'))) { + } else if (await fileExists(join(r, 'oclif.lock'))) { this.debug(`oclif.lock exists at ${r}. Installing prod dependencies`) - await fs.promises.rename(path.join(r, 'oclif.lock'), path.join(r, 'yarn.lock')) + await rename(join(r, 'oclif.lock'), join(r, 'yarn.lock')) await doRefresh(r) } else { this.debug(`no yarn.lock or oclif.lock exists at ${r}. Skipping dependency refresh`) @@ -338,7 +337,7 @@ export default class Plugins { } private async createPJSON() { - if (!fs.existsSync(this.pjsonPath)) { + if (await fileExists(this.pjsonPath)) { this.debug(`creating ${this.pjsonPath} with pjson: ${JSON.stringify(initPJSON, null, 2)}`) await this.savePJSON(initPJSON) } @@ -411,7 +410,7 @@ export default class Plugins { } private get pjsonPath() { - return path.join(this.config.dataDir, 'package.json') + return join(this.config.dataDir, 'package.json') } private async readPJSON(): Promise { @@ -426,7 +425,7 @@ export default class Plugins { private async savePJSON(pjson: UserPJSON) { this.debug(`saving pjson at ${this.pjsonPath}`, JSON.stringify(pjson, null, 2)) - await fs.promises.mkdir(path.dirname(this.pjsonPath), {recursive: true}) - await fs.promises.writeFile(this.pjsonPath, JSON.stringify(pjson, null, 2)) + await mkdir(dirname(this.pjsonPath), {recursive: true}) + await writeFile(this.pjsonPath, JSON.stringify(pjson, null, 2)) } } diff --git a/src/yarn.ts b/src/yarn.ts index 9de76c33..7db64d55 100644 --- a/src/yarn.ts +++ b/src/yarn.ts @@ -2,7 +2,7 @@ import {Interfaces, ux} from '@oclif/core' import makeDebug from 'debug' import {fork} from 'node:child_process' import {createRequire} from 'node:module' -import * as path from 'node:path' +import {join} from 'node:path' import NpmRunPath from 'npm-run-path' import {WarningsCache} from './util.js' @@ -38,8 +38,8 @@ export default class Yarn { const optionalPort = port ? `:${port}` : '' const mutex = this.config.scopedEnvVar('USE_NETWORK_MUTEX') ? `network${optionalPort}` - : `file:${path.join(cwd, 'yarn.lock')}` - const cacheDir = path.join(this.config.cacheDir, 'yarn') + : `file:${join(cwd, 'yarn.lock')}` + const cacheDir = join(this.config.cacheDir, 'yarn') args = [...args, '--non-interactive', `--mutex=${mutex}`, `--preferred-cache-folder=${cacheDir}`, '--check-files'] const networkTimeout = this.config.scopedEnvVar('NETWORK_TIMEOUT')