feima-shortcuts 0.3.4 → 0.3.5
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/package.json
CHANGED
|
@@ -2,11 +2,11 @@ const fs = require("fs");
|
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const { makeDir } = require("../../utils/makeDir");
|
|
4
4
|
|
|
5
|
-
const fileName = "index.
|
|
5
|
+
const fileName = "index.ts";
|
|
6
6
|
|
|
7
7
|
// 生成 API 函数模板
|
|
8
8
|
const postTemplate = (url, functionName) => {
|
|
9
|
-
return `export const ${functionName} = (options) => {
|
|
9
|
+
return `export const ${functionName} = (options: any) => {
|
|
10
10
|
return request('${url}', {
|
|
11
11
|
method: "post",
|
|
12
12
|
...(options || {}),
|
|
@@ -15,11 +15,12 @@ const postTemplate = (url, functionName) => {
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
// 读取文件内容,如果函数已存在则返回 null,否则返回文件内容
|
|
18
|
-
const fileData = (functionName, filePath) => {
|
|
18
|
+
const fileData = (functionName, filePath, apiPath) => {
|
|
19
19
|
if (fs.existsSync(filePath)) {
|
|
20
20
|
const data = fs.readFileSync(filePath, "utf8");
|
|
21
21
|
if (data.includes(`export const ${functionName} `)) {
|
|
22
22
|
console.log(`⚠️ 函数名 ${functionName} 已存在,跳过生成`);
|
|
23
|
+
console.log(`import { ${functionName} } from '@/service/${getDirFromApi(apiPath)}'`)
|
|
23
24
|
return null;
|
|
24
25
|
}
|
|
25
26
|
return data;
|
|
@@ -43,7 +44,7 @@ const getDirFromApi = (api) => {
|
|
|
43
44
|
// 生成 API 方法并写入文件
|
|
44
45
|
const apiAllTemplate = (apiPath, functionName, pagePath) => {
|
|
45
46
|
const fullFilePath = path.join(pagePath, fileName);
|
|
46
|
-
const fileTemplate = fileData(functionName, fullFilePath);
|
|
47
|
+
const fileTemplate = fileData(functionName, fullFilePath, apiPath);
|
|
47
48
|
|
|
48
49
|
if (!fileTemplate) return;
|
|
49
50
|
|
|
@@ -51,6 +52,7 @@ const apiAllTemplate = (apiPath, functionName, pagePath) => {
|
|
|
51
52
|
|
|
52
53
|
fs.writeFileSync(fullFilePath, newContent, "utf8");
|
|
53
54
|
console.log(`✅ 成功生成 API 方法: ${functionName} -> ${apiPath}`);
|
|
55
|
+
console.log(`import { ${functionName} } from '@/service/${getDirFromApi(apiPath)}'`)
|
|
54
56
|
};
|
|
55
57
|
|
|
56
58
|
// 脚本入口函数
|