Skip to content

Commit

Permalink
fix: refactor code since Node 14 support was removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ansidev committed Jan 10, 2023
1 parent f6471f5 commit d8a50e0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 143 deletions.
69 changes: 32 additions & 37 deletions packages/create-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
196 changes: 96 additions & 100 deletions packages/create-astro/test/package-step.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
});
}
});
6 changes: 0 additions & 6 deletions packages/create-astro/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

0 comments on commit d8a50e0

Please sign in to comment.