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

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,114 @@ 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} 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}`,
106
+ 'registry-proxy',
107
+ '.registry-proxy.yml',
108
+ '.yarnrc.yml',
109
+ path.join(process.env.HOME || '', '.yarnrc.yml'),
110
+ /*之前是写死的静态端口40061,它有个缺点就是本地无法为多个项目工程并发执行yarn-install,现改为使用随机可用端口作为本地代理服务器端口,传'0'/''空串即可*/ '0'
111
+ ], {
112
+ detached: true,
113
+ stdio: 'inherit'
114
+ });
115
+ registerCleanup(async (_exitCode) => {
116
+ if (proxyProcess && !proxyProcess.killed) {
117
+ console.log('Stopping proxy server...');
83
118
  try {
84
- await fs.unlink(LOCK_FILE);
119
+ proxyProcess.kill('SIGTERM');
120
+ await proxyProcess;
121
+ console.log('Proxy server stopped.');
85
122
  }
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
- }
123
+ catch (err) { // cleanup程序不要抛出异常
124
+ console.error('Proxy server stopping with error', err);
117
125
  }
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
126
  }
130
- // Configure yarn
131
- const { exitCode, stdout } = await execa('yarn', ['config', 'get', 'npmRegistryServer']);
132
- let npmRegistryServer = stdout.trim();
133
- let localNpmRegistryServer;
127
+ });
128
+ // Wait for proxy to start
129
+ console.log('Waiting for proxy server to start (up to 30 seconds)...');
130
+ const fileExists = await waitForFile(PORT_FILE, MAX_WAIT_TIME_MS);
131
+ if (!fileExists) {
132
+ throw new Error(`Proxy server failed to create port file after ${MAX_WAIT_TIME_MS / 1000} seconds`);
133
+ }
134
+ const PROXY_PORT = await readPortFile(PORT_FILE);
135
+ const portConnectable = await isPortConnectable(PROXY_PORT);
136
+ if (!portConnectable) {
137
+ throw new Error(`Proxy server not listening on port ${PROXY_PORT}`);
138
+ }
139
+ // Configure yarn
140
+ const { exitCode, stdout } = await execa('yarn', ['config', 'get', 'npmRegistryServer']);
141
+ const npmRegistryServer = (exitCode === 0 && stdout) ? stdout.trim() : undefined;
142
+ const localNpmRegistryServer = (await readYarnConfig('.yarnrc.yml')).npmRegistryServer;
143
+ if (localNpmRegistryServer && localNpmRegistryServer === npmRegistryServer)
144
+ console.log("Using npmRegistryServer value in project local .yarnrc.yml config file.");
145
+ else
146
+ console.log("Using npmRegistryServer value in global config file.");
147
+ await execa('yarn', ['config', 'set', 'npmRegistryServer', `http://127.0.0.1:${PROXY_PORT}`]);
148
+ console.log(`Set npmRegistryServer to http://127.0.0.1:${PROXY_PORT}`);
149
+ registerCleanup(async () => {
134
150
  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
- }
151
+ //if (npmRegistryServer) {//不能用这个变量来恢复为原来的 npmRegistryServer,因为它可能是全局配置~/.yarnrc.yml内的配置值或yarn工具官方默认值,而非本地.yarnrc.yml配置值。
152
+ if (localNpmRegistryServer && localNpmRegistryServer.trim()) { //恢复为本地配置文件原来的 npmRegistryServer 配置值
153
+ await execa('yarn', ['config', 'set', 'npmRegistryServer', localNpmRegistryServer]);
154
+ console.log(`Recover npmRegistryServer to ${localNpmRegistryServer}`);
154
155
  }
155
- catch (err) { //cleanup程序不要抛出异常
156
- console.error('Recover yarn config npmRegistryServer error.', err);
156
+ else { //原来本地配置文件中没有npmRegistryServer,则重置npmRegistryServer
157
+ await execa('yarn', ['config', 'unset', 'npmRegistryServer']);
158
+ console.log(`Unset npmRegistryServer.`);
157
159
  }
158
- });
159
- // Run yarn install
160
- console.log('Running yarn install...');
161
- try {
162
- await execa('yarn', ['install'], { stdio: 'inherit' });
163
160
  }
164
- catch (err) {
165
- throw new Error('yarn install failed');
161
+ catch (err) { //cleanup程序不要抛出异常
162
+ console.error('Recover yarn config npmRegistryServer error.', err);
166
163
  }
167
- // Success
168
- console.info("Yarn install with local registry-proxy server success.");
169
- await cleanup(0);
164
+ });
165
+ // Run yarn install
166
+ console.log('Running yarn install...');
167
+ try {
168
+ await execa('yarn', ['install'], { stdio: 'inherit' });
170
169
  }
171
170
  catch (err) {
172
- console.error('Error:', err instanceof Error ? err.message : String(err));
173
- await cleanup(1);
171
+ throw new Error('yarn install failed');
174
172
  }
173
+ // Success
174
+ console.info("Yarn install with local registry-proxy server success.");
175
175
  }
176
176
  // 当前模块是否是直接运行的入口文件,而不是被其他模块导入的
177
177
  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.24",
4
4
  "description": "A lightweight npm registry local proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/server/index.js",