rsnext/packages/next/cli/next-telemetry.ts
Tom Lienard a720dbdc4c
fix(next): dev server starting when importing a file using get-projec… (#38274)
Fixes: #38232 
Fixes: https://github.com/vercel/next.js/issues/36893

Version [12.1.1-canary.5](https://github.com/vercel/next.js/releases/tag/v12.1.1-canary.5) introduced a bug, more specifically this PR: https://github.com/vercel/next.js/pull/34836

The issue described in #38232 is that the following code starts both the dev and prod servers:
```js
const start = require('next/dist/cli/next-start')
start.nextStart()
```

I searched a bit and found that `lib/get-project-dir.ts#getProjectDir()` now imports `bin/next.ts`

6b8e499c7b/packages/next/lib/get-project-dir.ts (L3)

and it calls a CLI command via

6b8e499c7b/packages/next/bin/next.ts (L137)

This `command` should not be defined, but it fallbacks to `defaultCommand`, which is `dev` (that explains why the dev server is also started)

This PR moves the `cliCommand` types and `commands` variable to a new separate file instead of `bin/next.ts`, to avoid running a CLI command when we import any file that also imports `lib/get-project-dir.ts`

Not sure how integration tests can be added for this issue, but feel free to tell me.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-08-07 17:31:30 +00:00

96 lines
2.3 KiB
JavaScript
Executable file

#!/usr/bin/env node
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 '../lib/commands'
import { Telemetry } from '../telemetry/storage'
import isError from '../lib/is-error'
const nextTelemetry: cliCommand = (argv) => {
const validArgs: arg.Spec = {
// Types
'--help': Boolean,
'--enable': Boolean,
'--disable': 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
Allows you to control Next.js' telemetry collection
Usage
$ next telemetry [enable/disable]
You may pass the 'enable' or 'disable' argument to turn Next.js' telemetry collection on or off.
Learn more: ${chalk.cyan('https://nextjs.org/telemetry')}
`
)
return
}
const telemetry = new Telemetry({ distDir: process.cwd() })
let isEnabled = telemetry.isEnabled
if (args['--enable'] || args._[0] === 'enable') {
telemetry.setEnabled(true)
console.log(chalk.cyan('Success!'))
console.log()
isEnabled = true
} else if (args['--disable'] || args._[0] === 'disable') {
const path = telemetry.setEnabled(false)
if (isEnabled) {
console.log(
chalk.cyan(
`Your preference has been saved${path ? ` to ${path}` : ''}.`
)
)
} else {
console.log(
chalk.yellow(`Next.js' telemetry collection is already disabled.`)
)
}
console.log()
isEnabled = false
} else {
console.log(chalk.bold('Next.js Telemetry'))
console.log()
}
console.log(
`Status: ${
isEnabled ? chalk.bold.green('Enabled') : chalk.bold.red('Disabled')
}`
)
console.log()
if (isEnabled) {
console.log(
`Next.js telemetry is completely anonymous. Thank you for participating!`
)
} else {
console.log(`You have opted-out of Next.js' anonymous telemetry program.`)
console.log(`No data will be collected from your machine.`)
}
console.log(`Learn more: https://nextjs.org/telemetry`)
console.log()
}
export { nextTelemetry }