Skip to content

Commit 93b20ee

Browse files
committed
move some functions into common/util/file
1 parent 5d8abe5 commit 93b20ee

File tree

7 files changed

+42
-40
lines changed

7 files changed

+42
-40
lines changed

common/src/project-file-tree.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import fs from 'fs'
2-
import os from 'os'
32
import path from 'path'
43

54
import * as ignore from 'ignore'
65
import { sortBy } from 'lodash'
76

87
import { DEFAULT_IGNORED_PATHS } from './constants'
9-
import { DirectoryNode, FileTreeNode } from './util/file'
8+
import { DirectoryNode, FileTreeNode, isValidProjectRoot } from './util/file'
109

1110
export const DEFAULT_MAX_FILES = 10_000
1211

1312
export function getProjectFileTree(
1413
projectRoot: string,
1514
{ maxFiles = DEFAULT_MAX_FILES }: { maxFiles?: number } = {}
1615
): FileTreeNode[] {
16+
const start = Date.now()
1717
const defaultIgnore = ignore.default()
1818
for (const pattern of DEFAULT_IGNORED_PATHS) {
1919
defaultIgnore.add(pattern)
2020
}
2121

22-
const isHomeDir = projectRoot === os.homedir()
23-
if (isHomeDir) {
22+
if (!isValidProjectRoot(projectRoot)) {
2423
defaultIgnore.add('.*')
2524
maxFiles = 0
2625
}

common/src/util/file.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as fs from 'fs'
2+
import * as os from 'os'
3+
import * as path from 'path'
24
import { z } from 'zod'
35

46
export const FileTreeNodeSchema: z.ZodType<FileTreeNode> = z.object({
@@ -201,3 +203,33 @@ export function isValidFilePath(path: string) {
201203

202204
return true
203205
}
206+
207+
export function isDir(p: string): boolean {
208+
try {
209+
return fs.statSync(p).isDirectory()
210+
} catch {
211+
return false
212+
}
213+
}
214+
215+
/**
216+
* Returns true if the `toPath` is a subdirectory of `fromPath`.
217+
*/
218+
export function isSubdir(fromPath: string, toPath: string) {
219+
const resolvedFrom = path.resolve(fromPath)
220+
const resolvedTo = path.resolve(toPath)
221+
222+
if (process.platform === 'win32') {
223+
const fromDrive = path.parse(resolvedFrom).root.toLowerCase()
224+
const toDrive = path.parse(resolvedTo).root.toLowerCase()
225+
if (fromDrive !== toDrive) {
226+
return false
227+
}
228+
}
229+
230+
return !path.relative(resolvedFrom, resolvedTo).startsWith('..')
231+
}
232+
233+
export function isValidProjectRoot(dir: string): boolean {
234+
return !isSubdir(dir, os.homedir())
235+
}

npm-app/src/cli.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as readline from 'readline'
77
import { type ApiKeyType } from 'common/api-keys/constants'
88
import type { CostMode } from 'common/constants'
99
import { AnalyticsEvent } from 'common/constants/analytics-events'
10-
import { ProjectFileContext } from 'common/util/file'
10+
import { isDir, ProjectFileContext } from 'common/util/file'
1111
import { pluralize } from 'common/util/string'
1212
import {
1313
blueBright,
@@ -51,7 +51,6 @@ import {
5151
getProjectRoot,
5252
getWorkingDirectory,
5353
initProjectFileContextWithWorker,
54-
isDir,
5554
} from './project-files'
5655
import {
5756
clearScreen,

npm-app/src/menu.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import {
1717
} from 'picocolors'
1818

1919
import { codebuffConfigFile } from 'common/json-config/constants'
20+
import { isValidProjectRoot } from 'common/util/file'
2021
import { Formatter } from 'picocolors/types'
21-
import { getProjectRoot, isValidProjectRoot } from './project-files'
22+
import { getProjectRoot } from './project-files'
2223

2324
export interface CommandInfo {
2425
commandText: string // e.g., 'type "login"', 'type "diff" or "d"', 'hit ESC key or Ctrl-C'

npm-app/src/project-files.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ export function startNewChat() {
4444
return currentChatId
4545
}
4646

47-
export function isDir(p: string): boolean {
48-
try {
49-
return statSync(p).isDirectory()
50-
} catch {
51-
return false
52-
}
53-
}
54-
5547
// Get the project-specific data directory
5648
export function getProjectDataDir(): string {
5749
const root = getProjectRoot()
@@ -73,10 +65,6 @@ export function getCurrentChatDir(): string {
7365
return dir
7466
}
7567

76-
export function isValidProjectRoot(dir: string): boolean {
77-
return !isSubdir(dir, os.homedir())
78-
}
79-
8068
const execAsync = promisify(exec)
8169

8270
let projectRoot: string
@@ -159,24 +147,6 @@ export function toAbsolutePath(filepath: string, projectRoot: string): string {
159147
return path.normalize(path.resolve(projectRoot, filepath))
160148
}
161149

162-
/**
163-
* Returns true if the `toPath` is a subdirectory of `fromPath`.
164-
*/
165-
export function isSubdir(fromPath: string, toPath: string) {
166-
const resolvedFrom = path.resolve(fromPath)
167-
const resolvedTo = path.resolve(toPath)
168-
169-
if (process.platform === 'win32') {
170-
const fromDrive = path.parse(resolvedFrom).root.toLowerCase()
171-
const toDrive = path.parse(resolvedTo).root.toLowerCase()
172-
if (fromDrive !== toDrive) {
173-
return false
174-
}
175-
}
176-
177-
return !path.relative(resolvedFrom, resolvedTo).startsWith('..')
178-
}
179-
180150
let cachedProjectFileContext: ProjectFileContext | undefined
181151

182152
export function initProjectFileContextWithWorker(
@@ -229,7 +199,7 @@ export function initProjectFileContextWithWorker(
229199
export const getProjectFileContext = async (
230200
projectRoot: string,
231201
lastFileVersion: Record<string, string>
232-
) => {
202+
): Promise<ProjectFileContext> => {
233203
const gitChanges = await getGitChanges()
234204
const changesSinceLastChat = getChangesSinceLastFileVersion(lastFileVersion)
235205

npm-app/src/terminal/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
} from 'common/util/string'
1313
import { green } from 'picocolors'
1414

15+
import { isSubdir } from 'common/util/file'
1516
import {
1617
getProjectRoot,
1718
getWorkingDirectory,
18-
isSubdir,
1919
setWorkingDirectory,
2020
} from '../project-files'
2121
import { trackEvent } from '../utils/analytics'

npm-app/src/utils/git.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { execFileSync } from 'child_process'
2+
import { isValidProjectRoot } from 'common/util/file'
23
import fs, { existsSync, statSync } from 'fs'
34
import gitUrlParse from 'git-url-parse'
45
import { getConfig, listFiles, log, ReadCommitResult } from 'isomorphic-git'
56
import path from 'path'
6-
import { getWorkingDirectory, isValidProjectRoot } from '../project-files'
7+
import { getWorkingDirectory } from '../project-files'
78
import { logger } from './logger'
89

910
/**

0 commit comments

Comments
 (0)