Skip to content

Commit cfd76de

Browse files
committed
feat( Creator): 优化项目文件生成过程
- 添加生成项目文件的进度指示器 - 改进错误处理和用户提示信息 - 优化文件写入逻辑,支持异步操作 - 调整代码结构,提高可维护性
1 parent 4844a23 commit cfd76de

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Creator.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Creator<T extends Record<string, unknown>> extends TypedEvents<{
8383
await fse.emptyDir(context.projectRoot);
8484
break;
8585
case 'cancel':
86-
throw new ExitError('Canceled by user', 0);
86+
throw new ExitError('Operation cancelled', 0);
8787
default:
8888
break;
8989
}
@@ -119,6 +119,21 @@ export class Creator<T extends Record<string, unknown>> extends TypedEvents<{
119119
);
120120
}
121121

122+
const spinner = prompts.spinner();
123+
spinner.start('Generating project files.');
124+
const [err] = await tryFlatten(this.#generateWriteFiles(paths));
125+
126+
if (err) {
127+
spinner.stop('Failed to generate project files', 1);
128+
throw new ExitError(`Failed to generate project files: ${err.message}`, 1);
129+
}
130+
131+
spinner.stop('Generated project files', 0);
132+
}
133+
134+
async #generateWriteFiles(paths: string[]) {
135+
const { context, options } = this;
136+
122137
for (const sourcePath of paths) {
123138
const sourceFileName = path.basename(sourcePath);
124139
const sourceFolder = path.dirname(sourcePath);
@@ -161,11 +176,11 @@ export class Creator<T extends Record<string, unknown>> extends TypedEvents<{
161176
targetFileName: path.basename(targetPath),
162177
};
163178

164-
await this.#write(fileMeta);
179+
await this.#generateWriteFile(fileMeta);
165180
}
166181
}
167182

168-
async #write(fileMeta: FileMeta) {
183+
async #generateWriteFile(fileMeta: FileMeta) {
169184
const { context, options } = this;
170185
const overrideFileMeta = await this.#writeMW.when(
171186
path.join(context.templateName, fileMeta.sourcePath),
@@ -184,9 +199,9 @@ export class Creator<T extends Record<string, unknown>> extends TypedEvents<{
184199

185200
if (fileMeta.isEjsFile && !disableRenderEjs) {
186201
const template = await fse.readFile(fileMeta.sourceFile, 'utf8');
187-
fse.outputFileSync(targetFile, ejs.render(template, this.data));
202+
await fse.outputFile(targetFile, ejs.render(template, this.data));
188203
} else {
189-
fse.copySync(fileMeta.sourceFile, targetFile);
204+
await fse.copy(fileMeta.sourceFile, targetFile);
190205
}
191206

192207
await this.emit('written', fileMeta, this.data, overrideFileMeta);

0 commit comments

Comments
 (0)