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
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import {
|
|
3
|
+
accessSync,
|
|
4
|
+
constants,
|
|
5
|
+
existsSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
realpathSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
rmSync,
|
|
11
|
+
statSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from 'fs';
|
|
14
|
+
import { homedir } from 'os';
|
|
15
|
+
import path from 'path';
|
|
16
|
+
|
|
17
|
+
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
18
|
+
|
|
19
|
+
interface AutoUpdateCandidate {
|
|
20
|
+
currentVersion: string;
|
|
21
|
+
targetVersion: string;
|
|
22
|
+
packageRoot: string;
|
|
23
|
+
packageManager: PackageManager;
|
|
24
|
+
registryUrl: string;
|
|
25
|
+
installPrefix?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface AutoUpdateState extends AutoUpdateCandidate {
|
|
29
|
+
status: 'running' | 'updated' | 'failed';
|
|
30
|
+
attemptedAt: number;
|
|
31
|
+
finishedAt?: number;
|
|
32
|
+
retryAt?: number;
|
|
33
|
+
failureCount?: number;
|
|
34
|
+
failureKind?: 'network' | 'permission' | 'package-manager' | 'unknown';
|
|
35
|
+
message?: string;
|
|
36
|
+
reportedAt?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AutoUpdateNotice {
|
|
40
|
+
kind: 'updated' | 'permission';
|
|
41
|
+
currentVersion: string;
|
|
42
|
+
targetVersion: string;
|
|
43
|
+
command?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface PackageManagerDetectionOptions {
|
|
47
|
+
packageRoot: string;
|
|
48
|
+
env?: NodeJS.ProcessEnv;
|
|
49
|
+
npmGlobalDir?: string;
|
|
50
|
+
yarnGlobalDir?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const PACKAGE_NAME = 'react-native-update-cli';
|
|
54
|
+
const WORKER_ENV = 'RNU_AUTO_UPDATE_WORKER';
|
|
55
|
+
const CANDIDATE_ENV = 'RNU_AUTO_UPDATE_CANDIDATE';
|
|
56
|
+
const LOCK_STALE_MS = 10 * 60 * 1000;
|
|
57
|
+
const UPDATE_TIMEOUT_MS = 2 * 60 * 1000;
|
|
58
|
+
const NETWORK_RETRY_MS = 6 * 60 * 60 * 1000;
|
|
59
|
+
const FAILURE_RETRY_MS = 24 * 60 * 60 * 1000;
|
|
60
|
+
const PERMISSION_RETRY_MS = 7 * 24 * 60 * 60 * 1000;
|
|
61
|
+
const OUTPUT_LIMIT = 32 * 1024;
|
|
62
|
+
|
|
63
|
+
function cacheDir(): string {
|
|
64
|
+
if (process.env.XDG_CACHE_HOME) {
|
|
65
|
+
return path.join(process.env.XDG_CACHE_HOME, PACKAGE_NAME);
|
|
66
|
+
}
|
|
67
|
+
switch (process.platform) {
|
|
68
|
+
case 'darwin':
|
|
69
|
+
return path.join(homedir(), 'Library', 'Caches', PACKAGE_NAME);
|
|
70
|
+
case 'win32':
|
|
71
|
+
return path.join(
|
|
72
|
+
process.env.LOCALAPPDATA ?? path.join(homedir(), 'AppData', 'Local'),
|
|
73
|
+
PACKAGE_NAME,
|
|
74
|
+
'Cache',
|
|
75
|
+
);
|
|
76
|
+
default:
|
|
77
|
+
return path.join(homedir(), '.cache', PACKAGE_NAME);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function stateFile(): string {
|
|
82
|
+
return path.join(cacheDir(), 'auto-update.json');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function lockDir(): string {
|
|
86
|
+
return path.join(cacheDir(), 'auto-update.lock');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function readState(): AutoUpdateState | undefined {
|
|
90
|
+
try {
|
|
91
|
+
const parsed = JSON.parse(readFileSync(stateFile(), 'utf8'));
|
|
92
|
+
if (
|
|
93
|
+
!parsed ||
|
|
94
|
+
!['running', 'updated', 'failed'].includes(parsed.status) ||
|
|
95
|
+
typeof parsed.targetVersion !== 'string' ||
|
|
96
|
+
typeof parsed.attemptedAt !== 'number'
|
|
97
|
+
) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
return parsed as AutoUpdateState;
|
|
101
|
+
} catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function writeState(state: AutoUpdateState): void {
|
|
107
|
+
try {
|
|
108
|
+
mkdirSync(cacheDir(), { recursive: true });
|
|
109
|
+
const temporary = `${stateFile()}.${process.pid}.tmp`;
|
|
110
|
+
writeFileSync(temporary, JSON.stringify(state));
|
|
111
|
+
renameSync(temporary, stateFile());
|
|
112
|
+
} catch {
|
|
113
|
+
// A read-only cache must never interfere with the CLI or the update.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function normalizePath(value: string): string {
|
|
118
|
+
let resolved = path.resolve(value);
|
|
119
|
+
try {
|
|
120
|
+
resolved = realpathSync.native(resolved);
|
|
121
|
+
} catch {
|
|
122
|
+
// The caller may supply a path that was replaced during an update.
|
|
123
|
+
}
|
|
124
|
+
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function isWithin(child: string, parent: string): boolean {
|
|
128
|
+
const relative = path.relative(normalizePath(parent), normalizePath(child));
|
|
129
|
+
return (
|
|
130
|
+
relative === '' ||
|
|
131
|
+
(!relative.startsWith('..') && !path.isAbsolute(relative))
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function managerFromUserAgent(
|
|
136
|
+
env: NodeJS.ProcessEnv,
|
|
137
|
+
): PackageManager | undefined {
|
|
138
|
+
const value =
|
|
139
|
+
`${env.npm_config_user_agent ?? ''} ${env.npm_execpath ?? ''}`.toLowerCase();
|
|
140
|
+
if (/\bbun(?:\/|\s|$)/.test(value)) return 'bun';
|
|
141
|
+
if (/\bpnpm(?:\/|\s|$)/.test(value)) return 'pnpm';
|
|
142
|
+
if (/\byarn(?:\/|\s|$)/.test(value)) return 'yarn';
|
|
143
|
+
if (/\bnpm(?:\/|\s|$)/.test(value)) return 'npm';
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function parseManager(value: string | undefined): PackageManager | undefined {
|
|
148
|
+
const manager = value?.trim().toLowerCase();
|
|
149
|
+
return manager === 'npm' ||
|
|
150
|
+
manager === 'pnpm' ||
|
|
151
|
+
manager === 'yarn' ||
|
|
152
|
+
manager === 'bun'
|
|
153
|
+
? manager
|
|
154
|
+
: undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Identify the package manager that owns this global installation. Project
|
|
159
|
+
* lockfiles are deliberately ignored: they say how the app is installed, not
|
|
160
|
+
* how this CLI was installed.
|
|
161
|
+
*/
|
|
162
|
+
export function detectGlobalPackageManager({
|
|
163
|
+
packageRoot,
|
|
164
|
+
env = process.env,
|
|
165
|
+
npmGlobalDir,
|
|
166
|
+
yarnGlobalDir,
|
|
167
|
+
}: PackageManagerDetectionOptions): PackageManager | undefined {
|
|
168
|
+
const root = normalizePath(packageRoot);
|
|
169
|
+
const normalized = root.replaceAll('\\', '/').toLowerCase();
|
|
170
|
+
|
|
171
|
+
if (yarnGlobalDir && isWithin(root, yarnGlobalDir)) return 'yarn';
|
|
172
|
+
if (npmGlobalDir && isWithin(root, npmGlobalDir)) return 'npm';
|
|
173
|
+
if (/\/(?:\.bun|bun)\/install\/global\/node_modules\//.test(normalized)) {
|
|
174
|
+
return 'bun';
|
|
175
|
+
}
|
|
176
|
+
if (/\/pnpm\/global\/[^/]+\//.test(normalized)) return 'pnpm';
|
|
177
|
+
if (
|
|
178
|
+
/\/(?:\.config\/yarn\/global|yarn\/data\/global)\/node_modules\//.test(
|
|
179
|
+
normalized,
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
return 'yarn';
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Custom npm prefixes conventionally use <prefix>/lib/node_modules on
|
|
186
|
+
// Unix and <prefix>/node_modules on Windows. Only accept the generic form
|
|
187
|
+
// when the package is outside the current project, so npx and local installs
|
|
188
|
+
// are never self-modified.
|
|
189
|
+
const cwdNodeModules = path.join(
|
|
190
|
+
normalizePath(process.cwd()),
|
|
191
|
+
'node_modules',
|
|
192
|
+
);
|
|
193
|
+
const configuredPrefixModules = env.npm_config_prefix
|
|
194
|
+
? path.join(normalizePath(env.npm_config_prefix), 'node_modules')
|
|
195
|
+
: undefined;
|
|
196
|
+
const looksGlobal =
|
|
197
|
+
normalized.includes('/lib/node_modules/') ||
|
|
198
|
+
(process.platform === 'win32' &&
|
|
199
|
+
(normalized.includes('/npm/node_modules/') ||
|
|
200
|
+
(configuredPrefixModules && isWithin(root, configuredPrefixModules))));
|
|
201
|
+
if (looksGlobal && !isWithin(root, cwdNodeModules)) {
|
|
202
|
+
return (
|
|
203
|
+
parseManager(env.RNU_AUTO_UPDATE_PACKAGE_MANAGER) ??
|
|
204
|
+
managerFromUserAgent(env) ??
|
|
205
|
+
'npm'
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function globalDirectories(): {
|
|
212
|
+
npmGlobalDir?: string;
|
|
213
|
+
npmPrefix?: string;
|
|
214
|
+
yarnGlobalDir?: string;
|
|
215
|
+
} {
|
|
216
|
+
try {
|
|
217
|
+
const dirs = require('global-dirs') as typeof import('global-dirs');
|
|
218
|
+
return {
|
|
219
|
+
npmGlobalDir: dirs.npm?.packages,
|
|
220
|
+
npmPrefix: dirs.npm?.prefix,
|
|
221
|
+
yarnGlobalDir: dirs.yarn?.packages,
|
|
222
|
+
};
|
|
223
|
+
} catch {
|
|
224
|
+
return {};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function sanitizeRegistryUrl(value: string): string {
|
|
229
|
+
try {
|
|
230
|
+
const registry = new URL(value);
|
|
231
|
+
if (registry.protocol !== 'http:' && registry.protocol !== 'https:') {
|
|
232
|
+
return 'https://registry.npmjs.org/';
|
|
233
|
+
}
|
|
234
|
+
// Credentials belong in the user's package-manager configuration, not in
|
|
235
|
+
// a process argument or our state file.
|
|
236
|
+
registry.username = '';
|
|
237
|
+
registry.password = '';
|
|
238
|
+
return registry.toString();
|
|
239
|
+
} catch {
|
|
240
|
+
return 'https://registry.npmjs.org/';
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function buildUpdateCommand(
|
|
245
|
+
manager: PackageManager,
|
|
246
|
+
targetVersion: string,
|
|
247
|
+
registryUrl: string,
|
|
248
|
+
installPrefix?: string,
|
|
249
|
+
): { command: string; args: string[] } {
|
|
250
|
+
const target = `${PACKAGE_NAME}@${targetVersion}`;
|
|
251
|
+
const registry = sanitizeRegistryUrl(registryUrl);
|
|
252
|
+
// Windows package managers are .cmd shims and therefore run through
|
|
253
|
+
// cmd.exe. Keep the URL out of that command line (it may contain shell
|
|
254
|
+
// metacharacters); runPackageManager supplies the same value through the
|
|
255
|
+
// environment instead.
|
|
256
|
+
const registryArgs =
|
|
257
|
+
process.platform === 'win32' ? [] : [`--registry=${registry}`];
|
|
258
|
+
switch (manager) {
|
|
259
|
+
case 'pnpm':
|
|
260
|
+
return {
|
|
261
|
+
command: process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm',
|
|
262
|
+
args: ['add', '--global', target, '--reporter=silent', ...registryArgs],
|
|
263
|
+
};
|
|
264
|
+
case 'yarn':
|
|
265
|
+
return {
|
|
266
|
+
command: process.platform === 'win32' ? 'yarn.cmd' : 'yarn',
|
|
267
|
+
args: [
|
|
268
|
+
'global',
|
|
269
|
+
'add',
|
|
270
|
+
target,
|
|
271
|
+
'--non-interactive',
|
|
272
|
+
'--silent',
|
|
273
|
+
...registryArgs,
|
|
274
|
+
],
|
|
275
|
+
};
|
|
276
|
+
case 'bun':
|
|
277
|
+
return {
|
|
278
|
+
command: process.platform === 'win32' ? 'bun.exe' : 'bun',
|
|
279
|
+
args: ['add', '--global', target, '--no-progress', ...registryArgs],
|
|
280
|
+
};
|
|
281
|
+
default:
|
|
282
|
+
return {
|
|
283
|
+
command: process.platform === 'win32' ? 'npm.cmd' : 'npm',
|
|
284
|
+
args: [
|
|
285
|
+
'install',
|
|
286
|
+
'--global',
|
|
287
|
+
target,
|
|
288
|
+
...(installPrefix && process.platform !== 'win32'
|
|
289
|
+
? [`--prefix=${installPrefix}`]
|
|
290
|
+
: []),
|
|
291
|
+
'--no-audit',
|
|
292
|
+
'--no-fund',
|
|
293
|
+
'--loglevel=error',
|
|
294
|
+
...registryArgs,
|
|
295
|
+
],
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function inferNpmPrefix(
|
|
301
|
+
packageRoot: string,
|
|
302
|
+
npmGlobalDir?: string,
|
|
303
|
+
npmPrefix?: string,
|
|
304
|
+
): string | undefined {
|
|
305
|
+
if (npmGlobalDir && npmPrefix && isWithin(packageRoot, npmGlobalDir)) {
|
|
306
|
+
return normalizePath(npmPrefix);
|
|
307
|
+
}
|
|
308
|
+
const modules = path.dirname(normalizePath(packageRoot));
|
|
309
|
+
if (path.basename(modules).toLowerCase() !== 'node_modules') {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
const modulesParent = path.dirname(modules);
|
|
313
|
+
return path.basename(modulesParent).toLowerCase() === 'lib'
|
|
314
|
+
? path.dirname(modulesParent)
|
|
315
|
+
: modulesParent;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function isDisabled(env: NodeJS.ProcessEnv): boolean {
|
|
319
|
+
const disabled = (value: string | undefined) =>
|
|
320
|
+
value === '0' || value?.toLowerCase() === 'false';
|
|
321
|
+
const enabled = (value: string | undefined) =>
|
|
322
|
+
value === '1' || value?.toLowerCase() === 'true';
|
|
323
|
+
return (
|
|
324
|
+
disabled(env.RNU_AUTO_UPDATE) ||
|
|
325
|
+
enabled(env.RNU_DISABLE_AUTO_UPDATE) ||
|
|
326
|
+
enabled(env.NO_UPDATE_NOTIFIER) ||
|
|
327
|
+
enabled(env.CI)
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function shouldRetry(
|
|
332
|
+
state: AutoUpdateState | undefined,
|
|
333
|
+
target: string,
|
|
334
|
+
): boolean {
|
|
335
|
+
if (!state || state.targetVersion !== target) return true;
|
|
336
|
+
if (state.status === 'updated') return false;
|
|
337
|
+
if (state.status === 'running') {
|
|
338
|
+
return Date.now() - state.attemptedAt >= LOCK_STALE_MS;
|
|
339
|
+
}
|
|
340
|
+
return !state.retryAt || state.retryAt <= Date.now();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Start a detached updater after the foreground command has completed. This
|
|
345
|
+
* function is synchronous on purpose: it is safe to call from process.exit.
|
|
346
|
+
*/
|
|
347
|
+
export function launchAutoUpdate(
|
|
348
|
+
currentVersion: string,
|
|
349
|
+
targetVersion: string | undefined,
|
|
350
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
351
|
+
): boolean {
|
|
352
|
+
if (
|
|
353
|
+
!targetVersion ||
|
|
354
|
+
targetVersion === currentVersion ||
|
|
355
|
+
isDisabled(env) ||
|
|
356
|
+
!shouldRetry(readState(), targetVersion)
|
|
357
|
+
) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const packageRoot = path.resolve(__dirname, '..');
|
|
362
|
+
const directories = globalDirectories();
|
|
363
|
+
const detected = detectGlobalPackageManager({
|
|
364
|
+
packageRoot,
|
|
365
|
+
env,
|
|
366
|
+
npmGlobalDir: directories.npmGlobalDir,
|
|
367
|
+
yarnGlobalDir: directories.yarnGlobalDir,
|
|
368
|
+
});
|
|
369
|
+
if (!detected) return false;
|
|
370
|
+
const packageManager =
|
|
371
|
+
parseManager(env.RNU_AUTO_UPDATE_PACKAGE_MANAGER) ?? detected;
|
|
372
|
+
|
|
373
|
+
let registryUrl = 'https://registry.npmjs.org/';
|
|
374
|
+
try {
|
|
375
|
+
const registryModule = require('registry-auth-token/registry-url');
|
|
376
|
+
const getRegistryUrl = (registryModule.default ?? registryModule) as (
|
|
377
|
+
scope?: string,
|
|
378
|
+
) => string;
|
|
379
|
+
registryUrl = getRegistryUrl('');
|
|
380
|
+
} catch {
|
|
381
|
+
// Keep the public npm registry fallback when config parsing fails.
|
|
382
|
+
}
|
|
383
|
+
const candidate: AutoUpdateCandidate = {
|
|
384
|
+
currentVersion,
|
|
385
|
+
targetVersion,
|
|
386
|
+
packageRoot: normalizePath(packageRoot),
|
|
387
|
+
packageManager,
|
|
388
|
+
registryUrl: sanitizeRegistryUrl(registryUrl),
|
|
389
|
+
installPrefix:
|
|
390
|
+
packageManager === 'npm' && detected === 'npm'
|
|
391
|
+
? inferNpmPrefix(
|
|
392
|
+
packageRoot,
|
|
393
|
+
directories.npmGlobalDir,
|
|
394
|
+
directories.npmPrefix,
|
|
395
|
+
)
|
|
396
|
+
: undefined,
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
try {
|
|
400
|
+
const worker = spawn(process.execPath, [__filename], {
|
|
401
|
+
detached: true,
|
|
402
|
+
env: {
|
|
403
|
+
...env,
|
|
404
|
+
[WORKER_ENV]: '1',
|
|
405
|
+
[CANDIDATE_ENV]: JSON.stringify(candidate),
|
|
406
|
+
},
|
|
407
|
+
stdio: 'ignore',
|
|
408
|
+
windowsHide: true,
|
|
409
|
+
});
|
|
410
|
+
worker.once('error', () => {});
|
|
411
|
+
worker.unref();
|
|
412
|
+
return true;
|
|
413
|
+
} catch {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function acquireLock(): boolean {
|
|
419
|
+
try {
|
|
420
|
+
mkdirSync(cacheDir(), { recursive: true });
|
|
421
|
+
try {
|
|
422
|
+
mkdirSync(lockDir());
|
|
423
|
+
return true;
|
|
424
|
+
} catch {
|
|
425
|
+
if (
|
|
426
|
+
existsSync(lockDir()) &&
|
|
427
|
+
Date.now() - statSync(lockDir()).mtimeMs >= LOCK_STALE_MS
|
|
428
|
+
) {
|
|
429
|
+
rmSync(lockDir(), { force: true, recursive: true });
|
|
430
|
+
mkdirSync(lockDir());
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
} catch {
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function appendBounded(current: string, chunk: Buffer | string): string {
|
|
441
|
+
const combined = `${current}${chunk.toString()}`;
|
|
442
|
+
return combined.length > OUTPUT_LIMIT
|
|
443
|
+
? combined.slice(combined.length - OUTPUT_LIMIT)
|
|
444
|
+
: combined;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function classifyFailure(
|
|
448
|
+
error: string,
|
|
449
|
+
): NonNullable<AutoUpdateState['failureKind']> {
|
|
450
|
+
if (/eacces|eperm|permission denied|access is denied/i.test(error)) {
|
|
451
|
+
return 'permission';
|
|
452
|
+
}
|
|
453
|
+
if (
|
|
454
|
+
/econn|enet|eai_again|enotfound|timed?\s*out|timeout|certificate|socket|network|fetch/i.test(
|
|
455
|
+
error,
|
|
456
|
+
)
|
|
457
|
+
) {
|
|
458
|
+
return 'network';
|
|
459
|
+
}
|
|
460
|
+
if (/enoent|not recognized|command not found/i.test(error)) {
|
|
461
|
+
return 'package-manager';
|
|
462
|
+
}
|
|
463
|
+
return 'unknown';
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function retryDelay(kind: NonNullable<AutoUpdateState['failureKind']>): number {
|
|
467
|
+
if (kind === 'permission') return PERMISSION_RETRY_MS;
|
|
468
|
+
if (kind === 'network') return NETWORK_RETRY_MS;
|
|
469
|
+
return FAILURE_RETRY_MS;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function compactMessage(value: string): string {
|
|
473
|
+
return value
|
|
474
|
+
.split(String.fromCharCode(27))
|
|
475
|
+
.map((part, index) =>
|
|
476
|
+
index === 0 ? part : part.replace(/^\[[0-9;]*m/, ''),
|
|
477
|
+
)
|
|
478
|
+
.join('')
|
|
479
|
+
.trim()
|
|
480
|
+
.slice(-2000);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async function runPackageManager(
|
|
484
|
+
command: string,
|
|
485
|
+
args: string[],
|
|
486
|
+
registryUrl: string,
|
|
487
|
+
installPrefix?: string,
|
|
488
|
+
): Promise<{ code: number; output: string }> {
|
|
489
|
+
return new Promise((resolve) => {
|
|
490
|
+
let output = '';
|
|
491
|
+
let settled = false;
|
|
492
|
+
const child = spawn(command, args, {
|
|
493
|
+
env: {
|
|
494
|
+
...process.env,
|
|
495
|
+
npm_config_audit: 'false',
|
|
496
|
+
npm_config_fund: 'false',
|
|
497
|
+
...(installPrefix
|
|
498
|
+
? {
|
|
499
|
+
NPM_CONFIG_PREFIX: installPrefix,
|
|
500
|
+
npm_config_prefix: installPrefix,
|
|
501
|
+
}
|
|
502
|
+
: {}),
|
|
503
|
+
npm_config_registry: sanitizeRegistryUrl(registryUrl),
|
|
504
|
+
npm_config_update_notifier: 'false',
|
|
505
|
+
},
|
|
506
|
+
shell: process.platform === 'win32' && command.endsWith('.cmd'),
|
|
507
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
508
|
+
windowsHide: true,
|
|
509
|
+
});
|
|
510
|
+
const finish = (code: number, extra = '') => {
|
|
511
|
+
if (settled) return;
|
|
512
|
+
settled = true;
|
|
513
|
+
clearTimeout(timer);
|
|
514
|
+
resolve({ code, output: appendBounded(output, extra) });
|
|
515
|
+
};
|
|
516
|
+
child.stdout?.on('data', (chunk) => {
|
|
517
|
+
output = appendBounded(output, chunk);
|
|
518
|
+
});
|
|
519
|
+
child.stderr?.on('data', (chunk) => {
|
|
520
|
+
output = appendBounded(output, chunk);
|
|
521
|
+
});
|
|
522
|
+
child.once('error', (error) => finish(127, error.message));
|
|
523
|
+
child.once('close', (code, signal) =>
|
|
524
|
+
finish(code ?? 1, signal ? `\nTerminated by ${signal}` : ''),
|
|
525
|
+
);
|
|
526
|
+
const timer = setTimeout(() => {
|
|
527
|
+
child.kill('SIGTERM');
|
|
528
|
+
finish(124, '\nUpdate timed out');
|
|
529
|
+
}, UPDATE_TIMEOUT_MS);
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function runWorker(candidate: AutoUpdateCandidate): Promise<void> {
|
|
534
|
+
if (!acquireLock()) return;
|
|
535
|
+
const previous = readState();
|
|
536
|
+
const running: AutoUpdateState = {
|
|
537
|
+
...candidate,
|
|
538
|
+
status: 'running',
|
|
539
|
+
attemptedAt: Date.now(),
|
|
540
|
+
failureCount:
|
|
541
|
+
previous?.targetVersion === candidate.targetVersion
|
|
542
|
+
? (previous.failureCount ?? 0) + 1
|
|
543
|
+
: 1,
|
|
544
|
+
};
|
|
545
|
+
writeState(running);
|
|
546
|
+
|
|
547
|
+
try {
|
|
548
|
+
if (process.platform !== 'win32') {
|
|
549
|
+
accessSync(path.dirname(candidate.packageRoot), constants.W_OK);
|
|
550
|
+
}
|
|
551
|
+
} catch (error) {
|
|
552
|
+
const failureKind = 'permission' as const;
|
|
553
|
+
writeState({
|
|
554
|
+
...running,
|
|
555
|
+
status: 'failed',
|
|
556
|
+
finishedAt: Date.now(),
|
|
557
|
+
retryAt: Date.now() + retryDelay(failureKind),
|
|
558
|
+
failureKind,
|
|
559
|
+
message: compactMessage(String(error)),
|
|
560
|
+
});
|
|
561
|
+
rmSync(lockDir(), { force: true, recursive: true });
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const update = buildUpdateCommand(
|
|
566
|
+
candidate.packageManager,
|
|
567
|
+
candidate.targetVersion,
|
|
568
|
+
candidate.registryUrl,
|
|
569
|
+
candidate.installPrefix,
|
|
570
|
+
);
|
|
571
|
+
const result = await runPackageManager(
|
|
572
|
+
update.command,
|
|
573
|
+
update.args,
|
|
574
|
+
candidate.registryUrl,
|
|
575
|
+
candidate.installPrefix,
|
|
576
|
+
);
|
|
577
|
+
if (result.code === 0) {
|
|
578
|
+
writeState({
|
|
579
|
+
...running,
|
|
580
|
+
status: 'updated',
|
|
581
|
+
finishedAt: Date.now(),
|
|
582
|
+
message: undefined,
|
|
583
|
+
});
|
|
584
|
+
} else {
|
|
585
|
+
const detail = compactMessage(result.output);
|
|
586
|
+
const failureKind = classifyFailure(detail);
|
|
587
|
+
writeState({
|
|
588
|
+
...running,
|
|
589
|
+
status: 'failed',
|
|
590
|
+
finishedAt: Date.now(),
|
|
591
|
+
retryAt: Date.now() + retryDelay(failureKind),
|
|
592
|
+
failureKind,
|
|
593
|
+
message: detail,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
rmSync(lockDir(), { force: true, recursive: true });
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
export function consumeAutoUpdateNotice(
|
|
600
|
+
currentVersion: string,
|
|
601
|
+
): AutoUpdateNotice | undefined {
|
|
602
|
+
const state = readState();
|
|
603
|
+
if (!state || state.reportedAt) return undefined;
|
|
604
|
+
if (state.status === 'updated' && currentVersion === state.targetVersion) {
|
|
605
|
+
writeState({ ...state, reportedAt: Date.now() });
|
|
606
|
+
return {
|
|
607
|
+
kind: 'updated',
|
|
608
|
+
currentVersion: state.currentVersion,
|
|
609
|
+
targetVersion: state.targetVersion,
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
if (state.status === 'failed' && state.failureKind === 'permission') {
|
|
613
|
+
writeState({ ...state, reportedAt: Date.now() });
|
|
614
|
+
const update = buildUpdateCommand(
|
|
615
|
+
state.packageManager,
|
|
616
|
+
state.targetVersion,
|
|
617
|
+
state.registryUrl,
|
|
618
|
+
state.installPrefix,
|
|
619
|
+
);
|
|
620
|
+
return {
|
|
621
|
+
kind: 'permission',
|
|
622
|
+
currentVersion,
|
|
623
|
+
targetVersion: state.targetVersion,
|
|
624
|
+
command: [update.command, ...update.args].join(' '),
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
return undefined;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
if (process.env[WORKER_ENV] === '1') {
|
|
631
|
+
try {
|
|
632
|
+
const candidate = JSON.parse(
|
|
633
|
+
process.env[CANDIDATE_ENV] ?? '',
|
|
634
|
+
) as AutoUpdateCandidate;
|
|
635
|
+
if (
|
|
636
|
+
candidate?.packageRoot &&
|
|
637
|
+
candidate.targetVersion &&
|
|
638
|
+
parseManager(candidate.packageManager)
|
|
639
|
+
) {
|
|
640
|
+
void runWorker(candidate).catch(() => {
|
|
641
|
+
rmSync(lockDir(), { force: true, recursive: true });
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
} catch {
|
|
645
|
+
// Malformed worker input is ignored; foreground CLI operation is over.
|
|
646
|
+
}
|
|
647
|
+
}
|
package/src/bin.ts
CHANGED
|
@@ -67,11 +67,13 @@ async function run() {
|
|
|
67
67
|
// exit on their own.
|
|
68
68
|
const versionCheck = await printVersionCommand({ wait: versionOnly });
|
|
69
69
|
if (versionOnly) {
|
|
70
|
+
versionCheck.startAutoUpdate();
|
|
70
71
|
process.exit();
|
|
71
72
|
}
|
|
72
73
|
process.on('exit', (code) => {
|
|
73
74
|
if (code === 0) {
|
|
74
75
|
versionCheck.printHints();
|
|
76
|
+
versionCheck.startAutoUpdate();
|
|
75
77
|
}
|
|
76
78
|
});
|
|
77
79
|
|
|
@@ -96,6 +98,7 @@ async function run() {
|
|
|
96
98
|
// registry request itself is unref'd, so exiting never waits on it
|
|
97
99
|
await versionCheck.settle(500);
|
|
98
100
|
versionCheck.printHints();
|
|
101
|
+
versionCheck.startAutoUpdate();
|
|
99
102
|
} else {
|
|
100
103
|
throw new Error(t('unknownCommand', { command: argv.command }));
|
|
101
104
|
}
|
package/src/bundle-runner.ts
CHANGED
|
@@ -766,14 +766,23 @@ async function composeSourceMaps(
|
|
|
766
766
|
): Promise<boolean> {
|
|
767
767
|
let composerPath: string;
|
|
768
768
|
try {
|
|
769
|
-
// resolve through the project so hoisted node_modules (monorepos) work
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
769
|
+
// resolve through the project so hoisted node_modules (monorepos) work.
|
|
770
|
+
// Extensionless first: RN >= 0.87 maps "./scripts/*" to "./scripts/*.js"
|
|
771
|
+
// in its exports, so the ".js" specifier resolves to ".js.js" and
|
|
772
|
+
// fails; CJS resolution adds the extension itself on older RN.
|
|
773
|
+
composerPath = require.resolve('react-native/scripts/compose-source-maps', {
|
|
774
|
+
paths: [process.cwd()],
|
|
775
|
+
});
|
|
774
776
|
} catch {
|
|
775
|
-
|
|
776
|
-
|
|
777
|
+
try {
|
|
778
|
+
composerPath = require.resolve(
|
|
779
|
+
'react-native/scripts/compose-source-maps.js',
|
|
780
|
+
{ paths: [process.cwd()] },
|
|
781
|
+
);
|
|
782
|
+
} catch {
|
|
783
|
+
console.warn(t('composeSourceMapsNotFound'));
|
|
784
|
+
return false;
|
|
785
|
+
}
|
|
777
786
|
}
|
|
778
787
|
console.log(t('composingSourceMap'));
|
|
779
788
|
assertSuccessfulSyncProcess(
|
package/src/locales/en.ts
CHANGED
|
@@ -35,6 +35,10 @@ export default {
|
|
|
35
35
|
'App not selected. run `cresc selectApp --platform {{platform}}` first!',
|
|
36
36
|
appUploadSuccess:
|
|
37
37
|
'Successfully uploaded APP native package (id: {{id}}, version: {{version}}, buildTime: {{buildTime}})',
|
|
38
|
+
autoUpdatePermission:
|
|
39
|
+
'CLI auto-update was skipped because the global package directory is not writable. Fix the package-manager prefix permissions, then run: {{- command}}',
|
|
40
|
+
autoUpdateSuccess:
|
|
41
|
+
'CLI was automatically updated in the background: {{from}} -> {{to}}',
|
|
38
42
|
apkUploadSuccess:
|
|
39
43
|
'Successfully uploaded APK native package (id: {{id}}, version: {{version}}, buildTime: {{buildTime}})',
|
|
40
44
|
boundTo: ', bound to: {{name}} ({{id}})',
|