wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
import { createWriteStream } from "fs";
import { join } from "path";
import { createDir, formatContent } from "../utils";
import { isArray } from "lodash";
 
interface Item {
    path: string;
    code: string;
}
 
// 创建文件
export async function createFile(data: Item | Item[]) {
    const list = isArray(data) ? data : [data];
 
    for (const item of list) {
        const { path, code } = item;
 
        // 格式化内容
        const content = await formatContent(code, {
            parser: "vue",
        });
 
        // 目录路径
        const dir = (path || "").split("/");
 
        // 文件名
        const fname = dir.pop();
 
        // 源码路径
        const srcPath = `./src/${dir.join("/")}`;
 
        // 创建目录
        createDir(srcPath, true);
 
        // 创建文件
        createWriteStream(join(srcPath, fname!), {
            flags: "w",
        }).write(content);
    }
}