Skip to content

Commit 62ba0a2

Browse files
committed
feat: support config, appId, and appKey options globally
1 parent 41461b2 commit 62ba0a2

7 files changed

Lines changed: 140 additions & 14 deletions

File tree

cli.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@
364364
},
365365
"acc": {
366366
"default": false
367+
},
368+
"config": {
369+
"hasValue": true
370+
},
371+
"appId": {
372+
"hasValue": true
373+
},
374+
"appKey": {
375+
"hasValue": true
367376
}
368377
}
369378
}

src/app.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ export function assertPlatform(platform: string): Platform {
2626

2727
export async function getSelectedApp(
2828
platform: Platform,
29+
configPath?: string,
2930
): Promise<{ appId: string; appKey: string; platform: Platform }> {
3031
assertPlatform(platform);
3132

3233
let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
3334
{};
3435
try {
35-
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
36+
updateInfo = JSON.parse(
37+
await fs.promises.readFile(configPath || 'update.json', 'utf8'),
38+
);
3639
} catch (e: any) {
3740
if (e.code === 'ENOENT') {
3841
throw new Error(t('appNotSelected', { platform }));
@@ -97,10 +100,13 @@ async function selectApp({
97100
? Number.parseInt(args[0], 10)
98101
: (await chooseApp(platform)).id;
99102

103+
const configPath = (options as any).config as string | undefined;
100104
let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
101105
{};
102106
try {
103-
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
107+
updateInfo = JSON.parse(
108+
await fs.promises.readFile(configPath || 'update.json', 'utf8'),
109+
);
104110
} catch (e: any) {
105111
if (e.code !== 'ENOENT') {
106112
console.error(t('failedToParseUpdateJson'));
@@ -113,7 +119,7 @@ async function selectApp({
113119
appKey,
114120
};
115121
await fs.promises.writeFile(
116-
'update.json',
122+
configPath || 'update.json',
117123
JSON.stringify(updateInfo, null, 4),
118124
'utf8',
119125
);

src/package.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ async function uploadNativePackage(
118118
appId: String(options.appId),
119119
appKey: typeof options.appKey === 'string' ? options.appKey : undefined,
120120
}
121-
: await getSelectedApp(config.platform);
121+
: await getSelectedApp(
122+
config.platform,
123+
options.config as string | undefined,
124+
);
122125
const { appId, appKey } = selectedApp;
123126

124127
if (appIdInPkg && String(appIdInPkg) !== appId) {
@@ -348,9 +351,18 @@ export const packageCommands = {
348351

349352
console.log(t('apkExtracted', { output }));
350353
},
351-
packages: async ({ options }: { options: { platform: Platform } }) => {
352-
const platform = await getPlatform(options.platform);
353-
const { appId } = await getSelectedApp(platform);
354+
packages: async ({
355+
options,
356+
}: {
357+
options: { platform: Platform; appId?: string; config?: string };
358+
}) => {
359+
let appId = options.appId;
360+
if (!appId) {
361+
const platform = await getPlatform(options.platform);
362+
appId = (
363+
await getSelectedApp(platform, options.config as string | undefined)
364+
).appId;
365+
}
354366
await listPackage(String(appId));
355367
},
356368
deletePackage: async ({ options }: { options: PackageCommandOptions }) => {
@@ -361,7 +373,9 @@ export const packageCommands = {
361373

362374
if (!appId) {
363375
const platform = await getPlatform(options.platform);
364-
appId = (await getSelectedApp(platform)).appId;
376+
appId = (
377+
await getSelectedApp(platform, options.config as string | undefined)
378+
).appId;
365379
}
366380

367381
if (!packageIds) {

src/provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export class CLIProviderImpl implements CLIProvider {
148148
appId: options.appId,
149149
appKey: options.appKey,
150150
version: options.version,
151+
config: options.config,
151152
},
152153
[filePath],
153154
);

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export interface PublishOptions {
7474
packageVersionRange?: string;
7575
rollout?: number | string;
7676
dryRun?: boolean;
77+
appId?: string;
78+
config?: string;
7779
}
7880

7981
export interface UploadOptions {
@@ -82,6 +84,7 @@ export interface UploadOptions {
8284
appId?: string;
8385
appKey?: string;
8486
version?: string;
87+
config?: string;
8588
}
8689

8790
export interface UpdateVersionOptions {

src/versions.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { getBooleanOption, getStringListOption } from './utils/options';
1313

1414
interface VersionCommandOptions {
1515
[key: string]: unknown;
16+
config?: string;
17+
appKey?: string;
1618
appId?: string;
1719
name?: string;
1820
description?: string;
@@ -464,7 +466,12 @@ export const versionCommands = {
464466
}
465467

466468
const platform = await getPlatform(options.platform);
467-
const { appId } = await getSelectedApp(platform);
469+
let appId = options.appId;
470+
if (!appId) {
471+
appId = (
472+
await getSelectedApp(platform, options.config as string | undefined)
473+
).appId;
474+
}
468475
const nonInteractive =
469476
getBooleanOption(options, 'no-interactive', false) || isNonInteractive();
470477

@@ -539,7 +546,9 @@ export const versionCommands = {
539546
let appId = options.appId;
540547
if (!appId) {
541548
const platform = await getPlatform(options.platform);
542-
appId = (await getSelectedApp(platform)).appId;
549+
appId = (
550+
await getSelectedApp(platform, options.config as string | undefined)
551+
).appId;
543552
}
544553
const interactive = !(
545554
getBooleanOption(options, 'no-interactive', false) || isNonInteractive()
@@ -553,7 +562,9 @@ export const versionCommands = {
553562
let platform = options.platform;
554563
if (!appId) {
555564
platform = await getPlatform(platform);
556-
appId = (await getSelectedApp(platform)).appId;
565+
appId = (
566+
await getSelectedApp(platform, options.config as string | undefined)
567+
).appId;
557568
} else if (platform) {
558569
platform = await getPlatform(platform);
559570
}
@@ -683,7 +694,9 @@ export const versionCommands = {
683694
let platform = options.platform;
684695
if (!appId) {
685696
platform = await getPlatform(platform);
686-
appId = (await getSelectedApp(platform)).appId;
697+
appId = (
698+
await getSelectedApp(platform, options.config as string | undefined)
699+
).appId;
687700
} else if (platform) {
688701
await getPlatform(platform);
689702
}
@@ -710,7 +723,9 @@ export const versionCommands = {
710723
let appId = options.appId;
711724
if (!appId) {
712725
const platform = await getPlatform(options.platform);
713-
appId = (await getSelectedApp(platform)).appId;
726+
appId = (
727+
await getSelectedApp(platform, options.config as string | undefined)
728+
).appId;
714729
}
715730

716731
const parsedVersionIds =

tests/cli-e2e.test.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function createMockServer(requests: string[], options: MockServerOptions = {}) {
9595
return;
9696
}
9797

98-
if (url === '/api/app/100/version/create') {
98+
if (url.startsWith('/api/app/') && url.endsWith('/version/create')) {
9999
const body = await readBody(request);
100100
options.createdVersions?.push(JSON.parse(body));
101101
sendJson(response, 200, { id: '200' });
@@ -324,4 +324,82 @@ describe('CLI e2e', () => {
324324
}
325325
}
326326
});
327+
328+
test('publishes a ppk using custom config path', async () => {
329+
const requests: string[] = [];
330+
const createdVersions: unknown[] = [];
331+
const server = createMockServer(requests, { createdVersions });
332+
let tempRoot: string | undefined;
333+
334+
await new Promise<void>((resolve) => {
335+
server.listen(0, '127.0.0.1', resolve);
336+
});
337+
closeServer = () =>
338+
new Promise((resolve) => {
339+
if (!server.listening) {
340+
resolve();
341+
return;
342+
}
343+
server.close(() => {
344+
server.closeAllConnections?.();
345+
resolve();
346+
});
347+
});
348+
349+
try {
350+
tempRoot = await mkdtemp(path.join(tmpdir(), 'rn-update-cli-e2e-'));
351+
await writeFile(
352+
path.join(tempRoot, 'update-custom.json'),
353+
JSON.stringify({
354+
android: {
355+
appId: 105,
356+
appKey: 'android-key-custom',
357+
},
358+
}),
359+
);
360+
await writeFile(path.join(tempRoot, 'bundle.ppk'), 'fake-ppk');
361+
362+
const { port } = server.address() as AddressInfo;
363+
const origin = `http://127.0.0.1:${port}`;
364+
const result = await runCli({
365+
args: [
366+
path.join(repoRoot, 'src/bin.ts'),
367+
'publish',
368+
'bundle.ppk',
369+
'--platform',
370+
'android',
371+
'--name',
372+
'v1-custom',
373+
'--config',
374+
'update-custom.json',
375+
'--no-interactive',
376+
],
377+
cwd: tempRoot,
378+
env: {
379+
...process.env,
380+
NO_INTERACTIVE: 'true',
381+
PUSHY_REGISTRY: `${origin}/api`,
382+
npm_config_registry: `${origin}/registry/`,
383+
},
384+
});
385+
386+
expect(result.status).toBe(0);
387+
expect(result.stderr.trim()).toBe('');
388+
expect(requests).toContain('POST /api/upload');
389+
expect(requests).toContain('POST /oss/upload');
390+
expect(requests).toContain('POST /api/app/105/version/create');
391+
expect(createdVersions).toEqual([
392+
expect.objectContaining({
393+
name: 'v1-custom',
394+
hash: 'hash-from-oss',
395+
description: '',
396+
metaInfo: '',
397+
}),
398+
]);
399+
} finally {
400+
if (tempRoot) {
401+
await rm(tempRoot, { force: true, recursive: true });
402+
}
403+
}
404+
});
327405
});

0 commit comments

Comments
 (0)