-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-utils.ts
More file actions
249 lines (201 loc) · 6.88 KB
/
Copy pathfile-utils.ts
File metadata and controls
249 lines (201 loc) · 6.88 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import * as fsSync from 'node:fs';
import * as fs from 'node:fs/promises';
import path from 'node:path';
import { Readable } from 'node:stream';
import { finished } from 'node:stream/promises';
import { ApplyNotes } from '../common/apply-notes.js';
import { CodifyCliSender } from '../messages/sender.js';
import { Utils } from './index.js';
const SPACE_REGEX = /^\s*$/
export class FileUtils {
static async downloadFile(url: string, destination: string): Promise<void> {
console.log(`Downloading file from ${url} to ${destination}`);
const { body } = await fetch(url)
const dirname = path.dirname(destination);
if (!await fs.stat(dirname).then((s) => s.isDirectory()).catch(() => false)) {
await fs.mkdir(dirname, { recursive: true });
}
const ws = fsSync.createWriteStream(destination)
// Different type definitions here for readable stream (NodeJS vs DOM). Small hack to fix that
await finished(Readable.fromWeb(body as never).pipe(ws));
console.log(`Finished downloading to ${destination}`);
}
static async addToShellRc(line: string): Promise<void> {
await FileUtils.createShellRcIfNotExists();
const lineToInsert = addLeadingSpacer(
addTrailingSpacer(line)
);
await fs.appendFile(Utils.getPrimaryShellRc(), lineToInsert)
CodifyCliSender.sendApplyNote(ApplyNotes.sourceShellRc());
function addLeadingSpacer(line: string): string {
return line.startsWith('\n')
? line
: '\n' + line;
}
function addTrailingSpacer(line: string): string {
return line.endsWith('\n')
? line
: line + '\n';
}
}
static async addAllToShellRc(lines: string[]): Promise<void> {
await FileUtils.createShellRcIfNotExists();
const formattedLines = '\n' + lines.join('\n') + '\n';
const shellRc = Utils.getPrimaryShellRc();
console.log(`Adding to ${path.basename(shellRc)}:
${lines.join('\n')}`)
await fs.appendFile(shellRc, formattedLines)
CodifyCliSender.sendApplyNote(ApplyNotes.sourceShellRc());
}
/**
* This method adds a directory path to the shell rc file if it doesn't already exist.
*
* @param value - The directory path to add.
* @param prepend - Whether to prepend the path to the existing PATH variable.
*/
static async addPathToShellRc(value: string, prepend: boolean): Promise<void> {
await FileUtils.createShellRcIfNotExists();
if (await Utils.isDirectoryOnPath(value)) {
return;
}
const shellRc = Utils.getPrimaryShellRc();
console.log(`Saving path: ${value} to ${shellRc}`);
if (prepend) {
await fs.appendFile(shellRc, `\nexport PATH=$PATH:${value};`, { encoding: 'utf8' });
} else {
await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
}
CodifyCliSender.sendApplyNote(ApplyNotes.sourceShellRc());
}
static async removeFromFile(filePath: string, search: string): Promise<void> {
const contents = await fs.readFile(filePath, 'utf8');
const newContents = contents.replaceAll(search, '');
await fs.writeFile(filePath, newContents, 'utf8');
}
static async removeLineFromFile(filePath: string, search: RegExp | string): Promise<void> {
const file = await fs.readFile(filePath, 'utf8')
const lines = file.split('\n');
let searchRegex;
let searchString;
if (typeof search === 'object') {
const startRegex = /^([\t ]*)?/;
const endRegex = /([\t ]*)?/;
// Augment regex with spaces criteria to make sure this function is not deleting lines that are comments or has other content.
searchRegex = search
? new RegExp(
startRegex.source + search.source + endRegex.source,
search.flags
)
: search;
}
if (typeof search === 'string') {
searchString = search;
}
for (let counter = lines.length; counter >= 0; counter--) {
if (!lines[counter]) {
continue;
}
if (searchString && lines[counter].includes(searchString)) {
lines.splice(counter, 1);
continue;
}
if (searchRegex && lines[counter].search(searchRegex) !== -1) {
lines.splice(counter, 1);
}
}
await fs.writeFile(filePath, lines.join('\n'));
console.log(`Removed line: ${search} from ${filePath}`)
}
static async removeLineFromShellRc(search: RegExp | string): Promise<void> {
return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
}
static async removeAllLinesFromShellRc(searches: Array<RegExp | string>): Promise<void> {
for (const search of searches) {
await FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
}
}
// Append the string to the end of a file ensuring at least 1 lines of space between.
// Ex result:
// something something;
//
// newline;
static appendToFileWithSpacing(file: string, textToInsert: string): string {
const lines = file.trimEnd().split(/\n/);
if (lines.length === 0) {
return textToInsert;
}
const endingNewLines = FileUtils.calculateEndingNewLines(lines);
const numNewLines = endingNewLines === -1
? 0
: Math.max(0, 2 - endingNewLines);
return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert
}
static async dirExists(path: string): Promise<boolean> {
let stat;
try {
stat = await fs.stat(path);
return stat.isDirectory();
} catch {
return false;
}
}
static async fileExists(path: string): Promise<boolean> {
let stat;
try {
stat = await fs.stat(path);
return stat.isFile();
} catch {
return false;
}
}
static async exists(path: string): Promise<boolean> {
try {
await fs.stat(path);
return true;
} catch {
return false;
}
}
static async checkDirExistsOrThrowIfFile(path: string): Promise<boolean> {
let stat;
try {
stat = await fs.stat(path);
} catch {
return false;
}
if (stat.isDirectory()) {
return true;
}
throw new Error(`Directory ${path} already exists and is a file`);
}
static async createDirIfNotExists(path: string): Promise<void> {
if (!fsSync.existsSync(path)) {
await fs.mkdir(path, { recursive: true });
}
}
// This is overly complicated but it can be used to insert into any
// position in the future
private static calculateEndingNewLines(lines: string[]): number {
let counter = 0;
while (true) {
const line = lines.at(-counter - 1);
if (!line) {
return -1
}
if (!SPACE_REGEX.test(line)) {
return counter;
}
counter++;
// Short circuit here because we don't need to check over 2;
if (counter > 2) {
return counter;
}
}
}
static async createShellRcIfNotExists(): Promise<void> {
const shellRc = Utils.getPrimaryShellRc();
if (!await FileUtils.fileExists(shellRc)) {
await fs.writeFile(shellRc, '', 'utf8');
}
}
}