Unverified Commit 5cd31e41 authored by Néstor's avatar Néstor Committed by GitHub

Add npm to `create-next-app` environment package manager parser (#41279)

Gracefully parse the environment package manager when calling
`create-next-app` instead of passing extra arguments.

Reference: #41090

## Feature

- [ ] 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 a helpful link attached, see `contributing.md`
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
parent e505e754
import { execSync } from 'child_process'
export type PackageManager = 'npm' | 'pnpm' | 'yarn' export type PackageManager = 'npm' | 'pnpm' | 'yarn'
export function getPkgManager(): PackageManager { export function getPkgManager(): PackageManager {
try { const userAgent = process.env.npm_config_user_agent
const userAgent = process.env.npm_config_user_agent
if (userAgent) { if (userAgent) {
if (userAgent.startsWith('yarn')) { if (userAgent.startsWith('yarn')) {
return 'yarn'
} else if (userAgent.startsWith('pnpm')) {
return 'pnpm'
}
}
try {
execSync('yarn --version', { stdio: 'ignore' })
return 'yarn' return 'yarn'
} catch { } else if (userAgent.startsWith('pnpm')) {
execSync('pnpm --version', { stdio: 'ignore' })
return 'pnpm' return 'pnpm'
} else {
return 'npm'
} }
} catch { } else {
return 'npm' return 'npm'
} }
} }
...@@ -62,6 +62,12 @@ const program = new Commander.Command(packageJson.name) ...@@ -62,6 +62,12 @@ const program = new Commander.Command(packageJson.name)
.allowUnknownOption() .allowUnknownOption()
.parse(process.argv) .parse(process.argv)
const packageManager = !!program.useNpm
? 'npm'
: !!program.usePnpm
? 'pnpm'
: getPkgManager()
async function run(): Promise<void> { async function run(): Promise<void> {
if (typeof projectPath === 'string') { if (typeof projectPath === 'string') {
projectPath = projectPath.trim() projectPath = projectPath.trim()
...@@ -122,12 +128,6 @@ async function run(): Promise<void> { ...@@ -122,12 +128,6 @@ async function run(): Promise<void> {
process.exit(1) process.exit(1)
} }
const packageManager = !!program.useNpm
? 'npm'
: !!program.usePnpm
? 'pnpm'
: getPkgManager()
const example = typeof program.example === 'string' && program.example.trim() const example = typeof program.example === 'string' && program.example.trim()
try { try {
await createApp({ await createApp({
...@@ -168,16 +168,18 @@ async function notifyUpdate(): Promise<void> { ...@@ -168,16 +168,18 @@ async function notifyUpdate(): Promise<void> {
try { try {
const res = await update const res = await update
if (res?.latest) { if (res?.latest) {
const pkgManager = getPkgManager() const updateMessage =
packageManager === 'yarn'
? 'yarn global add create-next-app'
: packageManager === 'pnpm'
? 'pnpm add -g create-next-app'
: 'npm i -g create-next-app'
console.log( console.log(
chalk.yellow.bold('A new version of `create-next-app` is available!') + chalk.yellow.bold('A new version of `create-next-app` is available!') +
'\n' + '\n' +
'You can update by running: ' + 'You can update by running: ' +
chalk.cyan( chalk.cyan(updateMessage) +
pkgManager === 'yarn'
? 'yarn global add create-next-app'
: `${pkgManager} install --global create-next-app`
) +
'\n' '\n'
) )
} }
......
...@@ -506,10 +506,172 @@ describe('create next app', () => { ...@@ -506,10 +506,172 @@ describe('create next app', () => {
'pnpm-lock.yaml', 'pnpm-lock.yaml',
'node_modules/next', 'node_modules/next',
] ]
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
})
it('should infer npm as the package manager', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'infer-package-manager-npm'
const res = await run([projectName], {
cwd,
env: { ...process.env, npm_config_user_agent: 'npm' },
})
expect(res.exitCode).toBe(0)
const files = [
'package.json',
'pages/index.js',
'.gitignore',
'.eslintrc.json',
'package-lock.json',
'node_modules/next',
]
files.forEach((file) => files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy() expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
) )
}) })
}) })
it('should infer npm as the package manager with example', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'infer-package-manager-npm'
const res = await run(
[projectName, '--example', `${exampleRepo}/${examplePath}`],
{ cwd, env: { ...process.env, npm_config_user_agent: 'npm' } }
)
expect(res.exitCode).toBe(0)
const files = [
'package.json',
'pages/index.tsx',
'.gitignore',
'package-lock.json',
'node_modules/next',
]
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
})
it('should infer yarn as the package manager', async () => {
try {
await execa('yarn', ['--version'])
} catch (_) {
// install yarn if not available
await execa('npm', ['i', '-g', 'yarn'])
}
await usingTempDir(async (cwd) => {
const projectName = 'infer-package-manager-yarn'
const res = await run([projectName], {
cwd,
env: { ...process.env, npm_config_user_agent: 'yarn' },
})
expect(res.exitCode).toBe(0)
const files = [
'package.json',
'pages/index.js',
'.gitignore',
'.eslintrc.json',
'yarn.lock',
'node_modules/next',
]
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
})
it('should infer yarn as the package manager with example', async () => {
try {
await execa('yarn', ['--version'])
} catch (_) {
// install yarn if not available
await execa('npm', ['i', '-g', 'yarn'])
}
await usingTempDir(async (cwd) => {
const projectName = 'infer-package-manager-npm'
const res = await run(
[projectName, '--example', `${exampleRepo}/${examplePath}`],
{ cwd, env: { ...process.env, npm_config_user_agent: 'yarn' } }
)
expect(res.exitCode).toBe(0)
const files = [
'package.json',
'pages/index.tsx',
'.gitignore',
'yarn.lock',
'node_modules/next',
]
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
})
it('should infer pnpm as the package manager', async () => {
try {
await execa('pnpm', ['--version'])
} catch (_) {
// install pnpm if not available
await execa('npm', ['i', '-g', 'pnpm'])
}
await usingTempDir(async (cwd) => {
const projectName = 'infer-package-manager'
const res = await run([projectName], {
cwd,
env: { ...process.env, npm_config_user_agent: 'pnpm' },
})
expect(res.exitCode).toBe(0)
const files = [
'package.json',
'pages/index.js',
'.gitignore',
'.eslintrc.json',
'pnpm-lock.yaml',
'node_modules/next',
]
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
})
})
it('should infer pnpm as the package manager with example', async () => {
try {
await execa('pnpm', ['--version'])
} catch (_) {
// install pnpm if not available
await execa('npm', ['i', '-g', 'pnpm'])
}
await usingTempDir(async (cwd) => {
const projectName = 'infer-package-manager-npm'
const res = await run(
[projectName, '--example', `${exampleRepo}/${examplePath}`],
{ cwd, env: { ...process.env, npm_config_user_agent: 'pnpm' } }
)
expect(res.exitCode).toBe(0)
const files = [
'package.json',
'pages/index.tsx',
'.gitignore',
'pnpm-lock.yaml',
'node_modules/next',
]
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment