Skip to content

Commit e33876d

Browse files
committed
refactor: replace Result.try with Result.fromCallable
1 parent 71cb120 commit e33876d

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/common/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const safeParse = <T = any>(
1313
const fn = () => {
1414
return JSON.parse(text, reviver);
1515
};
16-
const result = Result.try<T>(fn, Error).context(
16+
const result = Result.fromCallable<T>(fn, Error).context(
1717
`Failed to parse JSON string: ${text.length > 100 ? text.slice(0, 100) + "..." : text}`,
1818
);
1919

src/common/shell.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export async function $(cmd: string | TemplateStringsArray, ...values: any[]) {
3737

3838
return await promise;
3939
};
40-
const result = (await Result.try(fn, Error)).context(`Failed to execute command: ${cmd}`);
40+
const result = (await Result.fromCallable(fn, Error)).context(
41+
`Failed to execute command: ${cmd}`,
42+
);
4143

4244
return result;
4345
}

src/fs/safe.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export const appendFile = async (
4848
const fn = async () => {
4949
await fsp.appendFile(path, newline ? `\n${data}` : data, parseEncodingOptions(options));
5050
};
51-
const result = (await Result.try(fn, Error)).context(`Failed to append file: ${path}`);
51+
const result = (await Result.fromCallable(fn, Error)).context(
52+
`Failed to append file: ${path}`,
53+
);
5254

5355
return result;
5456
});
@@ -66,7 +68,7 @@ export const appendFileSync = (
6668
const fn = () => {
6769
fs.appendFileSync(path, newline ? `\n${data}` : data, parseEncodingOptions(options));
6870
};
69-
const result = Result.try(fn, Error).context(`Failed to append file: ${path}`);
71+
const result = Result.fromCallable(fn, Error).context(`Failed to append file: ${path}`);
7072

7173
return result;
7274
});
@@ -81,7 +83,7 @@ export const cp = async (
8183
const fn = async () => {
8284
await fsp.cp(source, destination, { recursive });
8385
};
84-
const result = (await Result.try(fn, Error)).context(
86+
const result = (await Result.fromCallable(fn, Error)).context(
8587
`Failed to copy path: ${source} to ${destination}`,
8688
);
8789

@@ -98,7 +100,7 @@ export const cpSync = (
98100
const fn = () => {
99101
fs.cpSync(source, destination, { recursive });
100102
};
101-
const result = Result.try(fn, Error).context(
103+
const result = Result.fromCallable(fn, Error).context(
102104
`Failed to copy path: ${source} to ${destination}`,
103105
);
104106

@@ -111,7 +113,7 @@ export const exists = async (path: PathLike): Promise<Result<true, Error>> => {
111113

112114
return true as const;
113115
};
114-
const result = (await Result.try(fn, Error)).context(
116+
const result = (await Result.fromCallable(fn, Error)).context(
115117
`Failed to check exists for path: ${path}`,
116118
);
117119

@@ -124,7 +126,9 @@ export const existsSync = (path: PathLike): Result<true, Error> => {
124126

125127
return true as const;
126128
};
127-
const result = Result.try(fn, Error).context(`Failed to check exists for path: ${path}`);
129+
const result = Result.fromCallable(fn, Error).context(
130+
`Failed to check exists for path: ${path}`,
131+
);
128132

129133
return result;
130134
};
@@ -140,7 +144,9 @@ export const mkdir = async (
140144
const fn = async () => {
141145
await fsp.mkdir(path, { recursive });
142146
};
143-
const result = (await Result.try(fn, Error)).context(`Failed to create directory: ${path}`);
147+
const result = (await Result.fromCallable(fn, Error)).context(
148+
`Failed to create directory: ${path}`,
149+
);
144150

145151
return result;
146152
};
@@ -153,7 +159,7 @@ export const mkdirSync = (path: PathLike, options?: MkdirOptions): Result<void,
153159
const fn = () => {
154160
fs.mkdirSync(path, { recursive });
155161
};
156-
const result = Result.try(fn, Error).context(`Failed to create directory: ${path}`);
162+
const result = Result.fromCallable(fn, Error).context(`Failed to create directory: ${path}`);
157163

158164
return result;
159165
};
@@ -173,7 +179,9 @@ export async function readFile(path: any, options?: any): Promise<Result<any, Er
173179
const fn = async () => {
174180
return await fsp.readFile(path, parseEncodingOptions(options));
175181
};
176-
const result = (await Result.try(fn, Error)).context(`Failed to read file: ${path}`);
182+
const result = (await Result.fromCallable(fn, Error)).context(
183+
`Failed to read file: ${path}`,
184+
);
177185

178186
return result;
179187
});
@@ -191,7 +199,7 @@ export function readFileSync(path: any, options?: any): Result<any, Error> {
191199
const fn = () => {
192200
return fs.readFileSync(path, parseEncodingOptions(options));
193201
};
194-
const result = Result.try(fn, Error).context(`Failed to read file: ${path}`);
202+
const result = Result.fromCallable(fn, Error).context(`Failed to read file: ${path}`);
195203

196204
return result;
197205
});
@@ -222,7 +230,7 @@ export const readFileByLine = async (
222230

223231
return reader[Symbol.asyncIterator]();
224232
};
225-
const result = Result.try(fn, Error).context(`Failed to read file: ${path}`);
233+
const result = Result.fromCallable(fn, Error).context(`Failed to read file: ${path}`);
226234

227235
return result;
228236
});
@@ -259,7 +267,7 @@ export const rm = async (path: PathLike, options?: RmOptions): Promise<Result<vo
259267
const fn = async () => {
260268
await fsp.rm(path, { force, recursive });
261269
};
262-
const result = (await Result.try(fn, Error)).context(`Failed to remove path: ${path}`);
270+
const result = (await Result.fromCallable(fn, Error)).context(`Failed to remove path: ${path}`);
263271

264272
return result;
265273
};
@@ -270,7 +278,7 @@ export const rmSync = (path: PathLike, options?: RmOptions): Result<void, Error>
270278
const fn = () => {
271279
fs.rmSync(path, { force, recursive });
272280
};
273-
const result = Result.try(fn, Error).context(`Failed to remove path: ${path}`);
281+
const result = Result.fromCallable(fn, Error).context(`Failed to remove path: ${path}`);
274282

275283
return result;
276284
};
@@ -286,7 +294,9 @@ export const writeFile = async (
286294
const fn = async () => {
287295
await fsp.writeFile(path, data, parseEncodingOptions(options));
288296
};
289-
const result = (await Result.try(fn, Error)).context(`Failed to write file: ${path}`);
297+
const result = (await Result.fromCallable(fn, Error)).context(
298+
`Failed to write file: ${path}`,
299+
);
290300

291301
return result;
292302
});
@@ -299,7 +309,7 @@ export const writeFileSync = (
299309
const fn = () => {
300310
fs.writeFileSync(path, data, parseEncodingOptions(options));
301311
};
302-
const result = Result.try(fn, Error).context(`Failed to write file: ${path}`);
312+
const result = Result.fromCallable(fn, Error).context(`Failed to write file: ${path}`);
303313

304314
return result;
305315
};

0 commit comments

Comments
 (0)