|
1 | 1 | import type { CAC } from "cac";
|
2 | 2 |
|
| 3 | +import fs from "fs-extra"; |
| 4 | + |
3 | 5 | import path from "node:path";
|
4 | 6 | import { randomUUID } from "node:crypto";
|
5 | 7 |
|
6 | 8 | import enquirer from "enquirer";
|
7 | 9 |
|
8 | 10 | import {
|
| 11 | + emptyDir, |
9 | 12 | getVariantByFramework,
|
10 | 13 | isEmptyDir,
|
11 | 14 | isValidFramework,
|
12 | 15 | isValidPackageName,
|
13 | 16 | isValidVariant,
|
14 | 17 | loggerInfo,
|
| 18 | + pkgFromUserAgent, |
15 | 19 | toValidPackageName,
|
16 | 20 | } from "@/shared/index";
|
17 |
| -import { ACTIVATION, FRAMEWORKS } from "@/shared/config"; |
| 21 | +import { ACTIVATION, fileIgnore, FRAMEWORKS } from "@/shared/config"; |
18 | 22 | import { TemplateOptions } from "@/shared/types";
|
| 23 | +import { fileURLToPath } from "node:url"; |
19 | 24 |
|
20 | 25 | interface PromptResult {
|
21 | 26 | projectName: string;
|
@@ -99,25 +104,47 @@ export const template = async (options: TemplateOptions) => {
|
99 | 104 | console.log(projectName, framework, variant, overwrite, packageName);
|
100 | 105 |
|
101 | 106 | const root = path.join(process.cwd(), projectName);
|
102 |
| - // if (overwrite) emptyDir(root); |
103 |
| - // const template: string = variant || framework; |
104 |
| - // const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent); |
105 |
| - // const pkgManager = pkgInfo ? pkgInfo.name : "npm"; |
| 107 | + if (overwrite) emptyDir(root); |
| 108 | + const template: string = variant || framework; |
| 109 | + const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent); |
| 110 | + const pkgManager = pkgInfo ? pkgInfo.name : "npm"; |
106 | 111 |
|
107 | 112 | console.log(`\nScaffolding project in ${root}...`);
|
108 |
| - // const templateDir = path.resolve( |
109 |
| - // fileURLToPath(import.meta.url), |
110 |
| - // "../../../template", |
111 |
| - // `template-${template}` |
112 |
| - // ); |
113 |
| - // console.log(templateDir); |
114 |
| - // fs.copySync(templateDir, projectName, { |
115 |
| - // filter: (src: string) => { |
116 |
| - // return !fileIgnore.find( |
117 |
| - // (f) => f === `${path.parse(src).name}${path.parse(src).ext}` |
118 |
| - // ); |
119 |
| - // }, |
120 |
| - // }); |
| 113 | + const templateDir = path.resolve( |
| 114 | + fileURLToPath(import.meta.url), |
| 115 | + "../../../templates", |
| 116 | + `template-${template}`, |
| 117 | + ); |
| 118 | + console.log(templateDir); |
| 119 | + fs.copySync(templateDir, projectName, { |
| 120 | + filter: (src: string) => { |
| 121 | + return !fileIgnore.find( |
| 122 | + (f) => f === `${path.parse(src).name}${path.parse(src).ext}`, |
| 123 | + ); |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + const gitignoreInfo = fs.readFileSync( |
| 128 | + path.resolve(templateDir, "_gitignore"), |
| 129 | + ); |
| 130 | + fs.outputFile(path.join(root, ".gitignore"), gitignoreInfo); |
| 131 | + |
| 132 | + const pkg = fs.readJsonSync(path.resolve(templateDir, "package.json")); |
| 133 | + pkg.name = packageName; |
| 134 | + fs.outputJSONSync(path.join(root, "package.json"), pkg, { |
| 135 | + spaces: 2, |
| 136 | + }); |
| 137 | + |
| 138 | + switch (pkgManager) { |
| 139 | + case "yarn": |
| 140 | + console.log(" yarn"); |
| 141 | + console.log(" yarn dev"); |
| 142 | + break; |
| 143 | + default: |
| 144 | + console.log(` ${pkgManager} install`); |
| 145 | + console.log(` ${pkgManager} run dev`); |
| 146 | + break; |
| 147 | + } |
121 | 148 | };
|
122 | 149 |
|
123 | 150 | export default function templateInstaller(cli: CAC) {
|
|
0 commit comments