Skip to content

Commit

Permalink
feat(cli): introduce next info CLI command (#32972)
Browse files Browse the repository at this point in the history
This PR adds a new command to the `next` CLI.

Running `next info` will print useful information to the terminal about how/where Next.js is run. This information can be added to the Bug report when opening an issue in the repository.

This makes reporting issues more accurate and doesn't require the user to guess certain details, the command will retrieve it on their behalf.

Example output:

```sh
$ npx --no-install next info

    Operating System:
      Platform: linux
      Version: #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021
    Binaries:
      Node: 16.13.0
      npm: 8.1.0
      Yarn: 1.22.17
      pnpm: 6.24.2
    Relevant packages:
      next: 12.0.8-canary.14
      react: 17.0.2
      react-dom: 17.0.2


```

The idea is based on #32858

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`


Co-authored-by: Steven <229881+styfle@users.noreply.github.com>
  • Loading branch information
balazsorban44 and styfle committed Jan 4, 2022
1 parent 1be8704 commit 3ad2035
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .github/ISSUE_TEMPLATE/1.bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ body:
- type: markdown
attributes:
value: 'next@canary is the beta version of Next.js. It includes all features and fixes that are pending to land on the stable release line.'
- type: textarea
attributes:
label: Run `next info` (available from version 12.0.8 and up)
description: Please run `next info` in the root directory of your project and paste the results. You might need to use `npx --no-install next info` if next is not in the current PATH.
validations:
required: false
- type: input
attributes:
label: What version of Next.js are you using?
Expand Down
35 changes: 34 additions & 1 deletion docs/api-reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Usage
$ next <command>

Available commands
build, start, export, dev, lint, telemetry
build, start, export, dev, lint, telemetry, info

Options
--version, -v Version number
Expand Down Expand Up @@ -125,3 +125,36 @@ Next.js collects **completely anonymous** telemetry data about general usage.
Participation in this anonymous program is optional, and you may opt-out if you'd not like to share any information.

To learn more about Telemetry, [please read this document](https://nextjs.org/telemetry/).

## Info

`next info` prints relevant details about the current system which can be used to report Next.js bugs.
This information includes Operating System platform/arch/version, Binaries (Node.js, npm, Yarn, pnpm) and npm package versions (`next`, `react`, `react-dom`).

Running the following in your project's root directory:

```bash
next info
```

will give you information like this example:

```bash

Operating System:
Platform: linux
Arch: x64
Version: #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021
Binaries:
Node: 16.13.0
npm: 8.1.0
Yarn: 1.22.17
pnpm: 6.24.2
Relevant packages:
next: 12.0.8
react: 17.0.2
react-dom: 17.0.2

```

This information should then be pasted into GitHub Issues.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@types/fs-extra": "8.1.0",
"@types/http-proxy": "1.17.3",
"@types/jest": "24.0.13",
"@types/node": "13.11.0",
"@types/selenium-webdriver": "4.0.15",
"@types/sharp": "0.29.3",
"@types/string-hash": "1.1.1",
Expand Down
1 change: 1 addition & 0 deletions packages/next/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const commands: { [command: string]: () => Promise<cliCommand> } = {
lint: () => Promise.resolve(require('../cli/next-lint').nextLint),
telemetry: () =>
Promise.resolve(require('../cli/next-telemetry').nextTelemetry),
info: () => Promise.resolve(require('../cli/next-info').nextInfo),
}

const args = arg(
Expand Down
76 changes: 76 additions & 0 deletions packages/next/cli/next-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node
import os from 'os'
import childProcess from 'child_process'

import chalk from 'next/dist/compiled/chalk'
import arg from 'next/dist/compiled/arg/index.js'
import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next'
import isError from '../lib/is-error'

const nextInfo: cliCommand = async (argv) => {
const validArgs: arg.Spec = {
// Types
'--help': Boolean,
// Aliases
'-h': '--help',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}

if (args['--help']) {
console.log(
`
Description
Prints relevant details about the current system which can be used to report Next.js bugs
Usage
$ next info
Learn more: ${chalk.cyan(
'https://nextjs.org/docs/api-reference/cli#info'
)}`
)
return
}

console.log(`
Operating System:
Platform: ${os.platform()}
Arch: ${os.arch()}
Version: ${os.version()}
Binaries:
Node: ${process.versions.node}
npm: ${getBinaryVersion('npm')}
Yarn: ${getBinaryVersion('yarn')}
pnpm: ${getBinaryVersion('pnpm')}
Relevant packages:
next: ${getPackageVersion('next')}
react: ${getPackageVersion('react')}
react-dom: ${getPackageVersion('react-dom')}`)
}

export { nextInfo }

function getPackageVersion(packageName: string) {
try {
return require(`${packageName}/package.json`).version
} catch {
return 'N/A'
}
}

function getBinaryVersion(binaryName: string) {
try {
return childProcess.execSync(`${binaryName} --version`).toString().trim()
} catch {
return 'N/A'
}
}
2 changes: 1 addition & 1 deletion packages/next/shared/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,6 @@ export interface CacheFs {
readFile(f: string): Promise<string>
readFileSync(f: string): string
writeFile(f: string, d: any): Promise<void>
mkdir(dir: string): Promise<void>
mkdir(dir: string): Promise<void | string>
stat(f: string): Promise<{ mtime: Date }>
}
43 changes: 43 additions & 0 deletions test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,47 @@ describe('CLI Usage', () => {
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
})

describe('info', () => {
test('--help', async () => {
const help = await runNextCommand(['info', '--help'], {
stdout: true,
})
expect(help.stdout).toMatch(
/Prints relevant details about the current system which can be used to report Next\.js bugs/
)
})

test('-h', async () => {
const help = await runNextCommand(['info', '-h'], {
stdout: true,
})
expect(help.stdout).toMatch(
/Prints relevant details about the current system which can be used to report Next\.js bugs/
)
})

test('should print output', async () => {
const info = await runNextCommand(['info'], {
stdout: true,
})
expect(info.stdout).toMatch(
new RegExp(`
Operating System:
Platform: .*
Arch: .*
Version: .*
Binaries:
Node: .*
npm: .*
Yarn: .*
pnpm: .*
Relevant packages:
next: .*
react: .*
react-dom: .*
`)
)
})
})
})
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4694,9 +4694,10 @@
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@>= 8":
version "13.1.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.1.4.tgz#4cfd90175a200ee9b02bd6b1cd19bc349741607e"
"@types/node@*", "@types/node@13.11.0", "@types/node@>= 8":
version "13.11.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.11.0.tgz#390ea202539c61c8fa6ba4428b57e05bc36dc47b"
integrity sha512-uM4mnmsIIPK/yeO+42F2RQhGUIs39K2RFmugcJANppXe6J1nvH87PvzPZYpza7Xhhs8Yn9yIAVdLZ84z61+0xQ==

"@types/node@10.12.18":
version "10.12.18"
Expand Down

0 comments on commit 3ad2035

Please sign in to comment.