Skip to content

Commit

Permalink
chore: update fs and path imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 19, 2023
1 parent aaab8ec commit 1a483fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
16 changes: 8 additions & 8 deletions src/commands/plugins/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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<number[]>(
(result, current, index) => (current === part ? [...result, index] : result),
[],
)
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<string, {from: string; version: string}>
Expand Down Expand Up @@ -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'))
Expand Down
29 changes: 14 additions & 15 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -25,7 +24,7 @@ const initPJSON: UserPJSON = {

async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.promises.access(filePath)
await access(filePath)
return true
} catch {
return false
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -153,7 +152,7 @@ export default class Plugins {
}

async link(p: string, {install}: {install: boolean}): Promise<void> {
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)

Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<Interfaces.PJSON.User | undefined> {
Expand All @@ -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))
}
}
6 changes: 3 additions & 3 deletions src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 1a483fa

Please sign in to comment.