From 95bf4718d6717630ab0bedabfeea209ce295acb0 Mon Sep 17 00:00:00 2001 From: Christina Wang Date: Mon, 16 May 2022 13:11:10 -0700 Subject: [PATCH] Only migrate apps.json on preload after target has been set Change-type: patch Signed-off-by: Christina Wang Signed-off-by: Felipe Lalanne --- src/device-state/preload.ts | 39 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/device-state/preload.ts b/src/device-state/preload.ts index e062d014..fa7b856c 100644 --- a/src/device-state/preload.ts +++ b/src/device-state/preload.ts @@ -26,6 +26,26 @@ export function appsJsonBackup(appsPath: string) { return `${appsPath}.preloaded`; } +async function migrateAppsJson(appsPath: string) { + const targetPath = appsJsonBackup(appsPath); + if (!(await fsUtils.exists(targetPath))) { + // Try to rename the path so the preload target state won't + // be used again if the database gets deleted for any reason. + // If the target file already exists or something fails, just debug + // the failure. + await fsUtils + .safeRename(appsPath, targetPath) + .then(() => fsUtils.writeFileAtomic(appsPath, '{}')) + .then(() => log.debug(`Migrated existing apps.json`)) + .catch((e) => + log.debug( + `Continuing without migrating apps.json because of`, + e.message, + ), + ); + } +} + export async function loadTargetFromFile(appsPath: string): Promise { log.info('Attempting to load any preloaded applications'); try { @@ -119,6 +139,7 @@ export async function loadTargetFromFile(appsPath: string): Promise { }; await deviceState.setTarget(localState); + await migrateAppsJson(appsPath); log.success('Preloading complete'); if (preloadState.pinDevice) { // Multi-app warning! @@ -152,24 +173,6 @@ export async function loadTargetFromFile(appsPath: string): Promise { error: e, }); } - } finally { - const targetPath = appsJsonBackup(appsPath); - if (!(await fsUtils.exists(targetPath))) { - // Try to rename the path so the preload target state won't - // be used again if the database gets deleted for any reason. - // If the target file already exists or something fails, just debug - // the failure. - await fsUtils - .safeRename(appsPath, targetPath) - .then(() => fsUtils.writeFileAtomic(appsPath, '{}')) - .then(() => log.debug(`Migrated existing apps.json`)) - .catch((e) => - log.debug( - `Continuing without migrating apps.json because of`, - e.message, - ), - ); - } } return false; }