Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
julieg18 committed Feb 1, 2023
1 parent 373ccea commit 171440e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 49 deletions.
6 changes: 2 additions & 4 deletions extension/src/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { relative } from 'path'
import { EventEmitter, Event } from 'vscode'
import {
createFileSystemWatcher,
getRelativePattern
} from '../fileSystem/watcher'
import { getRelativePattern } from '../fileSystem/relativePattern'
import { createFileSystemWatcher } from '../fileSystem/watcher'
import { ProcessManager } from '../processManager'
import { InternalCommands } from '../commands/internal'
import { ExperimentsOutput, PlotsOutputOrError } from '../cli/dvc/contract'
Expand Down
6 changes: 2 additions & 4 deletions extension/src/experiments/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
EXPERIMENTS_GIT_REFS,
EXPERIMENTS_GIT_REFS_EXEC
} from './constants'
import {
createFileSystemWatcher,
getRelativePattern
} from '../../fileSystem/watcher'
import { getRelativePattern } from '../../fileSystem/relativePattern'
import { createFileSystemWatcher } from '../../fileSystem/watcher'
import { AvailableCommands, InternalCommands } from '../../commands/internal'
import { ExperimentsOutput } from '../../cli/dvc/contract'
import { BaseData } from '../../data'
Expand Down
3 changes: 2 additions & 1 deletion extension/src/fileSystem/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { join } from 'path'
import { Event, EventEmitter } from 'vscode'
import { isSameOrChild, loadYaml, PartialDvcYaml } from '..'
import { findFiles } from '../workspace'
import { createFileSystemWatcher, getRelativePattern } from '../watcher'
import { getRelativePattern } from '../relativePattern'
import { createFileSystemWatcher } from '../watcher'
import { DeferredDisposable } from '../../class/deferred'

export class FileSystemData extends DeferredDisposable {
Expand Down
28 changes: 28 additions & 0 deletions extension/src/fileSystem/relativePattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { relative } from 'path'
import { RelativePattern, Uri, workspace } from 'vscode'
import { isSameOrChild } from '.'

const joinWithForwardSlashes = (strings: string[]) =>
strings.filter(Boolean).join('/')

const getRelativePatternForOutsideWorkspace = (
uri: Uri,
pattern: string
): RelativePattern => new RelativePattern(uri, pattern)

export const getRelativePattern = (
path: string,
pattern: string
): RelativePattern => {
for (const workspaceFolder of workspace.workspaceFolders || []) {
const workspaceFolderPath = workspaceFolder.uri.fsPath
if (isSameOrChild(workspaceFolderPath, path)) {
return new RelativePattern(
workspaceFolder,
joinWithForwardSlashes([relative(workspaceFolderPath, path), pattern])
)
}
}

return getRelativePatternForOutsideWorkspace(Uri.file(path), pattern)
}
28 changes: 2 additions & 26 deletions extension/src/fileSystem/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
import { relative } from 'path'
import { utimes } from 'fs-extra'
import { GlobPattern, RelativePattern, Uri, workspace } from 'vscode'
import { GlobPattern, workspace } from 'vscode'
import { Disposable } from '@hediet/std/disposable'
import { isDirectory, isSameOrChild } from '.'
import { joinWithForwardSlashes } from '../util/string'

const getRelativePatternForOutsideWorkspace = (
uri: Uri,
pattern: string
): RelativePattern => new RelativePattern(uri, pattern)

export const getRelativePattern = (
path: string,
pattern: string
): RelativePattern => {
for (const workspaceFolder of workspace.workspaceFolders || []) {
const workspaceFolderPath = workspaceFolder.uri.fsPath
if (isSameOrChild(workspaceFolderPath, path)) {
return new RelativePattern(
workspaceFolder,
joinWithForwardSlashes([relative(workspaceFolderPath, path), pattern])
)
}
}

return getRelativePatternForOutsideWorkspace(Uri.file(path), pattern)
}
import { isDirectory } from '.'

export const fireWatcher = (path: string): Promise<void> => {
const now = Date.now() / 1000
Expand Down
4 changes: 2 additions & 2 deletions extension/src/repository/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Event, EventEmitter } from 'vscode'
import { AvailableCommands, InternalCommands } from '../../commands/internal'
import { ProcessManager } from '../../processManager'
import { getRelativePattern } from '../../fileSystem/relativePattern'
import {
createFileSystemWatcher,
ignoredDotDirectories,
getRelativePattern
ignoredDotDirectories
} from '../../fileSystem/watcher'
import {
EXPERIMENTS_GIT_LOGS_REFS,
Expand Down
6 changes: 2 additions & 4 deletions extension/src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ import { WorkspaceExperiments } from '../experiments/workspace'
import { DvcRunner } from '../cli/dvc/runner'
import { sendTelemetryEvent, sendTelemetryEventAndThrow } from '../telemetry'
import { StopWatch } from '../util/time'
import {
createFileSystemWatcher,
getRelativePattern
} from '../fileSystem/watcher'
import { getRelativePattern } from '../fileSystem/relativePattern'
import { createFileSystemWatcher } from '../fileSystem/watcher'
import { EventName } from '../telemetry/constants'
import { WorkspaceScale } from '../telemetry/collect'
import { gitPath } from '../cli/git/constants'
Expand Down
7 changes: 2 additions & 5 deletions extension/src/test/suite/fileSystem/watcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { expect } from 'chai'
import { Uri } from 'vscode'
import { Disposable } from '@hediet/std/disposable'
import { restore } from 'sinon'
import { getRelativePattern } from '../../../fileSystem/watcher'
import { getRelativePattern } from '../../../fileSystem/relativePattern'
import { dvcDemoPath, getTestWorkspaceFolder } from '../../util'
import { joinWithForwardSlashes } from '../../../util/string'

suite('File System Watcher Test Suite', () => {
const disposable = Disposable.fn()
Expand All @@ -28,9 +27,7 @@ suite('File System Watcher Test Suite', () => {
expect(relativePattern.baseUri).to.deep.equal(
getTestWorkspaceFolder().uri
)
expect(relativePattern.pattern).equal(
joinWithForwardSlashes(['.git', '**'])
)
expect(relativePattern.pattern).equal('.git/**')
})

it('should return the expected relative pattern for a path outside of the workspace', () => {
Expand Down
3 changes: 0 additions & 3 deletions extension/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ export const truncateFromLeft = (str: string, length: number): string => {
export const truncate = (str: string, length: number): string => {
return str.length > length ? str.slice(0, length - 1) + '...' : str
}

export const joinWithForwardSlashes = (strings: string[]) =>
strings.filter(Boolean).join('/')

0 comments on commit 171440e

Please sign in to comment.