Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sync): push @oclif/config dependency resolution changes upstream #309

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {Manifest} from '../interfaces/manifest'
import {PJSON} from '../interfaces/pjson'
import {Topic} from '../interfaces/topic'
import {tsPath} from './ts-node'
import {compact, exists, flatMap, loadJSON, mapValues} from './util'
import {compact, exists, resolvePackage, flatMap, loadJSON, mapValues} from './util'
import {isProd} from '../util'
import ModuleLoader from '../module-loader'

Expand All @@ -30,25 +30,36 @@ function topicsToArray(input: any, base?: string): Topic[] {
})
}

// eslint-disable-next-line valid-jsdoc
// essentially just "cd .."
function * up(from: string) {
while (path.dirname(from) !== from) {
yield from
from = path.dirname(from)
}

yield from
}

async function findSourcesRoot(root: string) {
for (const next of up(root)) {
const cur = path.join(next, 'package.json')
// eslint-disable-next-line no-await-in-loop
if (await exists(cur)) return path.dirname(cur)
}
}

/**
* @returns string
* @param name string
* @param root string
* find package root
* for packages installed into node_modules this will go up directories until
* it finds a node_modules directory with the plugin installed into it
*
* This is needed because of the deduping npm does
* This is needed because some oclif plugins do not declare the `main` field in their package.json
* https://github.com/oclif/config/pull/289#issuecomment-983904051
*/
async function findRoot(name: string | undefined, root: string) {
// essentially just "cd .."
function * up(from: string) {
while (path.dirname(from) !== from) {
yield from
from = path.dirname(from)
}

yield from
}

async function findRootLegacy(name: string | undefined, root: string): Promise<string | undefined> {
for (const next of up(root)) {
let cur
if (name) {
Expand All @@ -68,6 +79,19 @@ async function findRoot(name: string | undefined, root: string) {
}
}

async function findRoot(name: string | undefined, root: string) {
if (name) {
let pkgPath
try {
pkgPath = resolvePackage(name, {paths: [__dirname, root]})
} catch {}

return pkgPath ? findSourcesRoot(path.dirname(pkgPath)) : findRootLegacy(name, root)
}

return findSourcesRoot(root)
}

export class Plugin implements IPlugin {
// static loadedPlugins: {[name: string]: Plugin} = {}
_base = `${_pjson.name}@${_pjson.version}`
Expand Down
4 changes: 4 additions & 0 deletions src/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export function exists(path: string): Promise<boolean> {
return new Promise(resolve => resolve(fs.existsSync(path)))
}

export function resolvePackage(id: string, paths: { paths: string[] }): string {
return require.resolve(id, paths)
}

export function loadJSON(path: string): Promise<any> {
debug('config')('loadJSON %s', path)
return new Promise((resolve, reject) => {
Expand Down