Skip to content

Commit

Permalink
typescript hygiene
Browse files Browse the repository at this point in the history
  • Loading branch information
ole1986 committed Dec 3, 2019
1 parent a932c2c commit e1de4d1
Show file tree
Hide file tree
Showing 26 changed files with 82 additions and 93 deletions.
20 changes: 10 additions & 10 deletions src/adapter/avatar/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ export class GithubAvatarProvider extends BaseAvatarProvider implements IAvatarP
}
protected async getAvatarsImplementation(repository: IGitService): Promise<Avatar[]> {
const remoteUrl = await repository.getOriginUrl();
const remoteRepoPath = remoteUrl.replace(/.*?github.com\//,'');
const remoteRepoPath = remoteUrl.replace(/.*?github.com\//, '');
const remoteRepoWithNoGitSuffix = remoteRepoPath.replace(/\.git\/?$/, '');
const contributors = await this.getContributors(remoteRepoWithNoGitSuffix);

const githubUsers = await Promise.all(contributors.map(async user => {
const u = await this.getUserByLogin(user.login);
return u;
return await this.getUserByLogin(user.login);
}));

let avatars : Avatar[] = [];

githubUsers.forEach(user => {
if(!user) return;
if (!user) {
return;
}
avatars.push({
login: user.login,
name: user.name,
Expand All @@ -111,15 +112,15 @@ export class GithubAvatarProvider extends BaseAvatarProvider implements IAvatarP
}
/**
* Fetch the user details through Github API
* @param loginName
* @param loginName the user login name from github
*/
private async getUserByLogin(loginName: string) {
const key = `GitHub:User:${loginName}`;

const cachedUser = await this.stateStore.get<GithubUserResponse>(key);
let headers = {};

if(cachedUser) {
if (cachedUser) {
// Use GitHub API with conditional check on last modified
// to avoid API request rate limitation
headers = {'If-Modified-Since': cachedUser.last_modified};
Expand All @@ -138,8 +139,8 @@ export class GithubAvatarProvider extends BaseAvatarProvider implements IAvatarP
// can either be '302 Not Modified' or any other error
// in case of '302 Not Modified' this API request is not counted and returns nothing
});
if(info) {

if (info) {
await this.stateStore.set(key, info);
return info;
} else {
Expand All @@ -153,10 +154,9 @@ export class GithubAvatarProvider extends BaseAvatarProvider implements IAvatarP
*/
private async getContributors(repoPath: string) {
const proxy = this.proxy;
const info = await axios.get(`https://api.github.com/repos/${repoPath}/contributors`, { proxy })
return axios.get(`https://api.github.com/repos/${repoPath}/contributors`, { proxy })
.then((result: { data: GithubUserSearchResponseItem[] }) => {
return result.data;
});
return info;
}
}
2 changes: 1 addition & 1 deletion src/adapter/avatar/gravatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class GravatarAvatarProvider extends BaseAvatarProvider implements IAvata
avatarUrl: gravatar.url(user.email),
name: user.name,
email: user.email
}
};
});
}
}
9 changes: 4 additions & 5 deletions src/adapter/exec/gitCommandExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import * as iconv from 'iconv-lite';
import { injectable, multiInject } from 'inversify';
import { Writable } from 'stream';
import { Disposable, extensions } from 'vscode';
import { IGitCommandExecutor } from './types';
import { GitExtension } from '../repository/git.d';
import { StopWatch } from '../../common/stopWatch';
import { ILogService } from '../../common/types';
import { IGitCommandExecutor } from './types';


const DEFAULT_ENCODING = 'utf8';
const isWindows = /^win/.test(process.platform);

@injectable()
export class GitCommandExecutor implements IGitCommandExecutor {
public gitExtension : GitExtension;
private gitExecutablePath : string;
public gitExtension: GitExtension;
private gitExecutablePath: string;

constructor(@multiInject(ILogService) private loggers: ILogService[]) {
this.gitExtension = extensions.getExtension<GitExtension>('vscode.git')!.exports;
Expand All @@ -36,7 +35,7 @@ export class GitCommandExecutor implements IGitCommandExecutor {
childProcOptions.encoding = DEFAULT_ENCODING;
}
const binaryOuput = childProcOptions.encoding === 'binary';
const destination: Writable = binaryOuput ? args.shift()! : undefined;
const destination: Writable = binaryOuput ? args.shift() : undefined;
const gitPathCommand = childProcOptions.shell && gitPath.indexOf(' ') > 0 ? `"${gitPath}"` : gitPath;
const stopWatch = new StopWatch();
const gitShow = spawn(gitPathCommand, args, childProcOptions);
Expand Down
12 changes: 3 additions & 9 deletions src/adapter/parsers/fileStat/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FileStatParser implements IFileStatParser {
constructor( @inject(IServiceContainer) private serviceContainer: IServiceContainer) {
}

private static parseFileMovement(fileInfo: string): { original: string, current: string } | undefined {
private static parseFileMovement(fileInfo: string): { original: string; current: string } | undefined {
// src/client/{common/comms => }/Socket Stream.ts
// src/client/common/{space in folder => comms}/id Dispenser.ts
// src/client/common/space in folder/{idDispenser.ts => id Dispenser.ts}
Expand Down Expand Up @@ -67,13 +67,11 @@ export class FileStatParser implements IFileStatParser {

/**
* Parses a line containing file information returned by `git log --name-stat` and returns just the file names
* @private
* @static
* @param {string} line
* @returns {({ original?: string, current: string } | undefined)}
* @memberof FileStatParser
*/
private static getNewAndOldFileNameFromNumStatLine(line: string, status: Status): { original?: string, current: string } | undefined {
private static getNewAndOldFileNameFromNumStatLine(line: string, status: Status): { original?: string; current: string } | undefined {
const statusParts = line.split('\t');
const fileName = statusParts[1].trim();
if (status === Status.Renamed || status === Status.Copied) {
Expand All @@ -84,8 +82,6 @@ export class FileStatParser implements IFileStatParser {

/**
* Parses a line containing file information returned by `git log --numstat`
* @private
* @static
* @param {string} line
* @returns {({ additions?: number, deletions?: number } | undefined)}
* @memberof FileStatParser
Expand All @@ -108,8 +104,7 @@ export class FileStatParser implements IFileStatParser {
* Parsers the file status
* @param {string[]} filesWithNumStat Files returned using `git log --numstat`
* @param {string[]} filesWithNameStat Files returned using `git log --name-status`
* @returns {CommittedFile[]}
* @memberof FileStatParser
* @returns {CommittedFile[]} An array of committed files
*/
public parse(gitRootPath: string, filesWithNumStat: string[], filesWithNameStat: string[]): CommittedFile[] {
return filesWithNameStat.map((line, index) => {
Expand Down Expand Up @@ -142,7 +137,6 @@ export class FileStatParser implements IFileStatParser {
uri: Uri.file(path.join(gitRootPath, relativePath)),
oldUri
};

// uri.fsPath getter sporadically becomes a slash as prefix (E.g "/z:/folder/subfolder").
// By fetching fsPath through the getter, the internal method _makeFsPath(this) immediate get called here
// and the fsPath is set correctly.
Expand Down
5 changes: 2 additions & 3 deletions src/adapter/parsers/refs/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export class RefsParser implements IRefsParser {
* git branch --all (only considers)
* git show-refs
* git log --format=%D
* @param {string} refContent
* @returns {Ref[]}
* @memberof RefParser
* @param {string} refContent the ref content as string to be parsed
* @returns {Ref[]} A reference which can either be a branch, tag or origin
*/
public parse(refContent: string): Ref[] {
return (refContent || '').split(',')
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/repository/git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ export const enum GitErrorCodes {
PatchDoesNotApply = 'PatchDoesNotApply',
NoPathFound = 'NoPathFound',
UnknownPath = 'UnknownPath',
}
}
6 changes: 3 additions & 3 deletions src/adapter/repository/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class Git implements IGitService {
} else if (url.indexOf('visualstudio') > 0) {
return GitOriginType.vsts;
}

return undefined;
}
@cache('IGitService')
Expand Down Expand Up @@ -397,8 +397,8 @@ export class Git implements IGitService {
const hashes = output.split(/\r?\n/g).filter(item => item.length > 0)[0].split('-');

return {
short: hashes[1]!,
full: hashes[0]!
short: hashes[1],
full: hashes[0]
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/repository/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ export enum GitOriginType {
github = 2,
bitbucket = 3,
tfs = 4,
vsts = 5,
vsts = 5
}
26 changes: 13 additions & 13 deletions src/application/stateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import { Memento } from 'vscode';
import { IServiceContainer } from '../ioc/types';
import { IStateStore, IStateStoreFactory } from './types/stateStore';

export class WorkspaceMementoStore implements IStateStore {
constructor(private store: Memento) { }
public has(key: string): boolean {
return this.store.get(key) !== undefined;
}
public async set<T>(key: string, data: T): Promise<void> {
await this.store.update(key, data);
}
public async get<T>(key: string): Promise<T | undefined> {
return this.store.get(key);
}
}

@injectable()
export class WorkspaceStateStoreFactory implements IStateStoreFactory {
constructor( @inject(IServiceContainer) private serviceContainer: IServiceContainer) { }
Expand All @@ -20,16 +33,3 @@ export class GlobalStateStoreFactory implements IStateStoreFactory {
return new WorkspaceMementoStore(this.serviceContainer.get<Memento>('globalMementoStore'));
}
}

export class WorkspaceMementoStore implements IStateStore {
constructor(private store: Memento) { }
public has(key: string): boolean {
return this.store.get(key) !== undefined;
}
public async set<T>(key: string, data: T): Promise<void> {
await this.store.update(key, data);
}
public async get<T>(key: string): Promise<T | undefined> {
return this.store.get(key);
}
}
2 changes: 1 addition & 1 deletion src/commandHandlers/commit/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class GitCompareCommitCommandHandler implements IGitCompareCommandHandler
// display explorer view when running compare
await this.commandManager.executeCommand('workbench.view.explorer');
const gitService = await this.serviceContainer.get<IGitServiceFactory>(IGitServiceFactory).createGitService(commit.workspaceFolder, commit.logEntry.gitRoot);
const fileDiffs = await gitService.getDifferences(this.selectedCommit!.logEntry.hash.full, commit.logEntry.hash.full);
const fileDiffs = await gitService.getDifferences(this.selectedCommit.logEntry.hash.full, commit.logEntry.hash.full);
const compareCommit = new CompareCommitDetails(this.selectedCommit, commit, fileDiffs);
this.commitViewerFactory.getCompareCommitViewer().showCommitTree(compareCommit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commandHandlers/fileCommit/fileCompare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class GitCompareFileCommitCommandHandler implements IGitCompareFileComman
}
const fileCommit = nodeOrFileCommit instanceof FileCommitDetails ? nodeOrFileCommit : nodeOrFileCommit.data!;
const gitService = await this.serviceContainer.get<IGitServiceFactory>(IGitServiceFactory).createGitService(fileCommit.workspaceFolder, fileCommit.logEntry.gitRoot);
const fileDiffs = await gitService.getDifferences(this.selectedCommit!.logEntry.hash.full, fileCommit.logEntry.hash.full);
await this.commandManager.executeCommand('git.commit.diff.view', this.selectedCommit!, fileCommit, fileDiffs);
const fileDiffs = await gitService.getDifferences(this.selectedCommit.logEntry.hash.full, fileCommit.logEntry.hash.full);
await this.commandManager.executeCommand('git.commit.diff.view', this.selectedCommit, fileCommit, fileDiffs);
}
}
20 changes: 10 additions & 10 deletions src/commandHandlers/fileCommit/fileHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ export class GitFileHistoryCommandHandler implements IGitFileHistoryCommandHandl
return this.applicationShell.showErrorMessage('File cannot be compared with previous, as this is a new file').then(() => void 0);
}

const tmpFilePromise = gitService.getCommitFile(fileCommit.logEntry.hash.full, fileCommit.committedFile!.uri);
const previousCommitHashPromise = gitService.getPreviousCommitHashForFile(fileCommit.logEntry.hash.full, fileCommit.committedFile!.uri);
const tmpFilePromise = gitService.getCommitFile(fileCommit.logEntry.hash.full, fileCommit.committedFile.uri);
const previousCommitHashPromise = gitService.getPreviousCommitHashForFile(fileCommit.logEntry.hash.full, fileCommit.committedFile.uri);

const values = await Promise.all([tmpFilePromise, previousCommitHashPromise]);
const tmpFile = values[0];
const previousCommitHash = values[1];

const previousFile = fileCommit.committedFile!.oldUri ? fileCommit.committedFile!.oldUri! : fileCommit.committedFile!.uri;
const previousFile = fileCommit.committedFile.oldUri ? fileCommit.committedFile.oldUri : fileCommit.committedFile.uri;
const previousTmpFile = await gitService.getCommitFile(previousCommitHash.full, previousFile);

const title = this.getComparisonTitle({ file: Uri.file(fileCommit.committedFile!.uri.fsPath), hash: fileCommit.logEntry.hash }, { file: Uri.file(previousFile.fsPath), hash: previousCommitHash });
const title = this.getComparisonTitle({ file: Uri.file(fileCommit.committedFile.uri.fsPath), hash: fileCommit.logEntry.hash }, { file: Uri.file(previousFile.fsPath), hash: previousCommitHash });
await this.commandManager.executeCommand('vscode.diff', previousTmpFile, tmpFile, title, { preview: true });
}
@command('git.commit.FileEntry.ViewPreviousFileContents', IGitFileHistoryCommandHandler)
Expand All @@ -90,9 +90,9 @@ export class GitFileHistoryCommandHandler implements IGitFileHistoryCommandHandl
return this.applicationShell.showErrorMessage('Previous version of the file cannot be opened, as this is a new file').then(() => void 0);
}

const previousCommitHash = await gitService.getPreviousCommitHashForFile(fileCommit.logEntry.hash.full, fileCommit.committedFile!.uri);
const previousCommitHash = await gitService.getPreviousCommitHashForFile(fileCommit.logEntry.hash.full, fileCommit.committedFile.uri);

const previousFile = fileCommit.committedFile!.oldUri ? fileCommit.committedFile!.oldUri! : fileCommit.committedFile!.uri;
const previousFile = fileCommit.committedFile.oldUri ? fileCommit.committedFile.oldUri : fileCommit.committedFile.uri;
const previousTmpFile = await gitService.getCommitFile(previousCommitHash.full, previousFile);

await this.commandManager.executeCommand('git.openFileInViewer', Uri.file(previousTmpFile.fsPath));
Expand All @@ -108,13 +108,13 @@ export class GitFileHistoryCommandHandler implements IGitFileHistoryCommandHandl
return this.applicationShell.showErrorMessage('File cannot be compared, as this is a new file').then(() => void 0);
}

const leftFilePromise = gitService.getCommitFile(fileCommit.logEntry.hash.full, fileCommit.committedFile!.uri);
const rightFilePromise = gitService.getCommitFile(fileCommit.rightCommit.logEntry.hash.full, fileCommit.committedFile!.uri);
const leftFilePromise = gitService.getCommitFile(fileCommit.logEntry.hash.full, fileCommit.committedFile.uri);
const rightFilePromise = gitService.getCommitFile(fileCommit.rightCommit.logEntry.hash.full, fileCommit.committedFile.uri);

const [leftFile, rightFile] = await Promise.all([leftFilePromise, rightFilePromise]);

const title = this.getComparisonTitle({ file: Uri.file(fileCommit.committedFile!.uri.fsPath), hash: fileCommit.logEntry.hash },
{ file: Uri.file(fileCommit.committedFile!.uri.fsPath), hash: fileCommit.rightCommit.logEntry.hash });
const title = this.getComparisonTitle({ file: Uri.file(fileCommit.committedFile.uri.fsPath), hash: fileCommit.logEntry.hash },
{ file: Uri.file(fileCommit.committedFile.uri.fsPath), hash: fileCommit.rightCommit.logEntry.hash });

await this.commandManager.executeCommand('vscode.diff', leftFile, rightFile, title, { preview: true });
}
Expand Down
8 changes: 4 additions & 4 deletions src/commandHandlers/gitHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class GitHistoryCommandHandler implements IGitHistoryCommandHandler {
this._server = this.serviceContainer.get<IServerHost>(IServerHost);
this.disposableRegistry.register(this._server);
}
return this._server!;
return this._server;
}
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(IDisposableRegistry) private disposableRegistry: IDisposableRegistry,
Expand All @@ -34,10 +34,10 @@ export class GitHistoryCommandHandler implements IGitHistoryCommandHandler {
if (info) {
if (info instanceof FileCommitDetails) {
const committedFile = info.committedFile;
fileUri = committedFile.uri ? Uri.file(committedFile.uri!.fsPath!) : Uri.file(committedFile.oldUri!.fsPath);
fileUri = committedFile.uri ? Uri.file(committedFile.uri.fsPath) : Uri.file(committedFile.oldUri!.fsPath);
} else if (info instanceof FileNode) {
const committedFile = info.data!.committedFile;
fileUri = committedFile.uri ? Uri.file(committedFile.uri!.fsPath!) : Uri.file(committedFile.oldUri!.fsPath);
fileUri = committedFile.uri ? Uri.file(committedFile.uri.fsPath) : Uri.file(committedFile.oldUri!.fsPath);
} else if (info instanceof Uri) {
fileUri = info;
// tslint:disable-next-line:no-any
Expand Down Expand Up @@ -81,7 +81,7 @@ export class GitHistoryCommandHandler implements IGitHistoryCommandHandler {
const gitService = await this.serviceContainer.get<IGitServiceFactory>(IGitServiceFactory)
.createGitService(workspaceFolder, gitRoot);
const branchNamePromise = gitService.getCurrentBranch();
const startupInfoPromise = this.server!.start(workspaceFolder);
const startupInfoPromise = this.server.start(workspaceFolder);
const localePromise = osLocale();
const gitRootsUnderWorkspacePromise = gitService.getGitRoots(workspaceFolder);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/fileCommit/compareFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class CompareFileCommand extends BaseFileCommitCommand {
constructor(fileCommit: FileCommitDetails, private handler: IGitCompareFileCommandHandler) {
super(fileCommit);
if (handler.selectedCommit) {
this.setTitle(`$(git-compare) Compare with ${handler.selectedCommit!.logEntry.hash.short}`);
this.setTitle(`$(git-compare) Compare with ${handler.selectedCommit.logEntry.hash.short}`);
}
this.setCommand('git.commit.FileEntry.compare');
this.setCommandArguments([fileCommit]);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/fileCommit/viewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ViewFileCommand extends BaseFileCommitCommand {
this.setCommandArguments([fileCommit]);
}
public async preExecute(): Promise<boolean> {
return this.data.committedFile!.status !== Status.Deleted;
return this.data.committedFile.status !== Status.Deleted;
}
public execute() {
this.handler.viewFile(this.data);
Expand Down
Loading

0 comments on commit e1de4d1

Please sign in to comment.