|
1 |
| -import fs from 'fs'; |
| 1 | +import { promises as fs } from 'fs'; |
2 | 2 | import path from 'path';
|
3 | 3 | import FsFixture from './fs-fixture';
|
4 | 4 | import { temporaryDirectory, hasOwn, getId } from './utils';
|
5 | 5 |
|
| 6 | +export type { FsFixture }; |
| 7 | + |
6 | 8 | export type FileTree = {
|
7 | 9 | [path: string]: string | FileTree;
|
8 | 10 | };
|
@@ -40,20 +42,35 @@ function flattenFileTree(
|
40 | 42 | export async function createFixture(
|
41 | 43 | source: string | FileTree,
|
42 | 44 | ) {
|
| 45 | + let fixturePath: string; |
| 46 | + |
43 | 47 | // create from directory path
|
44 | 48 | if (typeof source === 'string') {
|
45 |
| - return await FsFixture.createFromTemplate(source); |
46 |
| - } |
| 49 | + fixturePath = path.join(temporaryDirectory, `${path.basename(source)}-${getId()}`); |
47 | 50 |
|
48 |
| - // create from json |
49 |
| - const fixturePath = path.join(temporaryDirectory, `fixture-${getId()}`); |
| 51 | + await fs.mkdir(fixturePath, { |
| 52 | + recursive: true, |
| 53 | + }); |
50 | 54 |
|
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 | + } |
57 | 74 |
|
58 | 75 | return new FsFixture(fixturePath);
|
59 | 76 | }
|
0 commit comments