@karry.sun/yapi-gen 0.2.8 → 0.2.9

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