react-native-update-cli 2.24.2 → 2.26.0
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 +12 -5
- package/README.zh-CN.md +12 -5
- package/cli.json +7 -1
- package/lib/api.d.ts +2 -0
- package/lib/api.js +63 -18
- package/lib/app.js +25 -3
- package/lib/bin.js +39 -45
- package/lib/bundle-runner.d.ts +45 -1
- package/lib/bundle-runner.js +303 -34
- package/lib/bundle.d.ts +1 -1
- package/lib/bundle.js +23 -7
- package/lib/commands.d.ts +6 -0
- package/lib/commands.js +65 -0
- package/lib/locales/en.d.ts +7 -14
- package/lib/locales/en.js +10 -17
- package/lib/locales/zh.d.ts +7 -13
- package/lib/locales/zh.js +10 -16
- package/lib/package.js +4 -0
- package/lib/provider.d.ts +1 -1
- package/lib/provider.js +3 -1
- package/lib/symbolicate.js +7 -2
- package/lib/types.d.ts +1 -0
- package/lib/user.js +6 -0
- package/lib/utils/add-gitignore.js +7 -1
- package/lib/utils/dep-versions.d.ts +7 -0
- package/lib/utils/dep-versions.js +78 -26
- package/lib/utils/git.js +9 -6
- package/lib/utils/hbcTransform.d.ts +15 -0
- package/lib/utils/hbcTransform.js +22 -0
- package/lib/utils/hermes-base.d.ts +61 -6
- package/lib/utils/hermes-base.js +328 -46
- package/lib/utils/hermes-literals.d.ts +51 -0
- package/lib/utils/hermes-literals.js +210 -0
- package/lib/utils/i18n.d.ts +5 -0
- package/lib/utils/i18n.js +14 -1
- package/lib/utils/index.d.ts +2 -1
- package/lib/utils/index.js +9 -4
- package/lib/utils/latest-version/index.js +8 -0
- package/lib/utils/runtime.d.ts +21 -0
- package/lib/utils/runtime.js +138 -5
- package/lib/utils/zip-range.js +3 -2
- package/lib/versions.js +14 -4
- package/package.json +6 -3
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -17
- package/src/api.ts +0 -470
- package/src/app.ts +0 -241
- package/src/auto-update.ts +0 -647
- package/src/bin-cresc.ts +0 -8
- package/src/bin.ts +0 -115
- package/src/bundle-pack.ts +0 -61
- package/src/bundle-runner.ts +0 -1430
- package/src/bundle.ts +0 -371
- package/src/cache.ts +0 -20
- package/src/diff.ts +0 -1010
- package/src/exports.ts +0 -17
- package/src/index.ts +0 -1
- package/src/install.ts +0 -50
- package/src/locales/en.ts +0 -257
- package/src/locales/zh.ts +0 -234
- package/src/native-package.ts +0 -338
- package/src/package.ts +0 -482
- package/src/provider.ts +0 -294
- package/src/symbolicate.ts +0 -164
- package/src/types.ts +0 -150
- package/src/user.ts +0 -39
- package/src/utils/add-gitignore.ts +0 -35
- package/src/utils/app-info-parser/aab.ts +0 -241
- package/src/utils/app-info-parser/apk.ts +0 -92
- package/src/utils/app-info-parser/app.ts +0 -7
- package/src/utils/app-info-parser/index.ts +0 -54
- package/src/utils/app-info-parser/ipa.ts +0 -109
- package/src/utils/app-info-parser/resource-finder.ts +0 -508
- package/src/utils/app-info-parser/utils.ts +0 -161
- package/src/utils/app-info-parser/xml-parser/binary.ts +0 -693
- package/src/utils/app-info-parser/xml-parser/manifest.ts +0 -228
- package/src/utils/app-info-parser/zip.ts +0 -108
- package/src/utils/check-lockfile.ts +0 -93
- package/src/utils/check-plugin.ts +0 -38
- package/src/utils/command-result.ts +0 -24
- package/src/utils/constants.ts +0 -25
- package/src/utils/dep-versions.ts +0 -83
- package/src/utils/git.ts +0 -71
- package/src/utils/hbcTransform.ts +0 -649
- package/src/utils/hermes-base.ts +0 -1137
- package/src/utils/http-helper.ts +0 -152
- package/src/utils/i18n.ts +0 -39
- package/src/utils/index.ts +0 -698
- package/src/utils/latest-version/index.ts +0 -573
- package/src/utils/options.ts +0 -78
- package/src/utils/plugin-config.ts +0 -32
- package/src/utils/runtime.ts +0 -187
- package/src/utils/slim-sourcemap.ts +0 -105
- package/src/utils/zip-entries.ts +0 -209
- package/src/utils/zip-options.ts +0 -178
- package/src/utils/zip-range.ts +0 -727
- package/src/utils/zip-raw-writer.ts +0 -247
- package/src/versions.ts +0 -892
package/src/auto-update.ts
DELETED
|
@@ -1,647 +0,0 @@
|
|
|
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-cresc.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// Dedicated entry for the `cresc` bin so brand detection does not depend on
|
|
4
|
-
// process.argv[1], which on Windows npm shims always points to the js file
|
|
5
|
-
// instead of the invoked command name.
|
|
6
|
-
process.env.RNU_BRAND = 'cresc';
|
|
7
|
-
|
|
8
|
-
require('./bin');
|