feima-shortcuts 0.0.1 → 0.0.3
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 +1 -1
- package/bin/feima.js +20 -19
- package/package.json +1 -1
- package/src/scripts/new.js +72 -0
- package/src/scripts/old.js +58 -0
- package/src/scripts/restful-api.js +7 -55
package/README.md
CHANGED
package/bin/feima.js
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const inquirer = require("inquirer");
|
|
3
3
|
const feima = require("../src/generate");
|
|
4
|
-
// const utils = require('../src/utils/checkUpdate');
|
|
5
|
-
// // 当前系统是Windows还是mac;
|
|
6
|
-
// const isWin = /^win/.test(process.platform);
|
|
7
|
-
// let packagePath = '/usr/local/lib/node_modules';// node_modules全局路径
|
|
8
4
|
|
|
9
5
|
const run = () => {
|
|
10
6
|
inquirer
|
|
11
7
|
.prompt([
|
|
8
|
+
{
|
|
9
|
+
type: "list",
|
|
10
|
+
name: "template",
|
|
11
|
+
message: "请选择接口类型",
|
|
12
|
+
choices: ["new",'old'],
|
|
13
|
+
},
|
|
12
14
|
{
|
|
13
15
|
type: "input",
|
|
14
16
|
message: "输入接口路径:",
|
|
15
17
|
name: "path",
|
|
16
|
-
default: "admin.product.
|
|
18
|
+
default: "admin.product.xxx", // 默认值
|
|
19
|
+
when: function (answers) {
|
|
20
|
+
return ["old"].includes(answers.template);
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
{
|
|
25
|
+
type: "input",
|
|
26
|
+
message: "输入接口路径:",
|
|
27
|
+
name: "api",
|
|
28
|
+
default: "/tenant/xxx", // 默认值
|
|
29
|
+
when: function (answers) {
|
|
30
|
+
return ["new"].includes(answers.template);
|
|
31
|
+
},
|
|
17
32
|
},
|
|
18
|
-
// {
|
|
19
|
-
// type: "list",
|
|
20
|
-
// name: "type",
|
|
21
|
-
// message: "api",
|
|
22
|
-
// choices: [
|
|
23
|
-
// "post",
|
|
24
|
-
// "get",
|
|
25
|
-
// "delete",
|
|
26
|
-
// "put",
|
|
27
|
-
// ],
|
|
28
|
-
// when: function (answers) {
|
|
29
|
-
// return answers["path"];
|
|
30
|
-
// },
|
|
31
|
-
// },
|
|
32
33
|
])
|
|
33
34
|
.then((answers) => {
|
|
34
35
|
feima.run(answers);
|
package/package.json
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { makeDir } = require("../utils/makeDir");
|
|
4
|
+
|
|
5
|
+
const fileName = "index.js";
|
|
6
|
+
|
|
7
|
+
// 生成 API 函数模板
|
|
8
|
+
const postTemplate = (url, functionName) => {
|
|
9
|
+
return `export const ${functionName} = (data) => {
|
|
10
|
+
return request('${url}', {
|
|
11
|
+
method: "post",
|
|
12
|
+
data,
|
|
13
|
+
});
|
|
14
|
+
};`;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// 读取文件内容,如果函数已存在则返回 null,否则返回文件内容
|
|
18
|
+
const fileData = (functionName, filePath) => {
|
|
19
|
+
if (fs.existsSync(filePath)) {
|
|
20
|
+
const data = fs.readFileSync(filePath, "utf8");
|
|
21
|
+
if (data.includes(`export const ${functionName}`)) {
|
|
22
|
+
console.log(`⚠️ 函数名 ${functionName} 已存在,跳过生成`);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
return `import request from '@/utils/request'`;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// 根据 URL 获取函数名(取最后一段)
|
|
31
|
+
const getFunctionName = (url) => {
|
|
32
|
+
const parts = url.split('/').filter(Boolean);
|
|
33
|
+
return parts[parts.length - 1];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 从 API 路径获取目录结构(去掉最后一段)
|
|
37
|
+
const getDirFromApi = (api) => {
|
|
38
|
+
const parts = api.split('/').filter(Boolean);
|
|
39
|
+
parts.pop(); // 移除最后一部分(函数名)
|
|
40
|
+
return parts.join('/');
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// 生成 API 方法并写入文件
|
|
44
|
+
const apiAllTemplate = (apiPath, functionName, pagePath) => {
|
|
45
|
+
const fullFilePath = path.join(pagePath, fileName);
|
|
46
|
+
const fileTemplate = fileData(functionName, fullFilePath);
|
|
47
|
+
|
|
48
|
+
if (!fileTemplate) return;
|
|
49
|
+
|
|
50
|
+
const newContent = `${fileTemplate}\n\n${postTemplate(apiPath, functionName)}\n`;
|
|
51
|
+
|
|
52
|
+
fs.writeFileSync(fullFilePath, newContent, "utf8");
|
|
53
|
+
console.log(`✅ 成功生成 API 方法: ${functionName} -> ${apiPath}`);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// 脚本入口函数
|
|
57
|
+
exports.run = function ({ api }) {
|
|
58
|
+
if (!api || typeof api !== "string") {
|
|
59
|
+
console.error("❌ 参数错误,需传入 { api: string }");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const functionName = getFunctionName(api);
|
|
64
|
+
const dir = getDirFromApi(api);
|
|
65
|
+
|
|
66
|
+
// 基于当前工作目录计算 API 路径
|
|
67
|
+
const pagePath = path.resolve(process.cwd(), `src/api/${dir}`);
|
|
68
|
+
|
|
69
|
+
// 创建目录并生成 API 方法
|
|
70
|
+
makeDir(pagePath);
|
|
71
|
+
apiAllTemplate(api, functionName, pagePath);
|
|
72
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const { makeDir } = require("../utils/makeDir");
|
|
3
|
+
|
|
4
|
+
const fileName = "index.js";
|
|
5
|
+
|
|
6
|
+
const postTemplat = (apiPath, functionName) => {
|
|
7
|
+
return `export const ${functionName} = (params) => {
|
|
8
|
+
return request({
|
|
9
|
+
method: "post",
|
|
10
|
+
params: {
|
|
11
|
+
...params,
|
|
12
|
+
api: "${apiPath}",
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
`
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function splitString(input) {
|
|
20
|
+
const parts = input.split('.');
|
|
21
|
+
const path = parts.slice(0, -1).join('/'); // 取前面的部分并用 '/' 连接
|
|
22
|
+
const functionName = parts[parts.length - 1]; // 取最后一部分
|
|
23
|
+
return { path, functionName };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const fileData = (answers, functionName) => {
|
|
28
|
+
if (fs.existsSync(`./${fileName}`)) {
|
|
29
|
+
const data = fs.readFileSync(`./${fileName}`, "utf8");
|
|
30
|
+
if (data.includes(functionName)) {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
return `import request from '@/api/https'`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const apiAllTemplate = (answers,functionName) => {
|
|
39
|
+
const fileTemplate = fileData(answers, functionName);
|
|
40
|
+
|
|
41
|
+
if (!fileTemplate) return console.log(`无需重复添加 ${type}`);
|
|
42
|
+
|
|
43
|
+
const template = `${fileTemplate}
|
|
44
|
+
|
|
45
|
+
${postTemplat(answers.path, functionName)}`;
|
|
46
|
+
|
|
47
|
+
fs.writeFileSync(`./${fileName}`, template);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
exports.run = function (answers) {
|
|
51
|
+
const { path, functionName } = splitString(answers.path);
|
|
52
|
+
const pagePath = `./src/api/${path}`;
|
|
53
|
+
|
|
54
|
+
makeDir(pagePath);
|
|
55
|
+
process.chdir(pagePath);
|
|
56
|
+
|
|
57
|
+
apiAllTemplate(answers, functionName);
|
|
58
|
+
};
|
|
@@ -1,58 +1,10 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
const fileName = "index.js";
|
|
5
|
-
|
|
6
|
-
const postTemplat = (apiPath, functionName) => {
|
|
7
|
-
return `export const ${functionName} = (params) => {
|
|
8
|
-
return request({
|
|
9
|
-
method: "post",
|
|
10
|
-
params: {
|
|
11
|
-
...params,
|
|
12
|
-
api: "${apiPath}",
|
|
13
|
-
},
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
`
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function splitString(input) {
|
|
20
|
-
const parts = input.split('.');
|
|
21
|
-
const path = parts.slice(0, -1).join('/'); // 取前面的部分并用 '/' 连接
|
|
22
|
-
const functionName = parts[parts.length - 1]; // 取最后一部分
|
|
23
|
-
return { path, functionName };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const fileData = (answers, functionName) => {
|
|
28
|
-
if (fs.existsSync(`./${fileName}`)) {
|
|
29
|
-
const data = fs.readFileSync(`./${fileName}`, "utf8");
|
|
30
|
-
if (data.includes(functionName)) {
|
|
31
|
-
return "";
|
|
32
|
-
}
|
|
33
|
-
return data;
|
|
34
|
-
}
|
|
35
|
-
return `import request from '@/api/https'`
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const apiAllTemplate = (answers,functionName) => {
|
|
39
|
-
const fileTemplate = fileData(answers, functionName);
|
|
40
|
-
|
|
41
|
-
if (!fileTemplate) return console.log(`无需重复添加 ${type}`);
|
|
42
|
-
|
|
43
|
-
const template = `${fileTemplate}
|
|
44
|
-
|
|
45
|
-
${postTemplat(answers.path, functionName)}`;
|
|
46
|
-
|
|
47
|
-
fs.writeFileSync(`./${fileName}`, template);
|
|
48
|
-
};
|
|
1
|
+
const oldApi = require("./old");
|
|
2
|
+
const newApi = require("./new");
|
|
49
3
|
|
|
50
4
|
exports.run = function (answers) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
apiAllTemplate(answers, functionName);
|
|
5
|
+
if (answers.template === "old") {
|
|
6
|
+
oldApi.run(answers);
|
|
7
|
+
} else {
|
|
8
|
+
newApi.run(answers);
|
|
9
|
+
}
|
|
58
10
|
};
|