feima-shortcuts 0.3.1 → 0.3.3-beta.1

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feima-shortcuts",
3
- "version": "0.3.1",
3
+ "version": "0.3.3-beta.1",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -1,9 +1,10 @@
1
1
  const fs = require("fs");
2
2
  const { makeDir } = require("../../utils/makeDir");
3
3
 
4
- const fileName = "index.ts";
4
+ const fileNameTs = "index.ts";
5
+ const fileNameJs = "index.js";
5
6
 
6
- const postTemplat = (apiPath, functionName) => {
7
+ const postTemplatTs = (apiPath, functionName) => {
7
8
  return `export const ${functionName} = (options: any) => {
8
9
  const isFormData = options?.data instanceof FormData;
9
10
 
@@ -27,6 +28,30 @@ const postTemplat = (apiPath, functionName) => {
27
28
  `
28
29
  };
29
30
 
31
+ const postTemplatJs = (apiPath, functionName) => {
32
+ return `export const ${functionName} = (options) => {
33
+ const isFormData = options?.data instanceof FormData;
34
+
35
+ const data = isFormData
36
+ ? (() => {
37
+ const formData = options.data;
38
+ formData.append("api", "${apiPath}");
39
+ return formData;
40
+ })()
41
+ : {
42
+ ...(options?.data || {}),
43
+ api: "${apiPath}",
44
+ };
45
+
46
+ return request({
47
+ method: "post",
48
+ ...(options || {}),
49
+ data,
50
+ });
51
+ };
52
+ `
53
+ };
54
+
30
55
  function splitString(input) {
31
56
  const parts = input.split('.');
32
57
  const path = parts.slice(0, -1).join('/'); // 取前面的部分并用 '/' 连接
@@ -34,10 +59,10 @@ function splitString(input) {
34
59
  return { path, functionName };
35
60
  }
36
61
 
37
- const fileData = (answers, functionName) => {
62
+ const fileData = (fileName, functionName) => {
38
63
  if (fs.existsSync(`./${fileName}`)) {
39
64
  const data = fs.readFileSync(`./${fileName}`, "utf8");
40
- if (data.includes(`export const ${functionName} `)) {
65
+ if (data.includes(`export const ${functionName} `) || data.includes(`export const ${functionName}: any`)) {
41
66
  return "";
42
67
  }
43
68
  return data;
@@ -45,18 +70,35 @@ const fileData = (answers, functionName) => {
45
70
  return `import request from '@/utils/request'`
46
71
  }
47
72
 
48
- const apiAllTemplate = (answers,functionName) => {
49
- const fileTemplate = fileData(answers, functionName);
73
+ const apiAllTemplate = (answers, functionName, fileName, templateFn) => {
74
+ const fileTemplate = fileData(fileName, functionName);
50
75
 
51
76
  if (!fileTemplate) return console.error(`无需重复添加 ${answers.path}`);
52
77
 
53
78
  const template = `${fileTemplate}
54
79
 
55
- ${postTemplat(answers.path, functionName)}`;
80
+ ${templateFn(answers.path, functionName)}`;
56
81
 
57
82
  fs.writeFileSync(`./${fileName}`, template);
58
83
  };
59
84
 
85
+ const resolveFileAndTemplate = () => {
86
+ const tsExists = fs.existsSync(`./${fileNameTs}`);
87
+ const jsExists = fs.existsSync(`./${fileNameJs}`);
88
+
89
+ if (tsExists && jsExists) {
90
+ console.error("index.ts 和 index.js 同时存在,请手动处理后再运行脚本。");
91
+ return null;
92
+ }
93
+
94
+ if (jsExists) {
95
+ return { fileName: fileNameJs, templateFn: postTemplatJs };
96
+ }
97
+
98
+ // 只存在 ts 或两个都不存在时,统一使用 ts
99
+ return { fileName: fileNameTs, templateFn: postTemplatTs };
100
+ };
101
+
60
102
  exports.run = function (answers) {
61
103
  const { path, functionName } = splitString(answers.path);
62
104
  const pagePath = `./src/api/${path}`;
@@ -64,6 +106,11 @@ exports.run = function (answers) {
64
106
  makeDir(pagePath);
65
107
  process.chdir(pagePath);
66
108
  console.log(`import { ${functionName} } from '@/api/${path}'`)
109
+
110
+ const resolved = resolveFileAndTemplate();
111
+ if (!resolved) return;
112
+
113
+ const { fileName, templateFn } = resolved;
67
114
 
68
- apiAllTemplate(answers, functionName);
115
+ apiAllTemplate(answers, functionName, fileName, templateFn);
69
116
  };