@koishi-ce/plugin-market 1.0.0 → 1.0.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/lib/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import { createRequire } from "node:module";
2
- import { resolve } from "node:path";
2
+ import { join, resolve } from "node:path";
3
3
  import { Logger, Schema, Service, Time, defineProperty, pick, valueMap } from "@koishi-ce/koishi";
4
4
  import { compare, gt, satisfies, valid } from "semver";
5
5
  import { DataService } from "@koishi-ce/console";
6
6
  import { readFileSync } from "node:fs";
7
+ import { homedir } from "node:os";
7
8
  import Scanner from "@koishi-ce/registry";
8
9
  import spawn from "execa";
9
- import getRegistry from "get-registry";
10
10
  import pMap from "p-map";
11
11
  import messageZhCN from "./assets/message.zh-CN-B_nH77kB.yml";
12
12
  import schemaZhCN from "./assets/schema.zh-CN-CLU01Lrj.yml";
@@ -33,6 +33,31 @@ var RegistryProvider = class extends DataService {
33
33
  //#endregion
34
34
  //#region src/node/installer.ts
35
35
  const logger$1 = new Logger("market");
36
+ /** 从单个 .npmrc 文件提取 registry 配置项;文件不存在或读取出错一律视为未配置 */
37
+ function readNpmrcRegistry(file) {
38
+ try {
39
+ for (const line of readFileSync(file, "utf8").split(/\r?\n/)) {
40
+ const value = /^\s*registry\s*=\s*(\S+)\s*$/.exec(line)?.[1];
41
+ if (value) return value;
42
+ }
43
+ } catch {}
44
+ }
45
+ /**
46
+ * 读取本机 npm registry 配置(优先级对齐 npm 自身:环境变量 > 项目
47
+ * .npmrc > 用户 ~/.npmrc)。刻意不 spawn 子进程探测——原依赖
48
+ * get-registry 按 user-agent 选 `bun config get registry`,而 Bun 没有
49
+ * config 子命令,Bun 运行时下子进程退出码 1 直接炸掉 market 服务启动;
50
+ * 读 npmrc 是零子进程的等价路径。任何一步都拿不到时返回 undefined,
51
+ * 由调用方回落默认 endpoint。
52
+ */
53
+ function getLocalRegistry(cwd, userHome = homedir()) {
54
+ const candidates = [
55
+ process.env["npm_config_registry"],
56
+ readNpmrcRegistry(join(cwd, ".npmrc")),
57
+ readNpmrcRegistry(join(userHome, ".npmrc"))
58
+ ];
59
+ for (const candidate of candidates) if (candidate?.startsWith("https://") || candidate?.startsWith("http://")) return candidate;
60
+ }
36
61
  const whichPMRuns = createRequire(import.meta.url)("which-pm-runs");
37
62
  const levelMap = {
38
63
  info: "info",
@@ -75,7 +100,7 @@ var Installer = class extends Service {
75
100
  }
76
101
  async start() {
77
102
  const { endpoint, timeout } = this.config;
78
- this.endpoint = endpoint ?? await getRegistry();
103
+ this.endpoint = endpoint ?? getLocalRegistry(this.cwd);
79
104
  const options = {};
80
105
  if (this.endpoint) options.endpoint = this.endpoint;
81
106
  if (timeout !== void 0) options.timeout = timeout;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@koishi-ce/plugin-market",
3
3
  "description": "Manage your bots and plugins with console",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "type": "module",
6
6
  "main": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -71,7 +71,6 @@
71
71
  "@koishi-ce/console": "^1.0.0",
72
72
  "@koishi-ce/registry": "^1.0.0",
73
73
  "execa": "^5.1.1",
74
- "get-registry": "^1.2.0",
75
74
  "p-map": "^4.0.0",
76
75
  "semver": "^7.6.3",
77
76
  "which-pm-runs": "^1.1.0"
@@ -1,6 +1,7 @@
1
1
  import { readFileSync } from "node:fs";
2
2
  import { createRequire } from "node:module";
3
- import { resolve } from "node:path";
3
+ import { homedir } from "node:os";
4
+ import { join, resolve } from "node:path";
4
5
  import type {} from "@koishi-ce/console";
5
6
  import {
6
7
  type Context,
@@ -23,12 +24,48 @@ import Scanner, {
23
24
  type RemotePackage,
24
25
  } from "@koishi-ce/registry";
25
26
  import spawn from "execa";
26
- import getRegistry from "get-registry";
27
27
  import pMap from "p-map";
28
28
  import { compare, satisfies, valid } from "semver";
29
29
 
30
30
  const logger = new Logger("market");
31
31
 
32
+ /** 从单个 .npmrc 文件提取 registry 配置项;文件不存在或读取出错一律视为未配置 */
33
+ function readNpmrcRegistry(file: string): string | undefined {
34
+ try {
35
+ for (const line of readFileSync(file, "utf8").split(/\r?\n/)) {
36
+ const matched = /^\s*registry\s*=\s*(\S+)\s*$/.exec(line);
37
+ const value = matched?.[1];
38
+ if (value) return value;
39
+ }
40
+ } catch {}
41
+ return undefined;
42
+ }
43
+
44
+ /**
45
+ * 读取本机 npm registry 配置(优先级对齐 npm 自身:环境变量 > 项目
46
+ * .npmrc > 用户 ~/.npmrc)。刻意不 spawn 子进程探测——原依赖
47
+ * get-registry 按 user-agent 选 `bun config get registry`,而 Bun 没有
48
+ * config 子命令,Bun 运行时下子进程退出码 1 直接炸掉 market 服务启动;
49
+ * 读 npmrc 是零子进程的等价路径。任何一步都拿不到时返回 undefined,
50
+ * 由调用方回落默认 endpoint。
51
+ */
52
+ function getLocalRegistry(
53
+ cwd: string,
54
+ userHome: string = homedir(),
55
+ ): string | undefined {
56
+ const candidates = [
57
+ process.env["npm_config_registry"],
58
+ readNpmrcRegistry(join(cwd, ".npmrc")),
59
+ readNpmrcRegistry(join(userHome, ".npmrc")),
60
+ ];
61
+ for (const candidate of candidates) {
62
+ if (candidate?.startsWith("https://") || candidate?.startsWith("http://")) {
63
+ return candidate;
64
+ }
65
+ }
66
+ return undefined;
67
+ }
68
+
32
69
  // 经 createRequire 加载 CJS 包并就地断言签名:不走 ESM 导入互操作,
33
70
  // 规避多包合并类型检查(大一统 tsconfig)下 export = 交织失效问题
34
71
  const whichPMRuns = createRequire(import.meta.url)("which-pm-runs") as () =>
@@ -133,7 +170,7 @@ class Installer extends Service {
133
170
 
134
171
  override async start() {
135
172
  const { endpoint, timeout } = this.config;
136
- this.endpoint = endpoint ?? (await getRegistry());
173
+ this.endpoint = endpoint ?? getLocalRegistry(this.cwd);
137
174
  const options: HTTP.Config = {};
138
175
  if (this.endpoint) options.endpoint = this.endpoint;
139
176
  if (timeout !== undefined) options.timeout = timeout;