Skip to content

Commit

Permalink
fix: copy files & better logging and error handling (#167)
Browse files Browse the repository at this point in the history
This pull request includes several changes to improve error handling,
logging, and code organization in the
`src/providers/foldersCompareProvider.ts` and related files. The most
important
[change](diffhunk://#diff-4b422bda5a0007c1c97c9190d7300c608819eb4605d55a172cce02d3ac4ddb71L270-L274)
is building the `from` and `to` paths - got rid of the string
manipulations, which often lead to... cross platform issues
_

fix #162
fix #164
  • Loading branch information
moshfeu authored Oct 25, 2024
1 parent cf4df09 commit 0b74e03
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
19 changes: 8 additions & 11 deletions src/providers/foldersCompareProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ProgressLocation,
commands,
Uri,
Command,
} from 'vscode';
import * as path from 'path';
import { copySync, removeSync } from 'fs-extra';
Expand All @@ -30,8 +29,8 @@ import { ViewOnlyProvider } from './viewOnlyProvider';
import { getConfiguration } from '../services/configuration';
import { setContext } from '../context/global';
import { HAS_FOLDERS } from '../constants/contextKeys';
import { log } from '../services/logger';
import { showErrorMessageWithMoreInfo, showInfoMessageWithTimeout } from '../utils/ui';
import * as logger from '../services/logger';
import { showErrorMessage, showErrorMessageWithMoreInfo, showInfoMessageWithTimeout } from '../utils/ui';
import { showUnaccessibleWarning } from '../services/validators';
import { uiContext, type DiffViewMode } from '../context/ui';

Expand Down Expand Up @@ -208,7 +207,7 @@ export class CompareFoldersProvider implements TreeDataProvider<File> {
}
this.updateUI();
} catch (error) {
log(error);
logger.error(error);
}
};

Expand Down Expand Up @@ -267,15 +266,13 @@ export class CompareFoldersProvider implements TreeDataProvider<File> {
const [folder1Path, folder2Path] = pathContext.getPaths();
const [from, to] =
direction === 'to-compared' ? [folder1Path, folder2Path] : [folder2Path, folder1Path];
const { root, dir, base } = path.parse(from);
const pathWithoutSchema = dir.replace(root, '');
const fileCopiedRelativePath = uri.fsPath.replace(pathWithoutSchema, '').replace(base, '');
const fromPath = path.join(from, fileCopiedRelativePath);
const toPath = path.join(to, fileCopiedRelativePath);
const fromPath = uri.fsPath;
const toPath = path.join(to, path.relative(from, fromPath));
copySync(fromPath, toPath);
this.refresh(false);
} catch (error) {
log(error);
showErrorMessage('Failed to copy file', error);
logger.error(error);
}
}

Expand Down Expand Up @@ -310,7 +307,7 @@ export class CompareFoldersProvider implements TreeDataProvider<File> {

return children;
} catch (error) {
log(error);
logger.error(error);
return [];
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/services/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import { log } from './logger';

class GlobalState {
private readonly KEY = 'compareFolders.paths';
private readonly VERSION_KEY = 'compareFolders.version';
private globalState?: Memento;

init(context: ExtensionContext) {
this.globalState = context.globalState;
this.globalState.update(this.VERSION_KEY, context.extension.packageJSON.version);
}

get extensionVersion() {
return this.globalState?.get<string>(this.VERSION_KEY);
}

updatePaths(path1: string, path2: string) {
Expand Down
13 changes: 12 additions & 1 deletion src/services/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import { type CompareOptions } from '../types';

const logger = window.createOutputChannel('Compare Folders');

export function log(...data: (object | string | undefined | unknown)[]) {
function printData(...data: (object | string | undefined | unknown)[]) {
data.forEach(item => {
if (typeof item === 'string') {
logger.appendLine(item);
} else {
logger.appendLine(JSON.stringify(item, null, 2));
}
});
}

export function error(...data: (object | string | undefined | unknown)[]) {
logger.appendLine('====error====');
printData(...data);
console.error(...data);
logger.appendLine('===============');
}

export function log(...data: (object | string | undefined | unknown)[]) {
printData(...data);
console.log(...data);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtensionContext, workspace, WorkspaceConfiguration } from 'vscode';
import { ExtensionContext, Uri, workspace, WorkspaceConfiguration } from 'vscode';

import { IConfigurations } from '../../../services/configuration';
import * as fs from '../../../services/fs';
Expand Down Expand Up @@ -28,6 +28,18 @@ const defaultSettings: Partial<IConfigurations> = {
const contextFactory = (): Partial<ExtensionContext> => {
const state: Record<string, any> = {};
return {
extension: {
id: 'test',
extensionPath: '/',
extensionKind: 1,
extensionUri: Uri.parse('/'),
isActive: true,
activate: () => Promise.resolve(),
exports: {},
packageJSON: {
version: '1.0.0',
},
},
globalState: {
keys: () => Object.keys(state),
setKeysForSync: () => {},
Expand Down
16 changes: 4 additions & 12 deletions src/utils/ui.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { window, ProgressLocation, env, Uri, version } from 'vscode';
import { join } from 'path';
import { readFileSync } from 'fs';
import os from 'os';
import { log } from '../services/logger';
import * as logger from '../services/logger';
import { globalState } from '../services/globalState';

export function showInfoMessageWithTimeout(message: string, timeout: number = 3000) {
const upTo = timeout / 10;
Expand Down Expand Up @@ -44,7 +43,7 @@ export async function showErrorMessage(message: string, error: any) {
**System Info**
Editor version: ${version}
Extension version: ${getExtensionVersion()}
Extension version: ${globalState.extensionVersion}
OS: ${os.platform()} ${os.release()}
**Stack**
Expand All @@ -57,7 +56,7 @@ ${error.stack || error.message || error}
const uri = Uri.parse(url);
env.openExternal(uri);
} catch (error) {
log(error);
logger.log(error);
}
}
}
Expand All @@ -69,10 +68,3 @@ export async function showErrorMessageWithMoreInfo(message: string, link: string
env.openExternal(Uri.parse(link));
}
}

function getExtensionVersion() {
const { version: extVersion } = JSON.parse(
readFileSync(join(__dirname, '..', 'package.json'), { encoding: 'utf8' })
);
return extVersion;
}

0 comments on commit 0b74e03

Please sign in to comment.