1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import type { Context } from './context.js';
import { color, generateProjectName } from '@astrojs/cli-kit';
import path from 'node:path';
import { info, log, title } from '../messages.js';
import { isEmpty, toValidName } from './shared.js';
export async function projectName(
ctx: Pick<Context, 'cwd' | 'yes' | 'dryRun' | '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!`)}`);
}
if (ctx.yes) {
ctx.projectName = generateProjectName();
ctx.cwd = `./${ctx.projectName}`;
await info('dir', `Project created at ./${ctx.projectName}`);
return;
}
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!`;
}
// Check for non-printable characters
if (value.match(/[^\x20-\x7E]/g) !== null)
return `Invalid non-printable character present!`;
return true;
},
});
ctx.cwd = name!.trim();
ctx.projectName = toValidName(name!);
if (ctx.dryRun) {
await info('--dry-run', 'Skipping project naming');
return;
}
} 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;
}
|