forked from patternfly/patternfly-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ts
More file actions
187 lines (165 loc) · 5.95 KB
/
create.ts
File metadata and controls
187 lines (165 loc) · 5.95 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import path from 'path';
import fs from 'fs-extra';
import { execa } from 'execa';
import inquirer from 'inquirer';
import { defaultTemplates } from './templates.js';
import { mergeTemplates } from './template-loader.js';
import { offerAndCreateGitHubRepo } from './github.js';
/** Project data provided by the user */
type ProjectData = {
/** Project name */
name: string;
/** Project version */
version: string;
/** Project description */
description: string;
/** Project author */
author: string;
};
export type RunCreateOptions = {
templateFile?: string;
ssh?: boolean;
};
/**
* Runs the create flow: clone template, customize package.json, install deps, optionally create GitHub repo.
* Throws on fatal errors. Caller should catch and process.exit(1).
*/
export async function runCreate(
projectDirectory: string | undefined,
templateName: string | undefined,
options?: RunCreateOptions
): Promise<void> {
const templatesToUse = mergeTemplates(defaultTemplates, options?.templateFile);
// If project directory is not provided, prompt for it
if (!projectDirectory) {
const projectDirAnswer = await inquirer.prompt([
{
type: 'input',
name: 'projectDirectory',
message: 'Please provide the directory where you want to create the project?',
default: 'my-app',
},
]);
projectDirectory = projectDirAnswer.projectDirectory;
}
// If template name is not provided, show available templates and let user select
if (!templateName) {
console.log('\n📋 Available templates:\n');
templatesToUse.forEach(t => {
console.log(` ${t.name.padEnd(12)} - ${t.description}`);
});
console.log('');
const templateQuestion = [
{
type: 'list',
name: 'templateName',
message: 'Select a template:',
choices: templatesToUse.map(t => ({
name: `${t.name} - ${t.description}`,
value: t.name,
})),
},
];
const templateAnswer = await inquirer.prompt(templateQuestion);
templateName = templateAnswer.templateName;
}
// Look up the template by name
const template = templatesToUse.find(t => t.name === templateName);
if (!template) {
console.error(`❌ Template "${templateName}" not found.\n`);
console.log('📋 Available templates:\n');
templatesToUse.forEach(t => {
console.log(` ${t.name.padEnd(12)} - ${t.description}`);
});
console.log('');
throw new Error(`Template "${templateName}" not found`);
}
const templateRepoUrl = options?.ssh && template.repoSSH ? template.repoSSH : template.repo;
// Define the full path for the new project (projectDirectory is set above via arg or prompt)
const dir = projectDirectory ?? 'my-app';
const projectPath = path.resolve(dir);
console.log(`Cloning template "${templateName}" from ${templateRepoUrl} into ${projectPath}...`);
try {
// Clone the repository
const cloneArgs = ['clone'];
if (template.options && Array.isArray(template.options)) {
cloneArgs.push(...template.options);
}
cloneArgs.push(templateRepoUrl, projectPath);
await execa('git', cloneArgs, { stdio: 'inherit' });
console.log('✅ Template cloned successfully.');
// Remove the .git folder from the *new* project
await fs.remove(path.join(projectPath, '.git'));
console.log('🧹 Cleaned up template .git directory.');
// Ask user for customization details
const questions = [
{
type: 'input',
name: 'name',
message: 'What is the project name?',
default: path.basename(projectPath),
},
{
type: 'input',
name: 'version',
message: 'What version number would you like to use?',
default: '1.0.0',
},
{
type: 'input',
name: 'description',
message: 'What is the project description?',
default: '',
},
{
type: 'input',
name: 'author',
message: 'Who is the author of the project?',
default: '',
},
];
const answers: ProjectData = await inquirer.prompt(questions);
// Update the package.json in the new project
const pkgJsonPath = path.join(projectPath, 'package.json');
if (await fs.pathExists(pkgJsonPath)) {
const pkgJson = await fs.readJson(pkgJsonPath);
// Overwrite fields with user's answers
pkgJson.name = answers.name;
pkgJson.version = answers.version;
pkgJson.description = answers.description;
pkgJson.author = answers.author;
// Write the updated package.json back
await fs.writeJson(pkgJsonPath, pkgJson, { spaces: 2 });
console.log('📝 Customized package.json.');
} else {
console.log('ℹ️ No package.json found in template, skipping customization.');
}
const packageManager = template.packageManager || 'npm';
// Install dependencies
console.log('📦 Installing dependencies... (This may take a moment)');
await execa(packageManager, ['install'], { cwd: projectPath, stdio: 'inherit' });
console.log('✅ Dependencies installed.');
// Optional: Create GitHub repository (explains what to check if it does not complete)
await offerAndCreateGitHubRepo(projectPath);
// Let the user know the project was created successfully
console.log('\n✨ Project created successfully! ✨\n');
console.log(`To get started:`);
console.log(` cd ${dir}`);
console.log(' Happy coding! 🚀');
} catch (error) {
console.error('❌ An error occurred:');
if (error instanceof Error) {
console.error(error.message);
} else if (error && typeof error === 'object' && 'stderr' in error) {
console.error((error as { stderr?: string }).stderr || String(error));
} else {
console.error(String(error));
}
// Clean up the created directory if an error occurred
if (await fs.pathExists(projectPath)) {
await fs.remove(projectPath);
console.log('🧹 Cleaned up failed project directory.');
}
throw error;
}
}