Skip to content

Commit b4b5fd7

Browse files
committed
feat: export FsFixture type
1 parent 22e878f commit b4b5fd7

File tree

3 files changed

+28
-50
lines changed

3 files changed

+28
-50
lines changed

README.md

-5
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ class FsFixture {
9393
*/
9494
constructor(fixturePath: string)
9595

96-
/**
97-
Static method to create a fixture from a template directory.
98-
*/
99-
static createFromTemplate(fromTemplatePath: string): Promise<Fixture>
100-
10196
/**
10297
Check if the fixture exists. Pass in a subpath to check if it exists.
10398
*/

src/fs-fixture.ts

-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { promises as fsPromises } from 'fs';
22
import path from 'path';
3-
import { temporaryDirectory, getId } from './utils';
43

54
class FsFixture {
65
/**
@@ -15,39 +14,6 @@ class FsFixture {
1514
this.path = fixturePath;
1615
}
1716

18-
/**
19-
Static method to create a fixture from a template directory.
20-
*/
21-
static async createFromTemplate(
22-
fromTemplatePath: string,
23-
) {
24-
const fixturePath = path.resolve(
25-
temporaryDirectory,
26-
`${path.basename(fromTemplatePath)}-${getId()}`,
27-
);
28-
29-
const fixture = new this(fixturePath);
30-
31-
if (await fixture.exists()) {
32-
await fixture.rm();
33-
}
34-
35-
await fsPromises.mkdir(fixture.path, {
36-
recursive: true,
37-
});
38-
39-
await fsPromises.cp(
40-
fromTemplatePath,
41-
fixture.path,
42-
{
43-
recursive: true,
44-
filter: source => !path.basename(source).startsWith('.'),
45-
},
46-
);
47-
48-
return fixture;
49-
}
50-
5117
/**
5218
Check if the fixture exists. Pass in a subpath to check if it exists.
5319
*/

src/index.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import fs from 'fs';
1+
import { promises as fs } from 'fs';
22
import path from 'path';
33
import FsFixture from './fs-fixture';
44
import { temporaryDirectory, hasOwn, getId } from './utils';
55

6+
export type { FsFixture };
7+
68
export type FileTree = {
79
[path: string]: string | FileTree;
810
};
@@ -40,20 +42,35 @@ function flattenFileTree(
4042
export async function createFixture(
4143
source: string | FileTree,
4244
) {
45+
let fixturePath: string;
46+
4347
// create from directory path
4448
if (typeof source === 'string') {
45-
return await FsFixture.createFromTemplate(source);
46-
}
49+
fixturePath = path.join(temporaryDirectory, `${path.basename(source)}-${getId()}`);
4750

48-
// create from json
49-
const fixturePath = path.join(temporaryDirectory, `fixture-${getId()}`);
51+
await fs.mkdir(fixturePath, {
52+
recursive: true,
53+
});
5054

51-
await Promise.all(
52-
flattenFileTree(source, fixturePath).map(async (file) => {
53-
await fs.promises.mkdir(path.dirname(file.path), { recursive: true });
54-
await fs.promises.writeFile(file.path, file.content);
55-
}),
56-
);
55+
await fs.cp(
56+
source,
57+
fixturePath,
58+
{
59+
recursive: true,
60+
// filter: source => !path.basename(source).startsWith('.'),
61+
},
62+
);
63+
} else {
64+
// create from json
65+
fixturePath = path.join(temporaryDirectory, `fixture-${getId()}`);
66+
67+
await Promise.all(
68+
flattenFileTree(source, fixturePath).map(async (file) => {
69+
await fs.mkdir(path.dirname(file.path), { recursive: true });
70+
await fs.writeFile(file.path, file.content);
71+
}),
72+
);
73+
}
5774

5875
return new FsFixture(fixturePath);
5976
}

0 commit comments

Comments
 (0)