create-for-yeyu 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +99 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3,8 +3,6 @@
3
3
  // src/cli.ts
4
4
  import { Command } from "commander";
5
5
  import chalk2 from "chalk";
6
- import fs from "fs-extra";
7
- import path2 from "path";
8
6
 
9
7
  // src/prompts.ts
10
8
  import inquirer from "inquirer";
@@ -42,6 +40,27 @@ function getTemplateByValue(value) {
42
40
  return templates.find((t) => t.value === value);
43
41
  }
44
42
 
43
+ // src/utils/resolve-project-name.ts
44
+ import fs from "fs-extra";
45
+ import path from "path";
46
+ function checkDirectoryExists(projectName) {
47
+ const targetPath = path.resolve(process.cwd(), projectName);
48
+ return fs.existsSync(targetPath);
49
+ }
50
+ function generateUniqueProjectName(baseName) {
51
+ let counter = 1;
52
+ let newName = `${baseName}-${counter}`;
53
+ while (checkDirectoryExists(newName)) {
54
+ counter++;
55
+ newName = `${baseName}-${counter}`;
56
+ }
57
+ return newName;
58
+ }
59
+ async function removeDirectory(projectName) {
60
+ const targetPath = path.resolve(process.cwd(), projectName);
61
+ await fs.remove(targetPath);
62
+ }
63
+
45
64
  // src/prompts.ts
46
65
  async function promptProjectName() {
47
66
  const { projectName } = await inquirer.prompt([
@@ -91,10 +110,76 @@ async function promptTemplate() {
91
110
  }
92
111
  return selectedTemplate;
93
112
  }
113
+ async function promptCustomProjectName() {
114
+ const { projectName } = await inquirer.prompt([
115
+ {
116
+ type: "input",
117
+ name: "projectName",
118
+ message: "Enter a new project name:",
119
+ validate: (input) => {
120
+ if (!input.trim()) {
121
+ return "Project name cannot be empty";
122
+ }
123
+ if (!/^[a-zA-Z0-9-_]+$/.test(input)) {
124
+ return "Project name can only contain letters, numbers, hyphens and underscores";
125
+ }
126
+ return true;
127
+ }
128
+ }
129
+ ]);
130
+ return projectName;
131
+ }
132
+ async function promptConflictResolution(projectName) {
133
+ const suggestedName = generateUniqueProjectName(projectName);
134
+ const { action } = await inquirer.prompt([
135
+ {
136
+ type: "list",
137
+ name: "action",
138
+ message: `Directory "${projectName}" already exists. What would you like to do?`,
139
+ choices: [
140
+ {
141
+ name: "Enter a custom name",
142
+ value: "custom"
143
+ },
144
+ {
145
+ name: "Overwrite existing directory",
146
+ value: "overwrite"
147
+ },
148
+ {
149
+ name: `Rename to "${suggestedName}"`,
150
+ value: "rename"
151
+ }
152
+ ]
153
+ }
154
+ ]);
155
+ if (action === "overwrite") {
156
+ return {
157
+ projectName,
158
+ shouldOverwrite: true
159
+ };
160
+ }
161
+ if (action === "rename") {
162
+ return {
163
+ projectName: suggestedName,
164
+ shouldOverwrite: false
165
+ };
166
+ }
167
+ const customName = await promptCustomProjectName();
168
+ return resolveProjectName(customName);
169
+ }
170
+ async function resolveProjectName(projectName) {
171
+ if (!checkDirectoryExists(projectName)) {
172
+ return {
173
+ projectName,
174
+ shouldOverwrite: false
175
+ };
176
+ }
177
+ return promptConflictResolution(projectName);
178
+ }
94
179
 
95
180
  // src/actions/clone-repo.ts
96
181
  import degit from "degit";
97
- import path from "path";
182
+ import path2 from "path";
98
183
 
99
184
  // src/utils/train-animation.ts
100
185
  import readline from "readline";
@@ -230,7 +315,7 @@ var logger = {
230
315
 
231
316
  // src/actions/clone-repo.ts
232
317
  async function cloneRepo(repo, projectName) {
233
- const targetPath = path.resolve(process.cwd(), projectName);
318
+ const targetPath = path2.resolve(process.cwd(), projectName);
234
319
  const train = createTrainAnimation(`Cloning template ${repo}...`);
235
320
  train.start();
236
321
  try {
@@ -345,13 +430,12 @@ Today is ${date}`;
345
430
 
346
431
  // src/cli.ts
347
432
  var VERSION = "1.0.0";
348
- async function executeTemplate(template, projectName) {
349
- const targetPath = path2.resolve(process.cwd(), projectName);
433
+ async function executeTemplate(template, projectName, shouldOverwrite) {
434
+ if (shouldOverwrite) {
435
+ logger.info(`Removing existing directory ${projectName}...`);
436
+ await removeDirectory(projectName);
437
+ }
350
438
  if (template.type === "git") {
351
- if (fs.existsSync(targetPath)) {
352
- logger.error(`Directory ${projectName} already exists`);
353
- process.exit(1);
354
- }
355
439
  if (!template.repo) {
356
440
  logger.error("Template repository not configured");
357
441
  process.exit(1);
@@ -387,11 +471,12 @@ async function run() {
387
471
  ).action(async (projectName, options) => {
388
472
  printBanner();
389
473
  try {
390
- let finalProjectName = projectName;
474
+ let inputProjectName = projectName;
391
475
  let template;
392
- if (!finalProjectName) {
393
- finalProjectName = await promptProjectName();
476
+ if (!inputProjectName) {
477
+ inputProjectName = await promptProjectName();
394
478
  }
479
+ const { projectName: resolvedName, shouldOverwrite } = await resolveProjectName(inputProjectName);
395
480
  if (options.template) {
396
481
  const foundTemplate = getTemplateByValue(options.template);
397
482
  if (!foundTemplate) {
@@ -403,7 +488,7 @@ async function run() {
403
488
  } else {
404
489
  template = await promptTemplate();
405
490
  }
406
- await executeTemplate(template, finalProjectName);
491
+ await executeTemplate(template, resolvedName, shouldOverwrite);
407
492
  } catch (error) {
408
493
  if (error instanceof Error) {
409
494
  logger.error(error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-for-yeyu",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A CLI tool to scaffold projects from templates",
5
5
  "type": "module",
6
6
  "bin": {