-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.ts
More file actions
122 lines (102 loc) · 2.53 KB
/
Copy pathoperations.ts
File metadata and controls
122 lines (102 loc) · 2.53 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
existsSync,
mkdirSync,
readdirSync,
rmSync,
statSync,
writeFileSync,
} from "node:fs";
import { join } from "node:path";
import type { Config, StashItem } from "./types";
export type CreateStashItemInput = {
text: string;
isFile: boolean;
prefix: boolean;
};
export type CreateStashItemResult = {
success: boolean;
message: string;
data: {
name: string;
type: "file" | "directory";
path: string;
mtime?: Date;
size?: number;
};
};
export const currentDate = new Date().toISOString().split("T")[0];
export function getStashDir(config: Config) {
if (!existsSync(config.stashDir)) {
console.error(
`Stash directory ${config.stashDir} does not exist, creating...`,
);
mkdirSync(config.stashDir, { recursive: true });
}
return config.stashDir;
}
export const getStashItems = (config: Config): StashItem[] => {
const stashPath = getStashDir(config);
const entries = readdirSync(stashPath);
const items: Array<StashItem> = [];
for (const entry of entries) {
const fullPath = join(stashPath, entry);
const stats = statSync(fullPath);
items.push({
name: entry,
type: stats.isFile() ? "file" : "directory",
path: fullPath,
mtime: stats.mtime,
size: stats.size,
score: 0,
matchedIndices: [],
});
}
return items.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
};
export function isStashEmpty(config: Config) {
const items = getStashItems(config);
return items.length === 0;
}
export function createStashItem(
state: CreateStashItemInput,
config: Config,
): CreateStashItemResult {
const name = state.prefix ? `${currentDate}-${state.text}` : state.text;
const fullPath = join(config.stashDir, name);
const type = state.isFile ? "file" : "directory";
if (existsSync(fullPath)) {
return {
success: false,
message: `File/directory already exists: ${name}`,
data: {
name,
type,
path: fullPath,
},
};
}
if (state.isFile) {
writeFileSync(fullPath, "", { encoding: "utf-8" });
} else {
mkdirSync(fullPath, { recursive: true });
}
const fileStats = statSync(fullPath);
return {
success: true,
message: `Created ${type}: ${name}`,
data: {
name,
type,
path: fullPath,
mtime: fileStats.mtime,
size: fileStats.size,
},
};
}
export function deleteStashItem(path: string): boolean {
if (!existsSync(path)) {
return false;
}
rmSync(path, { recursive: true, force: true });
return true;
}