Skip to content

Commit

Permalink
sm/pr-feedback (#685)
Browse files Browse the repository at this point in the history
* refactor: hasPlugin type uses this.list types

* refactor: omit class props, parallelization from TODO

* refactor: warningCache use set

* chore: dev-dep cleanup

* chore(release): 3.9.4-qa.0 [skip ci]

---------

Co-authored-by: svc-cli-bot <[email protected]>
  • Loading branch information
mshanemc and svc-cli-bot authored Oct 23, 2023
1 parent 29e64ff commit f11e12b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 124 deletions.
55 changes: 32 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ plugins plugin for oclif
- [Aliases](#aliases)
- [Environment Variables](#environment-variables)
- [Commands](#commands)
- [`mycli plugins`](#mycli-plugins)
- [`mycli plugins:inspect PLUGIN...`](#mycli-pluginsinspect-plugin)
- [`mycli plugins:install PLUGIN...`](#mycli-pluginsinstall-plugin)
- [`mycli plugins:link PLUGIN`](#mycli-pluginslink-plugin)
- [`mycli plugins:uninstall PLUGIN...`](#mycli-pluginsuninstall-plugin)
- [`mycli plugins:update`](#mycli-pluginsupdate)
<!-- tocstop -->
<!-- tocstop -->

# What is this?

Expand Down Expand Up @@ -94,25 +88,35 @@ For removing plugins that are no longer needed (either because they're sunset or

<!-- commands -->

- [`mycli plugins`](#mycli-plugins)
- [`mycli plugins:inspect PLUGIN...`](#mycli-pluginsinspect-plugin)
- [`mycli plugins:install PLUGIN...`](#mycli-pluginsinstall-plugin)
- [`mycli plugins:link PLUGIN`](#mycli-pluginslink-plugin)
- [`mycli plugins:uninstall PLUGIN...`](#mycli-pluginsuninstall-plugin)
- [`mycli plugins update`](#mycli-plugins-update)

## `mycli plugins`

List installed plugins.

```
USAGE
$ mycli plugins [--core]
$ mycli plugins [--json] [--core]
FLAGS
--core Show core plugins.
GLOBAL FLAGS
--json Format output as json.
DESCRIPTION
List installed plugins.
EXAMPLES
$ mycli plugins
```

_See code: [src/commands/plugins/index.ts](https://github.com/oclif/plugin-plugins/blob/v2.0.12/src/commands/plugins/index.ts)_
_See code: [src/commands/plugins/index.ts](https://github.com/oclif/plugin-plugins/blob/3.9.4-qa.0/src/commands/plugins/index.ts)_

## `mycli plugins:inspect PLUGIN...`

Expand All @@ -129,14 +133,17 @@ FLAGS
-h, --help Show CLI help.
-v, --verbose
GLOBAL FLAGS
--json Format output as json.
DESCRIPTION
Displays installation properties of a plugin.
EXAMPLES
$ mycli plugins:inspect myplugin
```

_See code: [src/commands/plugins/inspect.ts](https://github.com/oclif/plugin-plugins/blob/v2.0.12/src/commands/plugins/inspect.ts)_
_See code: [src/commands/plugins/inspect.ts](https://github.com/oclif/plugin-plugins/blob/3.9.4-qa.0/src/commands/plugins/inspect.ts)_

## `mycli plugins:install PLUGIN...`

Expand All @@ -152,11 +159,11 @@ ARGUMENTS
FLAGS
-f, --force Run yarn install with force flag.
-h, --help Show CLI help.
-v, --verbose
-s, --silent Silences yarn output.
-v, --verbose Show verbose yarn output.
DESCRIPTION
Installs a plugin into the CLI.
Can be installed from npm or a git url.
Installation of a user-installed plugin will override a core plugin.
Expand All @@ -165,8 +172,9 @@ DESCRIPTION
will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in
the CLI without the need to patch and update the whole CLI.
ALIASES
$ mycli plugins:add
$ mycli plugins add
EXAMPLES
$ mycli plugins:install myplugin
Expand All @@ -176,7 +184,7 @@ EXAMPLES
$ mycli plugins:install someuser/someplugin
```

_See code: [src/commands/plugins/install.ts](https://github.com/oclif/plugin-plugins/blob/v2.0.12/src/commands/plugins/install.ts)_
_See code: [src/commands/plugins/install.ts](https://github.com/oclif/plugin-plugins/blob/3.9.4-qa.0/src/commands/plugins/install.ts)_

## `mycli plugins:link PLUGIN`

Expand All @@ -190,22 +198,23 @@ ARGUMENTS
PATH [default: .] path to plugin
FLAGS
-h, --help Show CLI help.
-h, --help Show CLI help.
-v, --verbose
--[no-]install Install dependencies after linking the plugin.
DESCRIPTION
Links a plugin into the CLI for development.
Installation of a linked plugin will override a user-installed or core plugin.
e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
command will override the user-installed or core plugin implementation. This is useful for development work.
EXAMPLES
$ mycli plugins:link myplugin
```

_See code: [src/commands/plugins/link.ts](https://github.com/oclif/plugin-plugins/blob/v2.0.12/src/commands/plugins/link.ts)_
_See code: [src/commands/plugins/link.ts](https://github.com/oclif/plugin-plugins/blob/3.9.4-qa.0/src/commands/plugins/link.ts)_

## `mycli plugins:uninstall PLUGIN...`

Expand All @@ -226,19 +235,19 @@ DESCRIPTION
Removes a plugin from the CLI.
ALIASES
$ mycli plugins:unlink
$ mycli plugins:remove
$ mycli plugins unlink
$ mycli plugins remove
```

_See code: [src/commands/plugins/uninstall.ts](https://github.com/oclif/plugin-plugins/blob/v2.0.12/src/commands/plugins/uninstall.ts)_
_See code: [src/commands/plugins/uninstall.ts](https://github.com/oclif/plugin-plugins/blob/3.9.4-qa.0/src/commands/plugins/uninstall.ts)_

## `mycli plugins:update`
## `mycli plugins update`

Update installed plugins.

```
USAGE
$ mycli plugins:update [-h] [-v]
$ mycli plugins update [-h] [-v]
FLAGS
-h, --help Show CLI help.
Expand All @@ -248,6 +257,6 @@ DESCRIPTION
Update installed plugins.
```

_See code: [src/commands/plugins/update.ts](https://github.com/oclif/plugin-plugins/blob/v2.0.12/src/commands/plugins/update.ts)_
_See code: [src/commands/plugins/update.ts](https://github.com/oclif/plugin-plugins/blob/3.9.4-qa.0/src/commands/plugins/update.ts)_

<!-- commandsstop -->
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oclif/plugin-plugins",
"description": "plugins plugin for oclif",
"version": "3.9.3",
"version": "3.9.4-qa.0",
"author": "Salesforce",
"bugs": "https://github.com/oclif/plugin-plugins/issues",
"dependencies": {
Expand All @@ -21,25 +21,21 @@
"@oclif/prettier-config": "^0.2.1",
"@types/chai": "^4.3.6",
"@types/debug": "^4.1.9",
"@types/glob": "^8.1.0",
"@types/mocha": "^10.0.2",
"@types/node": "^18",
"@types/semver": "^7.5.3",
"@types/shelljs": "^0.8.13",
"@types/sinon": "^10.0.19",
"@types/supports-color": "^7.2.0",
"@types/validate-npm-package-name": "^4.0.0",
"chai": "^4.3.10",
"commitlint": "^17.8.0",
"eslint": "^8.51.0",
"eslint-config-oclif": "^5.0.0",
"eslint-config-oclif-typescript": "^3.0.6",
"eslint-config-prettier": "^9.0.0",
"globby": "^11.1.0",
"husky": "^8.0.3",
"lint-staged": "^15.0.1",
"mocha": "^10.2.0",
"nock": "^13.3.3",
"oclif": "^4.0.2",
"prettier": "^3.0.3",
"shx": "^0.3.4",
Expand Down
45 changes: 27 additions & 18 deletions src/commands/plugins/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ function trimUntil(fsPath: string, part: string): string {
}

type Dependencies = Record<string, {from: string; version: string}>
type PluginWithDeps = Plugin & {deps: Dependencies}
type PluginWithDeps = Omit<
Plugin,
| '_commandsDir'
| '_debug'
| '_manifest'
| 'addErrorScope'
| 'commandIDs'
| 'commandsDir'
| 'findCommand'
| 'flexibleTaxonomy'
| 'load'
| 'topics'
| 'warn'
| 'warned'
> & {deps: Dependencies}

export default class PluginsInspect extends Command {
static args = {
Expand Down Expand Up @@ -47,8 +61,6 @@ export default class PluginsInspect extends Command {

plugins = new Plugins(this.config)

// 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 = join(...dependency.split('/'))
let start = join(plugin.root, 'node_modules')
Expand All @@ -58,21 +70,18 @@ export default class PluginsInspect extends Command {
paths.push(start)
}

// TODO: use promise.any to check the paths in parallel
// requires node >= 16
for (const p of paths) {
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'))
return {pkgPath: fullPath, version: pkgJson.version as string}
} catch {
// try the next path
}
try {
return await Promise.any(
paths.map(async (p) => {
const fullPath = join(p, dependencyPath)
const pkgJsonPath = join(fullPath, 'package.json')
const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf8'))
return {pkgPath: fullPath, version: pkgJson.version as string}
}),
)
} catch {
return {pkgPath: null, version: null}
}

return {pkgPath: null, version: null}
}

findPlugin(pluginName: string): Plugin {
Expand Down Expand Up @@ -122,7 +131,7 @@ export default class PluginsInspect extends Command {

if (!this.jsonEnabled()) tree.display()

return {...plugin, deps: depsJson} as PluginWithDeps
return {...plugin, deps: depsJson}
}

/* eslint-disable no-await-in-loop */
Expand Down
17 changes: 9 additions & 8 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ export default class Plugins {
return match?.[1] ?? name
}

async hasPlugin(name: string): Promise<Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.User | false> {
async hasPlugin(
name: string,
): Promise<Interfaces.PJSON.PluginTypes.Link | Interfaces.PJSON.PluginTypes.User | false> {
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' && resolve(p.root) === resolve(name))
return (friendly ?? unfriendly ?? link ?? false) as
| Interfaces.PJSON.PluginTypes.Link
| Interfaces.PJSON.User
| false
return (
list.find((p) => this.friendlyName(p.name) === this.friendlyName(name)) ?? // friendly
list.find((p) => this.unfriendlyName(p.name) === this.unfriendlyName(name)) ?? // unfriendly
list.find((p) => p.type === 'link' && resolve(p.root) === resolve(name)) ?? // link
false
)
}

async install(name: string, {force = false, tag = 'latest'} = {}): Promise<Interfaces.Config> {
Expand Down
6 changes: 2 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function findNpm(): Promise<string> {
}

export class WarningsCache {
private static cache: string[] = []
private static cache = new Set<string>()
private static instance: WarningsCache
public static getInstance(): WarningsCache {
if (!WarningsCache.instance) {
Expand All @@ -109,9 +109,7 @@ export class WarningsCache {

public add(...warnings: string[]): void {
for (const warning of warnings) {
if (!WarningsCache.cache.includes(warning)) {
WarningsCache.cache.push(warning)
}
WarningsCache.cache.add(warning)
}
}

Expand Down
Loading

0 comments on commit f11e12b

Please sign in to comment.