@karry.sun/yapi-gen 0.2.9 → 0.3.0

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.
@@ -13,25 +13,16 @@ function forYttCwd(p) {
13
13
  }
14
14
  const fs_1 = require("fs");
15
15
  const path_1 = require("path");
16
- function getPackageName(cwd) {
16
+ // 从本包 package.json 读取 name,不依赖项目 node_modules 解析
17
+ function getPackageName() {
17
18
  try {
18
- const { createRequire } = require('module');
19
- const req = createRequire((0, path_1.join)(cwd, 'package.json'));
20
- for (const id of ['@karry.sun/yapi-gen/package.json', 'yapi-gen/package.json']) {
21
- try {
22
- const pkgPath = req.resolve(id);
23
- const pkg = JSON.parse((0, fs_1.readFileSync)(pkgPath, 'utf-8'));
24
- return (pkg && pkg.name) || 'yapi-gen';
25
- }
26
- catch {
27
- continue;
28
- }
29
- }
19
+ const pkgPath = (0, path_1.join)(__dirname, '..', '..', 'package.json');
20
+ const pkg = JSON.parse((0, fs_1.readFileSync)(pkgPath, 'utf-8'));
21
+ return (pkg && pkg.name) || 'yapi-gen';
30
22
  }
31
23
  catch {
32
- /* fallthrough */
24
+ return 'yapi-gen';
33
25
  }
34
- return 'yapi-gen';
35
26
  }
36
27
  const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
37
28
  const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
@@ -90,7 +81,7 @@ function resolveConfig(config, cwd) {
90
81
  const resolved = { ...config };
91
82
  const adapter = normalizeRequestAdapter(resolved.requestAdapter);
92
83
  const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
93
- const packageName = getPackageName(cwd);
84
+ const packageName = getPackageName();
94
85
  if (adapter.type === 'axios') {
95
86
  const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
96
87
  const absolutePath = (0, path_1.resolve)(cwd, requestFilePath);
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-config.cjs.d.ts","sourceRoot":"","sources":["../src/resolve-config.cjs.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,oBAAoB,CAAC;AAiFnF,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAoCvF"}
1
+ {"version":3,"file":"resolve-config.cjs.d.ts","sourceRoot":"","sources":["../src/resolve-config.cjs.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,oBAAoB,CAAC;AAyEnF,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAoCvF"}
@@ -10,25 +10,16 @@ function forYttCwd(p) {
10
10
  }
11
11
  import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
12
12
  import { resolve, dirname, join } from 'path';
13
- function getPackageName(cwd) {
13
+ // 从本包 package.json 读取 name,不依赖项目 node_modules 解析
14
+ function getPackageName() {
14
15
  try {
15
- const { createRequire } = require('module');
16
- const req = createRequire(join(cwd, 'package.json'));
17
- for (const id of ['@karry.sun/yapi-gen/package.json', 'yapi-gen/package.json']) {
18
- try {
19
- const pkgPath = req.resolve(id);
20
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
21
- return (pkg && pkg.name) || 'yapi-gen';
22
- }
23
- catch {
24
- continue;
25
- }
26
- }
16
+ const pkgPath = join(__dirname, '..', '..', 'package.json');
17
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
18
+ return (pkg && pkg.name) || 'yapi-gen';
27
19
  }
28
20
  catch {
29
- /* fallthrough */
21
+ return 'yapi-gen';
30
22
  }
31
- return 'yapi-gen';
32
23
  }
33
24
  const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
34
25
  const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
@@ -87,7 +78,7 @@ export function resolveConfig(config, cwd) {
87
78
  const resolved = { ...config };
88
79
  const adapter = normalizeRequestAdapter(resolved.requestAdapter);
89
80
  const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
90
- const packageName = getPackageName(cwd);
81
+ const packageName = getPackageName();
91
82
  if (adapter.type === 'axios') {
92
83
  const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
93
84
  const absolutePath = resolve(cwd, requestFilePath);
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-config.d.ts","sourceRoot":"","sources":["../src/resolve-config.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,oBAAoB,CAAC;AA4GnF;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CA2CvF"}
1
+ {"version":3,"file":"resolve-config.d.ts","sourceRoot":"","sources":["../src/resolve-config.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,oBAAoB,CAAC;AAuGnF;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CA2CvF"}
@@ -3,25 +3,18 @@
3
3
  */
4
4
  import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
5
5
  import { resolve, dirname, join } from 'path';
6
- import { createRequire } from 'module';
7
- function getPackageName(cwd) {
6
+ import { fileURLToPath } from 'url';
7
+ const __dirname = dirname(fileURLToPath(import.meta.url));
8
+ // 从本包 package.json 读取 name,不依赖项目 node_modules 解析
9
+ function getPackageName() {
8
10
  try {
9
- const req = createRequire(join(cwd, 'package.json'));
10
- for (const id of ['@karry.sun/yapi-gen/package.json', 'yapi-gen/package.json']) {
11
- try {
12
- const pkgPath = req.resolve(id);
13
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
14
- return (pkg && pkg.name) || 'yapi-gen';
15
- }
16
- catch {
17
- continue;
18
- }
19
- }
11
+ const pkgPath = join(__dirname, '..', 'package.json');
12
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
13
+ return (pkg && pkg.name) || 'yapi-gen';
20
14
  }
21
15
  catch {
22
- /* fallthrough */
16
+ return 'yapi-gen';
23
17
  }
24
- return 'yapi-gen';
25
18
  }
26
19
  const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
27
20
  const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
@@ -108,7 +101,7 @@ export function resolveConfig(config, cwd) {
108
101
  const resolved = { ...config };
109
102
  const adapter = normalizeRequestAdapter(resolved.requestAdapter);
110
103
  const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
111
- const packageName = getPackageName(cwd);
104
+ const packageName = getPackageName();
112
105
  if (adapter.type === 'axios') {
113
106
  const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
114
107
  const absolutePath = resolve(cwd, requestFilePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karry.sun/yapi-gen",
3
- "version": "0.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "基于 yapi-to-typescript 的 YApi 前端代码生成 CLI,支持可配置的请求适配器与多项目复用",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",