Skip to content

Commit

Permalink
feat: use core v4
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed May 23, 2024
1 parent cbd3ef8 commit 4d3852e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Salesforce",
"bugs": "https://github.com/oclif/plugin-plugins/issues",
"dependencies": {
"@oclif/core": "^3.26.6",
"@oclif/core": "4.0.0-beta.10",
"chalk": "^5.3.0",
"debug": "^4.3.4",
"npm": "^10.8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class NPM {
if (this.config.npmRegistry) args.push(`--registry=${this.config.npmRegistry}`)

if (options.logLevel !== 'notice' && options.logLevel !== 'silent') {
ux.logToStderr(`${options.cwd}: ${bin} ${args.join(' ')}`)
ux.stderr(`${options.cwd}: ${bin} ${args.join(' ')}`)
}

debug(`${options.cwd}: ${bin} ${args.join(' ')}`)
Expand Down
70 changes: 36 additions & 34 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ import {Output} from './spawn.js'
import {uniqWith} from './util.js'
import {Yarn} from './yarn.js'

type Plugin = Interfaces.LinkedPlugin | Interfaces.UserPlugin

type UserPJSON = {
dependencies: Record<string, string>
oclif: {
plugins: Array<Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User>
plugins: Plugin[]
schema: number
}
private: boolean
}

type NormalizedUserPJSON = {
dependencies: Record<string, string>
oclif: {
plugins: Plugin[]
schema: number
}
private: boolean
Expand All @@ -37,14 +48,8 @@ async function fileExists(filePath: string): Promise<boolean> {
}
}

function dedupePlugins(
plugins: Interfaces.PJSON.PluginTypes[],
): (Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User)[] {
return uniqWith(
plugins,
// @ts-expect-error because typescript doesn't think it's possible for a plugin to have the `link` type here
(a, b) => a.name === b.name || (a.type === 'link' && b.type === 'link' && a.root === b.root),
) as (Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User)[]
function dedupePlugins(plugins: Plugin[]): Plugin[] {
return uniqWith(plugins, (a, b) => a.name === b.name || (a.type === 'link' && b.type === 'link' && a.root === b.root))
}

function extractIssuesLocation(
Expand All @@ -63,10 +68,10 @@ function extractIssuesLocation(
function notifyUser(plugin: Config, output: Output): void {
const containsWarnings = [...output.stdout, ...output.stderr].some((l) => l.includes('npm WARN'))
if (containsWarnings) {
ux.logToStderr(chalk.bold.yellow(`\nThese warnings can only be addressed by the owner(s) of ${plugin.name}.`))
ux.stderr(chalk.bold.yellow(`\nThese warnings can only be addressed by the owner(s) of ${plugin.name}.`))

if (plugin.pjson.bugs || plugin.pjson.repository) {
ux.logToStderr(
ux.stderr(
`We suggest that you create an issue at ${extractIssuesLocation(
plugin.pjson.bugs,
plugin.pjson.repository,
Expand All @@ -93,7 +98,7 @@ export default class Plugins {
})
}

public async add(...plugins: Interfaces.PJSON.PluginTypes[]): Promise<void> {
public async add(...plugins: Plugin[]): Promise<void> {
const pjson = await this.pjson()
const mergedPlugins = [...(pjson.oclif.plugins || []), ...plugins] as typeof pjson.oclif.plugins
await this.savePJSON({
Expand All @@ -112,9 +117,7 @@ export default class Plugins {
return match?.[1] ?? name
}

public async hasPlugin(
name: string,
): Promise<Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User | false> {
public async hasPlugin(name: string): Promise<Plugin | false> {
const list = await this.list()
const friendlyName = this.friendlyName(name)
const unfriendlyName = this.unfriendlyName(name) ?? name
Expand Down Expand Up @@ -263,7 +266,7 @@ export default class Plugins {
return c
}

public async list(): Promise<(Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User)[]> {
public async list(): Promise<Plugin[]> {
const pjson = await this.pjson()
return pjson.oclif.plugins
}
Expand All @@ -280,7 +283,7 @@ export default class Plugins {
return name
}

public async pjson(): Promise<UserPJSON> {
public async pjson(): Promise<NormalizedUserPJSON> {
const pjson = await this.readPJSON()
const plugins = pjson ? normalizePlugins(pjson.oclif.plugins) : []
return {
Expand Down Expand Up @@ -330,7 +333,7 @@ export default class Plugins {
}

public async update(): Promise<void> {
let plugins = (await this.list()).filter((p): p is Interfaces.PJSON.PluginTypes.User => p.type === 'user')
let plugins = (await this.list()).filter((p): p is Interfaces.UserPlugin => p.type === 'user')
if (plugins.length === 0) return

await this.maybeCleanUp()
Expand Down Expand Up @@ -360,7 +363,7 @@ export default class Plugins {

const npmPlugins = plugins.filter((p) => !p.url)
const jitPlugins = this.config.pjson.oclif.jitPlugins ?? {}
const modifiedPlugins: Interfaces.PJSON.PluginTypes[] = []
const modifiedPlugins: Plugin[] = []
if (npmPlugins.length > 0) {
await this.npm.install(
npmPlugins.map((p) => {
Expand Down Expand Up @@ -455,9 +458,9 @@ export default class Plugins {
return join(this.config.dataDir, 'package.json')
}

private async readPJSON(): Promise<Interfaces.PJSON.User | undefined> {
private async readPJSON(): Promise<UserPJSON | undefined> {
try {
return JSON.parse(await readFile(this.pjsonPath, 'utf8')) as Interfaces.PJSON.User
return JSON.parse(await readFile(this.pjsonPath, 'utf8')) as UserPJSON
} catch (error: unknown) {
this.debug(error)
const err = error as {code?: string} & Error
Expand All @@ -473,17 +476,16 @@ export default class Plugins {
}

// if the plugin is a simple string, convert it to an object
const normalizePlugins = (
input: Interfaces.PJSON.User['oclif']['plugins'],
): (Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User)[] =>
dedupePlugins(
(input ?? []).map((p) =>
typeof p === 'string'
? {
name: p,
tag: 'latest',
type: 'user' as const,
}
: p,
),
const normalizePlugins = (input: Plugin[]): Plugin[] => {
const normalized = (input ?? []).map((p) =>
typeof p === 'string'
? {
name: p,
tag: 'latest',
type: 'user' as const,
}
: p,
)

return dedupePlugins(normalized)
}
4 changes: 2 additions & 2 deletions src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function spawn(modulePath: string, args: string[] = [], {cwd, logLe
stderr.push(output)
if (shouldPrint(output)) {
loggedStderr.push(output)
ux.log(output)
ux.stdout(output)
} else debug(output)
})

Expand All @@ -74,7 +74,7 @@ export async function spawn(modulePath: string, args: string[] = [], {cwd, logLe
stdout.push(output)
if (shouldPrint(output)) {
loggedStdout.push(output)
ux.log(output)
ux.stdout(output)
} else debug(output)
})

Expand Down
2 changes: 1 addition & 1 deletion src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Yarn {
if (this.config.npmRegistry) args.push(`--registry=${this.config.npmRegistry}`)

if (options.logLevel !== 'notice' && options.logLevel !== 'silent') {
ux.logToStderr(`${options.cwd}: ${bin} ${args.join(' ')}`)
ux.stderr(`${options.cwd}: ${bin} ${args.join(' ')}`)
}

debug(`${options.cwd}: ${bin} ${args.join(' ')}`)
Expand Down
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,29 @@
proc-log "^4.0.0"
which "^4.0.0"

"@oclif/[email protected]":
version "4.0.0-beta.10"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-4.0.0-beta.10.tgz#809c9f7b5639aacc163944b24ec0ca706cdbff3c"
integrity sha512-Nuzh6ySmNSQQYc/nfup8quaK6ZEZQq59ddljVgCqyYyHP4AvpeGmIh/9aBUUjdI2jScpj6Sck4erKFrVPKbLsQ==
dependencies:
ansi-escapes "^4.3.2"
ansis "^3.0.1"
clean-stack "^3.0.1"
cli-spinners "^2.9.2"
cosmiconfig "^9.0.0"
debug "^4.3.4"
ejs "^3.1.10"
get-package-type "^0.1.0"
globby "^11.1.0"
indent-string "^4.0.0"
is-wsl "^2.2.0"
minimatch "^9.0.4"
string-width "^4.2.3"
supports-color "^9.4.0"
widest-line "^3.1.0"
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"

"@oclif/core@^3.26.5", "@oclif/core@^3.26.6":
version "3.26.6"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-3.26.6.tgz#f371868cfa0fe150a6547e6af98b359065d2f971"
Expand Down Expand Up @@ -2993,6 +3016,11 @@ ansicolors@~0.3.2:
resolved "https://registry.npmjs.org/ansicolors/-/ansicolors-0.3.2.tgz"
integrity sha512-QXu7BPrP29VllRxH8GwB7x5iX5qWKAAMLqKQGWTeLWVlNHNOpVMJ91dsxQAIWXpjuW5wqvxu3Jd/nRjrJ+0pqg==

ansis@^3.0.1:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.2.0.tgz#0e050c5be94784f32ffdac4b84fccba064aeae4b"
integrity sha512-Yk3BkHH9U7oPyCN3gL5Tc7CpahG/+UFv/6UG03C311Vy9lzRmA5uoxDTpU9CO3rGHL6KzJz/pdDeXZCZ5Mu/Sg==

anymatch@~3.1.2:
version "3.1.3"
resolved "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz"
Expand Down

0 comments on commit 4d3852e

Please sign in to comment.