File size: 2,256 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { z } from 'zod';

import { createDecorator, type ServiceIdentifier } from '#/_base/di/instantiation';
import { CoreErrors } from '#/_base/errors/codes';
import { Error2 } from '#/_base/errors/errors';
import { FsErrors } from '#/workspace/workspaceFs/internal/errors';

export const fsBrowseQuerySchema = z.object({
  path: z.string().min(1).optional(),
});
export type FsBrowseQuery = z.infer<typeof fsBrowseQuerySchema>;

export const fsBrowseEntrySchema = z.object({
  name: z.string().min(1),
  path: z.string().min(1),
  is_dir: z.literal(true),
});
export type FsBrowseEntry = z.infer<typeof fsBrowseEntrySchema>;

export const fsBrowseResponseSchema = z.object({
  path: z.string().min(1),
  parent: z.string().min(1).nullable(),
  entries: z.array(fsBrowseEntrySchema),
});
export type FsBrowseResponse = z.infer<typeof fsBrowseResponseSchema>;

export const fsHomeResponseSchema = z.object({
  home: z.string().min(1),
  recent_roots: z.array(z.string().min(1)),
});
export type FsHomeResponse = z.infer<typeof fsHomeResponseSchema>;

export class HostFolderNotAbsoluteError extends Error2 {
  readonly path: string;
  constructor(path: string) {
    super(CoreErrors.codes.VALIDATION_FAILED, `path must be absolute: ${path}`, {
      details: { path },
    });
    this.name = 'HostFolderNotAbsoluteError';
    this.path = path;
  }
}

export class HostFolderNotFoundError extends Error2 {
  readonly path: string;
  constructor(path: string) {
    super(FsErrors.codes.FS_PATH_NOT_FOUND, `path not found: ${path}`, { details: { path } });
    this.name = 'HostFolderNotFoundError';
    this.path = path;
  }
}

export class HostFolderPermissionError extends Error2 {
  readonly path: string;
  constructor(path: string) {
    super(FsErrors.codes.FS_PERMISSION_DENIED, `permission denied: ${path}`, { details: { path } });
    this.name = 'HostFolderPermissionError';
    this.path = path;
  }
}

export interface IHostFolderBrowser {
  readonly _serviceBrand: undefined;

  browse(absPath?: string): Promise<FsBrowseResponse>;
  home(): Promise<FsHomeResponse>;
}

export const IHostFolderBrowser: ServiceIdentifier<IHostFolderBrowser> =
  createDecorator<IHostFolderBrowser>('hostFolderBrowser');

export const RECENT_ROOTS_LIMIT = 8;