diff --git a/src/fetchRepo.ts b/src/fetchRepo.ts index 984a820..32d85c6 100644 --- a/src/fetchRepo.ts +++ b/src/fetchRepo.ts @@ -7,7 +7,7 @@ export interface ConverterRepo { repo: string } -export async function fetchRepo(url: string): Promise { +export async function fetchRepo (url: string): Promise { const tmpRootDir = await createTempDir() const tmpRepoDir = path.join(tmpRootDir, 'repo') diff --git a/src/idea/convert.ts b/src/idea/convert.ts index b05fd00..cb8a9c1 100644 --- a/src/idea/convert.ts +++ b/src/idea/convert.ts @@ -97,8 +97,14 @@ export async function convertToIdea ( }) return Promise.all(promises) } catch (error) { - console.trace(chalk.red(error)) - if(error.includes('checkout') - console.log(chalk.red('You may need to add the branch name explicitly to the end of your URL (eg "#main")')) + const message = error instanceof Error ? error.message : String(error) + console.trace(chalk.red(message)) + if (message.includes('checkout')) { + console.log( + chalk.red( + 'You may need to add the branch name explicitly to the end of your URL (e.g. "#main")' + ) + ) + } } } diff --git a/src/sublime/convert.ts b/src/sublime/convert.ts index 53496fc..efe3479 100644 --- a/src/sublime/convert.ts +++ b/src/sublime/convert.ts @@ -85,8 +85,14 @@ export async function convertToSublime ( }) return Promise.all(promises) } catch (error) { - console.log(chalk.red(error)) - if(error.includes('checkout') - console.log(chalk.red('You may need to add the branch name explicitly to the end of your URL (eg "#main")')) + const message = error instanceof Error ? error.message : String(error) + console.log(chalk.red(message)) + if (message.includes('checkout')) { + console.log( + chalk.red( + 'You may need to add the branch name explicitly to the end of your URL (e.g. "#main")' + ) + ) + } } } diff --git a/src/util/__tests__/sublime.test.ts b/src/util/__tests__/sublime.test.ts index 7e53f7d..307657b 100644 --- a/src/util/__tests__/sublime.test.ts +++ b/src/util/__tests__/sublime.test.ts @@ -1,4 +1,5 @@ import os from 'os' +import fs from 'fs' import { getSublimeTextPackageDir } from '../sublime' describe('util/sublime', () => { @@ -29,9 +30,12 @@ describe('util/sublime', () => { shell: '' })) + const dirSpy = jest.spyOn(fs, 'existsSync').mockReturnValueOnce(true) + expect(getSublimeTextPackageDir()).toEqual(result) expect(osSpy).toHaveBeenCalled() expect(usernameSpy).toHaveBeenCalled() + expect(dirSpy).toHaveBeenCalled() } ) }) diff --git a/src/util/sublime.ts b/src/util/sublime.ts index 0eafa1a..29de525 100644 --- a/src/util/sublime.ts +++ b/src/util/sublime.ts @@ -1,5 +1,6 @@ import os from 'os' import fs from 'fs' +import chalk from 'chalk' function getUsername (): string { return os.userInfo().username @@ -7,21 +8,35 @@ function getUsername (): string { export function getSublimeTextPackageDir (): string { const username = getUsername() + const testPaths: string[] = [] switch (os.platform()) { case 'win32': - if(fs.existsSync('%APPDATA%/Roaming/Sublime Text 3/Packages/')) - return '%APPDATA%/Roaming/Sublime Text 3/Packages/' - else - return '%APPDATA%/Roaming/Sublime Text/Packages/' + testPaths.push( + '%APPDATA%/Roaming/Sublime Text 3/Packages/', + '%APPDATA%/Roaming/Sublime Text/Packages/' + ) + break case 'darwin': - if(fs.existsSync(`/Users/${username}/Library/Application Support/Sublime Text 3/Packages/`)) - return `/Users/${username}/Library/Application Support/Sublime Text 3/Packages/` - else - return `/Users/${username}/Library/Application Support/Sublime Text/Packages/` + testPaths.push( + `/Users/${username}/Library/Application Support/Sublime Text 3/Packages/`, + `/Users/${username}/Library/Application Support/Sublime Text/Packages/` + ) + break default: - if(fs.existsSync(`/${username}/.config/sublime-text-3/Packages/`)) - return `/${username}/.config/sublime-text-3/Packages/` - else - return `/${username}/.config/sublime-text/Packages/` + testPaths.push( + `/${username}/.config/sublime-text-3/Packages/`, + `/${username}/.config/sublime-text/Packages/` + ) } + + for (const testPath of testPaths) { + if (fs.existsSync(testPath)) return testPath + } + + console.log( + chalk.yellow( + 'Could not find Sublime theme dir, falling back to current working dir' + ) + ) + return process.cwd() }