diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index d1ae6bb363c17..5b906bd8dfff1 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -234,47 +234,42 @@ export async function main() { templateSpinner.succeed(); const printIgnoreUpdatingPkgNameMsg = () => info('Opps', 'You have to update the NPM package name manually.'); - const { stdout: npmVersion } = await execa('npm', ['-v']); - const npmVersionMajorPart = Number(npmVersion.split('.')[0]); - if (npmVersionMajorPart <= 6) { - // Ignore updating package name if npm version is v6 and older - await printIgnoreUpdatingPkgNameMsg(); - } else { - // Validate and update the NPM package name - let pkgName: string = projectDir; - if (args.package) { - pkgName = args.package; - } + // Validate and update the NPM package name + let pkgName: string = projectDir; + if (args.package) { + pkgName = args.package; + } - const validatePackageNameResult = await validatePackageName(pkgName); - if (!validatePackageNameResult.isValid) { - const pkgNameResponse = await prompts( - { - type: 'text', - name: 'package', - message: 'What is the new NPM package name?', - initial: generateProjectName(), - async validate(value) { - const { isValid, notices } = await validatePackageName(value); - if (!isValid) { - const errorMsg = `"${bold(value)}" is an invalid NPM package name`; - const errors = notices.join(', '); - return `${errorMsg}: ${errors}`; - } - return true; - }, + const validatePackageNameResult = await validatePackageName(pkgName); + if (!validatePackageNameResult.isValid) { + const pkgNameResponse = await prompts( + { + type: 'text', + name: 'package', + message: 'What is the new NPM package name?', + initial: generateProjectName(), + async validate(value) { + const { isValid, notices } = await validatePackageName(value); + if (!isValid) { + const errorMsg = `"${bold(value)}" is an invalid NPM package name`; + const errors = notices.join(', '); + return `${errorMsg}: ${errors}`; + } + return true; }, - { onCancel: printIgnoreUpdatingPkgNameMsg } - ); - pkgName = pkgNameResponse.package as string; - } + }, + { onCancel: printIgnoreUpdatingPkgNameMsg } + ); + pkgName = pkgNameResponse.package as string; + } - if (typeof pkgName === 'string' && !!pkgName) { - // Change project name in package.json - await execaCommand(`${pkgManager} -v`, { cwd }); - await execaCommand(`npm -v`, { cwd }); - await execaCommand(`npm pkg set name=${pkgName}`, { cwd }); + if (typeof pkgName === 'string' && !!pkgName) { + // Change project name in package.json + try { + await execaCommand(`${pkgManager} pkg set name=${pkgName}`, { cwd }); await info('Sounds good!', `The NPM package name was updated to ${bold(pkgName)}.`); + } catch(error) { + printIgnoreUpdatingPkgNameMsg(); } } } diff --git a/packages/create-astro/test/package-step.test.js b/packages/create-astro/test/package-step.test.js index 98b85f439719d..20aff7b29322b 100644 --- a/packages/create-astro/test/package-step.test.js +++ b/packages/create-astro/test/package-step.test.js @@ -3,7 +3,7 @@ import { expect } from 'chai'; import { deleteSync } from 'del'; import { existsSync, mkdirSync, readdirSync, readFileSync } from 'fs'; import path from 'path'; -import { getNPMVersion, PROMPT_MESSAGES, testDir, timeout } from './utils.js'; +import { PROMPT_MESSAGES, testDir, timeout } from './utils.js'; import stripAnsi from 'strip-ansi'; const cliPath = path.join(testDir, "../create-astro.mjs"); @@ -42,115 +42,111 @@ const testArgs = [ 'strict', ]; -const { major } = await getNPMVersion(); +describe('[create-astro] update package name', function () { + beforeEach(ensureEmptyDir); + + afterEach(ensureEmptyDir); + + it('should the package name is changed if no value is given by the user', async function () { + const template = 'minimal'; + const args = [ + ...testArgs, + ...['--template', template] + ] + const { exitCode, stdout } = await runTest([cliPath].concat(args), [ + ENTER, + ], { + testPath: testDir, + timeout, + }); -if (major > 6) { - describe('[create-astro] update package name', function () { - beforeEach(ensureEmptyDir); + await expect(exitCode).to.equal(0); + // TODO: This should true, so we have to fix the implementation + // await expect(stripAnsi(stderr).trim()).to.be.empty; + await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageSucceed); - afterEach(ensureEmptyDir); + const packageJson = getPackageJson(inputs.emptyDir); + const pkgName = packageJson.name; + await expect(pkgName).to.not.equal(`@example/${template}`); + }); - it('should the package name is changed if no value is given by the user', async function () { - const template = 'minimal'; - const args = [ - ...testArgs, - ...['--template', template] + it('should the package name is not changed if the user cancels the operation', async function () { + const template = 'minimal'; + const args = [ + ...testArgs, + ...[ + '--template', + template, ] - const { exitCode, stdout } = await runTest([cliPath].concat(args), [ - ENTER, - ], { - testPath: testDir, - timeout, - }); - - await expect(exitCode).to.equal(0); - // TODO: This should true, so we have to fix the implementation - // await expect(stripAnsi(stderr).trim()).to.be.empty; - await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageSucceed); - - const packageJson = getPackageJson(inputs.emptyDir); - const pkgName = packageJson.name; - await expect(pkgName).to.not.equal(`@example/${template}`); + ] + const { exitCode, stdout } = await runTest([cliPath].concat(args), [ + "\x1B", // ESC hex code + ], { + testPath: testDir, + timeout, }); - it('should the package name is not changed if the user cancels the operation', async function () { - const template = 'minimal'; - const args = [ - ...testArgs, - ...[ - '--template', - template, - ] - ] - const { exitCode, stdout } = await runTest([cliPath].concat(args), [ - "\x1B", // ESC hex code - ], { - testPath: testDir, - timeout, - }); - - await expect(exitCode).to.equal(0); - // TODO: This should true, so we have to fix the implementation - // await expect(stripAnsi(stderr).trim()).to.be.empty; - await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageIgnored); - - const packageJson = getPackageJson(inputs.emptyDir); - const pkgName = packageJson.name; - await expect(pkgName).to.equal(`@example/${template}`); - }); + await expect(exitCode).to.equal(0); + // TODO: This should true, so we have to fix the implementation + // await expect(stripAnsi(stderr).trim()).to.be.empty; + await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageIgnored); + + const packageJson = getPackageJson(inputs.emptyDir); + const pkgName = packageJson.name; + await expect(pkgName).to.equal(`@example/${template}`); + }); - it('should the package name matches the passed argument --package', async function () { - const expectedPkgName = 'astro-test'; - const args = [ - ...testArgs, - ...[ - '--template', - 'minimal', - '--package', - expectedPkgName, - ] + it('should the package name matches the passed argument --package', async function () { + const expectedPkgName = 'astro-test'; + const args = [ + ...testArgs, + ...[ + '--template', + 'minimal', + '--package', + expectedPkgName, ] - const { exitCode, stdout } = await runTest([cliPath].concat(args), [ - ENTER, - ], { - testPath: testDir, - timeout, - }); - - await expect(exitCode).to.equal(0); - // TODO: This should true, so we have to fix the implementation - // await expect(stripAnsi(stderr).trim()).to.be.empty; - await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageSucceed); - - const packageJson = getPackageJson(inputs.emptyDir); - const pkgName = packageJson.name; - await expect(pkgName).to.equal(expectedPkgName); + ] + const { exitCode, stdout } = await runTest([cliPath].concat(args), [ + ENTER, + ], { + testPath: testDir, + timeout, }); - it('should the package name matches the user input', async function () { - const expectedPkgName = 'astro-test'; - const args = [ - ...testArgs, - ...[ - '--template', - 'minimal', - ] + await expect(exitCode).to.equal(0); + // TODO: This should true, so we have to fix the implementation + // await expect(stripAnsi(stderr).trim()).to.be.empty; + await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageSucceed); + + const packageJson = getPackageJson(inputs.emptyDir); + const pkgName = packageJson.name; + await expect(pkgName).to.equal(expectedPkgName); + }); + + it('should the package name matches the user input', async function () { + const expectedPkgName = 'astro-test'; + const args = [ + ...testArgs, + ...[ + '--template', + 'minimal', ] - const { exitCode, stdout } = await runTest([cliPath].concat(args), [ - `${expectedPkgName}${ENTER}`, - ], { - testPath: testDir, - timeout, - }); - - await expect(exitCode).to.equal(0); - // TODO: This should true, so we have to fix the implementation - // await expect(stripAnsi(stderr).trim()).to.be.empty; - await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageSucceed); - - const packageJson = getPackageJson(inputs.emptyDir); - const pkgName = packageJson.name; - await expect(pkgName).to.equal(expectedPkgName); + ] + const { exitCode, stdout } = await runTest([cliPath].concat(args), [ + `${expectedPkgName}${ENTER}`, + ], { + testPath: testDir, + timeout, }); + + await expect(exitCode).to.equal(0); + // TODO: This should true, so we have to fix the implementation + // await expect(stripAnsi(stderr).trim()).to.be.empty; + await expect(stripAnsi(stdout).trim()).to.contain(PROMPT_MESSAGES.packageSucceed); + + const packageJson = getPackageJson(inputs.emptyDir); + const pkgName = packageJson.name; + await expect(pkgName).to.equal(expectedPkgName); }); -} +}); diff --git a/packages/create-astro/test/utils.js b/packages/create-astro/test/utils.js index b1d79283c9171..b4c0ace0208af 100644 --- a/packages/create-astro/test/utils.js +++ b/packages/create-astro/test/utils.js @@ -52,9 +52,3 @@ export function setup(args = []) { stdout, }; } - -export async function getNPMVersion() { - const { stdout: npmVersion } = await execa('npm', ['-v']); - const [major, minor, patch] = npmVersion.split('.').map(Number); - return { major, minor, patch } -}