Skip to content

Commit e877271

Browse files
committed
TempContext - new createFileFromString method
1 parent 2ff847b commit e877271

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/core/temp-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
export interface TempContext {
88
baseDir: string;
9+
createFileFromString: (
10+
content: string,
11+
options?: Deno.MakeTempOptions,
12+
) => string;
913
createFile: (options?: Deno.MakeTempOptions) => string;
1014
createDir: (options?: Deno.MakeTempOptions) => string;
1115
cleanup: () => void;

src/core/temp.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ export function createTempContext(options?: Deno.MakeTempOptions): TempContext {
6666

6767
const tempContextCleanupHandlers: VoidFunction[] = [];
6868

69-
return {
69+
const result: TempContext = {
7070
baseDir: dir,
71+
createFileFromString: (content: string, options?: Deno.MakeTempOptions) => {
72+
const file = result.createFile(options);
73+
Deno.writeTextFileSync(file, content);
74+
return file;
75+
},
7176
createFile: (options?: Deno.MakeTempOptions) => {
7277
return Deno.makeTempFileSync({ ...options, dir });
7378
},
@@ -93,6 +98,7 @@ export function createTempContext(options?: Deno.MakeTempOptions): TempContext {
9398
tempContextCleanupHandlers.push(handler);
9499
},
95100
};
101+
return result;
96102
}
97103

98104
export function systemTempDir(name: string) {

0 commit comments

Comments
 (0)