feima-shortcuts 0.0.1 → 0.0.2
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 +62 -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,62 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { makeDir } = require("../utils/makeDir");
|
|
4
|
+
|
|
5
|
+
const fileName = "index.js";
|
|
6
|
+
|
|
7
|
+
const postTemplate = (url, functionName) => {
|
|
8
|
+
return `export const ${functionName} = (data) => {
|
|
9
|
+
return request('${url}', {
|
|
10
|
+
method: "post",
|
|
11
|
+
data,
|
|
12
|
+
});
|
|
13
|
+
};`;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const fileData = (functionName, filePath) => {
|
|
17
|
+
if (fs.existsSync(filePath)) {
|
|
18
|
+
const data = fs.readFileSync(filePath, "utf8");
|
|
19
|
+
if (data.includes(`export const ${functionName}`)) {
|
|
20
|
+
console.log(`⚠️ 函数名 ${functionName} 已存在,跳过生成`);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
return `import request from '@/utils/request'`;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const getFunctionName = (url) => {
|
|
29
|
+
const parts = url.split('/').filter(Boolean);
|
|
30
|
+
return parts[parts.length - 1];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const getDirFromApi = (api) => {
|
|
34
|
+
const parts = api.split('/').filter(Boolean);
|
|
35
|
+
parts.pop(); // 移除最后一部分(用于函数名)
|
|
36
|
+
return parts.join('/');
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const apiAllTemplate = (apiPath, functionName, pagePath) => {
|
|
40
|
+
const fullFilePath = path.join(pagePath, fileName);
|
|
41
|
+
const fileTemplate = fileData(functionName, fullFilePath);
|
|
42
|
+
|
|
43
|
+
if (!fileTemplate) return;
|
|
44
|
+
|
|
45
|
+
const newContent = `${fileTemplate}\n\n${postTemplate(apiPath, functionName)}\n`;
|
|
46
|
+
|
|
47
|
+
fs.writeFileSync(fullFilePath, newContent, "utf8");
|
|
48
|
+
console.log(`✅ 成功生成 API 方法: ${functionName} -> ${apiPath}`);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
exports.run = function ({ api }) {
|
|
52
|
+
if (!api || typeof api !== "string") {
|
|
53
|
+
console.error("❌ 参数错误,需传入 { api: string }");
|
|
54
|
+
return;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const functionName = getFunctionName(api);
|
|
58
|
+
const dir = getDirFromApi(api);
|
|
59
|
+
const pagePath = path.resolve(__dirname, `../src/api/${dir}`);
|
|
60
|
+
makeDir(pagePath);
|
|
61
|
+
apiAllTemplate(api, functionName, pagePath);
|
|
62
|
+
};
|
|
@@ -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
|
};
|