feima-shortcuts 0.0.2 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/scripts/new.js +13 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feima-shortcuts",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -4,6 +4,7 @@ const { makeDir } = require("../utils/makeDir");
4
4
 
5
5
  const fileName = "index.js";
6
6
 
7
+ // 生成 API 函数模板
7
8
  const postTemplate = (url, functionName) => {
8
9
  return `export const ${functionName} = (data) => {
9
10
  return request('${url}', {
@@ -13,6 +14,7 @@ const postTemplate = (url, functionName) => {
13
14
  };`;
14
15
  };
15
16
 
17
+ // 读取文件内容,如果函数已存在则返回 null,否则返回文件内容
16
18
  const fileData = (functionName, filePath) => {
17
19
  if (fs.existsSync(filePath)) {
18
20
  const data = fs.readFileSync(filePath, "utf8");
@@ -25,17 +27,20 @@ const fileData = (functionName, filePath) => {
25
27
  return `import request from '@/utils/request'`;
26
28
  };
27
29
 
30
+ // 根据 URL 获取函数名(取最后一段)
28
31
  const getFunctionName = (url) => {
29
32
  const parts = url.split('/').filter(Boolean);
30
33
  return parts[parts.length - 1];
31
34
  };
32
35
 
36
+ // 从 API 路径获取目录结构(去掉最后一段)
33
37
  const getDirFromApi = (api) => {
34
38
  const parts = api.split('/').filter(Boolean);
35
- parts.pop(); // 移除最后一部分(用于函数名)
39
+ parts.pop(); // 移除最后一部分(函数名)
36
40
  return parts.join('/');
37
41
  };
38
42
 
43
+ // 生成 API 方法并写入文件
39
44
  const apiAllTemplate = (apiPath, functionName, pagePath) => {
40
45
  const fullFilePath = path.join(pagePath, fileName);
41
46
  const fileTemplate = fileData(functionName, fullFilePath);
@@ -48,15 +53,20 @@ const apiAllTemplate = (apiPath, functionName, pagePath) => {
48
53
  console.log(`✅ 成功生成 API 方法: ${functionName} -> ${apiPath}`);
49
54
  };
50
55
 
56
+ // 脚本入口函数
51
57
  exports.run = function ({ api }) {
52
58
  if (!api || typeof api !== "string") {
53
59
  console.error("❌ 参数错误,需传入 { api: string }");
54
60
  return;
55
- };
61
+ }
56
62
 
57
63
  const functionName = getFunctionName(api);
58
64
  const dir = getDirFromApi(api);
59
- const pagePath = path.resolve(__dirname, `../src/api/${dir}`);
65
+
66
+ // 基于当前工作目录计算 API 路径
67
+ const pagePath = path.resolve(process.cwd(), `src/api/${dir}`);
68
+
69
+ // 创建目录并生成 API 方法
60
70
  makeDir(pagePath);
61
71
  apiAllTemplate(api, functionName, pagePath);
62
72
  };