@doyuli/kits-core 0.1.2 → 0.2.2
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.
|
@@ -1,20 +1,39 @@
|
|
|
1
1
|
import { Option } from "@clack/prompts";
|
|
2
2
|
|
|
3
3
|
//#region src/helper/setupPrompts.d.ts
|
|
4
|
-
|
|
4
|
+
type PromptStep<T extends Record<string, any> = Record<string, any>> = (result: PromptResult) => Promise<T>;
|
|
5
|
+
interface PromptResult {
|
|
5
6
|
projectName?: string;
|
|
6
7
|
packageName?: string;
|
|
7
8
|
shouldOverwrite?: boolean;
|
|
8
|
-
features?: T[];
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
11
|
+
declare function setupPrompts<const Steps extends PromptStep<any>[]>(targetDir: string, steps: [...Steps]): Promise<{
|
|
12
|
+
result: PromptResult & UnionToIntersection<Steps[number] extends PromptStep<infer R extends Record<string, any>> ? R : never>;
|
|
12
13
|
targetDir: string;
|
|
13
14
|
}>;
|
|
14
15
|
//#endregion
|
|
15
16
|
//#region src/helper/setupProject.d.ts
|
|
16
17
|
declare function setupProject(cwd: string, result: PromptResult, targetDir: string): Promise<string>;
|
|
17
18
|
//#endregion
|
|
19
|
+
//#region src/helper/steps.d.ts
|
|
20
|
+
interface SelectOptions<T> {
|
|
21
|
+
message: string;
|
|
22
|
+
options: Option<T>[];
|
|
23
|
+
}
|
|
24
|
+
interface FeaturesOptions<T> {
|
|
25
|
+
message?: string;
|
|
26
|
+
options: Option<T>[];
|
|
27
|
+
required?: boolean;
|
|
28
|
+
}
|
|
29
|
+
interface ConfirmOptions {
|
|
30
|
+
message: string;
|
|
31
|
+
initialValue?: boolean;
|
|
32
|
+
}
|
|
33
|
+
declare function setupSelect<Key extends string, T extends string>(key: Key, options: SelectOptions<T>): PromptStep<Record<Key, T>>;
|
|
34
|
+
declare function setupFeatures<Key extends string, T extends string>(key: Key, options: FeaturesOptions<T>): PromptStep<Record<Key, T[]>>;
|
|
35
|
+
declare function setupConfirm<Key extends string>(key: Key, options: ConfirmOptions): PromptStep<Record<Key, boolean>>;
|
|
36
|
+
//#endregion
|
|
18
37
|
//#region src/utils/directoryTraverse.d.ts
|
|
19
38
|
/**
|
|
20
39
|
* @see https://github.com/vuejs/create-vue/blob/main/utils/directoryTraverse.ts
|
|
@@ -49,4 +68,4 @@ declare function unwrapPrompt<T>(maybeCancelPromise: Promise<T | symbol>): Promi
|
|
|
49
68
|
declare function renderTemplate(src: string, dest: string): void;
|
|
50
69
|
declare function renderFile(root: string, fileName: string, content: string): void;
|
|
51
70
|
//#endregion
|
|
52
|
-
export { type PromptResult, canSkipEmptying, dotGitDirectoryState, emptyDir, getCommand, getPackageManager, postOrderDirectoryTraverse, preOrderDirectoryTraverse, renderFile, renderTemplate, setupProject, setupPrompts, unwrapPrompt };
|
|
71
|
+
export { type PromptResult, type PromptStep, canSkipEmptying, dotGitDirectoryState, emptyDir, getCommand, getPackageManager, postOrderDirectoryTraverse, preOrderDirectoryTraverse, renderFile, renderTemplate, setupConfirm, setupFeatures, setupProject, setupPrompts, setupSelect, unwrapPrompt };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import process from "node:process";
|
|
4
|
-
import { cancel, confirm, isCancel, multiselect, text } from "@clack/prompts";
|
|
4
|
+
import { cancel, confirm, isCancel, multiselect, select, text } from "@clack/prompts";
|
|
5
5
|
import pico from "picocolors";
|
|
6
6
|
|
|
7
7
|
//#region src/utils/directoryTraverse.ts
|
|
@@ -48,7 +48,7 @@ function canSkipEmptying(dir) {
|
|
|
48
48
|
}
|
|
49
49
|
function emptyDir(dir) {
|
|
50
50
|
if (!fs.existsSync(dir)) return;
|
|
51
|
-
postOrderDirectoryTraverse(dir, (dir
|
|
51
|
+
postOrderDirectoryTraverse(dir, (dir) => fs.rmdirSync(dir), (file) => fs.unlinkSync(file));
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
//#endregion
|
|
@@ -174,13 +174,12 @@ async function setupProject(cwd, result, targetDir) {
|
|
|
174
174
|
|
|
175
175
|
//#endregion
|
|
176
176
|
//#region src/helper/setupPrompts.ts
|
|
177
|
-
async function setupPrompts(targetDir,
|
|
177
|
+
async function setupPrompts(targetDir, steps) {
|
|
178
178
|
const defaultProjectName = targetDir || "Template-Kits";
|
|
179
179
|
const result = {
|
|
180
180
|
projectName: defaultProjectName,
|
|
181
181
|
packageName: defaultProjectName,
|
|
182
|
-
shouldOverwrite: false
|
|
183
|
-
features: []
|
|
182
|
+
shouldOverwrite: false
|
|
184
183
|
};
|
|
185
184
|
if (!targetDir) targetDir = result.projectName = result.packageName = (await unwrapPrompt(text({
|
|
186
185
|
message: "请输入项目名称:",
|
|
@@ -198,11 +197,10 @@ async function setupPrompts(targetDir, featureOptions) {
|
|
|
198
197
|
process.exit(0);
|
|
199
198
|
}
|
|
200
199
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}));
|
|
200
|
+
for (const step of steps) {
|
|
201
|
+
const extra = await step(result);
|
|
202
|
+
Object.assign(result, extra);
|
|
203
|
+
}
|
|
206
204
|
return {
|
|
207
205
|
result,
|
|
208
206
|
targetDir
|
|
@@ -210,4 +208,35 @@ async function setupPrompts(targetDir, featureOptions) {
|
|
|
210
208
|
}
|
|
211
209
|
|
|
212
210
|
//#endregion
|
|
213
|
-
|
|
211
|
+
//#region src/helper/steps.ts
|
|
212
|
+
function setupSelect(key, options) {
|
|
213
|
+
return async () => {
|
|
214
|
+
const value = await unwrapPrompt(select({
|
|
215
|
+
message: options.message,
|
|
216
|
+
options: options.options
|
|
217
|
+
}));
|
|
218
|
+
return { [key]: value };
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function setupFeatures(key, options) {
|
|
222
|
+
return async () => {
|
|
223
|
+
const selected = await unwrapPrompt(multiselect({
|
|
224
|
+
message: options.message ?? `请选择要包含的功能: ${pico.dim("(↑/↓ 切换,空格选择,a 全选,回车确认)")}`,
|
|
225
|
+
options: options.options,
|
|
226
|
+
required: options.required ?? false
|
|
227
|
+
}));
|
|
228
|
+
return { [key]: selected };
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function setupConfirm(key, options) {
|
|
232
|
+
return async () => {
|
|
233
|
+
const value = await unwrapPrompt(confirm({
|
|
234
|
+
message: options.message,
|
|
235
|
+
initialValue: options.initialValue ?? false
|
|
236
|
+
}));
|
|
237
|
+
return { [key]: value };
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
//#endregion
|
|
242
|
+
export { canSkipEmptying, dotGitDirectoryState, emptyDir, getCommand, getPackageManager, postOrderDirectoryTraverse, preOrderDirectoryTraverse, renderFile, renderTemplate, setupConfirm, setupFeatures, setupProject, setupPrompts, setupSelect, unwrapPrompt };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doyuli/kits-core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.2",
|
|
5
5
|
"description": "@doyuli/kits-core",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -14,11 +14,9 @@
|
|
|
14
14
|
],
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"exports": {
|
|
17
|
-
".": "./dist/index.
|
|
17
|
+
".": "./dist/index.mjs",
|
|
18
18
|
"./package.json": "./package.json"
|
|
19
19
|
},
|
|
20
|
-
"main": "./dist/index.js",
|
|
21
|
-
"module": "./dist/index.js",
|
|
22
20
|
"types": "./dist/index.d.ts",
|
|
23
21
|
"files": [
|
|
24
22
|
"dist"
|