Skip to content

Commit

Permalink
fix: find-root debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Nov 1, 2023
1 parent ca475f5 commit b10562d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/chai-as-promised": "^7.1.5",
"@types/clean-stack": "^2.1.1",
"@types/cli-progress": "^3.11.0",
"@types/debug": "^4.1.10",
"@types/ejs": "^3.1.3",
"@types/indent-string": "^4.0.1",
"@types/js-yaml": "^3.12.7",
Expand Down
2 changes: 1 addition & 1 deletion src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class Plugin implements IPlugin {
this.type === 'link' && !this.parent ? this.options.root : await findRoot(this.options.name, this.options.root)
if (!root) throw new CLIError(`could not find package.json with ${inspect(this.options)}`)
this.root = root
this._debug('reading %s plugin %s', this.type, root)
this._debug(`loading ${this.type} plugin from ${root}`)
this.pjson = await readJson(join(root, 'package.json'))
this.flexibleTaxonomy = this.options?.flexibleTaxonomy || this.pjson.oclif?.flexibleTaxonomy || false
this.moduleType = this.pjson.type === 'module' ? 'module' : 'commonjs'
Expand Down
32 changes: 27 additions & 5 deletions src/util/find-root.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/* eslint-disable no-await-in-loop */
import type {PackageInformation, PackageLocator, getPackageInformation} from 'pnpapi'

import makeDebug from 'debug'
import {basename, dirname, join} from 'node:path'

import {PJSON} from '../interfaces'
import {safeReadJson} from './fs'

const debug = makeDebug('find-root')

// essentially just "cd .."
function* up(from: string) {
while (dirname(from) !== from) {
Expand All @@ -26,7 +29,9 @@ async function findPluginRoot(root: string, name?: string) {
// If we know the plugin name then we just need to traverse the file
// system until we find the directory that matches the plugin name.
if (name) {
debug.extend(name)(`Finding root starting at ${root}`)
for (const next of up(root)) {
debug.extend(name)(`Checking ${next}`)
if (next.endsWith(basename(name))) return next
}
}
Expand All @@ -44,6 +49,7 @@ async function findPluginRoot(root: string, name?: string) {

try {
const cur = join(next, 'package.json')
debug.extend('root-plugin')(`Checking ${cur}`)
if (await safeReadJson<PJSON>(cur)) return dirname(cur)
} catch {}
}
Expand All @@ -56,6 +62,7 @@ async function findPluginRoot(root: string, name?: string) {
* See https://github.com/oclif/config/pull/289#issuecomment-983904051
*/
async function findRootLegacy(name: string | undefined, root: string): Promise<string | undefined> {
debug.extend(name ?? 'root-plugin')('Finding root using legacy method')
for (const next of up(root)) {
let cur
if (name) {
Expand Down Expand Up @@ -106,9 +113,9 @@ const isPeerDependency = (
*/
function findPnpRoot(name: string, root: string): string | undefined {
maybeRequirePnpApi(root)

if (!pnp) return

debug.extend(name)('Finding root for using pnp method')
const seen = new Set()

const traverseDependencyTree = (locator: PackageLocator, parentPkg?: PackageInformation): string | undefined => {
Expand Down Expand Up @@ -165,15 +172,30 @@ function findPnpRoot(name: string, root: string): string | undefined {
*/
export async function findRoot(name: string | undefined, root: string) {
if (name) {
debug.extend(name)(`Finding root using ${root}`)
let pkgPath
try {
pkgPath = require.resolve(name, {paths: [root]})
} catch {}
debug.extend(name)(`Found starting point with require.resolve`)
} catch {
debug.extend(name)(`require.resolve could not find plugin starting point`)
}

if (pkgPath) return findPluginRoot(dirname(pkgPath), name)
if (pkgPath) {
const found = await findPluginRoot(dirname(pkgPath), name)
if (found) {
debug.extend(name)(`Found root at ${found}`)
return found
}
}

return process.versions.pnp ? findPnpRoot(name, root) : findRootLegacy(name, root)
const found = process.versions.pnp ? findPnpRoot(name, root) : await findRootLegacy(name, root)
debug.extend(name)(found ? `Found root at ${found}` : 'No root found!')
return found
}

return findPluginRoot(root)
debug.extend('root-plugin')(`Finding root plugin using ${root}`)
const found = await findPluginRoot(root)
debug.extend('root-plugin')(found ? `Found root at ${found}` : 'No root found!')
return found
}
3 changes: 0 additions & 3 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs'
import {readFile, stat} from 'node:fs/promises'
import {join} from 'node:path'

const debug = require('debug')

export function requireJson<T>(...pathParts: string[]): T {
return JSON.parse(readFileSync(join(...pathParts), 'utf8'))
}
Expand Down Expand Up @@ -51,7 +49,6 @@ export const fileExists = async (input: string): Promise<string> => {
}

export async function readJson<T = unknown>(path: string): Promise<T> {
debug('config')('readJson %s', path)
const contents = await readFile(path, 'utf8')
return JSON.parse(contents) as T
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,13 @@
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==

"@types/debug@^4.1.10":
version "4.1.10"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.10.tgz#f23148a6eb771a34c466a4fc28379d8101e84494"
integrity sha512-tOSCru6s732pofZ+sMv9o4o3Zc+Sa8l3bxd/tweTQudFn06vAzb13ZX46Zi6m6EJ+RUbRTHvgQJ1gBtSgkaUYA==
dependencies:
"@types/ms" "*"

"@types/ejs@^3.1.3":
version "3.1.3"
resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.1.3.tgz#ad91d1dd6e24fb60bbf96c534bce58b95eef9b57"
Expand Down Expand Up @@ -1077,6 +1084,11 @@
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-10.0.2.tgz#96d63314255540a36bf24da094cce7a13668d73b"
integrity sha512-NaHL0+0lLNhX6d9rs+NSt97WH/gIlRHmszXbQ/8/MV/eVcFNdeJ/GYhrFuUc8K7WuPhRhTSdMkCp8VMzhUq85w==

"@types/ms@*":
version "0.7.33"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.33.tgz#80bf1da64b15f21fd8c1dc387c31929317d99ee9"
integrity sha512-AuHIyzR5Hea7ij0P9q7vx7xu4z0C28ucwjAZC0ja7JhINyCnOw8/DnvAPQQ9TfOlCtZAmCERKQX9+o1mgQhuOQ==

"@types/node-notifier@^8.0.2":
version "8.0.2"
resolved "https://registry.yarnpkg.com/@types/node-notifier/-/node-notifier-8.0.2.tgz#77c5de29c6e8adb915222b01864128cc3e78d553"
Expand Down

0 comments on commit b10562d

Please sign in to comment.