@ruan-cat/utils 1.7.0 → 3.0.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/{readme.md → README.md} +6 -0
- package/dist/index.d.ts +1 -129
- package/dist/index.js +1 -56
- package/dist/index.js.map +1 -1
- package/dist/{node.cjs → node-cjs/index.cjs} +13 -15
- package/dist/node-cjs/index.cjs.map +1 -0
- package/dist/{node.d.cts → node-cjs/index.d.cts} +2 -3
- package/dist/node-esm/index.d.ts +33 -0
- package/dist/node-esm/index.js +114 -0
- package/dist/node-esm/index.js.map +1 -0
- package/package.json +26 -9
- package/src/index.ts +0 -6
- package/src/node-cjs/index.ts +1 -0
- package/src/{node.ts → node-cjs/tools.ts} +4 -2
- package/src/node-esm/index.ts +3 -0
- package/src/node-esm/scripts/clean.ts +45 -0
- package/src/unplugin-vue-router/README.md +21 -0
- package/src/unplugin-vue-router/index.ts +11 -0
- package/src/vite-plugin-autogeneration-import-file/README.md +37 -0
- package/src/vite-plugin-autogeneration-import-file/index.ts +120 -0
- package/src/vite-plugin-autogeneration-import-file/template/components.template.ts +21 -0
- package/src/vueuse/README.md +9 -0
- package/src/vueuse/index.ts +1 -0
- package/src/vueuse/useAxios/README.md +59 -0
- package/tsconfig.json +1 -1
- package/dist/node.cjs.map +0 -1
- package/dist/node.d.ts +0 -43
- package/dist/node.js +0 -62
- package/dist/node.js.map +0 -1
- /package/src/{ruan-cat-pkg-info.ts → node-esm/ruan-cat-pkg-info.ts} +0 -0
- /package/src/vueuse/{useAxios.ts → useAxios/index.ts} +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** 包的信息 */
|
|
2
|
+
interface PackageInfo {
|
|
3
|
+
/** 包名 */
|
|
4
|
+
name: string;
|
|
5
|
+
/** 包的描述 */
|
|
6
|
+
description: string;
|
|
7
|
+
/** 带有包名的官方镜像源地址 */
|
|
8
|
+
url: `https://npm.im/${string}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 获得阮喵喵全部的包信息
|
|
12
|
+
* @description
|
|
13
|
+
* 这是一个node环境下的函数,用于获取阮喵喵的所有包的信息。
|
|
14
|
+
*
|
|
15
|
+
* 使用的是node的child_process模块,调用pnpm命令获取包信息。
|
|
16
|
+
*
|
|
17
|
+
* - 默认仅考虑pnpm包
|
|
18
|
+
* - 在node环境下运行
|
|
19
|
+
*/
|
|
20
|
+
declare function getRuanCatPkgInfo(): Promise<PackageInfo[]>;
|
|
21
|
+
|
|
22
|
+
declare const defaultCleanTargets: readonly ["node_modules", "yarn.lock", "pnpm-lock.yaml", "package-lock.json", "dist", ".turbo", ".vercel", ".cache", ".temp"];
|
|
23
|
+
/**
|
|
24
|
+
* 删除node项目的依赖项便于重新安装依赖,也包括常见的各种垃圾文件。
|
|
25
|
+
* @description
|
|
26
|
+
*/
|
|
27
|
+
declare function clean(
|
|
28
|
+
/**
|
|
29
|
+
* 被清除的目标文件夹 也包括文件
|
|
30
|
+
*/
|
|
31
|
+
targets?: string[]): Promise<void>;
|
|
32
|
+
|
|
33
|
+
export { type PackageInfo, clean, defaultCleanTargets, getRuanCatPkgInfo };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/node-esm/ruan-cat-pkg-info.ts
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
async function getRuanCatPkgInfo() {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const result = spawnSync("pnpm", ["s", "@ruan-cat/*", "--registry", "https://registry.npmmirror.com/", "--json"], {
|
|
6
|
+
encoding: "utf-8"
|
|
7
|
+
});
|
|
8
|
+
if (result.error) {
|
|
9
|
+
console.error(`Error executing command: ${result.error.message}`);
|
|
10
|
+
reject(result.error);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (result.stderr) {
|
|
14
|
+
console.error(`Error in output: ${result.stderr}`);
|
|
15
|
+
reject(new Error(result.stderr));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const packages = JSON.parse(result.stdout);
|
|
19
|
+
const res = packages.map(
|
|
20
|
+
(pkg) => ({
|
|
21
|
+
name: pkg.name,
|
|
22
|
+
description: pkg.description,
|
|
23
|
+
url: `https://npm.im/${pkg.name}`
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
resolve(res);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/node-cjs/tools.ts
|
|
31
|
+
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
32
|
+
|
|
33
|
+
// src/simple-promise-tools.ts
|
|
34
|
+
function generateSimpleAsyncTask(func) {
|
|
35
|
+
return function(...args) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
resolve(func(...args));
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/node-cjs/tools.ts
|
|
43
|
+
import consola from "consola";
|
|
44
|
+
var defPrintCurrentCommand = function(params) {
|
|
45
|
+
const { command, parameters } = params;
|
|
46
|
+
consola.info(` \u5F53\u524D\u8FD0\u884C\u7684\u547D\u4EE4\u4E3A\uFF1A ${command} ${parameters.join(" ")}
|
|
47
|
+
`);
|
|
48
|
+
};
|
|
49
|
+
function generateSpawnSync(spawnSyncSimpleParams) {
|
|
50
|
+
const {
|
|
51
|
+
command,
|
|
52
|
+
parameters,
|
|
53
|
+
isFlow = true,
|
|
54
|
+
isShowCommand = true,
|
|
55
|
+
spawnOptions = {},
|
|
56
|
+
printCurrentCommand = defPrintCurrentCommand
|
|
57
|
+
} = spawnSyncSimpleParams;
|
|
58
|
+
if (isShowCommand) {
|
|
59
|
+
printCurrentCommand?.({ command, parameters });
|
|
60
|
+
}
|
|
61
|
+
return generateSimpleAsyncTask(() => {
|
|
62
|
+
const result = spawnSync2(command, parameters, {
|
|
63
|
+
/**
|
|
64
|
+
* 是否流式输出?
|
|
65
|
+
* 是流式输出就是继承父进程的流式输出
|
|
66
|
+
* 否则就使用默认值
|
|
67
|
+
* @see https://nodejs.org/api/child_process.html#optionsstdio
|
|
68
|
+
*/
|
|
69
|
+
stdio: isFlow ? "inherit" : "pipe",
|
|
70
|
+
shell: true,
|
|
71
|
+
...spawnOptions
|
|
72
|
+
});
|
|
73
|
+
if (!isFlow) {
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
if (result.error) {
|
|
77
|
+
throw result.error;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/node-esm/scripts/clean.ts
|
|
84
|
+
var defaultCleanTargets = [
|
|
85
|
+
// node常见文件
|
|
86
|
+
"node_modules",
|
|
87
|
+
"yarn.lock",
|
|
88
|
+
"pnpm-lock.yaml",
|
|
89
|
+
"package-lock.json",
|
|
90
|
+
// 项目常见文件
|
|
91
|
+
"dist",
|
|
92
|
+
//
|
|
93
|
+
".turbo",
|
|
94
|
+
".vercel",
|
|
95
|
+
// vuepress
|
|
96
|
+
".cache",
|
|
97
|
+
".temp"
|
|
98
|
+
];
|
|
99
|
+
async function clean(targets) {
|
|
100
|
+
const cleanTargets = targets ?? defaultCleanTargets;
|
|
101
|
+
const glob = `**/{${cleanTargets.join()}}`;
|
|
102
|
+
console.log(" \u5F53\u524D\u8FD0\u884C\u5730\u5740 process.cwd() ", process.cwd());
|
|
103
|
+
const doClean = generateSpawnSync({
|
|
104
|
+
command: "rimraf",
|
|
105
|
+
parameters: ["-g", glob]
|
|
106
|
+
});
|
|
107
|
+
await doClean();
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
clean,
|
|
111
|
+
defaultCleanTargets,
|
|
112
|
+
getRuanCatPkgInfo
|
|
113
|
+
};
|
|
114
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/node-esm/ruan-cat-pkg-info.ts","../../src/node-cjs/tools.ts","../../src/simple-promise-tools.ts","../../src/node-esm/scripts/clean.ts"],"sourcesContent":["import { spawnSync } from \"node:child_process\";\n\n/** 包的信息 */\nexport interface PackageInfo {\n\t/** 包名 */\n\tname: string;\n\t/** 包的描述 */\n\tdescription: string;\n\t/** 带有包名的官方镜像源地址 */\n\turl: `https://npm.im/${string}`;\n}\n\n/**\n * 获得阮喵喵全部的包信息\n * @description\n * 这是一个node环境下的函数,用于获取阮喵喵的所有包的信息。\n *\n * 使用的是node的child_process模块,调用pnpm命令获取包信息。\n *\n * - 默认仅考虑pnpm包\n * - 在node环境下运行\n */\nexport async function getRuanCatPkgInfo() {\n\treturn new Promise<PackageInfo[]>((resolve, reject) => {\n\t\t/**\n\t\t * pnpm s @ruan-cat/* --registry https://registry.npmmirror.com/ --json\n\t\t * 仅查询淘宝源的数据\n\t\t */\n\t\tconst result = spawnSync(\"pnpm\", [\"s\", \"@ruan-cat/*\", \"--registry\", \"https://registry.npmmirror.com/\", \"--json\"], {\n\t\t\tencoding: \"utf-8\",\n\t\t});\n\n\t\tif (result.error) {\n\t\t\tconsole.error(`Error executing command: ${result.error.message}`);\n\t\t\treject(result.error);\n\t\t\treturn;\n\t\t}\n\t\tif (result.stderr) {\n\t\t\tconsole.error(`Error in output: ${result.stderr}`);\n\t\t\treject(new Error(result.stderr));\n\t\t\treturn;\n\t\t}\n\n\t\tconst packages = <unknown[]>JSON.parse(result.stdout);\n\t\tconst res = packages.map(\n\t\t\t(pkg: any) =>\n\t\t\t\t({\n\t\t\t\t\tname: pkg.name,\n\t\t\t\t\tdescription: pkg.description,\n\t\t\t\t\turl: `https://npm.im/${pkg.name}`,\n\t\t\t\t}) satisfies PackageInfo,\n\t\t);\n\t\t// const res = packages;\n\t\tresolve(res);\n\t});\n}\n","/**\n * 一些node环境下的工具函数\n */\n\nimport { normalize } from \"node:path\";\nimport { spawnSync, type SpawnOptions } from \"node:child_process\";\n\nimport { generateSimpleAsyncTask } from \"@ruan-cat/utils\";\nimport consola from \"consola\";\n// import { normalizePath } from \"vite\";\n\n/**\n * 路径转换工具\n */\nexport function pathChange(path: string) {\n\treturn path.replace(/\\\\/g, \"/\");\n\t// FIXME: 无法有效地实现解析路径 测试用例不通过\n\t// return normalize(path);\n\t// FIXME: tsup打包时,无法处理好vite的依赖 会导致打包失败 不知道怎么单独使用并打包该函数\n\t// return normalizePath(path);\n}\n\nexport interface SpawnSyncSimpleParams {\n\tcommand: string;\n\tparameters: string[];\n\t/**\n\t * 是否流式输出内容\n\t * @description 默认输出的命令数据全部以流式的方式输出\n\t * @default true\n\t */\n\tisFlow?: boolean;\n\n\t/**\n\t * 是否显示命令?\n\t * @description\n\t * 是否打印目前正在执行的命令?\n\t * @default true\n\t */\n\tisShowCommand?: boolean;\n\tspawnOptions?: SpawnOptions;\n\n\t/** 打印当前运行的命令 */\n\tprintCurrentCommand?: (params: Pick<SpawnSyncSimpleParams, \"command\" | \"parameters\">) => void;\n}\n\n/**\n * 默认的打印当前运行命令 函数\n */\nexport const defPrintCurrentCommand: SpawnSyncSimpleParams[\"printCurrentCommand\"] = function (params) {\n\tconst { command, parameters } = params;\n\tconsola.info(` 当前运行的命令为: ${command} ${parameters.join(\" \")} \\n`);\n};\n\n/**\n * 生成简单的执行命令函数\n * @description\n * 对 spawnSync 做简单的包装\n *\n * 之前封装的是 execa 函数\n * @version 2\n */\nexport function generateSpawnSync(spawnSyncSimpleParams: SpawnSyncSimpleParams) {\n\tconst {\n\t\tcommand,\n\t\tparameters,\n\t\tisFlow = true,\n\t\tisShowCommand = true,\n\t\tspawnOptions = {},\n\t\tprintCurrentCommand = defPrintCurrentCommand,\n\t} = spawnSyncSimpleParams;\n\n\tif (isShowCommand) {\n\t\tprintCurrentCommand?.({ command, parameters });\n\t}\n\n\treturn generateSimpleAsyncTask(() => {\n\t\tconst result = spawnSync(command, parameters, {\n\t\t\t/**\n\t\t\t * 是否流式输出?\n\t\t\t * 是流式输出就是继承父进程的流式输出\n\t\t\t * 否则就使用默认值\n\t\t\t * @see https://nodejs.org/api/child_process.html#optionsstdio\n\t\t\t */\n\t\t\tstdio: isFlow ? \"inherit\" : \"pipe\",\n\t\t\tshell: true,\n\t\t\t...spawnOptions,\n\t\t});\n\n\t\t// 如果不是流式输出 就直接返回返回值即可\n\t\tif (!isFlow) {\n\t\t\treturn result;\n\t\t}\n\n\t\tif (result.error) {\n\t\t\tthrow result.error;\n\t\t}\n\n\t\treturn result;\n\t});\n}\n","// import { uniqueId } from \"lodash-es\";\n// const getCounter = () => uniqueId();\n\n/** 创建简单的异步任务 */\nexport function generateSimpleAsyncTask<T extends (...args: any) => any>(func: T) {\n\t// const taskId = getCounter();\n\n\treturn function (...args: any) {\n\t\t// consola.info(` 这是第 ${taskId} 个异步任务 `);\n\t\t// consola.start(\" 这里是新创建的异步函数 检查参数: \", ...args);\n\n\t\treturn new Promise<ReturnType<T>>((resolve, reject) => {\n\t\t\t// consola.start(\" 内部promise 检查参数: \", ...args);\n\t\t\tresolve(func(...args));\n\t\t});\n\t};\n}\n\nexport type SimpleAsyncTask = ReturnType<typeof generateSimpleAsyncTask>;\n\nexport const initFlag = <const>\"initFlag\";\n\n/**\n * 以队列串行的形式 串行运行异步函数\n * @see https://github.com/ascoders/weekly/blob/master/前沿技术/77.精读《用%20Reduce%20实现%20Promise%20串行执行》.md\n * @version 1\n */\nasync function runPromiseByQueueV1<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tpromises.reduce(\n\t\tasync function (previousPromise, nextPromise, currentIndex) {\n\t\t\tconst response = await previousPromise;\n\t\t\t// consola.log(` reduce串行函数 currentIndex= ${currentIndex} res =`, response);\n\t\t\treturn await nextPromise(response);\n\t\t},\n\t\tPromise.resolve(initFlag) as Promise<any>,\n\t);\n}\n\n/**\n * 以队列串行的形式 串行运行异步函数\n * @version 2\n */\nexport async function runPromiseByQueue<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tlet response: typeof initFlag | Awaited<T> = initFlag;\n\tfor await (const promise of promises) {\n\t\tresponse = await promise(response);\n\t}\n}\n\n/**\n * 以并行的形式 并发运行异步函数\n */\nexport async function runPromiseByConcurrency<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tawait Promise.all(promises.map((promise) => promise()));\n}\n","import { generateSpawnSync } from \"../../node-cjs/index.ts\";\n\ntype GlobString = `**/{${string}}`;\n\nexport const defaultCleanTargets = <const>[\n\t// node常见文件\n\t\"node_modules\",\n\t\"yarn.lock\",\n\t\"pnpm-lock.yaml\",\n\t\"package-lock.json\",\n\n\t// 项目常见文件\n\t\"dist\",\n\n\t//\n\t\".turbo\",\n\t\".vercel\",\n\n\t// vuepress\n\t\".cache\",\n\t\".temp\",\n];\n\n/**\n * 删除node项目的依赖项便于重新安装依赖,也包括常见的各种垃圾文件。\n * @description\n */\nexport async function clean(\n\t/**\n\t * 被清除的目标文件夹 也包括文件\n\t */\n\ttargets?: string[],\n) {\n\tconst cleanTargets = targets ?? defaultCleanTargets;\n\tconst glob: GlobString = `**/{${cleanTargets.join()}}`;\n\n\tconsole.log(\" 当前运行地址 process.cwd() \", process.cwd());\n\n\tconst doClean = generateSpawnSync({\n\t\tcommand: \"rimraf\",\n\t\tparameters: [\"-g\", glob],\n\t});\n\n\tawait doClean();\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAsB1B,eAAsB,oBAAoB;AACzC,SAAO,IAAI,QAAuB,CAAC,SAAS,WAAW;AAKtD,UAAM,SAAS,UAAU,QAAQ,CAAC,KAAK,eAAe,cAAc,mCAAmC,QAAQ,GAAG;AAAA,MACjH,UAAU;AAAA,IACX,CAAC;AAED,QAAI,OAAO,OAAO;AACjB,cAAQ,MAAM,4BAA4B,OAAO,MAAM,OAAO,EAAE;AAChE,aAAO,OAAO,KAAK;AACnB;AAAA,IACD;AACA,QAAI,OAAO,QAAQ;AAClB,cAAQ,MAAM,oBAAoB,OAAO,MAAM,EAAE;AACjD,aAAO,IAAI,MAAM,OAAO,MAAM,CAAC;AAC/B;AAAA,IACD;AAEA,UAAM,WAAsB,KAAK,MAAM,OAAO,MAAM;AACpD,UAAM,MAAM,SAAS;AAAA,MACpB,CAAC,SACC;AAAA,QACA,MAAM,IAAI;AAAA,QACV,aAAa,IAAI;AAAA,QACjB,KAAK,kBAAkB,IAAI,IAAI;AAAA,MAChC;AAAA,IACF;AAEA,YAAQ,GAAG;AAAA,EACZ,CAAC;AACF;;;AClDA,SAAS,aAAAA,kBAAoC;;;ACDtC,SAAS,wBAAyD,MAAS;AAGjF,SAAO,YAAa,MAAW;AAI9B,WAAO,IAAI,QAAuB,CAAC,SAAS,WAAW;AAEtD,cAAQ,KAAK,GAAG,IAAI,CAAC;AAAA,IACtB,CAAC;AAAA,EACF;AACD;;;ADRA,OAAO,aAAa;AAwCb,IAAM,yBAAuE,SAAU,QAAQ;AACrG,QAAM,EAAE,SAAS,WAAW,IAAI;AAChC,UAAQ,KAAK,2DAAc,OAAO,IAAI,WAAW,KAAK,GAAG,CAAC;AAAA,CAAK;AAChE;AAUO,SAAS,kBAAkB,uBAA8C;AAC/E,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA,SAAS;AAAA,IACT,gBAAgB;AAAA,IAChB,eAAe,CAAC;AAAA,IAChB,sBAAsB;AAAA,EACvB,IAAI;AAEJ,MAAI,eAAe;AAClB,0BAAsB,EAAE,SAAS,WAAW,CAAC;AAAA,EAC9C;AAEA,SAAO,wBAAwB,MAAM;AACpC,UAAM,SAASC,WAAU,SAAS,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAO7C,OAAO,SAAS,YAAY;AAAA,MAC5B,OAAO;AAAA,MACP,GAAG;AAAA,IACJ,CAAC;AAGD,QAAI,CAAC,QAAQ;AACZ,aAAO;AAAA,IACR;AAEA,QAAI,OAAO,OAAO;AACjB,YAAM,OAAO;AAAA,IACd;AAEA,WAAO;AAAA,EACR,CAAC;AACF;;;AE/FO,IAAM,sBAA6B;AAAA;AAAA,EAEzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA,EACA;AACD;AAMA,eAAsB,MAIrB,SACC;AACD,QAAM,eAAe,WAAW;AAChC,QAAM,OAAmB,OAAO,aAAa,KAAK,CAAC;AAEnD,UAAQ,IAAI,wDAA0B,QAAQ,IAAI,CAAC;AAEnD,QAAM,UAAU,kBAAkB;AAAA,IACjC,SAAS;AAAA,IACT,YAAY,CAAC,MAAM,IAAI;AAAA,EACxB,CAAC;AAED,QAAM,QAAQ;AACf;","names":["spawnSync","spawnSync"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "阮喵喵工具集合。一个纯typescript库,也提供纯js文件。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -20,11 +20,18 @@
|
|
|
20
20
|
"import": "./src/index.ts",
|
|
21
21
|
"require": "./dist/index.cjs"
|
|
22
22
|
},
|
|
23
|
-
"./node": {
|
|
24
|
-
"types": "./src/node.
|
|
25
|
-
"import": "./src/node.
|
|
26
|
-
"require": "./dist/node.cjs"
|
|
23
|
+
"./node-cjs": {
|
|
24
|
+
"types": "./src/node-cjs/index.ts",
|
|
25
|
+
"import": "./src/node-cjs/index.ts",
|
|
26
|
+
"require": "./dist/node-cjs/index.cjs"
|
|
27
27
|
},
|
|
28
|
+
"./node-esm": {
|
|
29
|
+
"types": "./src/node-esm/index.ts",
|
|
30
|
+
"import": "./src/node-esm/index.ts"
|
|
31
|
+
},
|
|
32
|
+
"./unplugin-vue-router": "./src/unplugin-vue-router/index.ts",
|
|
33
|
+
"./vite-plugin-autogeneration-import-file": "./src/vite-plugin-autogeneration-import-file/index.ts",
|
|
34
|
+
"./vueuse": "./src/vueuse/index.ts",
|
|
28
35
|
"./src/*": "./src/*",
|
|
29
36
|
"./dist/*": "./dist/*"
|
|
30
37
|
},
|
|
@@ -46,6 +53,8 @@
|
|
|
46
53
|
"src",
|
|
47
54
|
"dist/*",
|
|
48
55
|
"!src/tests",
|
|
56
|
+
"!src/**/tests",
|
|
57
|
+
"!src/**/*.md",
|
|
49
58
|
"tsconfig.json"
|
|
50
59
|
],
|
|
51
60
|
"dependencies": {
|
|
@@ -61,14 +70,17 @@
|
|
|
61
70
|
"tsup": "^8.3.5",
|
|
62
71
|
"type-plus": "^7.6.2",
|
|
63
72
|
"typedoc": "^0.27.0",
|
|
73
|
+
"typedoc-plugin-frontmatter": "^1.2.1",
|
|
64
74
|
"typedoc-plugin-markdown": "^4.3.0",
|
|
65
75
|
"typescript": "5.7.3",
|
|
66
|
-
"unplugin-vue-router": "^0.
|
|
76
|
+
"unplugin-vue-router": "^0.11.2",
|
|
77
|
+
"vite-plugin-autogeneration-import-file": "^3.0.0",
|
|
67
78
|
"@ruan-cat/vuepress-preset-config": "^0.1.19"
|
|
68
79
|
},
|
|
69
80
|
"peerDependencies": {
|
|
70
81
|
"typescript": "5.7.3",
|
|
71
|
-
"unplugin-vue-router": "^0.10.
|
|
82
|
+
"unplugin-vue-router": "^0.10.0",
|
|
83
|
+
"vite-plugin-autogeneration-import-file": ">=3"
|
|
72
84
|
},
|
|
73
85
|
"peerDependenciesMeta": {
|
|
74
86
|
"typescript": {
|
|
@@ -77,6 +89,9 @@
|
|
|
77
89
|
"unplugin-vue-router": {
|
|
78
90
|
"optional": true
|
|
79
91
|
},
|
|
92
|
+
"vite-plugin-autogeneration-import-file": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
80
95
|
"@vueuse/integrations": {
|
|
81
96
|
"optional": true
|
|
82
97
|
},
|
|
@@ -85,10 +100,12 @@
|
|
|
85
100
|
}
|
|
86
101
|
},
|
|
87
102
|
"scripts": {
|
|
88
|
-
"
|
|
103
|
+
"clean-docs": "rimraf -g \"docs/**/!(.vuepress)\"",
|
|
104
|
+
"copy-src": "cpx \"src/**/*\" docs",
|
|
105
|
+
"copy-readme": "cpx README.md docs",
|
|
89
106
|
"copy-changelog": "cpx CHANGELOG.md docs",
|
|
90
107
|
"typedoc": "typedoc --options typedoc.config.mjs",
|
|
91
|
-
"
|
|
108
|
+
"changelog-yaml": "node --import=tsx ./scripts/yaml-in-md.ts --md=./docs/CHANGELOG.md",
|
|
92
109
|
"docs:dev-main": "vuepress-vite dev docs --clean-cache --open",
|
|
93
110
|
"docs:dev": "turbo docs:dev",
|
|
94
111
|
"build:docs-main": "vuepress-vite build docs",
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
export * from "./rmmv-class-expand-tools.ts";
|
|
2
2
|
export * from "./conditions.ts";
|
|
3
|
-
export * from "./unplugin-vue-router/index.ts";
|
|
4
3
|
export * from "./define-promise-tasks.ts";
|
|
5
4
|
export * from "./simple-promise-tools.ts";
|
|
6
|
-
export * from "./vueuse/useAxios.ts";
|
|
7
|
-
export * from "./ruan-cat-pkg-info.ts";
|
|
8
5
|
|
|
9
6
|
export * from "./types/pnpm-workspace.yaml.shim.ts";
|
|
10
7
|
export * from "./types/Prettify.ts";
|
|
11
|
-
|
|
12
|
-
// 单独打包成cjs 并单独导出
|
|
13
|
-
// export * from "./node.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./tools.ts";
|
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { normalize } from "node:path";
|
|
6
|
-
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { spawnSync, type SpawnOptions } from "node:child_process";
|
|
7
7
|
|
|
8
8
|
import { generateSimpleAsyncTask } from "@ruan-cat/utils";
|
|
9
|
-
// import gradient from "gradient-string";
|
|
10
9
|
import consola from "consola";
|
|
11
10
|
// import { normalizePath } from "vite";
|
|
12
11
|
|
|
@@ -38,6 +37,7 @@ export interface SpawnSyncSimpleParams {
|
|
|
38
37
|
* @default true
|
|
39
38
|
*/
|
|
40
39
|
isShowCommand?: boolean;
|
|
40
|
+
spawnOptions?: SpawnOptions;
|
|
41
41
|
|
|
42
42
|
/** 打印当前运行的命令 */
|
|
43
43
|
printCurrentCommand?: (params: Pick<SpawnSyncSimpleParams, "command" | "parameters">) => void;
|
|
@@ -65,6 +65,7 @@ export function generateSpawnSync(spawnSyncSimpleParams: SpawnSyncSimpleParams)
|
|
|
65
65
|
parameters,
|
|
66
66
|
isFlow = true,
|
|
67
67
|
isShowCommand = true,
|
|
68
|
+
spawnOptions = {},
|
|
68
69
|
printCurrentCommand = defPrintCurrentCommand,
|
|
69
70
|
} = spawnSyncSimpleParams;
|
|
70
71
|
|
|
@@ -82,6 +83,7 @@ export function generateSpawnSync(spawnSyncSimpleParams: SpawnSyncSimpleParams)
|
|
|
82
83
|
*/
|
|
83
84
|
stdio: isFlow ? "inherit" : "pipe",
|
|
84
85
|
shell: true,
|
|
86
|
+
...spawnOptions,
|
|
85
87
|
});
|
|
86
88
|
|
|
87
89
|
// 如果不是流式输出 就直接返回返回值即可
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { generateSpawnSync } from "../../node-cjs/index.ts";
|
|
2
|
+
|
|
3
|
+
type GlobString = `**/{${string}}`;
|
|
4
|
+
|
|
5
|
+
export const defaultCleanTargets = <const>[
|
|
6
|
+
// node常见文件
|
|
7
|
+
"node_modules",
|
|
8
|
+
"yarn.lock",
|
|
9
|
+
"pnpm-lock.yaml",
|
|
10
|
+
"package-lock.json",
|
|
11
|
+
|
|
12
|
+
// 项目常见文件
|
|
13
|
+
"dist",
|
|
14
|
+
|
|
15
|
+
//
|
|
16
|
+
".turbo",
|
|
17
|
+
".vercel",
|
|
18
|
+
|
|
19
|
+
// vuepress
|
|
20
|
+
".cache",
|
|
21
|
+
".temp",
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 删除node项目的依赖项便于重新安装依赖,也包括常见的各种垃圾文件。
|
|
26
|
+
* @description
|
|
27
|
+
*/
|
|
28
|
+
export async function clean(
|
|
29
|
+
/**
|
|
30
|
+
* 被清除的目标文件夹 也包括文件
|
|
31
|
+
*/
|
|
32
|
+
targets?: string[],
|
|
33
|
+
) {
|
|
34
|
+
const cleanTargets = targets ?? defaultCleanTargets;
|
|
35
|
+
const glob: GlobString = `**/{${cleanTargets.join()}}`;
|
|
36
|
+
|
|
37
|
+
console.log(" 当前运行地址 process.cwd() ", process.cwd());
|
|
38
|
+
|
|
39
|
+
const doClean = generateSpawnSync({
|
|
40
|
+
command: "rimraf",
|
|
41
|
+
parameters: ["-g", glob],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
await doClean();
|
|
45
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
dir:
|
|
3
|
+
collapsible: false
|
|
4
|
+
link: true
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# unplugin-vue-router
|
|
8
|
+
|
|
9
|
+
## 安装依赖
|
|
10
|
+
|
|
11
|
+
你需要独立安装 unplugin-vue-router 。
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm i -D unplugin-vue-router
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 使用
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { getRouteName } from "@ruan-cat/utils/unplugin-vue-router";
|
|
21
|
+
```
|
|
@@ -10,6 +10,17 @@ type GetRouteName = NonNullable<Options["getRouteName"]>;
|
|
|
10
10
|
* 故自定义。
|
|
11
11
|
*
|
|
12
12
|
* unplugin-vue-router 插件的 getRouteName 配置项
|
|
13
|
+
*
|
|
14
|
+
* FIXME: https://github.com/vitejs/vite/issues/5370
|
|
15
|
+
*
|
|
16
|
+
* 该函数设计出来是为了解决这个问题
|
|
17
|
+
*
|
|
18
|
+
* 在vite符号链接未解决时,应该直接使用js文件,如下:
|
|
19
|
+
* (作废)
|
|
20
|
+
* import { getRouteName } from "@ruan-cat/utils/dist/index.js";
|
|
21
|
+
*
|
|
22
|
+
* 若已经彻底解决,请直接试图用来自符号链接的ts文件,如下:
|
|
23
|
+
* import { getRouteName } from "@ruan-cat/utils/unplugin-vue-router";
|
|
13
24
|
*/
|
|
14
25
|
export const getRouteName: GetRouteName = function _getRouteName(node): ReturnType<GetRouteName> {
|
|
15
26
|
// 如果是根节点 那么没有对应的文件夹名称 返回空字符串
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
dir:
|
|
3
|
+
collapsible: false
|
|
4
|
+
link: true
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# 自动类型生成插件的二次封装
|
|
8
|
+
|
|
9
|
+
[vite-plugin-autogeneration-import-file](https://github.com/yuntian001/vite-plugin-autogeneration-import-file),这个 vite 插件可以实现自动类型导入。在实际使用过程中,需要配置很多东西,故此处对该插件的使用做一些二次封装,并讲解该如何使用。
|
|
10
|
+
|
|
11
|
+
## 安装依赖
|
|
12
|
+
|
|
13
|
+
毕竟属于对该插件的二次封装,请自行安装依赖:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm i -D vite-plugin-autogeneration-import-file
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 在 vite 配置内使用
|
|
20
|
+
|
|
21
|
+
使用示例如下:
|
|
22
|
+
|
|
23
|
+
::: details
|
|
24
|
+
|
|
25
|
+
@[code](./tests/vite.config.ts)
|
|
26
|
+
|
|
27
|
+
:::
|
|
28
|
+
|
|
29
|
+
## 使用的模板
|
|
30
|
+
|
|
31
|
+
类型文件使用的模板如下:
|
|
32
|
+
|
|
33
|
+
::: details
|
|
34
|
+
|
|
35
|
+
@[code](./template/components.template.ts)
|
|
36
|
+
|
|
37
|
+
:::
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { dirname, join, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath, URL } from "node:url";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = dirname(__filename);
|
|
7
|
+
|
|
8
|
+
import { upperFirst, isUndefined } from "lodash-es";
|
|
9
|
+
import { createPlugin, getName } from "vite-plugin-autogeneration-import-file";
|
|
10
|
+
import consola from "consola";
|
|
11
|
+
|
|
12
|
+
const { autoImport } = createPlugin();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 解析目录
|
|
16
|
+
* @description
|
|
17
|
+
* 用于解析目录路径 默认从项目根目录开始
|
|
18
|
+
*
|
|
19
|
+
* 这里的项目根目录默认为 process.cwd()
|
|
20
|
+
*
|
|
21
|
+
* 作为工具函数对外导出 便于用户自己整理解析的文件路径
|
|
22
|
+
*/
|
|
23
|
+
export function pathResolve(dir: string) {
|
|
24
|
+
const resPath = resolve(process.cwd(), dir);
|
|
25
|
+
console.info(` 解析的文件路径: ${resPath}`);
|
|
26
|
+
return resPath;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type DirOptions = Parameters<typeof autoImport>["0"];
|
|
30
|
+
type DirOption = DirOptions[number];
|
|
31
|
+
type _DirOptionName = DirOption["name"];
|
|
32
|
+
|
|
33
|
+
type _DirOptionNameNotString = Exclude<_DirOptionName, string>;
|
|
34
|
+
type DirOptionName = NonNullable<_DirOptionNameNotString>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 创建名称生成函数
|
|
38
|
+
* @description
|
|
39
|
+
* 用于诸如特定的名称前缀 便于实现模块注册
|
|
40
|
+
*/
|
|
41
|
+
export function createDirOptionNameFunction(
|
|
42
|
+
/**
|
|
43
|
+
* 组件名称前缀
|
|
44
|
+
* @description
|
|
45
|
+
* 一般写成大写字母
|
|
46
|
+
* @example
|
|
47
|
+
* Components
|
|
48
|
+
* Page
|
|
49
|
+
*/
|
|
50
|
+
prefix: string = "",
|
|
51
|
+
) {
|
|
52
|
+
/**
|
|
53
|
+
* 组件名命名规则支持字符串模板和函数
|
|
54
|
+
* @description
|
|
55
|
+
* 设置首字母为大写
|
|
56
|
+
*/
|
|
57
|
+
const dirOptionName: DirOptionName = function name(fileName) {
|
|
58
|
+
const resFileName = getName(fileName);
|
|
59
|
+
const resFileNameWithPrefix = <const>`${upperFirst(prefix)}${upperFirst(resFileName)}`;
|
|
60
|
+
return resFileNameWithPrefix;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return dirOptionName;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 默认的自动导入模板文件名
|
|
68
|
+
* @description
|
|
69
|
+
* 你可以在项目内准备一个模板文件 这个模板文件建议取名为 components.template.d.ts
|
|
70
|
+
* 读取模板时 会默认读取该名称的文件
|
|
71
|
+
* @example components.template.d.ts
|
|
72
|
+
*/
|
|
73
|
+
export const defaultAutoImportTemplateFilename = <const>"components.template.ts";
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 默认的自动导入模板文件路径
|
|
77
|
+
* @description
|
|
78
|
+
* 我们默认你的模板文件在项目根目录下的 template 文件夹内
|
|
79
|
+
*/
|
|
80
|
+
export const defaultAutoImportTemplatePath = <const>`./template/${defaultAutoImportTemplateFilename}`;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 直接获得默认的自动导入模板
|
|
84
|
+
* @description
|
|
85
|
+
* 直接读取本项目内的 components.template.d.ts 文件
|
|
86
|
+
* 反正都读取默认模板了 直接获取字符串即可
|
|
87
|
+
*/
|
|
88
|
+
function getDefaultAutoImportTemplate() {
|
|
89
|
+
/** 相对路径文件 就在旁边的文件 */
|
|
90
|
+
const templatePath = join(__dirname, defaultAutoImportTemplatePath);
|
|
91
|
+
return fs.readFileSync(templatePath, "utf-8");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 默认的自动导入模板 */
|
|
95
|
+
export const defaultAutoImportTemplate = getDefaultAutoImportTemplate();
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 创建文件生成模板字符串
|
|
99
|
+
* @description
|
|
100
|
+
* 会生成一个字符串 用于作为生成类型声明文件的模板
|
|
101
|
+
*/
|
|
102
|
+
export function createAutoImportTemplate(
|
|
103
|
+
/**
|
|
104
|
+
* 模板路径
|
|
105
|
+
* @description
|
|
106
|
+
* 你可以传入一个模板路径 默认会读取项目根目录下的 template 文件夹内的 components.template.d.ts 文件
|
|
107
|
+
* @default ./template/components.template.d.ts
|
|
108
|
+
*/
|
|
109
|
+
path?: string,
|
|
110
|
+
) {
|
|
111
|
+
// 如果用户没传递路径 就直接返回默认的模板
|
|
112
|
+
if (isUndefined(path)) {
|
|
113
|
+
return defaultAutoImportTemplate;
|
|
114
|
+
} else {
|
|
115
|
+
const filepath = pathResolve(path);
|
|
116
|
+
consola.log(` 当前读取的文件路径为: ${filepath}`);
|
|
117
|
+
// 否则读取用户传递的路径
|
|
118
|
+
return fs.readFileSync(pathResolve(path), "utf-8");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 这是特定模板
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/* prettier-ignore */
|
|
9
|
+
declare module "vue" {
|
|
10
|
+
export interface GlobalComponents {
|
|
11
|
+
//code
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/* prettier-ignore */
|
|
16
|
+
declare global {
|
|
17
|
+
//typeCode
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* prettier-ignore */
|
|
21
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useAxios/index.ts";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
---
|
|
2
|
+
order: 10
|
|
3
|
+
dir:
|
|
4
|
+
collapsible: false
|
|
5
|
+
link: true
|
|
6
|
+
order: 10
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# useAxios 的包装函数
|
|
10
|
+
|
|
11
|
+
useAxiosWrapper,该函数旨在于实现 useAxios 的封装。
|
|
12
|
+
|
|
13
|
+
## 安装依赖
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm i -P @vueuse/integrations axios
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 准备接口返回数据的类型
|
|
20
|
+
|
|
21
|
+
接口请求返回类型,通常是一个泛型,请根据你的业务场景准备好类似于下面的类型:
|
|
22
|
+
|
|
23
|
+
::: details
|
|
24
|
+
|
|
25
|
+
@[code](./tests/types/ApifoxModel.ts)
|
|
26
|
+
|
|
27
|
+
:::
|
|
28
|
+
|
|
29
|
+
## 准备简单的 axios 请求实例
|
|
30
|
+
|
|
31
|
+
这里的本质是为了准备一个拦截器内没有解包 data 的 axios 实例,因为 useAxios 默认帮我们完成 axios 的 data 解包了。
|
|
32
|
+
|
|
33
|
+
比如以下的简单实例:
|
|
34
|
+
|
|
35
|
+
::: details
|
|
36
|
+
|
|
37
|
+
@[code](./tests/createAxiosInstance.ts)
|
|
38
|
+
|
|
39
|
+
:::
|
|
40
|
+
|
|
41
|
+
## 定义接口
|
|
42
|
+
|
|
43
|
+
如下:
|
|
44
|
+
|
|
45
|
+
::: details
|
|
46
|
+
|
|
47
|
+
@[code](./tests/homeCategoryHead.ts)
|
|
48
|
+
|
|
49
|
+
:::
|
|
50
|
+
|
|
51
|
+
## 使用接口
|
|
52
|
+
|
|
53
|
+
如下:
|
|
54
|
+
|
|
55
|
+
::: details
|
|
56
|
+
|
|
57
|
+
@[code](./tests/homeCategoryHead.test.ts)
|
|
58
|
+
|
|
59
|
+
:::
|
package/tsconfig.json
CHANGED