react-native-update-cli 2.23.1 → 2.24.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/README.md +17 -0
- package/README.zh-CN.md +8 -0
- package/lib/auto-update.d.ts +30 -0
- package/lib/auto-update.js +502 -0
- package/lib/bin.js +3 -0
- package/lib/bundle-runner.js +14 -4
- package/lib/locales/en.d.ts +2 -0
- package/lib/locales/en.js +2 -0
- package/lib/locales/zh.d.ts +2 -0
- package/lib/locales/zh.js +2 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +33 -1
- package/lib/utils/slim-sourcemap.d.ts +17 -0
- package/lib/utils/slim-sourcemap.js +70 -0
- package/lib/versions.js +22 -2
- package/package.json +1 -1
- package/src/auto-update.ts +647 -0
- package/src/bin.ts +3 -0
- package/src/bundle-runner.ts +16 -7
- package/src/locales/en.ts +4 -0
- package/src/locales/zh.ts +3 -0
- package/src/utils/index.ts +39 -1
- package/src/utils/slim-sourcemap.ts +83 -0
- package/src/versions.ts +28 -2
package/README.md
CHANGED
|
@@ -19,6 +19,23 @@ npm install -g react-native-update-cli
|
|
|
19
19
|
|
|
20
20
|
The command-line tool is installed globally. Invoke `pushy` or `cresc` directly; do not use a project-local CLI or prefix commands with `npx`.
|
|
21
21
|
|
|
22
|
+
### Background CLI updates
|
|
23
|
+
|
|
24
|
+
Global installations update themselves in a detached background process after
|
|
25
|
+
a successful command. The foreground command is never held open for the
|
|
26
|
+
install. The CLI reuses the package manager that owns the global installation
|
|
27
|
+
(`npm`, `pnpm`, Yarn Classic, or Bun) and the registry configured in the current
|
|
28
|
+
`.npmrc`/environment, so private mirrors, credentials, proxies, and custom
|
|
29
|
+
global prefixes keep working.
|
|
30
|
+
|
|
31
|
+
Network failures are silent and retried after a cooldown. Concurrent commands
|
|
32
|
+
share a lock. A non-writable global directory is never escalated with `sudo`;
|
|
33
|
+
the next invocation prints the exact manual command after the permission issue
|
|
34
|
+
has been fixed. Project-local/npx installations and CI are not modified.
|
|
35
|
+
|
|
36
|
+
Set `RNU_AUTO_UPDATE=0` (or `RNU_DISABLE_AUTO_UPDATE=1`) to disable the feature.
|
|
37
|
+
`RNU_AUTO_UPDATE_PACKAGE_MANAGER=npm|pnpm|yarn|bun` overrides manager detection.
|
|
38
|
+
|
|
22
39
|
## Basic Usage
|
|
23
40
|
|
|
24
41
|
```bash
|
package/README.zh-CN.md
CHANGED
|
@@ -19,6 +19,14 @@ npm install -g react-native-update-cli
|
|
|
19
19
|
|
|
20
20
|
命令行工具始终全局安装,直接调用 `pushy` 或 `cresc`;不要使用项目本地 CLI,也不要给命令添加 `npx` 前缀。
|
|
21
21
|
|
|
22
|
+
### CLI 后台自动更新
|
|
23
|
+
|
|
24
|
+
全局安装的 CLI 会在命令成功结束后启动独立后台进程完成自更新,不会让当前命令等待安装。更新会沿用这份全局安装实际所属的包管理器(`npm`、`pnpm`、Yarn Classic 或 Bun),并使用当前 `.npmrc` / 环境中的 registry 配置,因此私有镜像、认证、代理和自定义全局目录均可继续生效。
|
|
25
|
+
|
|
26
|
+
网络失败会静默降级并在冷却期后重试;并发命令通过锁避免重复安装。全局目录不可写时不会自动调用 `sudo`,下次运行会给出修复权限后可执行的准确命令。本地依赖、npx 临时安装和 CI 环境不会被修改。
|
|
27
|
+
|
|
28
|
+
设置 `RNU_AUTO_UPDATE=0`(或 `RNU_DISABLE_AUTO_UPDATE=1`)可关闭;`RNU_AUTO_UPDATE_PACKAGE_MANAGER=npm|pnpm|yarn|bun` 可覆盖包管理器识别结果。
|
|
29
|
+
|
|
22
30
|
## 基础用法
|
|
23
31
|
|
|
24
32
|
```bash
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
2
|
+
export interface AutoUpdateNotice {
|
|
3
|
+
kind: 'updated' | 'permission';
|
|
4
|
+
currentVersion: string;
|
|
5
|
+
targetVersion: string;
|
|
6
|
+
command?: string;
|
|
7
|
+
}
|
|
8
|
+
interface PackageManagerDetectionOptions {
|
|
9
|
+
packageRoot: string;
|
|
10
|
+
env?: NodeJS.ProcessEnv;
|
|
11
|
+
npmGlobalDir?: string;
|
|
12
|
+
yarnGlobalDir?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Identify the package manager that owns this global installation. Project
|
|
16
|
+
* lockfiles are deliberately ignored: they say how the app is installed, not
|
|
17
|
+
* how this CLI was installed.
|
|
18
|
+
*/
|
|
19
|
+
export declare function detectGlobalPackageManager({ packageRoot, env, npmGlobalDir, yarnGlobalDir, }: PackageManagerDetectionOptions): PackageManager | undefined;
|
|
20
|
+
export declare function buildUpdateCommand(manager: PackageManager, targetVersion: string, registryUrl: string, installPrefix?: string): {
|
|
21
|
+
command: string;
|
|
22
|
+
args: string[];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Start a detached updater after the foreground command has completed. This
|
|
26
|
+
* function is synchronous on purpose: it is safe to call from process.exit.
|
|
27
|
+
*/
|
|
28
|
+
export declare function launchAutoUpdate(currentVersion: string, targetVersion: string | undefined, env?: NodeJS.ProcessEnv): boolean;
|
|
29
|
+
export declare function consumeAutoUpdateNotice(currentVersion: string): AutoUpdateNotice | undefined;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.detectGlobalPackageManager = detectGlobalPackageManager;
|
|
7
|
+
exports.buildUpdateCommand = buildUpdateCommand;
|
|
8
|
+
exports.launchAutoUpdate = launchAutoUpdate;
|
|
9
|
+
exports.consumeAutoUpdateNotice = consumeAutoUpdateNotice;
|
|
10
|
+
const child_process_1 = require("child_process");
|
|
11
|
+
const fs_1 = require("fs");
|
|
12
|
+
const os_1 = require("os");
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
const PACKAGE_NAME = 'react-native-update-cli';
|
|
15
|
+
const WORKER_ENV = 'RNU_AUTO_UPDATE_WORKER';
|
|
16
|
+
const CANDIDATE_ENV = 'RNU_AUTO_UPDATE_CANDIDATE';
|
|
17
|
+
const LOCK_STALE_MS = 10 * 60 * 1000;
|
|
18
|
+
const UPDATE_TIMEOUT_MS = 2 * 60 * 1000;
|
|
19
|
+
const NETWORK_RETRY_MS = 6 * 60 * 60 * 1000;
|
|
20
|
+
const FAILURE_RETRY_MS = 24 * 60 * 60 * 1000;
|
|
21
|
+
const PERMISSION_RETRY_MS = 7 * 24 * 60 * 60 * 1000;
|
|
22
|
+
const OUTPUT_LIMIT = 32 * 1024;
|
|
23
|
+
function cacheDir() {
|
|
24
|
+
if (process.env.XDG_CACHE_HOME) {
|
|
25
|
+
return path_1.default.join(process.env.XDG_CACHE_HOME, PACKAGE_NAME);
|
|
26
|
+
}
|
|
27
|
+
switch (process.platform) {
|
|
28
|
+
case 'darwin':
|
|
29
|
+
return path_1.default.join((0, os_1.homedir)(), 'Library', 'Caches', PACKAGE_NAME);
|
|
30
|
+
case 'win32':
|
|
31
|
+
return path_1.default.join(process.env.LOCALAPPDATA ?? path_1.default.join((0, os_1.homedir)(), 'AppData', 'Local'), PACKAGE_NAME, 'Cache');
|
|
32
|
+
default:
|
|
33
|
+
return path_1.default.join((0, os_1.homedir)(), '.cache', PACKAGE_NAME);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function stateFile() {
|
|
37
|
+
return path_1.default.join(cacheDir(), 'auto-update.json');
|
|
38
|
+
}
|
|
39
|
+
function lockDir() {
|
|
40
|
+
return path_1.default.join(cacheDir(), 'auto-update.lock');
|
|
41
|
+
}
|
|
42
|
+
function readState() {
|
|
43
|
+
try {
|
|
44
|
+
const parsed = JSON.parse((0, fs_1.readFileSync)(stateFile(), 'utf8'));
|
|
45
|
+
if (!parsed ||
|
|
46
|
+
!['running', 'updated', 'failed'].includes(parsed.status) ||
|
|
47
|
+
typeof parsed.targetVersion !== 'string' ||
|
|
48
|
+
typeof parsed.attemptedAt !== 'number') {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return parsed;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function writeState(state) {
|
|
58
|
+
try {
|
|
59
|
+
(0, fs_1.mkdirSync)(cacheDir(), { recursive: true });
|
|
60
|
+
const temporary = `${stateFile()}.${process.pid}.tmp`;
|
|
61
|
+
(0, fs_1.writeFileSync)(temporary, JSON.stringify(state));
|
|
62
|
+
(0, fs_1.renameSync)(temporary, stateFile());
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// A read-only cache must never interfere with the CLI or the update.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function normalizePath(value) {
|
|
69
|
+
let resolved = path_1.default.resolve(value);
|
|
70
|
+
try {
|
|
71
|
+
resolved = fs_1.realpathSync.native(resolved);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// The caller may supply a path that was replaced during an update.
|
|
75
|
+
}
|
|
76
|
+
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
77
|
+
}
|
|
78
|
+
function isWithin(child, parent) {
|
|
79
|
+
const relative = path_1.default.relative(normalizePath(parent), normalizePath(child));
|
|
80
|
+
return (relative === '' ||
|
|
81
|
+
(!relative.startsWith('..') && !path_1.default.isAbsolute(relative)));
|
|
82
|
+
}
|
|
83
|
+
function managerFromUserAgent(env) {
|
|
84
|
+
const value = `${env.npm_config_user_agent ?? ''} ${env.npm_execpath ?? ''}`.toLowerCase();
|
|
85
|
+
if (/\bbun(?:\/|\s|$)/.test(value))
|
|
86
|
+
return 'bun';
|
|
87
|
+
if (/\bpnpm(?:\/|\s|$)/.test(value))
|
|
88
|
+
return 'pnpm';
|
|
89
|
+
if (/\byarn(?:\/|\s|$)/.test(value))
|
|
90
|
+
return 'yarn';
|
|
91
|
+
if (/\bnpm(?:\/|\s|$)/.test(value))
|
|
92
|
+
return 'npm';
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
function parseManager(value) {
|
|
96
|
+
const manager = value?.trim().toLowerCase();
|
|
97
|
+
return manager === 'npm' ||
|
|
98
|
+
manager === 'pnpm' ||
|
|
99
|
+
manager === 'yarn' ||
|
|
100
|
+
manager === 'bun'
|
|
101
|
+
? manager
|
|
102
|
+
: undefined;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Identify the package manager that owns this global installation. Project
|
|
106
|
+
* lockfiles are deliberately ignored: they say how the app is installed, not
|
|
107
|
+
* how this CLI was installed.
|
|
108
|
+
*/
|
|
109
|
+
function detectGlobalPackageManager({ packageRoot, env = process.env, npmGlobalDir, yarnGlobalDir, }) {
|
|
110
|
+
const root = normalizePath(packageRoot);
|
|
111
|
+
const normalized = root.replaceAll('\\', '/').toLowerCase();
|
|
112
|
+
if (yarnGlobalDir && isWithin(root, yarnGlobalDir))
|
|
113
|
+
return 'yarn';
|
|
114
|
+
if (npmGlobalDir && isWithin(root, npmGlobalDir))
|
|
115
|
+
return 'npm';
|
|
116
|
+
if (/\/(?:\.bun|bun)\/install\/global\/node_modules\//.test(normalized)) {
|
|
117
|
+
return 'bun';
|
|
118
|
+
}
|
|
119
|
+
if (/\/pnpm\/global\/[^/]+\//.test(normalized))
|
|
120
|
+
return 'pnpm';
|
|
121
|
+
if (/\/(?:\.config\/yarn\/global|yarn\/data\/global)\/node_modules\//.test(normalized)) {
|
|
122
|
+
return 'yarn';
|
|
123
|
+
}
|
|
124
|
+
// Custom npm prefixes conventionally use <prefix>/lib/node_modules on
|
|
125
|
+
// Unix and <prefix>/node_modules on Windows. Only accept the generic form
|
|
126
|
+
// when the package is outside the current project, so npx and local installs
|
|
127
|
+
// are never self-modified.
|
|
128
|
+
const cwdNodeModules = path_1.default.join(normalizePath(process.cwd()), 'node_modules');
|
|
129
|
+
const configuredPrefixModules = env.npm_config_prefix
|
|
130
|
+
? path_1.default.join(normalizePath(env.npm_config_prefix), 'node_modules')
|
|
131
|
+
: undefined;
|
|
132
|
+
const looksGlobal = normalized.includes('/lib/node_modules/') ||
|
|
133
|
+
(process.platform === 'win32' &&
|
|
134
|
+
(normalized.includes('/npm/node_modules/') ||
|
|
135
|
+
(configuredPrefixModules && isWithin(root, configuredPrefixModules))));
|
|
136
|
+
if (looksGlobal && !isWithin(root, cwdNodeModules)) {
|
|
137
|
+
return (parseManager(env.RNU_AUTO_UPDATE_PACKAGE_MANAGER) ??
|
|
138
|
+
managerFromUserAgent(env) ??
|
|
139
|
+
'npm');
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
function globalDirectories() {
|
|
144
|
+
try {
|
|
145
|
+
const dirs = require('global-dirs');
|
|
146
|
+
return {
|
|
147
|
+
npmGlobalDir: dirs.npm?.packages,
|
|
148
|
+
npmPrefix: dirs.npm?.prefix,
|
|
149
|
+
yarnGlobalDir: dirs.yarn?.packages,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function sanitizeRegistryUrl(value) {
|
|
157
|
+
try {
|
|
158
|
+
const registry = new URL(value);
|
|
159
|
+
if (registry.protocol !== 'http:' && registry.protocol !== 'https:') {
|
|
160
|
+
return 'https://registry.npmjs.org/';
|
|
161
|
+
}
|
|
162
|
+
// Credentials belong in the user's package-manager configuration, not in
|
|
163
|
+
// a process argument or our state file.
|
|
164
|
+
registry.username = '';
|
|
165
|
+
registry.password = '';
|
|
166
|
+
return registry.toString();
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return 'https://registry.npmjs.org/';
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function buildUpdateCommand(manager, targetVersion, registryUrl, installPrefix) {
|
|
173
|
+
const target = `${PACKAGE_NAME}@${targetVersion}`;
|
|
174
|
+
const registry = sanitizeRegistryUrl(registryUrl);
|
|
175
|
+
// Windows package managers are .cmd shims and therefore run through
|
|
176
|
+
// cmd.exe. Keep the URL out of that command line (it may contain shell
|
|
177
|
+
// metacharacters); runPackageManager supplies the same value through the
|
|
178
|
+
// environment instead.
|
|
179
|
+
const registryArgs = process.platform === 'win32' ? [] : [`--registry=${registry}`];
|
|
180
|
+
switch (manager) {
|
|
181
|
+
case 'pnpm':
|
|
182
|
+
return {
|
|
183
|
+
command: process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm',
|
|
184
|
+
args: ['add', '--global', target, '--reporter=silent', ...registryArgs],
|
|
185
|
+
};
|
|
186
|
+
case 'yarn':
|
|
187
|
+
return {
|
|
188
|
+
command: process.platform === 'win32' ? 'yarn.cmd' : 'yarn',
|
|
189
|
+
args: [
|
|
190
|
+
'global',
|
|
191
|
+
'add',
|
|
192
|
+
target,
|
|
193
|
+
'--non-interactive',
|
|
194
|
+
'--silent',
|
|
195
|
+
...registryArgs,
|
|
196
|
+
],
|
|
197
|
+
};
|
|
198
|
+
case 'bun':
|
|
199
|
+
return {
|
|
200
|
+
command: process.platform === 'win32' ? 'bun.exe' : 'bun',
|
|
201
|
+
args: ['add', '--global', target, '--no-progress', ...registryArgs],
|
|
202
|
+
};
|
|
203
|
+
default:
|
|
204
|
+
return {
|
|
205
|
+
command: process.platform === 'win32' ? 'npm.cmd' : 'npm',
|
|
206
|
+
args: [
|
|
207
|
+
'install',
|
|
208
|
+
'--global',
|
|
209
|
+
target,
|
|
210
|
+
...(installPrefix && process.platform !== 'win32'
|
|
211
|
+
? [`--prefix=${installPrefix}`]
|
|
212
|
+
: []),
|
|
213
|
+
'--no-audit',
|
|
214
|
+
'--no-fund',
|
|
215
|
+
'--loglevel=error',
|
|
216
|
+
...registryArgs,
|
|
217
|
+
],
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function inferNpmPrefix(packageRoot, npmGlobalDir, npmPrefix) {
|
|
222
|
+
if (npmGlobalDir && npmPrefix && isWithin(packageRoot, npmGlobalDir)) {
|
|
223
|
+
return normalizePath(npmPrefix);
|
|
224
|
+
}
|
|
225
|
+
const modules = path_1.default.dirname(normalizePath(packageRoot));
|
|
226
|
+
if (path_1.default.basename(modules).toLowerCase() !== 'node_modules') {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
const modulesParent = path_1.default.dirname(modules);
|
|
230
|
+
return path_1.default.basename(modulesParent).toLowerCase() === 'lib'
|
|
231
|
+
? path_1.default.dirname(modulesParent)
|
|
232
|
+
: modulesParent;
|
|
233
|
+
}
|
|
234
|
+
function isDisabled(env) {
|
|
235
|
+
const disabled = (value) => value === '0' || value?.toLowerCase() === 'false';
|
|
236
|
+
const enabled = (value) => value === '1' || value?.toLowerCase() === 'true';
|
|
237
|
+
return (disabled(env.RNU_AUTO_UPDATE) ||
|
|
238
|
+
enabled(env.RNU_DISABLE_AUTO_UPDATE) ||
|
|
239
|
+
enabled(env.NO_UPDATE_NOTIFIER) ||
|
|
240
|
+
enabled(env.CI));
|
|
241
|
+
}
|
|
242
|
+
function shouldRetry(state, target) {
|
|
243
|
+
if (!state || state.targetVersion !== target)
|
|
244
|
+
return true;
|
|
245
|
+
if (state.status === 'updated')
|
|
246
|
+
return false;
|
|
247
|
+
if (state.status === 'running') {
|
|
248
|
+
return Date.now() - state.attemptedAt >= LOCK_STALE_MS;
|
|
249
|
+
}
|
|
250
|
+
return !state.retryAt || state.retryAt <= Date.now();
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Start a detached updater after the foreground command has completed. This
|
|
254
|
+
* function is synchronous on purpose: it is safe to call from process.exit.
|
|
255
|
+
*/
|
|
256
|
+
function launchAutoUpdate(currentVersion, targetVersion, env = process.env) {
|
|
257
|
+
if (!targetVersion ||
|
|
258
|
+
targetVersion === currentVersion ||
|
|
259
|
+
isDisabled(env) ||
|
|
260
|
+
!shouldRetry(readState(), targetVersion)) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
const packageRoot = path_1.default.resolve(__dirname, '..');
|
|
264
|
+
const directories = globalDirectories();
|
|
265
|
+
const detected = detectGlobalPackageManager({
|
|
266
|
+
packageRoot,
|
|
267
|
+
env,
|
|
268
|
+
npmGlobalDir: directories.npmGlobalDir,
|
|
269
|
+
yarnGlobalDir: directories.yarnGlobalDir,
|
|
270
|
+
});
|
|
271
|
+
if (!detected)
|
|
272
|
+
return false;
|
|
273
|
+
const packageManager = parseManager(env.RNU_AUTO_UPDATE_PACKAGE_MANAGER) ?? detected;
|
|
274
|
+
let registryUrl = 'https://registry.npmjs.org/';
|
|
275
|
+
try {
|
|
276
|
+
const registryModule = require('registry-auth-token/registry-url');
|
|
277
|
+
const getRegistryUrl = (registryModule.default ?? registryModule);
|
|
278
|
+
registryUrl = getRegistryUrl('');
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
// Keep the public npm registry fallback when config parsing fails.
|
|
282
|
+
}
|
|
283
|
+
const candidate = {
|
|
284
|
+
currentVersion,
|
|
285
|
+
targetVersion,
|
|
286
|
+
packageRoot: normalizePath(packageRoot),
|
|
287
|
+
packageManager,
|
|
288
|
+
registryUrl: sanitizeRegistryUrl(registryUrl),
|
|
289
|
+
installPrefix: packageManager === 'npm' && detected === 'npm'
|
|
290
|
+
? inferNpmPrefix(packageRoot, directories.npmGlobalDir, directories.npmPrefix)
|
|
291
|
+
: undefined,
|
|
292
|
+
};
|
|
293
|
+
try {
|
|
294
|
+
const worker = (0, child_process_1.spawn)(process.execPath, [__filename], {
|
|
295
|
+
detached: true,
|
|
296
|
+
env: {
|
|
297
|
+
...env,
|
|
298
|
+
[WORKER_ENV]: '1',
|
|
299
|
+
[CANDIDATE_ENV]: JSON.stringify(candidate),
|
|
300
|
+
},
|
|
301
|
+
stdio: 'ignore',
|
|
302
|
+
windowsHide: true,
|
|
303
|
+
});
|
|
304
|
+
worker.once('error', () => { });
|
|
305
|
+
worker.unref();
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
function acquireLock() {
|
|
313
|
+
try {
|
|
314
|
+
(0, fs_1.mkdirSync)(cacheDir(), { recursive: true });
|
|
315
|
+
try {
|
|
316
|
+
(0, fs_1.mkdirSync)(lockDir());
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
if ((0, fs_1.existsSync)(lockDir()) &&
|
|
321
|
+
Date.now() - (0, fs_1.statSync)(lockDir()).mtimeMs >= LOCK_STALE_MS) {
|
|
322
|
+
(0, fs_1.rmSync)(lockDir(), { force: true, recursive: true });
|
|
323
|
+
(0, fs_1.mkdirSync)(lockDir());
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function appendBounded(current, chunk) {
|
|
334
|
+
const combined = `${current}${chunk.toString()}`;
|
|
335
|
+
return combined.length > OUTPUT_LIMIT
|
|
336
|
+
? combined.slice(combined.length - OUTPUT_LIMIT)
|
|
337
|
+
: combined;
|
|
338
|
+
}
|
|
339
|
+
function classifyFailure(error) {
|
|
340
|
+
if (/eacces|eperm|permission denied|access is denied/i.test(error)) {
|
|
341
|
+
return 'permission';
|
|
342
|
+
}
|
|
343
|
+
if (/econn|enet|eai_again|enotfound|timed?\s*out|timeout|certificate|socket|network|fetch/i.test(error)) {
|
|
344
|
+
return 'network';
|
|
345
|
+
}
|
|
346
|
+
if (/enoent|not recognized|command not found/i.test(error)) {
|
|
347
|
+
return 'package-manager';
|
|
348
|
+
}
|
|
349
|
+
return 'unknown';
|
|
350
|
+
}
|
|
351
|
+
function retryDelay(kind) {
|
|
352
|
+
if (kind === 'permission')
|
|
353
|
+
return PERMISSION_RETRY_MS;
|
|
354
|
+
if (kind === 'network')
|
|
355
|
+
return NETWORK_RETRY_MS;
|
|
356
|
+
return FAILURE_RETRY_MS;
|
|
357
|
+
}
|
|
358
|
+
function compactMessage(value) {
|
|
359
|
+
return value
|
|
360
|
+
.split(String.fromCharCode(27))
|
|
361
|
+
.map((part, index) => index === 0 ? part : part.replace(/^\[[0-9;]*m/, ''))
|
|
362
|
+
.join('')
|
|
363
|
+
.trim()
|
|
364
|
+
.slice(-2000);
|
|
365
|
+
}
|
|
366
|
+
async function runPackageManager(command, args, registryUrl, installPrefix) {
|
|
367
|
+
return new Promise((resolve) => {
|
|
368
|
+
let output = '';
|
|
369
|
+
let settled = false;
|
|
370
|
+
const child = (0, child_process_1.spawn)(command, args, {
|
|
371
|
+
env: {
|
|
372
|
+
...process.env,
|
|
373
|
+
npm_config_audit: 'false',
|
|
374
|
+
npm_config_fund: 'false',
|
|
375
|
+
...(installPrefix
|
|
376
|
+
? {
|
|
377
|
+
NPM_CONFIG_PREFIX: installPrefix,
|
|
378
|
+
npm_config_prefix: installPrefix,
|
|
379
|
+
}
|
|
380
|
+
: {}),
|
|
381
|
+
npm_config_registry: sanitizeRegistryUrl(registryUrl),
|
|
382
|
+
npm_config_update_notifier: 'false',
|
|
383
|
+
},
|
|
384
|
+
shell: process.platform === 'win32' && command.endsWith('.cmd'),
|
|
385
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
386
|
+
windowsHide: true,
|
|
387
|
+
});
|
|
388
|
+
const finish = (code, extra = '') => {
|
|
389
|
+
if (settled)
|
|
390
|
+
return;
|
|
391
|
+
settled = true;
|
|
392
|
+
clearTimeout(timer);
|
|
393
|
+
resolve({ code, output: appendBounded(output, extra) });
|
|
394
|
+
};
|
|
395
|
+
child.stdout?.on('data', (chunk) => {
|
|
396
|
+
output = appendBounded(output, chunk);
|
|
397
|
+
});
|
|
398
|
+
child.stderr?.on('data', (chunk) => {
|
|
399
|
+
output = appendBounded(output, chunk);
|
|
400
|
+
});
|
|
401
|
+
child.once('error', (error) => finish(127, error.message));
|
|
402
|
+
child.once('close', (code, signal) => finish(code ?? 1, signal ? `\nTerminated by ${signal}` : ''));
|
|
403
|
+
const timer = setTimeout(() => {
|
|
404
|
+
child.kill('SIGTERM');
|
|
405
|
+
finish(124, '\nUpdate timed out');
|
|
406
|
+
}, UPDATE_TIMEOUT_MS);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
async function runWorker(candidate) {
|
|
410
|
+
if (!acquireLock())
|
|
411
|
+
return;
|
|
412
|
+
const previous = readState();
|
|
413
|
+
const running = {
|
|
414
|
+
...candidate,
|
|
415
|
+
status: 'running',
|
|
416
|
+
attemptedAt: Date.now(),
|
|
417
|
+
failureCount: previous?.targetVersion === candidate.targetVersion
|
|
418
|
+
? (previous.failureCount ?? 0) + 1
|
|
419
|
+
: 1,
|
|
420
|
+
};
|
|
421
|
+
writeState(running);
|
|
422
|
+
try {
|
|
423
|
+
if (process.platform !== 'win32') {
|
|
424
|
+
(0, fs_1.accessSync)(path_1.default.dirname(candidate.packageRoot), fs_1.constants.W_OK);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
const failureKind = 'permission';
|
|
429
|
+
writeState({
|
|
430
|
+
...running,
|
|
431
|
+
status: 'failed',
|
|
432
|
+
finishedAt: Date.now(),
|
|
433
|
+
retryAt: Date.now() + retryDelay(failureKind),
|
|
434
|
+
failureKind,
|
|
435
|
+
message: compactMessage(String(error)),
|
|
436
|
+
});
|
|
437
|
+
(0, fs_1.rmSync)(lockDir(), { force: true, recursive: true });
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
const update = buildUpdateCommand(candidate.packageManager, candidate.targetVersion, candidate.registryUrl, candidate.installPrefix);
|
|
441
|
+
const result = await runPackageManager(update.command, update.args, candidate.registryUrl, candidate.installPrefix);
|
|
442
|
+
if (result.code === 0) {
|
|
443
|
+
writeState({
|
|
444
|
+
...running,
|
|
445
|
+
status: 'updated',
|
|
446
|
+
finishedAt: Date.now(),
|
|
447
|
+
message: undefined,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
const detail = compactMessage(result.output);
|
|
452
|
+
const failureKind = classifyFailure(detail);
|
|
453
|
+
writeState({
|
|
454
|
+
...running,
|
|
455
|
+
status: 'failed',
|
|
456
|
+
finishedAt: Date.now(),
|
|
457
|
+
retryAt: Date.now() + retryDelay(failureKind),
|
|
458
|
+
failureKind,
|
|
459
|
+
message: detail,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
(0, fs_1.rmSync)(lockDir(), { force: true, recursive: true });
|
|
463
|
+
}
|
|
464
|
+
function consumeAutoUpdateNotice(currentVersion) {
|
|
465
|
+
const state = readState();
|
|
466
|
+
if (!state || state.reportedAt)
|
|
467
|
+
return undefined;
|
|
468
|
+
if (state.status === 'updated' && currentVersion === state.targetVersion) {
|
|
469
|
+
writeState({ ...state, reportedAt: Date.now() });
|
|
470
|
+
return {
|
|
471
|
+
kind: 'updated',
|
|
472
|
+
currentVersion: state.currentVersion,
|
|
473
|
+
targetVersion: state.targetVersion,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
if (state.status === 'failed' && state.failureKind === 'permission') {
|
|
477
|
+
writeState({ ...state, reportedAt: Date.now() });
|
|
478
|
+
const update = buildUpdateCommand(state.packageManager, state.targetVersion, state.registryUrl, state.installPrefix);
|
|
479
|
+
return {
|
|
480
|
+
kind: 'permission',
|
|
481
|
+
currentVersion,
|
|
482
|
+
targetVersion: state.targetVersion,
|
|
483
|
+
command: [update.command, ...update.args].join(' '),
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
return undefined;
|
|
487
|
+
}
|
|
488
|
+
if (process.env[WORKER_ENV] === '1') {
|
|
489
|
+
try {
|
|
490
|
+
const candidate = JSON.parse(process.env[CANDIDATE_ENV] ?? '');
|
|
491
|
+
if (candidate?.packageRoot &&
|
|
492
|
+
candidate.targetVersion &&
|
|
493
|
+
parseManager(candidate.packageManager)) {
|
|
494
|
+
void runWorker(candidate).catch(() => {
|
|
495
|
+
(0, fs_1.rmSync)(lockDir(), { force: true, recursive: true });
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
catch {
|
|
500
|
+
// Malformed worker input is ignored; foreground CLI operation is over.
|
|
501
|
+
}
|
|
502
|
+
}
|
package/lib/bin.js
CHANGED
|
@@ -51,11 +51,13 @@ async function run() {
|
|
|
51
51
|
// exit on their own.
|
|
52
52
|
const versionCheck = await (0, utils_1.printVersionCommand)({ wait: versionOnly });
|
|
53
53
|
if (versionOnly) {
|
|
54
|
+
versionCheck.startAutoUpdate();
|
|
54
55
|
process.exit();
|
|
55
56
|
}
|
|
56
57
|
process.on('exit', (code) => {
|
|
57
58
|
if (code === 0) {
|
|
58
59
|
versionCheck.printHints();
|
|
60
|
+
versionCheck.startAutoUpdate();
|
|
59
61
|
}
|
|
60
62
|
});
|
|
61
63
|
const argv = require('cli-arguments').parse(require('../cli.json'));
|
|
@@ -79,6 +81,7 @@ async function run() {
|
|
|
79
81
|
// registry request itself is unref'd, so exiting never waits on it
|
|
80
82
|
await versionCheck.settle(500);
|
|
81
83
|
versionCheck.printHints();
|
|
84
|
+
versionCheck.startAutoUpdate();
|
|
82
85
|
}
|
|
83
86
|
else {
|
|
84
87
|
throw new Error((0, i18n_1.t)('unknownCommand', { command: argv.command }));
|
package/lib/bundle-runner.js
CHANGED
|
@@ -539,12 +539,22 @@ async function compilePlain(hermesCommand, out, input = out) {
|
|
|
539
539
|
async function composeSourceMaps(packagerMap, hermesMap, sourcemapOutput) {
|
|
540
540
|
let composerPath;
|
|
541
541
|
try {
|
|
542
|
-
// resolve through the project so hoisted node_modules (monorepos) work
|
|
543
|
-
|
|
542
|
+
// resolve through the project so hoisted node_modules (monorepos) work.
|
|
543
|
+
// Extensionless first: RN >= 0.87 maps "./scripts/*" to "./scripts/*.js"
|
|
544
|
+
// in its exports, so the ".js" specifier resolves to ".js.js" and
|
|
545
|
+
// fails; CJS resolution adds the extension itself on older RN.
|
|
546
|
+
composerPath = require.resolve('react-native/scripts/compose-source-maps', {
|
|
547
|
+
paths: [process.cwd()],
|
|
548
|
+
});
|
|
544
549
|
}
|
|
545
550
|
catch {
|
|
546
|
-
|
|
547
|
-
|
|
551
|
+
try {
|
|
552
|
+
composerPath = require.resolve('react-native/scripts/compose-source-maps.js', { paths: [process.cwd()] });
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
console.warn((0, i18n_1.t)('composeSourceMapsNotFound'));
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
548
558
|
}
|
|
549
559
|
console.log((0, i18n_1.t)('composingSourceMap'));
|
|
550
560
|
assertSuccessfulSyncProcess(await runProcess((0, runtime_1.getJavaScriptRuntime)(), [composerPath, packagerMap, hermesMap, '-o', sourcemapOutput], false), composerPath);
|
package/lib/locales/en.d.ts
CHANGED
package/lib/locales/en.js
CHANGED
|
@@ -24,6 +24,8 @@ exports.default = {
|
|
|
24
24
|
appPlatformMismatch: 'App {{appId}} is a {{appPlatform}} app, but this command targets {{platform}}',
|
|
25
25
|
appNotSelected: 'App not selected. run `cresc selectApp --platform {{platform}}` first!',
|
|
26
26
|
appUploadSuccess: 'Successfully uploaded APP native package (id: {{id}}, version: {{version}}, buildTime: {{buildTime}})',
|
|
27
|
+
autoUpdatePermission: 'CLI auto-update was skipped because the global package directory is not writable. Fix the package-manager prefix permissions, then run: {{- command}}',
|
|
28
|
+
autoUpdateSuccess: 'CLI was automatically updated in the background: {{from}} -> {{to}}',
|
|
27
29
|
apkUploadSuccess: 'Successfully uploaded APK native package (id: {{id}}, version: {{version}}, buildTime: {{buildTime}})',
|
|
28
30
|
boundTo: ', bound to: {{name}} ({{id}})',
|
|
29
31
|
buildTimeNotFound: 'Cannot get the build timestamp of this package. Please update `react-native-update` to the latest version and re-package and upload.',
|
package/lib/locales/zh.d.ts
CHANGED
package/lib/locales/zh.js
CHANGED
|
@@ -24,6 +24,8 @@ exports.default = {
|
|
|
24
24
|
appPlatformMismatch: '应用 {{appId}} 是 {{appPlatform}} 平台的应用,但当前命令的平台是 {{platform}}',
|
|
25
25
|
appNotSelected: '尚未选择应用。请先运行 `pushy selectApp --platform {{platform}}` 来选择应用',
|
|
26
26
|
appUploadSuccess: '已成功上传app原生包(id: {{id}}, version: {{version}}, buildTime: {{buildTime}})',
|
|
27
|
+
autoUpdatePermission: 'CLI 自动更新已跳过:全局包目录不可写。请修复包管理器的全局目录权限后运行:{{- command}}',
|
|
28
|
+
autoUpdateSuccess: 'CLI 已在后台自动更新:{{from}} -> {{to}}',
|
|
27
29
|
apkUploadSuccess: '已成功上传apk原生包(id: {{id}}, version: {{version}}, buildTime: {{buildTime}})',
|
|
28
30
|
boundTo: ', 已绑定:{{name}} ({{id}})',
|
|
29
31
|
buildTimeNotFound: '无法获取此包的编译时间戳。请更新 `react-native-update` 到最新版本后重新打包上传。',
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ export interface VersionCheck {
|
|
|
37
37
|
done: Promise<void>;
|
|
38
38
|
/** wait for the check, but never longer than `graceMs` */
|
|
39
39
|
settle: (graceMs: number) => Promise<void>;
|
|
40
|
+
/** launch a detached self-update after the foreground command is done */
|
|
41
|
+
startAutoUpdate: () => void;
|
|
40
42
|
/**
|
|
41
43
|
* Print the "newer version available" hints if the check has completed by
|
|
42
44
|
* now; a no-op while it is still pending, when nothing is newer, and after
|