From dc150ff01597652b5e415d807b491e1a80c9ba96 Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Wed, 10 Mar 2021 10:45:59 +1300 Subject: [PATCH] Better handling of .DS_Store files during migration (#3164) --- .../insomnia-app/app/common/migrate-from-designer.js | 12 ++++++++---- .../app/ui/components/wrapper-migration.js | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/insomnia-app/app/common/migrate-from-designer.js b/packages/insomnia-app/app/common/migrate-from-designer.js index 5c30082a3..89b4cfb27 100644 --- a/packages/insomnia-app/app/common/migrate-from-designer.js +++ b/packages/insomnia-app/app/common/migrate-from-designer.js @@ -116,7 +116,7 @@ async function migratePlugins(designerDataDir: string, coreDataDir: string) { } async function readDirs(srcDir: string): Array { - if (fs.existsSync(srcDir)) { + if (existsAndIsDirectory(srcDir)) { return await fs.promises.readdir(srcDir); } else { return []; @@ -129,7 +129,7 @@ async function copyDirs(dirs: Array, srcDir: string, destDir: string) { const dest = fsPath.join(destDir, dir); // If source exists, ensure the destination exists, and copy into it - if (fs.existsSync(src)) { + if (existsAndIsDirectory(src)) { await fsx.ensureDir(dest); await fsx.copy(src, dest); } @@ -139,12 +139,16 @@ async function copyDirs(dirs: Array, srcDir: string, destDir: string) { async function removeDirs(dirs: Array, srcDir: string) { for (const dir of dirs.filter(c => c)) { const dirToRemove = fsPath.join(srcDir, dir); - if (fs.existsSync(dirToRemove)) { + if (existsAndIsDirectory(dirToRemove)) { await fsx.remove(dirToRemove); } } } +export function existsAndIsDirectory(name: string): boolean { + return fs.existsSync(name) && fs.statSync(name).isDirectory(); +} + export default async function migrateFromDesigner({ useDesignerSettings, designerDataDir, @@ -254,7 +258,7 @@ export async function restoreCoreBackup(backupDir: string, coreDataDir: string) return; } - if (!fs.existsSync(backupDir)) { + if (!existsAndIsDirectory(backupDir)) { console.log(`[db-merge] nothing to restore: backup directory doesn't exist at ${backupDir}`); return; } diff --git a/packages/insomnia-app/app/ui/components/wrapper-migration.js b/packages/insomnia-app/app/ui/components/wrapper-migration.js index af49529a8..65856675e 100644 --- a/packages/insomnia-app/app/ui/components/wrapper-migration.js +++ b/packages/insomnia-app/app/ui/components/wrapper-migration.js @@ -4,14 +4,16 @@ import * as React from 'react'; import type { WrapperProps } from './wrapper'; import { ToggleSwitch, Button } from 'insomnia-components'; import type { MigrationOptions } from '../../common/migrate-from-designer'; -import migrateFromDesigner, { restartApp } from '../../common/migrate-from-designer'; +import migrateFromDesigner, { + existsAndIsDirectory, + restartApp, +} from '../../common/migrate-from-designer'; import { getDataDirectory, getDesignerDataDir } from '../../common/misc'; import { useDispatch } from 'react-redux'; import OnboardingContainer from './onboarding-container'; import { goToNextActivity } from '../redux/modules/global'; import HelpTooltip from './help-tooltip'; import { trackEvent } from '../../common/analytics'; -import fs from 'fs'; type Step = 'options' | 'migrating' | 'results'; @@ -106,9 +108,11 @@ const Options = ({ start, cancel }: OptionsProps) => { copyPlugins, } = options; - const coreExists = React.useMemo(() => fs.existsSync(coreDataDir), [coreDataDir]); + const coreExists = React.useMemo(() => existsAndIsDirectory(coreDataDir), [coreDataDir]); - const designerExists = React.useMemo(() => fs.existsSync(designerDataDir), [designerDataDir]); + const designerExists = React.useMemo(() => existsAndIsDirectory(designerDataDir), [ + designerDataDir, + ]); const hasSomethingToMigrate = useDesignerSettings || copyWorkspaces || copyPlugins; const dirsExist = coreExists && designerExists;