Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,30 @@ export function assertPlatform(platform: string): Platform {
return platform as Platform;
}

export function getSelectedApp(platform: Platform) {
export async function getSelectedApp(
platform: Platform,
): Promise<{ appId: string; appKey: string; platform: Platform }> {
assertPlatform(platform);

if (!fs.existsSync('update.json')) {
throw new Error(t('appNotSelected', { platform }));
let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
{};
try {
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
} catch (e: any) {
if (e.code === 'ENOENT') {
throw new Error(t('appNotSelected', { platform }));
}
throw e;
}
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
if (!updateInfo[platform]) {
const info = updateInfo[platform];
if (!info) {
throw new Error(t('appNotSelected', { platform }));
}
return updateInfo[platform];
return {
appId: String(info.appId),
appKey: info.appKey,
platform,
};
}

export async function listApp(platform: Platform | '' = '') {
Expand Down Expand Up @@ -125,10 +138,10 @@ export const appCommands = {
let updateInfo: Partial<
Record<Platform, { appId: number; appKey: string }>
> = {};
if (fs.existsSync('update.json')) {
try {
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
} catch (e) {
try {
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
} catch (e: any) {
if (e.code !== 'ENOENT') {
console.error(t('failedToParseUpdateJson'));
throw e;
}
Expand All @@ -138,7 +151,7 @@ export const appCommands = {
appId: id,
appKey,
};
fs.writeFileSync(
await fs.promises.writeFile(
'update.json',
JSON.stringify(updateInfo, null, 4),
'utf8',
Expand Down
6 changes: 3 additions & 3 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function uploadNativePackage(
const { appId: appIdInPkg, appKey: appKeyInPkg } = info;
const { appId, appKey } = await getSelectedApp(config.platform);

if (appIdInPkg && appIdInPkg != appId) {
if (appIdInPkg && String(appIdInPkg) !== appId) {
throw new Error(t(config.appIdMismatchKey, { appIdInPkg, appId }));
}

Expand Down Expand Up @@ -330,7 +330,7 @@ export const packageCommands = {
packages: async ({ options }: { options: { platform: Platform } }) => {
const platform = await getPlatform(options.platform);
const { appId } = await getSelectedApp(platform);
await listPackage(appId);
await listPackage(String(appId));
},
deletePackage: async ({
args,
Expand All @@ -348,7 +348,7 @@ export const packageCommands = {

if (!appId) {
const platform = await getPlatform(options.platform);
appId = (await getSelectedApp(platform)).appId as string;
appId = (await getSelectedApp(platform)).appId;
}

// If no packageId provided as argument, let user choose from list
Expand Down
24 changes: 12 additions & 12 deletions src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ export const versionCommands = {
versions: async ({ options }: { options: VersionCommandOptions }) => {
const platform = await getPlatform(options.platform);
const { appId } = await getSelectedApp(platform);
await listVersions(appId);
await listVersions(String(appId));
},
update: async ({ options }: { options: VersionCommandOptions }) => {
const platform = await getPlatform(options.platform);
const appId = options.appId || (await getSelectedApp(platform)).appId;
let versionId = options.versionId || (await chooseVersion(appId)).id;
let versionId = options.versionId || (await chooseVersion(String(appId))).id;
if (versionId === 'null') {
versionId = undefined;
}
Expand All @@ -508,7 +508,7 @@ export const versionCommands = {
}
}

const allPkgs = await getAllPackages(appId);
const allPkgs = await getAllPackages(String(appId));

if (!allPkgs) {
throw new Error(t('noPackagesFound', { appId }));
Expand Down Expand Up @@ -558,7 +558,7 @@ export const versionCommands = {
}
} else {
if (!pkgId) {
pkgId = (await choosePackage(appId)).id;
pkgId = (await choosePackage(String(appId))).id;
}

if (!pkgId) {
Expand All @@ -575,15 +575,15 @@ export const versionCommands = {
}

await printDepsChangesForPublish({
appId,
versionId,
appId: String(appId),
versionId: String(versionId),
pkgs: pkgsToBind,
Comment on lines 577 to 580
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Do not stringify possibly-undefined versionId.

At Line 579 and Line 586, String(versionId) turns an intentional undefined (set at Line 490) into the literal 'undefined'. That can cause incorrect API behavior when updating bindings.

πŸ’‘ Proposed fix
-    await printDepsChangesForPublish({
-      appId: String(appId),
-      versionId: String(versionId),
+    const normalizedVersionId =
+      versionId === undefined ? undefined : String(versionId);
+
+    await printDepsChangesForPublish({
+      appId: String(appId),
+      versionId: normalizedVersionId,
       pkgs: pkgsToBind,
       providedVersionDeps: options.versionDeps,
     });

     await bindVersionToPackages({
       appId: String(appId),
-      versionId: String(versionId),
+      versionId: normalizedVersionId as string,
       pkgs: pkgsToBind,
       rollout,
       dryRun: options.dryRun,
     });
 export const bindVersionToPackages = async ({
   appId,
   versionId,
   pkgs,
   rollout,
   dryRun,
 }: {
   appId: string;
-  versionId: string;
+  versionId?: string;
   pkgs: Package[];
   rollout?: number;
   dryRun?: boolean;
 }) => {

Also applies to: 585-587

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/versions.ts` around lines 577 - 580, The call sites (e.g., the
printDepsChangesForPublish({ appId: String(appId), versionId: String(versionId),
pkgs: pkgsToBind }) invocation and the similar block at lines 585–587) are
stringifying a deliberately undefined versionId; remove the String(...) wrapper
and pass versionId through as-is (versionId) so undefined remains undefined.
Update the call signatures or type annotations if needed so
printDepsChangesForPublish and any downstream handlers accept versionId?: string
| undefined rather than expecting a non-optional string.

providedVersionDeps: options.versionDeps,
});

await bindVersionToPackages({
appId,
versionId,
appId: String(appId),
versionId: String(versionId),
pkgs: pkgsToBind,
rollout,
dryRun: options.dryRun,
Expand All @@ -596,14 +596,14 @@ export const versionCommands = {
}) => {
const platform = await getPlatform(options.platform);
const { appId } = await getSelectedApp(platform);
const versionId = options.versionId || (await chooseVersion(appId)).id;
const versionId = options.versionId || (await chooseVersion(String(appId))).id;

const updateParams: Record<string, string> = {};
if (options.name) updateParams.name = options.name;
if (options.description) updateParams.description = options.description;
if (options.metaInfo) updateParams.metaInfo = options.metaInfo;

await put(`/app/${appId}/version/${versionId}`, updateParams);
await put(`/app/${String(appId)}/version/${versionId}`, updateParams);
console.log(t('operationSuccess'));
},
deleteVersion: async ({
Expand All @@ -619,11 +619,11 @@ export const versionCommands = {

let versionId = options.versionId;
if (!versionId) {
versionId = (await chooseVersion(appId as string)).id;
versionId = (await chooseVersion(String(appId))).id;
}

try {
await doDelete(`/app/${appId}/version/${versionId}`);
await doDelete(`/app/${String(appId)}/version/${versionId}`);
console.log(t('deleteVersionSuccess', { versionId }));
} catch (error: any) {
throw new Error(
Expand Down
Loading