Skip to content

Commit

Permalink
Add config option to include the URL (#77)
Browse files Browse the repository at this point in the history
* Add `outdatedIncludeURL` configuration

* Add URL config option

* Docs
  • Loading branch information
mskelton committed Dec 22, 2022
1 parent c6bc0f4 commit 970c93e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 43 deletions.
50 changes: 34 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Yarn plugin to show outdated dependencies.

### Yarn 3

```sh
```bash
yarn plugin import https://mskelton.dev/yarn-outdated/v3
```

### Yarn 2

```sh
```bash
yarn plugin import https://mskelton.dev/yarn-outdated/v1.2.0
```

Expand All @@ -23,7 +23,7 @@ yarn plugin import https://mskelton.dev/yarn-outdated/v1.2.0
When run without arguments, this plugin will find outdated plugins in all
workspaces of your project.

```sh
```bash
yarn outdated
```

Expand All @@ -34,13 +34,13 @@ You can easily filter dependencies using any valid
especially useful when you want to check a set of related dependencies such as a
component library or tools such as Babel or ESLint.

```sh
```bash
yarn outdated '@babel/*'
```

And, in case you were wondering, you can add multiple glob patterns!

```sh
```bash
yarn outdated '@babel/*' '@types/*'
```

Expand All @@ -53,14 +53,14 @@ be included.
The simplest way to filter a workspace is by it's name. And just like dependency
filtering, this flag supports glob patterns!

```sh
```bash
yarn outdated --workspace frontend
```

You can also filter workspaces by directory using either an absolute or relative
path.

```sh
```bash
yarn outdated --workspace packages/a
yarn outdated --workspace /Users/mark/project/packages/a
```
Expand All @@ -69,7 +69,7 @@ The `--workspace` flag can be added multiple times to specify multiple glob
patterns to match with. You can even mix and match directories and workspace
names!

```sh
```bash
yarn outdated --workspace packages/a --workspace frontend
```

Expand All @@ -83,15 +83,15 @@ got your back! With the `--severity` option, you can specify which severity
levels to include. By default, we show all but if you only want to display minor
versions updates, you could use this command.

```sh
```bash
yarn outdated --severity minor
```

Also, this flag can be specified multiple times if you would like to include
multiple severities, such as minor and patch versions, as shown in the following
example.

```sh
```bash
yarn outdated --severity minor --severity patch
```

Expand All @@ -100,7 +100,7 @@ yarn outdated --severity minor --severity patch
The final means of filtering outdated dependencies is by dependency type. For
example, to only display outdated `devDependencies`, use the following command.

```sh
```bash
yarn outdated --type devDependencies
```

Expand All @@ -110,14 +110,14 @@ By default, only the latest version of dependencies are displayed. However, in
some cases you may wish to know both the latest version and the version that
satisfies the range specified in your manifest.

```sh
```bash
yarn outdated --range
```

For example if you have `"glob": "^7.2.0"` in your manifest, the output with
this flag might look something like this.

```sh
```bash
➤ YN0000: Package Current Range Latest Package Type
➤ YN0000: glob 7.2.0 7.2.3 8.0.3 devDependencies
```
Expand All @@ -129,16 +129,16 @@ outdated dependencies. While this is perfect for normal use, if you want to use
this plugin with scripts and fail if there are outdated dependencies, you can
add the `--check` flag.

```sh
```bash
yarn outdated --check
```

### Display URL (`--url`)
### Display homepage URLs (`--url`)

It is possible to display dependency homepage URLs in the output. To do so,
simply add the `--url` flag to the command!

```sh
```bash
yarn outdated --url
```

Expand All @@ -158,3 +158,21 @@ then parse and use as your needs require.
If you are using this plugin in a GitHub action or other CI provider that
displays markdown content, use `--format=markdown` to display a formatted
markdown table.

## Configuration

### Include homepage URLs by default (`outdatedIncludeUrl`)

By default, homepage URLs are not included in the output. In addition to the
`--url` flag, you can configure URLs to show by default.

```yaml
outdatedIncludeUrl: true
```

When this setting is enabled, you can use the `--no-url` flag to disable it on a
per-command basis.

```bash
yarn outdated --no-url
```
8 changes: 4 additions & 4 deletions bundles/@yarnpkg/plugin-outdated.js

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions src/OutdatedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class OutdatedCommand extends BaseCommand {
validator: t.isEnum(dependencyTypes),
})

includeURL = Option.Boolean("--url", false, {
_includeURL = Option.Boolean("--url", {
description: "Include the homepage URL of each package in the output",
})

Expand All @@ -112,6 +112,7 @@ export class OutdatedCommand extends BaseCommand {

if (this.format !== "text" || this.json) {
const outdated = await this.getOutdatedDependencies(
configuration,
project,
fetcher,
dependencies
Expand Down Expand Up @@ -142,6 +143,10 @@ export class OutdatedCommand extends BaseCommand {
return report.exitCode()
}

includeURL(configuration: Configuration) {
return this._includeURL ?? configuration.get("outdatedIncludeUrl")
}

writeJson(outdated: OutdatedDependency[]) {
const json = outdated.map((dep) => ({
...dep,
Expand All @@ -168,7 +173,7 @@ export class OutdatedCommand extends BaseCommand {
outdated,
{
range: this.includeRange,
url: this.includeURL,
url: this.includeURL(configuration),
workspace: this.includeWorkspace(project),
}
)
Expand All @@ -192,6 +197,7 @@ export class OutdatedCommand extends BaseCommand {
report.reportProgress(progress)

outdated = await this.getOutdatedDependencies(
configuration,
project,
fetcher,
dependencies,
Expand All @@ -210,7 +216,7 @@ export class OutdatedCommand extends BaseCommand {
outdated,
{
range: this.includeRange,
url: this.includeURL,
url: this.includeURL(configuration),
workspace: this.includeWorkspace(project),
}
)
Expand Down Expand Up @@ -413,6 +419,7 @@ export class OutdatedCommand extends BaseCommand {
* sort them in ascending order.
*/
async getOutdatedDependencies(
configuration: Configuration,
project: Project,
fetcher: DependencyFetcher,
dependencies: DependencyInfo[],
Expand All @@ -423,7 +430,7 @@ export class OutdatedCommand extends BaseCommand {
const { latest, range, url } = await fetcher.fetch({
descriptor,
includeRange: this.includeRange,
includeURL: this.includeURL,
includeURL: this.includeURL(configuration),
pkg,
})

Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { Plugin } from "@yarnpkg/core"
import { Plugin, SettingsType } from "@yarnpkg/core"
import { OutdatedCommand } from "./OutdatedCommand"

declare module "@yarnpkg/core" {
interface ConfigurationValueMap {
outdatedIncludeUrl: boolean
}
}

const plugin: Plugin = {
commands: [OutdatedCommand],
configuration: {
outdatedIncludeUrl: {
default: false,
description: `If true, the outdated command will include the package homepage URL by default`,
type: SettingsType.BOOLEAN as const,
},
},
}

export default plugin
24 changes: 12 additions & 12 deletions test/fixtures/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import { makeTemporaryEnv } from "../utils/env"
type Environment = Awaited<ReturnType<typeof makeTemporaryEnv>>

interface EnvironmentFixtures extends Omit<Environment, "destroy"> {
env: Omit<Environment, "destroy">
env: Record<string, string>
yarnEnv: Omit<Environment, "destroy">
}

export const test = base.extend<EnvironmentFixtures>({
env: async ({}, use, testInfo) => {
// Will be moved to the global config after v1.19
// https://github.com/microsoft/playwright/pull/11132
cwd: ({ yarnEnv }, use) => use(yarnEnv.cwd),
env: {},
readFile: ({ yarnEnv }, use) => use(yarnEnv.readFile),
registry: ({ yarnEnv }, use) => use(yarnEnv.registry),
run: ({ yarnEnv }, use) => use(yarnEnv.run),
writeFile: ({ yarnEnv }, use) => use(yarnEnv.writeFile),
writeJSON: ({ yarnEnv }, use) => use(yarnEnv.writeJSON),
yarnEnv: async ({ env }, use, testInfo) => {
testInfo.snapshotSuffix = ""

const { destroy, ...env } = await makeTemporaryEnv()
await use(env)
const { destroy, ...yarnEnv } = await makeTemporaryEnv(env)
await use(yarnEnv)
await destroy()
},
readFile: ({ env }, use) => use(env.readFile),
registry: ({ env }, use) => use(env.registry),
run: ({ env }, use) => use(env.run),
writeFile: ({ env }, use) => use(env.writeFile),
writeJSON: ({ env }, use) => use(env.writeJSON),
})

export const expect = test.expect
25 changes: 25 additions & 0 deletions test/specs/url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,29 @@ test.describe("yarn outdated --url", () => {
"http://foo.com"
)
})

test.describe("when outdatedIncludeURL config is true", () => {
test.use({ env: { YARN_OUTDATED_INCLUDE_URL: "true" } })

test("displays the package homepage URL by default", async ({
run,
writeJSON,
}) => {
await writeJSON("package.json", manifest)
await run("install")

const { stderr, stdout } = await run("outdated")
expect(stdout).toMatchSnapshot("url.txt")
expect(stderr).toBe("")
})

test("favors the flag over the config", async ({ run, writeJSON }) => {
await writeJSON("package.json", manifest)
await run("install")

const { stderr, stdout } = await run("outdated --no-url")
expect(stdout).toMatchSnapshot("no-url.txt")
expect(stderr).toBe("")
})
})
})
10 changes: 10 additions & 0 deletions test/specs/url.spec.ts-snapshots/no-url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
➤ YN0000: ┌ Checking for outdated dependencies
➤ YN0000: └ Completed

➤ YN0000: Package Current Latest Package Type
➤ YN0000: major 1.0.0 2.0.0 dependencies
➤ YN0000: minor 1.0.0 1.1.0 dependencies
➤ YN0000: patch 1.0.0 1.0.1 dependencies

➤ YN0000: 3 dependencies are out of date
➤ YN0000: Done with warnings
8 changes: 4 additions & 4 deletions test/specs/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test.describe("workspaces", () => {

test.describe("--workspace", () => {
test("filters in the current workspace when a period is specified", async ({
env,
cwd,
run,
writeJSON,
}) => {
Expand All @@ -59,21 +59,21 @@ test.describe("workspaces", () => {
await run("install")

const { stderr, stdout } = await run("outdated --workspace .", {
cwd: ppath.join(env.cwd, toFilename("a")),
cwd: ppath.join(cwd, toFilename("a")),
})
expect(stdout).toMatchSnapshot("period.txt")
expect(stderr).toBe("")
})

test("filters by absolute directory", async ({ env, run, writeJSON }) => {
test("filters by absolute directory", async ({ cwd, run, writeJSON }) => {
const dependencies = { patch: "1.0.0" }
await writeJSON("package.json", { dependencies, workspaces: ["a"] })
await writeJSON("a/package.json", { dependencies })
await writeJSON("b/package.json", { dependencies })
await run("install")

const { stderr, stdout } = await run(
`outdated --workspace ${env.cwd}/a --workspace ${env.cwd}`
`outdated --workspace ${cwd}/a --workspace ${cwd}`
)
expect(stdout).toMatchSnapshot("directory-absolute.txt")
expect(stderr).toBe("")
Expand Down
7 changes: 5 additions & 2 deletions test/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const YARN_RC = stringifySyml({

interface RunOptions {
cwd?: PortablePath
env?: Record<string, string>
}

// We only need to setup the registry once
const registry = new Registry()

export async function makeTemporaryEnv() {
export async function makeTemporaryEnv(globalEnv: Record<string, string>) {
const [tempDir, homeDir, registryUrl] = await Promise.all([
xfs.mktempPromise(),
xfs.mktempPromise(),
Expand All @@ -44,7 +45,7 @@ export async function makeTemporaryEnv() {
await writeFile(target, JSON.stringify(body))
}

const run = (command: string, { cwd }: RunOptions = {}) => {
const run = (command: string, { cwd, env }: RunOptions = {}) => {
const yarnPath = `${__dirname}/../../.yarn/releases/yarn-3.2.1.cjs`
const yarnBinary = require.resolve(yarnPath)

Expand All @@ -67,6 +68,8 @@ export async function makeTemporaryEnv() {
YARN_NPM_REGISTRY_SERVER: registryUrl,
// Otherwise we would more often test the fallback rather than the real logic
YARN_UNSAFE_HTTP_WHITELIST: new URL(registryUrl).hostname,
...globalEnv,
...env,
},
})
}
Expand Down

0 comments on commit 970c93e

Please sign in to comment.