-
-
Notifications
You must be signed in to change notification settings - Fork 672
/
index.ts
194 lines (162 loc) · 4.83 KB
/
index.ts
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
188
189
190
191
192
193
194
#!/usr/bin/env node
import { existsSync, readdirSync } from "node:fs";
import { resolve } from "node:path";
import { cac } from "cac";
import { execaCommand, execaCommandSync } from "execa";
import inquirer from "inquirer";
import { getLanguage, generateTemplate, version } from "./config/index.js";
import { createPackageJson } from "./packageJson.js";
import { getRegistry } from "./registry.js";
import { ensureDirExistSync, getPackageManager } from "./utils/index.js";
import type { CreateI18n, Lang } from "./config/index.js";
import type { PackageManager } from "./utils/index.js";
const preAction = async (
targetDir: string,
preset?: "docs" | "blog" | null
): Promise<{
lang: Lang;
message: CreateI18n;
packageManager: PackageManager;
} | void> => {
// ensure targetDir is specified by user
if (!targetDir) return cli.outputHelp();
// get language
const { lang, message } = await getLanguage();
// check presets
if (preset && !["docs", "blog"].includes(preset))
return console.log(message.error.preset);
// get packageManager
const packageManager = await getPackageManager(
message.question.packageManager
);
// check if the user is a noob and warn him 🤪
if (targetDir.startsWith("[") && targetDir.endsWith("]"))
return console.log(message.error.updateDirMissing(packageManager));
const targetDirPath = resolve(process.cwd(), targetDir);
// check if the user is trying to cover his files
if (existsSync(targetDirPath) && readdirSync(targetDirPath).length)
return console.error(message.error.dirNotEmpty(targetDir));
ensureDirExistSync(targetDirPath);
// return choice
return { lang, message, packageManager };
};
const postAction = async ({
cwd = process.cwd(),
lang,
message,
packageManager,
}: {
lang: Lang;
cwd?: string;
message: CreateI18n;
packageManager: PackageManager;
}): Promise<void> => {
/*
* Install deps
*/
const registry =
packageManager === "pnpm" ? "" : await getRegistry(packageManager, lang);
console.log(message.flow.install);
console.warn(message.hint.install);
execaCommandSync(
`${packageManager} install ${registry ? `--registry ${registry}` : ""}`,
{ cwd, stdout: "inherit" }
);
console.log(message.hint.finish);
/*
* Open dev server
*/
const { choice } = await inquirer.prompt<{ choice: boolean }>([
{
name: "choice",
type: "confirm",
message: message.question.devServer,
default: true,
},
]);
if (choice) {
console.log(message.flow.devServer);
await execaCommand(`${packageManager} run docs:dev`, {
cwd,
stdout: "inherit",
});
} else console.log(message.hint.devServer(packageManager));
};
const cli = cac("vuepress-theme-hope");
cli
.command("[dir]", "Generate a new vuepress-theme-hope project")
.option("-p, --preset <preset>", "Choose preset to use")
.usage(
"pnpm create vuepress-theme-hope@next [dir] / npm init vuepress-theme-hope@next [dir]"
)
.example("docs")
.action(
async (
targetDir: string,
{
preset = null,
}: {
preset?: "docs" | "blog" | null;
}
) => {
const workingCWD = resolve(process.cwd(), targetDir);
const result = await preAction(targetDir, preset);
if (result) {
const { lang, message, packageManager } = result;
await createPackageJson(message, "src", targetDir);
await generateTemplate("src", {
cwd: workingCWD,
packageManager,
lang,
message,
preset,
});
await postAction({ cwd: workingCWD, lang, message, packageManager });
}
}
);
cli
.command("add [dir]", "Add vuepress template to dir")
.alias("inject")
.option("-p, --preset <preset>", "Choose preset to use")
.usage(
"pnpm create vuepress-theme-hope@next add [dir] / npm init vuepress-theme-hope@next add [dir]"
)
.example("docs")
.action(
async (
targetDir: string,
{
preset = null,
}: {
preset?: "docs" | "blog" | null;
}
) => {
const result = await preAction(targetDir, preset);
if (result) {
const { lang, message, packageManager } = result;
await createPackageJson(message, targetDir);
await generateTemplate(targetDir, {
packageManager,
lang,
message,
preset,
});
await postAction({ message, lang, packageManager });
}
}
);
cli.help(() => [
{
title:
"pnpm create vuepress-theme-hope@next [dir] / npm init vuepress-theme-hope@next [dir]",
body: "Create a vuepress-theme-hope template in [dir]",
},
{
title:
"pnpm create vuepress-theme-hope@next inject [dir] / npm init vuepress-theme-hope@next inject [dir]",
body: "Add vuepress-theme-hope template in [dir] under current project",
},
]);
cli.version(version);
cli.parse();