react-native-update-cli 2.23.0 → 2.24.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/src/app.ts CHANGED
@@ -2,6 +2,7 @@ import fs from 'fs';
2
2
  import { doDelete, get, post } from './api';
3
3
  import type { Platform } from './types';
4
4
  import { loadTtyTable, question } from './utils';
5
+ import { updateJson } from './utils/constants';
5
6
  import { t } from './utils/i18n';
6
7
 
7
8
  interface AppSummary {
@@ -10,12 +11,29 @@ interface AppSummary {
10
11
  platform: Platform;
11
12
  }
12
13
 
14
+ export interface AppTargetOptions {
15
+ appId?: string;
16
+ config?: string;
17
+ platform?: Platform | '';
18
+ }
19
+
20
+ /** The selected-app config file was missing or has no entry for the platform. */
21
+ export class AppNotSelectedError extends Error {
22
+ readonly code = 'APP_NOT_SELECTED';
23
+ constructor(platform: Platform) {
24
+ super(t('appNotSelected', { platform }));
25
+ this.name = 'AppNotSelectedError';
26
+ }
27
+ }
28
+
29
+ /** Resolve an explicit platform or prompt for one interactively. */
13
30
  export async function getPlatform(platform?: string) {
14
31
  return assertPlatform(
15
32
  platform || (await question(t('platformQuestion'))),
16
33
  ) as Platform;
17
34
  }
18
35
 
36
+ /** Validate that a string names a platform supported by the update service. */
19
37
  export function assertPlatform(platform: string): Platform {
20
38
  if (platform !== 'ios' && platform !== 'android' && platform !== 'harmony') {
21
39
  throw new Error(t('unsupportedPlatform', { platform }));
@@ -23,27 +41,34 @@ export function assertPlatform(platform: string): Platform {
23
41
  return platform as Platform;
24
42
  }
25
43
 
44
+ /** Read the selected app for a platform from the requested config file. */
26
45
  export async function getSelectedApp(
27
46
  platform: Platform,
28
47
  configPath?: string,
29
48
  ): Promise<{ appId: string; appKey: string; platform: Platform }> {
30
49
  assertPlatform(platform);
31
50
 
32
- let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
33
- {};
51
+ const resolvedConfigPath = configPath || updateJson;
52
+ let raw: string;
34
53
  try {
35
- updateInfo = JSON.parse(
36
- await fs.promises.readFile(configPath || 'update.json', 'utf8'),
37
- );
54
+ raw = await fs.promises.readFile(resolvedConfigPath, 'utf8');
38
55
  } catch (e: any) {
39
56
  if (e.code === 'ENOENT') {
40
- throw new Error(t('appNotSelected', { platform }));
57
+ throw new AppNotSelectedError(platform);
41
58
  }
42
59
  throw e;
43
60
  }
61
+ let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>>;
62
+ try {
63
+ updateInfo = JSON.parse(raw);
64
+ } catch {
65
+ throw new Error(
66
+ t('failedToParseUpdateJson', { configPath: resolvedConfigPath }),
67
+ );
68
+ }
44
69
  const info = updateInfo[platform];
45
70
  if (!info) {
46
- throw new Error(t('appNotSelected', { platform }));
71
+ throw new AppNotSelectedError(platform);
47
72
  }
48
73
  return {
49
74
  appId: String(info.appId),
@@ -52,6 +77,45 @@ export async function getSelectedApp(
52
77
  };
53
78
  }
54
79
 
80
+ /**
81
+ * Fail fast when an explicit `--appId` names an app of another platform.
82
+ * The server accepts a bundle for any app the account owns and never sees
83
+ * the platform it was built for, so this is the only place the mistake can
84
+ * be caught before it reaches devices. A missing or foreign app fails here
85
+ * too (403/404) instead of after the expensive work.
86
+ */
87
+ async function assertAppPlatform(appId: string, platform: Platform) {
88
+ const app = (await get(`/app/${appId}`)) as { platform?: Platform };
89
+ if (app.platform && app.platform !== platform) {
90
+ throw new Error(
91
+ t('appPlatformMismatch', { appId, appPlatform: app.platform, platform }),
92
+ );
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Resolve the app an operation targets: an explicit `--appId` wins, otherwise
98
+ * the app selected for the platform in `--config` (default: update.json).
99
+ * Prompts for the platform only when it is needed and not given.
100
+ */
101
+ export async function resolveAppId(
102
+ options: AppTargetOptions = {},
103
+ ): Promise<string> {
104
+ if (options.platform) {
105
+ assertPlatform(options.platform);
106
+ }
107
+ if (options.appId) {
108
+ const appId = String(options.appId);
109
+ if (options.platform) {
110
+ await assertAppPlatform(appId, options.platform);
111
+ }
112
+ return appId;
113
+ }
114
+ const platform = await getPlatform(options.platform || undefined);
115
+ return (await getSelectedApp(platform, options.config)).appId;
116
+ }
117
+
118
+ /** List apps, optionally filtering them to one platform. */
55
119
  export async function listApp(platform: Platform | '' = '') {
56
120
  const { data } = await get('/app/list');
57
121
  const allApps = data as AppSummary[];
@@ -77,6 +141,7 @@ export async function listApp(platform: Platform | '' = '') {
77
141
  return list;
78
142
  }
79
143
 
144
+ /** Prompt until the user chooses an app belonging to the target platform. */
80
145
  export async function chooseApp(platform: Platform) {
81
146
  const list = await listApp(platform);
82
147
 
@@ -89,28 +154,27 @@ export async function chooseApp(platform: Platform) {
89
154
  }
90
155
  }
91
156
 
157
+ /** Persist an app selection in the requested brand-aware config file. */
92
158
  async function selectApp({
93
159
  args,
94
160
  options,
95
161
  }: {
96
162
  args: string[];
97
- options: { platform?: Platform | '' };
163
+ options: { platform?: Platform | ''; config?: string };
98
164
  }) {
99
165
  const platform = await getPlatform(options.platform);
100
166
  const id = args[0]
101
167
  ? Number.parseInt(args[0], 10)
102
168
  : (await chooseApp(platform)).id;
103
169
 
104
- const configPath = (options as any).config as string | undefined;
170
+ const configPath = options.config || updateJson;
105
171
  let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
106
172
  {};
107
173
  try {
108
- updateInfo = JSON.parse(
109
- await fs.promises.readFile(configPath || 'update.json', 'utf8'),
110
- );
174
+ updateInfo = JSON.parse(await fs.promises.readFile(configPath, 'utf8'));
111
175
  } catch (e: any) {
112
176
  if (e.code !== 'ENOENT') {
113
- console.error(t('failedToParseUpdateJson'));
177
+ console.error(t('failedToParseUpdateJson', { configPath }));
114
178
  throw e;
115
179
  }
116
180
  }
@@ -120,18 +184,25 @@ async function selectApp({
120
184
  appKey,
121
185
  };
122
186
  await fs.promises.writeFile(
123
- configPath || 'update.json',
187
+ configPath,
124
188
  JSON.stringify(updateInfo, null, 4),
125
189
  'utf8',
126
190
  );
127
191
  }
128
192
 
193
+ /** Build the application-management command handlers used by the CLI. */
129
194
  export function getAppCommands() {
130
195
  return {
196
+ /** Create an app and select it in the same configuration file. */
131
197
  createApp: async ({
132
198
  options,
133
199
  }: {
134
- options: { name: string; downloadUrl: string; platform?: Platform | '' };
200
+ options: {
201
+ name: string;
202
+ downloadUrl: string;
203
+ platform?: Platform | '';
204
+ config?: string;
205
+ };
135
206
  }) => {
136
207
  const name = options.name || (await question(t('appNameQuestion')));
137
208
  const { downloadUrl } = options;
@@ -140,9 +211,10 @@ export function getAppCommands() {
140
211
  console.log(t('createAppSuccess', { id }));
141
212
  await selectApp({
142
213
  args: [String(id)],
143
- options: { platform },
214
+ options: { platform, config: options.config },
144
215
  });
145
216
  },
217
+ /** Delete the specified app, or prompt for one when no ID is supplied. */
146
218
  deleteApp: async ({
147
219
  args,
148
220
  options,
@@ -159,6 +231,7 @@ export function getAppCommands() {
159
231
  await doDelete(`/app/${id}`);
160
232
  console.log(t('operationSuccess'));
161
233
  },
234
+ /** List apps through the command interface. */
162
235
  apps: async ({ options }: { options: { platform?: Platform | '' } }) => {
163
236
  const { platform = '' } = options;
164
237
  return listApp(platform);