com.jimuwd.xian.registry-proxy 1.1.19 → 1.1.21

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.
@@ -36,7 +36,7 @@ async function readPortFile(filePath) {
36
36
  const content = await fs.readFile(filePath, 'utf-8');
37
37
  const port = parseInt(content.trim(), 10);
38
38
  if (isNaN(port) || port < 1 || port > 65535) {
39
- throw new Error(`Invalid port number in ${filePath}`);
39
+ throw new Error(`Invalid port number ${port} in ${filePath}`);
40
40
  }
41
41
  return port;
42
42
  }
@@ -51,6 +51,7 @@ async function cleanup(exitCode = 1) {
51
51
  console.error('Cleanup handler error:', err);
52
52
  }
53
53
  }
54
+ console.log("Process exited with exitCode", exitCode);
54
55
  process.exit(exitCode);
55
56
  }
56
57
  function registerCleanup(handler) {
@@ -81,20 +82,22 @@ async function main() {
81
82
  try {
82
83
  await fs.unlink(LOCK_FILE);
83
84
  }
84
- catch {
85
+ catch (err) { //cleanup程序不要抛出任何异常
86
+ console.error(`Failed to delete lock file: ${LOCK_FILE}`, err);
85
87
  }
86
88
  });
87
89
  // Change to project root
88
90
  process.chdir(INSTALLATION_ROOT);
89
91
  // Start registry proxy
90
92
  console.log(`Starting registry-proxy@${REGISTRY_PROXY_VERSION} in the background...`);
93
+ // 提示:这里借助了execa调用"yarn dlx"后台运行registry proxy server的功能,没有直接使用本地ts函数调用的方式启动本地代理服务器,因为后者不太容易达到后台运行的效果。
91
94
  proxyProcess = execa('yarn', [
92
95
  'dlx', '-p', `com.jimuwd.xian.registry-proxy@${REGISTRY_PROXY_VERSION}`,
93
96
  'registry-proxy',
94
97
  '.registry-proxy.yml',
95
98
  '.yarnrc.yml',
96
99
  path.join(process.env.HOME || '', '.yarnrc.yml'),
97
- '40061'
100
+ /*之前是写死的静态端口40061,它有个缺点就是本地无法为多个项目工程并发执行yarn-install,现改为使用随机可用端口作为本地代理服务器端口,传'0'/''空串即可*/ '0'
98
101
  ], {
99
102
  detached: true,
100
103
  stdio: 'inherit'
@@ -105,11 +108,11 @@ async function main() {
105
108
  try {
106
109
  proxyProcess.kill('SIGTERM');
107
110
  await proxyProcess;
111
+ console.log('Proxy server stopped.');
108
112
  }
109
- catch {
110
- // Ignore errors
113
+ catch (err) { // cleanup程序不要抛出异常
114
+ console.error('Proxy server stopping with error', err);
111
115
  }
112
- console.log('Proxy server stopped.');
113
116
  }
114
117
  });
115
118
  // Wait for proxy to start
@@ -124,14 +127,23 @@ async function main() {
124
127
  throw new Error(`Proxy server not listening on port ${PROXY_PORT}`);
125
128
  }
126
129
  // Configure yarn
130
+ const { exitCode, stdout } = await execa('yarn', ['config', 'get', 'npmRegistryServer']);
127
131
  await execa('yarn', ['config', 'set', 'npmRegistryServer', `http://127.0.0.1:${PROXY_PORT}`]);
128
132
  console.log(`Set npmRegistryServer to http://127.0.0.1:${PROXY_PORT}`);
129
133
  registerCleanup(async () => {
130
134
  try {
131
- await execa('yarn', ['config', 'unset', 'npmRegistryServer']);
132
- console.log('Cleared npmRegistryServer configuration');
135
+ let npmRegistryServer = stdout.trim();
136
+ if (exitCode === 0 && npmRegistryServer) { //恢复为原来的 npmRegistryServer
137
+ await execa('yarn', ['config', 'set', 'npmRegistryServer', npmRegistryServer]);
138
+ console.log(`Recover npmRegistryServer to ${npmRegistryServer}`);
139
+ }
140
+ else { //原来没有npmRegistryServer,则重置npmRegistryServer
141
+ await execa('yarn', ['config', 'unset', 'npmRegistryServer']);
142
+ console.log(`Unset npmRegistryServer.`);
143
+ }
133
144
  }
134
- catch {
145
+ catch (err) { //cleanup程序不要抛出异常
146
+ console.error('Recover yarn config npmRegistryServer error.', err);
135
147
  }
136
148
  });
137
149
  // Run yarn install
@@ -143,6 +155,7 @@ async function main() {
143
155
  throw new Error('yarn install failed');
144
156
  }
145
157
  // Success
158
+ console.info("Yarn install with local registry-proxy server success.");
146
159
  await cleanup(0);
147
160
  }
148
161
  catch (err) {
@@ -168,8 +181,5 @@ if (import.meta.url === `file://${process.argv[1]}`) {
168
181
  });
169
182
  });
170
183
  // Start the program
171
- main().catch(err => {
172
- console.error('Unhandled error:', err);
173
- cleanup(1);
174
- });
184
+ await main();
175
185
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.1.19",
3
+ "version": "1.1.21",
4
4
  "description": "A lightweight npm registry local proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/server/index.js",