@doyuli/kits-core 0.0.1-beta.3 → 0.1.0
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 +21 -0
- package/dist/index.d.ts +27 -8
- package/dist/index.js +84 -62
- package/package.json +5 -1
package/LICENSE
ADDED
|
@@ -0,0 +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
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Option } from "@clack/prompts";
|
|
2
|
+
|
|
3
|
+
//#region src/helper/setupPrompts.d.ts
|
|
4
|
+
interface PromptResult<T = string> {
|
|
5
|
+
projectName?: string;
|
|
6
|
+
packageName?: string;
|
|
7
|
+
shouldOverwrite?: boolean;
|
|
8
|
+
features?: T[];
|
|
9
|
+
}
|
|
10
|
+
declare function setupPrompts<T = string>(targetDir: string, featureOptions: ReadonlyArray<Option<T>>): Promise<{
|
|
11
|
+
result: PromptResult<T>;
|
|
12
|
+
targetDir: string;
|
|
13
|
+
}>;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/helper/setupProject.d.ts
|
|
16
|
+
declare function setupProject(cwd: string, result: PromptResult, targetDir: string): Promise<string>;
|
|
5
17
|
//#endregion
|
|
6
18
|
//#region src/utils/directoryTraverse.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* @see https://github.com/vuejs/create-vue/blob/main/utils/directoryTraverse.ts
|
|
21
|
+
*/
|
|
7
22
|
declare function preOrderDirectoryTraverse(dir: string, dirCallback: (dir: string) => void, fileCallback: (file: string) => void): void;
|
|
8
23
|
declare const dotGitDirectoryState: {
|
|
9
24
|
hasDotGitDirectory: boolean;
|
|
@@ -12,11 +27,17 @@ declare function postOrderDirectoryTraverse(dir: string, dirCallback: (dir: stri
|
|
|
12
27
|
declare function canSkipEmptying(dir: string): boolean;
|
|
13
28
|
declare function emptyDir(dir: string): void;
|
|
14
29
|
//#endregion
|
|
30
|
+
//#region src/utils/package.d.ts
|
|
31
|
+
declare function getPackageManager(): "pnpm" | "yarn" | "bun" | "npm";
|
|
32
|
+
declare function getCommand(manager: string, name: string, args?: string): string;
|
|
33
|
+
//#endregion
|
|
15
34
|
//#region src/utils/prompts.d.ts
|
|
16
35
|
declare function unwrapPrompt<T>(maybeCancelPromise: Promise<T | symbol>): Promise<T>;
|
|
17
36
|
//#endregion
|
|
18
37
|
//#region src/utils/renderTemplate.d.ts
|
|
19
38
|
/**
|
|
39
|
+
* @see https://github.com/vuejs/create-vue/blob/main/utils/renderTemplate.ts
|
|
40
|
+
*
|
|
20
41
|
* Renders a template folder/file to the file system,
|
|
21
42
|
* by recursively copying all files under the `src` directory,
|
|
22
43
|
* with the following exception:
|
|
@@ -25,9 +46,7 @@ declare function unwrapPrompt<T>(maybeCancelPromise: Promise<T | symbol>): Promi
|
|
|
25
46
|
* @param {string} src source filename to copy
|
|
26
47
|
* @param {string} dest destination filename of the copy operation
|
|
27
48
|
*/
|
|
28
|
-
declare function renderTemplate(src: string, dest: string
|
|
29
|
-
//#endregion
|
|
30
|
-
//#region src/utils/render.d.ts
|
|
49
|
+
declare function renderTemplate(src: string, dest: string): void;
|
|
31
50
|
declare function renderFile(root: string, fileName: string, content: string): void;
|
|
32
51
|
//#endregion
|
|
33
|
-
export { canSkipEmptying, dotGitDirectoryState, emptyDir,
|
|
52
|
+
export { PromptResult, canSkipEmptying, dotGitDirectoryState, emptyDir, getCommand, getPackageManager, postOrderDirectoryTraverse, preOrderDirectoryTraverse, renderFile, renderTemplate, setupProject, setupPrompts, unwrapPrompt };
|
package/dist/index.js
CHANGED
|
@@ -1,38 +1,13 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
1
2
|
import * as path from "node:path";
|
|
2
|
-
import { relative, resolve } from "node:path";
|
|
3
3
|
import process from "node:process";
|
|
4
|
+
import { cancel, confirm, isCancel, multiselect, text } from "@clack/prompts";
|
|
4
5
|
import pico from "picocolors";
|
|
5
|
-
import * as fs from "node:fs";
|
|
6
|
-
import { writeFileSync } from "node:fs";
|
|
7
|
-
import { cancel, isCancel } from "@clack/prompts";
|
|
8
|
-
import { pathToFileURL } from "node:url";
|
|
9
|
-
|
|
10
|
-
//#region src/utils/command.ts
|
|
11
|
-
function getPackageManager() {
|
|
12
|
-
const userAgent = process.env.npm_config_user_agent ?? "";
|
|
13
|
-
return /pnpm/.test(userAgent) ? "pnpm" : /yarn/.test(userAgent) ? "yarn" : /bun/.test(userAgent) ? "bun" : "npm";
|
|
14
|
-
}
|
|
15
|
-
function getPackageCommand(packageManager, scriptName, args) {
|
|
16
|
-
if (scriptName === "install") return packageManager === "yarn" ? "yarn" : `${packageManager} install`;
|
|
17
|
-
if (scriptName === "build") return packageManager === "npm" || packageManager === "bun" ? `${packageManager} run build` : `${packageManager} build`;
|
|
18
|
-
if (args) return packageManager === "npm" ? `npm run ${scriptName} -- ${args}` : `${packageManager} ${scriptName} ${args}`;
|
|
19
|
-
else return packageManager === "npm" ? `npm run ${scriptName}` : `${packageManager} ${scriptName}`;
|
|
20
|
-
}
|
|
21
|
-
function getOutroMessage(root, cwd) {
|
|
22
|
-
const packageManager = getPackageManager();
|
|
23
|
-
let message = `项目初始化完成,可执行以下命令:\n\n`;
|
|
24
|
-
if (root !== cwd) {
|
|
25
|
-
const cdProjectName = relative(cwd, root);
|
|
26
|
-
message += ` ${pico.bold(pico.green(`cd ${cdProjectName.includes(" ") ? `"${cdProjectName}"` : cdProjectName}`))}\n`;
|
|
27
|
-
}
|
|
28
|
-
message += ` ${pico.bold(pico.green(getPackageCommand(packageManager, "install")))}\n`;
|
|
29
|
-
message += ` ${pico.bold(pico.green(getPackageCommand(packageManager, "lint:fix")))}\n`;
|
|
30
|
-
message += ` ${pico.bold(pico.green(getPackageCommand(packageManager, "dev")))}\n`;
|
|
31
|
-
return message;
|
|
32
|
-
}
|
|
33
6
|
|
|
34
|
-
//#endregion
|
|
35
7
|
//#region src/utils/directoryTraverse.ts
|
|
8
|
+
/**
|
|
9
|
+
* @see https://github.com/vuejs/create-vue/blob/main/utils/directoryTraverse.ts
|
|
10
|
+
*/
|
|
36
11
|
function preOrderDirectoryTraverse(dir, dirCallback, fileCallback) {
|
|
37
12
|
for (const filename of fs.readdirSync(dir)) {
|
|
38
13
|
if (filename === ".git") continue;
|
|
@@ -76,6 +51,19 @@ function emptyDir(dir) {
|
|
|
76
51
|
postOrderDirectoryTraverse(dir, (dir$1) => fs.rmdirSync(dir$1), (file) => fs.unlinkSync(file));
|
|
77
52
|
}
|
|
78
53
|
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/utils/package.ts
|
|
56
|
+
function getPackageManager() {
|
|
57
|
+
const userAgent = process.env.npm_config_user_agent ?? "";
|
|
58
|
+
return /pnpm/.test(userAgent) ? "pnpm" : /yarn/.test(userAgent) ? "yarn" : /bun/.test(userAgent) ? "bun" : "npm";
|
|
59
|
+
}
|
|
60
|
+
function getCommand(manager, name, args) {
|
|
61
|
+
if (name === "install") return manager === "yarn" ? "yarn" : `${manager} install`;
|
|
62
|
+
if (name === "build") return manager === "npm" || manager === "bun" ? `${manager} run build` : `${manager} build`;
|
|
63
|
+
if (args) return manager === "npm" ? `npm run ${name} -- ${args}` : `${manager} ${name} ${args}`;
|
|
64
|
+
else return manager === "npm" ? `npm run ${name}` : `${manager} ${name}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
79
67
|
//#endregion
|
|
80
68
|
//#region src/utils/prompts.ts
|
|
81
69
|
async function unwrapPrompt(maybeCancelPromise) {
|
|
@@ -88,28 +76,19 @@ async function unwrapPrompt(maybeCancelPromise) {
|
|
|
88
76
|
}
|
|
89
77
|
|
|
90
78
|
//#endregion
|
|
91
|
-
//#region src/utils/
|
|
92
|
-
const isObject = (val) =>
|
|
93
|
-
const mergeArrayWithDedupe = (a, b) =>
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
* @param {object} target the existing object
|
|
97
|
-
* @param {object} obj the new object
|
|
98
|
-
*/
|
|
99
|
-
function deepMerge(target, obj) {
|
|
100
|
-
for (const key of Object.keys(obj)) {
|
|
79
|
+
//#region src/utils/util.ts
|
|
80
|
+
const isObject = (val) => typeof val === "object" && val !== null;
|
|
81
|
+
const mergeArrayWithDedupe = (a, b) => [...new Set([...a, ...b])];
|
|
82
|
+
function deepMerge(target, source) {
|
|
83
|
+
for (const key of Object.keys(source)) {
|
|
101
84
|
const oldVal = target[key];
|
|
102
|
-
const newVal =
|
|
85
|
+
const newVal = source[key];
|
|
103
86
|
if (Array.isArray(oldVal) && Array.isArray(newVal)) target[key] = mergeArrayWithDedupe(oldVal, newVal);
|
|
104
87
|
else if (isObject(oldVal) && isObject(newVal)) target[key] = deepMerge(oldVal, newVal);
|
|
105
88
|
else target[key] = newVal;
|
|
106
89
|
}
|
|
107
90
|
return target;
|
|
108
91
|
}
|
|
109
|
-
var deepMerge_default = deepMerge;
|
|
110
|
-
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/utils/sortDependencies.ts
|
|
113
92
|
function sortDependencies(packageJson) {
|
|
114
93
|
const sorted = {};
|
|
115
94
|
const depTypes = [
|
|
@@ -133,6 +112,8 @@ function sortDependencies(packageJson) {
|
|
|
133
112
|
//#endregion
|
|
134
113
|
//#region src/utils/renderTemplate.ts
|
|
135
114
|
/**
|
|
115
|
+
* @see https://github.com/vuejs/create-vue/blob/main/utils/renderTemplate.ts
|
|
116
|
+
*
|
|
136
117
|
* Renders a template folder/file to the file system,
|
|
137
118
|
* by recursively copying all files under the `src` directory,
|
|
138
119
|
* with the following exception:
|
|
@@ -141,32 +122,32 @@ function sortDependencies(packageJson) {
|
|
|
141
122
|
* @param {string} src source filename to copy
|
|
142
123
|
* @param {string} dest destination filename of the copy operation
|
|
143
124
|
*/
|
|
144
|
-
function renderTemplate(src, dest
|
|
125
|
+
function renderTemplate(src, dest) {
|
|
145
126
|
if (fs.statSync(src).isDirectory()) {
|
|
146
127
|
if (path.basename(src) === "node_modules") return;
|
|
147
128
|
fs.mkdirSync(dest, { recursive: true });
|
|
148
|
-
for (const file of fs.readdirSync(src)) renderTemplate(path.resolve(src, file), path.resolve(dest, file)
|
|
129
|
+
for (const file of fs.readdirSync(src)) renderTemplate(path.resolve(src, file), path.resolve(dest, file));
|
|
149
130
|
return;
|
|
150
131
|
}
|
|
151
132
|
const filename = path.basename(src);
|
|
152
133
|
if (filename === "package.json" && fs.existsSync(dest)) {
|
|
153
134
|
const existing = JSON.parse(fs.readFileSync(dest, "utf8"));
|
|
154
135
|
const newPackage = JSON.parse(fs.readFileSync(src, "utf8"));
|
|
155
|
-
const pkg = sortDependencies(
|
|
136
|
+
const pkg = sortDependencies(deepMerge(existing, newPackage));
|
|
156
137
|
fs.writeFileSync(dest, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
157
138
|
return;
|
|
158
139
|
}
|
|
159
140
|
if (filename === "extensions.json" && fs.existsSync(dest)) {
|
|
160
141
|
const existing = JSON.parse(fs.readFileSync(dest, "utf8"));
|
|
161
142
|
const newExtensions = JSON.parse(fs.readFileSync(src, "utf8"));
|
|
162
|
-
const extensions =
|
|
143
|
+
const extensions = deepMerge(existing, newExtensions);
|
|
163
144
|
fs.writeFileSync(dest, `${JSON.stringify(extensions, null, 2)}\n`);
|
|
164
145
|
return;
|
|
165
146
|
}
|
|
166
147
|
if (filename === "settings.json" && fs.existsSync(dest)) {
|
|
167
148
|
const existing = JSON.parse(fs.readFileSync(dest, "utf8"));
|
|
168
149
|
const newSettings = JSON.parse(fs.readFileSync(src, "utf8"));
|
|
169
|
-
const settings =
|
|
150
|
+
const settings = deepMerge(existing, newSettings);
|
|
170
151
|
fs.writeFileSync(dest, `${JSON.stringify(settings, null, 2)}\n`);
|
|
171
152
|
return;
|
|
172
153
|
}
|
|
@@ -177,22 +158,63 @@ function renderTemplate(src, dest, callbacks) {
|
|
|
177
158
|
fs.writeFileSync(dest, `${existing}\n${newGitignore}`);
|
|
178
159
|
return;
|
|
179
160
|
}
|
|
180
|
-
if (filename.endsWith(".data.mjs")) {
|
|
181
|
-
dest = dest.replace(/\.data\.mjs$/, "");
|
|
182
|
-
callbacks.push(async (dataStore) => {
|
|
183
|
-
const getData = (await import(pathToFileURL(src).toString())).default;
|
|
184
|
-
dataStore[dest] = await getData({ oldData: dataStore[dest] || {} });
|
|
185
|
-
});
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
161
|
fs.copyFileSync(src, dest);
|
|
189
162
|
}
|
|
163
|
+
function renderFile(root, fileName, content) {
|
|
164
|
+
fs.writeFileSync(path.resolve(root, fileName), content, "utf-8");
|
|
165
|
+
}
|
|
190
166
|
|
|
191
167
|
//#endregion
|
|
192
|
-
//#region src/
|
|
193
|
-
function
|
|
194
|
-
|
|
168
|
+
//#region src/helper/setupProject.ts
|
|
169
|
+
async function setupProject(cwd, result, targetDir) {
|
|
170
|
+
const root = path.join(cwd, targetDir);
|
|
171
|
+
if (fs.existsSync(root) && result.shouldOverwrite) emptyDir(root);
|
|
172
|
+
else if (!fs.existsSync(root)) fs.mkdirSync(root);
|
|
173
|
+
console.log(`\n正在初始化项目 ${root}...`);
|
|
174
|
+
const pkg = {
|
|
175
|
+
name: result.packageName,
|
|
176
|
+
version: "0.0.0"
|
|
177
|
+
};
|
|
178
|
+
renderFile(root, "package.json", JSON.stringify(pkg, null, 2));
|
|
179
|
+
return root;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/helper/setupPrompts.ts
|
|
184
|
+
async function setupPrompts(targetDir, featureOptions) {
|
|
185
|
+
const defaultProjectName = targetDir || "Template-Kits";
|
|
186
|
+
const result = {
|
|
187
|
+
projectName: defaultProjectName,
|
|
188
|
+
packageName: defaultProjectName,
|
|
189
|
+
shouldOverwrite: false,
|
|
190
|
+
features: []
|
|
191
|
+
};
|
|
192
|
+
if (!targetDir) targetDir = result.projectName = result.packageName = (await unwrapPrompt(text({
|
|
193
|
+
message: "请输入项目名称:",
|
|
194
|
+
placeholder: defaultProjectName,
|
|
195
|
+
defaultValue: "",
|
|
196
|
+
validate: (value) => value.trim().length === 0 ? "不能为空" : ""
|
|
197
|
+
}))).trim();
|
|
198
|
+
if (!canSkipEmptying(targetDir)) {
|
|
199
|
+
result.shouldOverwrite = await unwrapPrompt(confirm({
|
|
200
|
+
message: `${targetDir === "." ? "当前目录" : `目标文件夹 "${targetDir}"`} 非空,是否覆盖?`,
|
|
201
|
+
initialValue: false
|
|
202
|
+
}));
|
|
203
|
+
if (!result.shouldOverwrite) {
|
|
204
|
+
cancel(`${pico.red("✖")} 操作取消`);
|
|
205
|
+
process.exit(0);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
result.features = await unwrapPrompt(multiselect({
|
|
209
|
+
message: `请选择要包含的功能: ${pico.dim("(↑/↓ 切换,空格选择,a 全选,回车确认)")}`,
|
|
210
|
+
options: featureOptions,
|
|
211
|
+
required: false
|
|
212
|
+
}));
|
|
213
|
+
return {
|
|
214
|
+
result,
|
|
215
|
+
targetDir
|
|
216
|
+
};
|
|
195
217
|
}
|
|
196
218
|
|
|
197
219
|
//#endregion
|
|
198
|
-
export { canSkipEmptying, dotGitDirectoryState, emptyDir,
|
|
220
|
+
export { canSkipEmptying, dotGitDirectoryState, emptyDir, getCommand, getPackageManager, postOrderDirectoryTraverse, preOrderDirectoryTraverse, renderFile, renderTemplate, setupProject, setupPrompts, unwrapPrompt };
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doyuli/kits-core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"description": "@doyuli/kits-core",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/doyuli/template-kits.git",
|
|
9
9
|
"directory": "packages/kits-core"
|
|
10
10
|
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"templates",
|
|
13
|
+
"kits"
|
|
14
|
+
],
|
|
11
15
|
"sideEffects": false,
|
|
12
16
|
"exports": {
|
|
13
17
|
".": "./dist/index.js",
|