-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
117 lines (104 loc) · 3.14 KB
/
index.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
import * as fs from 'node:fs';
import * as path from 'node:path';
import { cac } from 'cac';
import prompts from 'prompts';
import pc from 'picocolors';
import { templates } from './templates.js';
import { fileURLToPath } from 'node:url';
const cli = cac('create-hats-app');
cli
.usage(`${pc.cyan('<project-directory>')} [options]`)
.command('[project-directory]', 'Create a new project')
.option('-t, --template [name]', 'Template to use. Options:', {
default: 'next',
})
.action(async (projectDirectory = '.', options) => {
const defaultTargetDir = 'my-hats-project';
let targetDir;
try {
if (projectDirectory === '.' || projectDirectory === '') {
const response = await prompts(
{
type: 'text',
name: 'dir',
message: 'Enter your project directory:',
initial: 'my-hats-project',
},
{
onCancel: () => {
throw new Error(`${pc.red('✖')} Operation cancelled by user.`);
},
}
);
targetDir = path.resolve(
process.cwd(),
response.dir || defaultTargetDir
);
} else {
targetDir = path.resolve(process.cwd(), projectDirectory);
}
const questions = [
{
type: 'select',
name: 'template',
message: 'Choose a template to use',
choices: templates.map((template) => ({
title: template.color(template.display),
value: template.name,
})),
initial: 0,
},
];
const responses = await prompts(
questions as prompts.PromptObject<string>[],
{
onCancel: () => {
throw new Error(`\n ${pc.red('✖')} Operation cancelled by user.`);
},
}
);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
options.template = responses.template;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const templateDir = path.resolve(
__dirname,
'..',
'templates',
options.template
);
const prettyTargetDirectory = targetDir.split('/').pop();
if (!fs.existsSync(templateDir)) {
console.error(
`\n ${pc.red('✖')}The requested template "${
responses.template
}" does not exist.`
);
return;
}
await fs.promises.cp(templateDir, targetDir, { recursive: true });
console.log(
`\n 🧢 ${
responses.template.charAt(0).toUpperCase() +
responses.template.slice(1)
} template created successfully in ${pc.blue(
`./${prettyTargetDirectory}`
)}`
);
console.log('\n 👒 Run the following command to get started building:');
console.log(
pc.green(
`\n ${pc.bgBlack(`cd ${prettyTargetDirectory}`)}\n ${pc.bgBlack(
'pnpm install'
)}`
) // can add in spaces after the \n
);
} catch (error) {
console.error((error as Error).message);
return;
}
});
cli.help();
cli.parse();