Skip to content

Commit

Permalink
Migrate JSDoc comments to typescript definitions for diff methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Lucas committed Oct 10, 2021
1 parent b44387d commit cbbe8c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/git/src/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { spawn } from 'child_process'
import { resolveRepo } from './resolve-repo'

import { STATE, STATE_FILES } from './constants'
import type { Commit, StatusFile, WatcherCallback, WatcherEvent } from './types'
import type {
Commit,
DiffResult,
StatusFile,
WatcherCallback,
WatcherEvent,
} from './types'

const PROFILING = true
let perfStart = (name: string) => {
Expand Down Expand Up @@ -522,22 +528,11 @@ export class Git {
}
}

/**
* @typedef DiffResult
* @property { {insertions: number, deletions: number, filesChanges: number} } stats
* @property {import("diff2html/lib-esm/types").DiffFile[]} files
*/

/**
* @param {string} shaOld
* @param {string} shaNew
* @returns {Promise<DiffResult>}
*/
getDiffFromShas = async (
shaNew: string,
shaOld: string | null = null,
{ contextLines = 10 } = {},
) => {
): Promise<DiffResult | null> => {
const spawn = await this._getSpawn()
if (!spawn) {
return null
Expand Down Expand Up @@ -566,15 +561,14 @@ export class Git {
}

const patchText = await spawn(cmd)
const diff = this._processDiff(patchText)
const diff = this.processDiff(patchText)

return diff
}

/**
* @returns {Promise<DiffResult>}
*/
getDiffFromIndex = async ({ contextLines = 5 }) => {
getDiffFromIndex = async ({
contextLines = 5,
}): Promise<DiffResult | null> => {
const spawn = await this._getSpawn()
if (!spawn) {
return null
Expand All @@ -583,12 +577,12 @@ export class Git {
const cmd = ['diff', '--unified=' + contextLines]

const patchText = await spawn(cmd)
const diff = this._processDiff(patchText)
const diff = this.processDiff(patchText)

return diff
}

_processDiff = (diffText: string) => {
private processDiff = async (diffText: string): Promise<DiffResult> => {
const files = parse(diffText)

return {
Expand Down
11 changes: 11 additions & 0 deletions packages/git/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Diff2Html from 'diff2html/lib-esm/types'

export type WatcherEvent =
| 'add'
| 'unlink'
Expand Down Expand Up @@ -30,3 +32,12 @@ export interface Commit {
parents: string[]
isHead: boolean
}

export interface DiffResult {
stats: {
insertions: number
deletions: number
filesChanged: number
}
files: Diff2Html.DiffFile[]
}

0 comments on commit cbbe8c1

Please sign in to comment.