com.jimuwd.xian.registry-proxy 1.1.23 → 1.1.25

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.
@@ -64,114 +64,113 @@ function registerSignalHandler(handler) {
64
64
  // Main implementation
65
65
  async function main() {
66
66
  try {
67
- // Find project root as base dir to get config file and other tmp files.
68
- const INSTALLATION_ROOT = await findProjectRoot();
69
- const LOCK_FILE = path.join(INSTALLATION_ROOT, LOCK_FILE_NAME);
70
- const PORT_FILE = path.join(INSTALLATION_ROOT, PORT_FILE_NAME);
71
- // Check for existing lock file
67
+ await startLocalRegistryProxyServerAndYarnInstallWithoutCleanup();
68
+ await cleanup(0);
69
+ }
70
+ catch (err) {
71
+ console.error('Error:', err instanceof Error ? err.message : String(err));
72
+ await cleanup(1);
73
+ }
74
+ }
75
+ async function startLocalRegistryProxyServerAndYarnInstallWithoutCleanup() {
76
+ // Find project root as base dir to get config file and other tmp files.
77
+ const INSTALLATION_ROOT = await findProjectRoot();
78
+ const LOCK_FILE = path.join(INSTALLATION_ROOT, LOCK_FILE_NAME);
79
+ const PORT_FILE = path.join(INSTALLATION_ROOT, PORT_FILE_NAME);
80
+ // Check for existing lock file
81
+ try {
82
+ await fs.access(LOCK_FILE);
83
+ console.log(`Custom install script is already running (lock file ${LOCK_FILE} exists).`);
84
+ console.log(`If this is unexpected, please remove ${LOCK_FILE} and try again.`);
85
+ return cleanup(0);
86
+ }
87
+ catch {
88
+ }
89
+ // Create lock file
90
+ await fs.writeFile(LOCK_FILE, process.pid.toString());
91
+ registerCleanup(async () => {
72
92
  try {
73
- await fs.access(LOCK_FILE);
74
- console.log(`Custom install script is already running (lock file ${LOCK_FILE} exists).`);
75
- console.log(`If this is unexpected, please remove ${LOCK_FILE} and try again.`);
76
- return cleanup(0);
93
+ await fs.unlink(LOCK_FILE);
77
94
  }
78
- catch {
95
+ catch (err) { //cleanup程序不要抛出任何异常
96
+ console.error(`Failed to delete lock file: ${LOCK_FILE}`, err);
79
97
  }
80
- // Create lock file
81
- await fs.writeFile(LOCK_FILE, process.pid.toString());
82
- registerCleanup(async () => {
98
+ });
99
+ // Change to project root
100
+ process.chdir(INSTALLATION_ROOT);
101
+ // Start registry proxy
102
+ console.log(`Starting registry-proxy@${REGISTRY_PROXY_VERSION} local server in the background...`);
103
+ // 提示:这里借助了execa调用"yarn dlx"后台运行registry proxy server的功能,没有直接使用本地ts函数调用的方式启动本地代理服务器,因为后者不太容易达到后台运行的效果。
104
+ proxyProcess = execa('yarn', [
105
+ 'dlx', '-p', `com.jimuwd.xian.registry-proxy@${REGISTRY_PROXY_VERSION}`, 'registry-proxy',
106
+ /*是不是可以传空,让server使用默认值?*/ '.registry-proxy.yml',
107
+ /*是不是可以传空,让server使用默认值?*/ '.yarnrc.yml',
108
+ /*是不是可以传空,让server使用默认值?*/ path.join(process.env.HOME || '', '.yarnrc.yml'),
109
+ /*之前是写死的静态端口40061,它有个缺点就是本地无法为多个项目工程并发执行yarn-install,现改为使用随机可用端口作为本地代理服务器端口,传'0'/''空串即可*/ '0'
110
+ ], {
111
+ detached: true,
112
+ stdio: 'inherit'
113
+ });
114
+ registerCleanup(async (_exitCode) => {
115
+ if (proxyProcess && !proxyProcess.killed) {
116
+ console.log('Stopping registry-proxy local server...');
83
117
  try {
84
- await fs.unlink(LOCK_FILE);
118
+ proxyProcess.kill('SIGTERM');
119
+ await proxyProcess;
120
+ console.log('Registry-proxy local server stopped.');
85
121
  }
86
- catch (err) { //cleanup程序不要抛出任何异常
87
- console.error(`Failed to delete lock file: ${LOCK_FILE}`, err);
88
- }
89
- });
90
- // Change to project root
91
- process.chdir(INSTALLATION_ROOT);
92
- // Start registry proxy
93
- console.log(`Starting registry-proxy@${REGISTRY_PROXY_VERSION} in the background...`);
94
- // 提示:这里借助了execa调用"yarn dlx"后台运行registry proxy server的功能,没有直接使用本地ts函数调用的方式启动本地代理服务器,因为后者不太容易达到后台运行的效果。
95
- proxyProcess = execa('yarn', [
96
- 'dlx', '-p', `com.jimuwd.xian.registry-proxy@${REGISTRY_PROXY_VERSION}`,
97
- 'registry-proxy',
98
- '.registry-proxy.yml',
99
- '.yarnrc.yml',
100
- path.join(process.env.HOME || '', '.yarnrc.yml'),
101
- /*之前是写死的静态端口40061,它有个缺点就是本地无法为多个项目工程并发执行yarn-install,现改为使用随机可用端口作为本地代理服务器端口,传'0'/''空串即可*/ '0'
102
- ], {
103
- detached: true,
104
- stdio: 'inherit'
105
- });
106
- registerCleanup(async (exitCode) => {
107
- if (proxyProcess && !proxyProcess.killed) {
108
- console.log('Stopping proxy server...');
109
- try {
110
- proxyProcess.kill('SIGTERM');
111
- await proxyProcess;
112
- console.log('Proxy server stopped.');
113
- }
114
- catch (err) { // cleanup程序不要抛出异常
115
- console.error('Proxy server stopping with error', err);
116
- }
122
+ catch (err) { // cleanup程序不要抛出异常
123
+ console.error('Registry-proxy local server stopping with error', err);
117
124
  }
118
- });
119
- // Wait for proxy to start
120
- console.log('Waiting for proxy server to start (up to 30 seconds)...');
121
- const fileExists = await waitForFile(PORT_FILE, MAX_WAIT_TIME_MS);
122
- if (!fileExists) {
123
- throw new Error(`Proxy server failed to create port file after ${MAX_WAIT_TIME_MS / 1000} seconds`);
124
- }
125
- const PROXY_PORT = await readPortFile(PORT_FILE);
126
- const portConnectable = await isPortConnectable(PROXY_PORT);
127
- if (!portConnectable) {
128
- throw new Error(`Proxy server not listening on port ${PROXY_PORT}`);
129
125
  }
130
- // Configure yarn
131
- const { exitCode, stdout } = await execa('yarn', ['config', 'get', 'npmRegistryServer']);
132
- let npmRegistryServer = stdout.trim();
133
- let localNpmRegistryServer;
126
+ });
127
+ // Wait for proxy to start
128
+ console.log('Waiting for proxy server to start (up to 30 seconds)...');
129
+ const fileExists = await waitForFile(PORT_FILE, MAX_WAIT_TIME_MS);
130
+ if (!fileExists) {
131
+ throw new Error(`Proxy server failed to create port file after ${MAX_WAIT_TIME_MS / 1000} seconds`);
132
+ }
133
+ const PROXY_PORT = await readPortFile(PORT_FILE);
134
+ const portConnectable = await isPortConnectable(PROXY_PORT);
135
+ if (!portConnectable) {
136
+ throw new Error(`Proxy server not listening on port ${PROXY_PORT}`);
137
+ }
138
+ // Configure yarn
139
+ const { exitCode, stdout } = await execa('yarn', ['config', 'get', 'npmRegistryServer']);
140
+ const npmRegistryServer = (exitCode === 0 && stdout) ? stdout.trim() : undefined;
141
+ const localNpmRegistryServer = (await readYarnConfig('.yarnrc.yml')).npmRegistryServer?.trim();
142
+ if (localNpmRegistryServer && localNpmRegistryServer === npmRegistryServer)
143
+ console.log(`Using npmRegistryServer value in project local .yarnrc.yml: ${localNpmRegistryServer}`);
144
+ else
145
+ console.log(`Using npmRegistryServer value in ${path.join(process.env.HOME || '', '.yarnrc.yml')}: ${npmRegistryServer}`);
146
+ await execa('yarn', ['config', 'set', 'npmRegistryServer', `http://127.0.0.1:${PROXY_PORT}`]);
147
+ console.log(`Set npmRegistryServer to http://127.0.0.1:${PROXY_PORT}`);
148
+ registerCleanup(async () => {
134
149
  try {
135
- const localYarnConfig = await readYarnConfig('.yarnrc.yml');
136
- localNpmRegistryServer = localYarnConfig.npmRegistryServer;
137
- }
138
- catch {
139
- localNpmRegistryServer = undefined;
140
- }
141
- await execa('yarn', ['config', 'set', 'npmRegistryServer', `http://127.0.0.1:${PROXY_PORT}`]);
142
- console.log(`Set npmRegistryServer to http://127.0.0.1:${PROXY_PORT}`);
143
- registerCleanup(async () => {
144
- try {
145
- //if (exitCode === 0 && npmRegistryServer) {//不能用这个变量来恢复为原来的 npmRegistryServer,因为它可能是全局配置~/.yarnrc.yml内的配置值或yarn工具官方默认值,而非本地.yarnrc.yml配置值。
146
- if (localNpmRegistryServer && localNpmRegistryServer.trim()) { //恢复为本地配置文件原来的 npmRegistryServer 配置值
147
- await execa('yarn', ['config', 'set', 'npmRegistryServer', localNpmRegistryServer]);
148
- console.log(`Recover npmRegistryServer to ${localNpmRegistryServer}`);
149
- }
150
- else { //原来本地配置文件中没有npmRegistryServer,则重置npmRegistryServer
151
- await execa('yarn', ['config', 'unset', 'npmRegistryServer']);
152
- console.log(`Unset npmRegistryServer.`);
153
- }
150
+ //if (npmRegistryServer) {//不能用这个变量来恢复为原来的 npmRegistryServer,因为它可能是全局配置~/.yarnrc.yml内的配置值或yarn工具官方默认值,而非本地.yarnrc.yml配置值。
151
+ if (localNpmRegistryServer) { //恢复为本地配置文件原来的 npmRegistryServer 配置值
152
+ await execa('yarn', ['config', 'set', 'npmRegistryServer', localNpmRegistryServer]);
153
+ console.log(`Recover npmRegistryServer to ${localNpmRegistryServer} in local '.yarnrc.yml'.`);
154
154
  }
155
- catch (err) { //cleanup程序不要抛出异常
156
- console.error('Recover yarn config npmRegistryServer error.', err);
155
+ else { //原来本地配置文件中没有npmRegistryServer,则重置npmRegistryServer
156
+ await execa('yarn', ['config', 'unset', 'npmRegistryServer']);
157
+ console.log(`Unset npmRegistryServer value in local '.yarnrc.yml'.`);
157
158
  }
158
- });
159
- // Run yarn install
160
- console.log('Running yarn install...');
161
- try {
162
- await execa('yarn', ['install'], { stdio: 'inherit' });
163
159
  }
164
- catch (err) {
165
- throw new Error('yarn install failed');
160
+ catch (err) { //cleanup程序不要抛出异常
161
+ console.error('Recover yarn config npmRegistryServer error.', err);
166
162
  }
167
- // Success
168
- console.info("Yarn install with local registry-proxy server success.");
169
- await cleanup(0);
163
+ });
164
+ // Run yarn install
165
+ console.log('Running yarn install...');
166
+ try {
167
+ await execa('yarn', ['install'], { stdio: 'inherit' });
170
168
  }
171
169
  catch (err) {
172
- console.error('Error:', err instanceof Error ? err.message : String(err));
173
- await cleanup(1);
170
+ throw new Error('yarn install failed');
174
171
  }
172
+ // Success
173
+ console.info("Yarn install with local registry-proxy server success.");
175
174
  }
176
175
  // 当前模块是否是直接运行的入口文件,而不是被其他模块导入的
177
176
  if (import.meta.url === `file://${process.argv[1]}`) {
@@ -1,6 +1,7 @@
1
1
  import { YarnConfig } from "../models.js";
2
2
  /**
3
3
  * 读取yml配置文件为yml对象
4
+ * @note 如果配置文件不存在,那么返回空对象,不抛异常。
4
5
  * @param path yml文件路径
5
6
  */
6
7
  declare function readYarnConfig(path: string): Promise<YarnConfig>;
@@ -6,6 +6,7 @@ import logger from "./logger.js";
6
6
  const { readFile } = fsPromises;
7
7
  /**
8
8
  * 读取yml配置文件为yml对象
9
+ * @note 如果配置文件不存在,那么返回空对象,不抛异常。
9
10
  * @param path yml文件路径
10
11
  */
11
12
  async function readYarnConfig(path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.1.23",
3
+ "version": "1.1.25",
4
4
  "description": "A lightweight npm registry local proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/server/index.js",