astro/packages/create-astro/src/actions/project-name.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

Refactor `create-astro` (#6082) * refactor: new version of create-astro * chore: update README * fix(create-astro): update project name logic * test(create-astro): fix test on windows * test(create-astro): fix test on windows * test(create-astro): remove unused import * chore: remove log * chore: increase test timeout * fix: message when skipping * fix: message for env.d.ts file * fix: always hard exit * fix: return from next-steps * chore: add message * refactor dependencies, bundle create-astro * chore: disable create-astro typings * chore: switch to arg * chore: update message * fix: split typescript into two steps, fix context test * chore: update wording * chore: update wording * Update packages/create-astro/src/actions/dependencies.ts Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> * refactor: move tests back to mocha/chai * chore: update cli-kit * update test script * chore: add comment about setStdout * chore: update cli-kit * Update packages/create-astro/src/messages.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update packages/create-astro/src/messages.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * chore: update lockfile * fix(create-astro): support scoped package names, improve project-name tests * better git initialization * update cli-kit --------- Co-authored-by: Nate Moore <nate@astro.build> Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
2023-02-06 16:19:37 +00:00
import type { Context } from "./context";
import { color, generateProjectName } from '@astrojs/cli-kit';
import { title, info, log } from '../messages.js';
import path from 'node:path';
import { isEmpty, toValidName } from './shared.js';
export async function projectName(ctx: Pick<Context, 'cwd'|'prompt'|'projectName'|'exit'>) {
await checkCwd(ctx.cwd);
if (!ctx.cwd || !isEmpty(ctx.cwd)) {
if (!isEmpty(ctx.cwd)) {
await info('Hmm...', `${color.reset(`"${ctx.cwd}"`)}${color.dim(` is not empty!`)}`);
}
const { name } = await ctx.prompt({
name: 'name',
type: 'text',
label: title('dir'),
message: 'Where should we create your new project?',
initial: `./${generateProjectName()}`,
validate(value: string) {
if (!isEmpty(value)) {
return `Directory is not empty!`;
}
return true;
},
});
ctx.cwd = name!;
ctx.projectName = toValidName(name!);
} else {
let name = ctx.cwd;
if (name === '.' || name === './') {
const parts = process.cwd().split(path.sep);
name = parts[parts.length - 1];
} else if (name.startsWith('./') || name.startsWith('../')) {
const parts = name.split('/');
name = parts[parts.length - 1];
}
ctx.projectName = toValidName(name);
}
if (!ctx.cwd) {
ctx.exit(1);
}
}
async function checkCwd(cwd: string | undefined) {
const empty = cwd && isEmpty(cwd);
if (empty) {
log('');
await info('dir', `Using ${color.reset(cwd)}${color.dim(' as project directory')}`);
}
return empty;
}