@doyuli/kits-core 0.1.1 → 0.2.1

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.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 doyuli
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2025 doyuli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
- ### Kits-Core
2
-
3
- Provides basic capabilities for creating templates
1
+ ### Kits-Core
2
+
3
+ Provides basic capabilities for creating templates
@@ -1,20 +1,39 @@
1
1
  import { Option } from "@clack/prompts";
2
2
 
3
3
  //#region src/helper/setupPrompts.d.ts
4
- interface PromptResult<T = string> {
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
- declare function setupPrompts<T = string>(targetDir: string, featureOptions: ReadonlyArray<Option<T>>): Promise<{
11
- result: PromptResult<T>;
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 { 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$1) => fs.rmdirSync(dir$1), (file) => fs.unlinkSync(file));
51
+ postOrderDirectoryTraverse(dir, (dir) => fs.rmdirSync(dir), (file) => fs.unlinkSync(file));
52
52
  }
53
53
 
54
54
  //#endregion
@@ -91,13 +91,12 @@ function deepMerge(target, source) {
91
91
  }
92
92
  function sortDependencies(packageJson) {
93
93
  const sorted = {};
94
- const depTypes = [
94
+ for (const depType of [
95
95
  "dependencies",
96
96
  "devDependencies",
97
97
  "peerDependencies",
98
98
  "optionalDependencies"
99
- ];
100
- for (const depType of depTypes) if (packageJson[depType]) {
99
+ ]) if (packageJson[depType]) {
101
100
  sorted[depType] = {};
102
101
  Object.keys(packageJson[depType]).sort().forEach((name) => {
103
102
  sorted[depType][name] = packageJson[depType][name];
@@ -131,23 +130,17 @@ function renderTemplate(src, dest) {
131
130
  }
132
131
  const filename = path.basename(src);
133
132
  if (filename === "package.json" && fs.existsSync(dest)) {
134
- const existing = JSON.parse(fs.readFileSync(dest, "utf8"));
135
- const newPackage = JSON.parse(fs.readFileSync(src, "utf8"));
136
- const pkg = sortDependencies(deepMerge(existing, newPackage));
133
+ const pkg = sortDependencies(deepMerge(JSON.parse(fs.readFileSync(dest, "utf8")), JSON.parse(fs.readFileSync(src, "utf8"))));
137
134
  fs.writeFileSync(dest, `${JSON.stringify(pkg, null, 2)}\n`);
138
135
  return;
139
136
  }
140
137
  if (filename === "extensions.json" && fs.existsSync(dest)) {
141
- const existing = JSON.parse(fs.readFileSync(dest, "utf8"));
142
- const newExtensions = JSON.parse(fs.readFileSync(src, "utf8"));
143
- const extensions = deepMerge(existing, newExtensions);
138
+ const extensions = deepMerge(JSON.parse(fs.readFileSync(dest, "utf8")), JSON.parse(fs.readFileSync(src, "utf8")));
144
139
  fs.writeFileSync(dest, `${JSON.stringify(extensions, null, 2)}\n`);
145
140
  return;
146
141
  }
147
142
  if (filename === "settings.json" && fs.existsSync(dest)) {
148
- const existing = JSON.parse(fs.readFileSync(dest, "utf8"));
149
- const newSettings = JSON.parse(fs.readFileSync(src, "utf8"));
150
- const settings = deepMerge(existing, newSettings);
143
+ const settings = deepMerge(JSON.parse(fs.readFileSync(dest, "utf8")), JSON.parse(fs.readFileSync(src, "utf8")));
151
144
  fs.writeFileSync(dest, `${JSON.stringify(settings, null, 2)}\n`);
152
145
  return;
153
146
  }
@@ -181,13 +174,12 @@ async function setupProject(cwd, result, targetDir) {
181
174
 
182
175
  //#endregion
183
176
  //#region src/helper/setupPrompts.ts
184
- async function setupPrompts(targetDir, featureOptions) {
177
+ async function setupPrompts(targetDir, steps) {
185
178
  const defaultProjectName = targetDir || "Template-Kits";
186
179
  const result = {
187
180
  projectName: defaultProjectName,
188
181
  packageName: defaultProjectName,
189
- shouldOverwrite: false,
190
- features: []
182
+ shouldOverwrite: false
191
183
  };
192
184
  if (!targetDir) targetDir = result.projectName = result.packageName = (await unwrapPrompt(text({
193
185
  message: "请输入项目名称:",
@@ -205,11 +197,10 @@ async function setupPrompts(targetDir, featureOptions) {
205
197
  process.exit(0);
206
198
  }
207
199
  }
208
- result.features = await unwrapPrompt(multiselect({
209
- message: `请选择要包含的功能: ${pico.dim("(↑/↓ 切换,空格选择,a 全选,回车确认)")}`,
210
- options: featureOptions,
211
- required: false
212
- }));
200
+ for (const step of steps) {
201
+ const extra = await step(result);
202
+ Object.assign(result, extra);
203
+ }
213
204
  return {
214
205
  result,
215
206
  targetDir
@@ -217,4 +208,35 @@ async function setupPrompts(targetDir, featureOptions) {
217
208
  }
218
209
 
219
210
  //#endregion
220
- export { canSkipEmptying, dotGitDirectoryState, emptyDir, getCommand, getPackageManager, postOrderDirectoryTraverse, preOrderDirectoryTraverse, renderFile, renderTemplate, setupProject, setupPrompts, unwrapPrompt };
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.1.1",
4
+ "version": "0.2.1",
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.js",
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"