fs-fixture 2.5.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Tiny (`560 B` gzipped) and no dependencies!
|
|
|
13
13
|
|
|
14
14
|
### Example
|
|
15
15
|
```ts
|
|
16
|
-
import fs from 'fs/promises'
|
|
16
|
+
import fs from 'node:fs/promises'
|
|
17
17
|
import { createFixture } from 'fs-fixture'
|
|
18
18
|
|
|
19
19
|
const fixture = await createFixture({
|
|
@@ -86,7 +86,7 @@ await using fixture = await createFixture({ file: 'hello' })
|
|
|
86
86
|
|
|
87
87
|
## API
|
|
88
88
|
|
|
89
|
-
### createFixture(source)
|
|
89
|
+
### createFixture(source, options)
|
|
90
90
|
|
|
91
91
|
An async function that creates a fixture from the `source` you pass in, and returns a `FsFixture` instance.
|
|
92
92
|
|
|
@@ -96,6 +96,23 @@ Type: `string | FileTree`
|
|
|
96
96
|
Path to a template fixture path, or a `FileTree` object that represents the fixture content.
|
|
97
97
|
|
|
98
98
|
|
|
99
|
+
#### options
|
|
100
|
+
|
|
101
|
+
##### tempDir
|
|
102
|
+
|
|
103
|
+
Type: `string`
|
|
104
|
+
|
|
105
|
+
Default: `os.tmpdir()`
|
|
106
|
+
|
|
107
|
+
The directory where the fixture will be created.
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
##### templateFilter
|
|
111
|
+
|
|
112
|
+
Type: `(source: string, destination: string) => boolean | Promise<boolean>`
|
|
113
|
+
|
|
114
|
+
Function to filter files to copy when using a template path. Return `true` to copy the item, `false` to ignore it.
|
|
115
|
+
|
|
99
116
|
### Types
|
|
100
117
|
#### FileTree
|
|
101
118
|
|
|
@@ -148,6 +165,11 @@ class FsFixture {
|
|
|
148
165
|
*/
|
|
149
166
|
rm(subpath?: string): Promise<void>
|
|
150
167
|
|
|
168
|
+
/**
|
|
169
|
+
Copy a path into the fixture directory.
|
|
170
|
+
*/
|
|
171
|
+
cp(sourcePath: string, destinationSubpath?: string): Promise<void>
|
|
172
|
+
|
|
151
173
|
/**
|
|
152
174
|
Create a file in the fixture directory.
|
|
153
175
|
*/
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var m=Object.defineProperty;var
|
|
1
|
+
"use strict";var m=Object.defineProperty;var c=(i,t)=>m(i,"name",{value:t,configurable:!0});var a=require("node:fs/promises"),o=require("node:path"),w=require("node:fs"),g=require("node:os");typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class b{static{c(this,"FsFixture")}path;constructor(t){this.path=t}getPath(...t){return o.join(this.path,...t)}exists(t=""){return a.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return a.rm(this.getPath(t),{recursive:!0,force:!0})}cp(t,r,s){return a.cp(t,this.getPath(r),s)}writeFile(t,r){return a.writeFile(this.getPath(t),r)}writeJson(t,r){return this.writeFile(t,JSON.stringify(r,null,2))}readFile(t,r){return a.readFile(this.getPath(t),r)}async[Symbol.asyncDispose](){await this.rm()}}const F=w.realpathSync(g.tmpdir()),P=`fs-fixture-${Date.now()}`;let p=0;const d=c(()=>(p+=1,p),"getId");class h{static{c(this,"Symlink")}target;type;path;constructor(t,r){this.target=t,this.type=r}}const y=c((i,t,r)=>{const s=[];for(const u in i){if(!Object.hasOwn(i,u))continue;const e=o.join(t,u);let n=i[u];if(typeof n=="function"){const f=Object.assign(Object.create(r),{filePath:e}),l=n(f);if(l instanceof h){l.path=e,s.push(l);continue}else n=l}typeof n=="string"?s.push({path:e,content:n}):s.push(...y(n,e,r))}return s},"flattenFileTree"),v=c(async(i,t)=>{const r=t?.tempDir?o.resolve(t.tempDir):F,s=o.join(r,`${P}-${d()}/`);if(await a.mkdir(s,{recursive:!0}),i){if(typeof i=="string")await a.cp(i,s,{recursive:!0,filter:t?.templateFilter});else if(typeof i=="object"){const u={fixturePath:s,getPath:c((...e)=>o.join(s,...e),"getPath"),symlink:c((e,n)=>new h(e,n),"symlink")};await Promise.all(y(i,s,u).map(async e=>{await a.mkdir(o.dirname(e.path),{recursive:!0}),e instanceof h?await a.symlink(e.target,e.path,e.type):await a.writeFile(e.path,e.content)}))}}return new b(s)},"createFixture");exports.createFixture=v;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { CopyOptions } from 'node:fs';
|
|
2
|
+
|
|
1
3
|
declare class FsFixture {
|
|
2
4
|
/**
|
|
3
5
|
Path to the fixture directory.
|
|
@@ -20,6 +22,10 @@ declare class FsFixture {
|
|
|
20
22
|
*/
|
|
21
23
|
rm(subpath?: string): Promise<void>;
|
|
22
24
|
/**
|
|
25
|
+
Copy a path into the fixture directory.
|
|
26
|
+
*/
|
|
27
|
+
cp(sourcePath: string, destinationSubpath: string, options?: CopyOptions): Promise<void>;
|
|
28
|
+
/**
|
|
23
29
|
Create a file in the fixture directory.
|
|
24
30
|
*/
|
|
25
31
|
writeFile(filePath: string, content: string): Promise<void>;
|
|
@@ -40,6 +46,7 @@ declare class FsFixture {
|
|
|
40
46
|
}
|
|
41
47
|
type FsFixtureType = FsFixture;
|
|
42
48
|
|
|
49
|
+
type FilterFunction = CopyOptions['filter'];
|
|
43
50
|
type SymlinkType = 'file' | 'dir' | 'junction';
|
|
44
51
|
declare class Symlink {
|
|
45
52
|
target: string;
|
|
@@ -68,6 +75,11 @@ type CreateFixtureOptions = {
|
|
|
68
75
|
* Defaults to `os.tmpdir()`.
|
|
69
76
|
*/
|
|
70
77
|
tempDir?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Function to filter files to copy when using a template path.
|
|
80
|
+
* Return `true` to copy the item, `false` to ignore it.
|
|
81
|
+
*/
|
|
82
|
+
templateFilter?: FilterFunction;
|
|
71
83
|
};
|
|
72
84
|
declare const createFixture: (source?: string | FileTree, options?: CreateFixtureOptions) => Promise<FsFixture>;
|
|
73
85
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { CopyOptions } from 'node:fs';
|
|
2
|
+
|
|
1
3
|
declare class FsFixture {
|
|
2
4
|
/**
|
|
3
5
|
Path to the fixture directory.
|
|
@@ -20,6 +22,10 @@ declare class FsFixture {
|
|
|
20
22
|
*/
|
|
21
23
|
rm(subpath?: string): Promise<void>;
|
|
22
24
|
/**
|
|
25
|
+
Copy a path into the fixture directory.
|
|
26
|
+
*/
|
|
27
|
+
cp(sourcePath: string, destinationSubpath: string, options?: CopyOptions): Promise<void>;
|
|
28
|
+
/**
|
|
23
29
|
Create a file in the fixture directory.
|
|
24
30
|
*/
|
|
25
31
|
writeFile(filePath: string, content: string): Promise<void>;
|
|
@@ -40,6 +46,7 @@ declare class FsFixture {
|
|
|
40
46
|
}
|
|
41
47
|
type FsFixtureType = FsFixture;
|
|
42
48
|
|
|
49
|
+
type FilterFunction = CopyOptions['filter'];
|
|
43
50
|
type SymlinkType = 'file' | 'dir' | 'junction';
|
|
44
51
|
declare class Symlink {
|
|
45
52
|
target: string;
|
|
@@ -68,6 +75,11 @@ type CreateFixtureOptions = {
|
|
|
68
75
|
* Defaults to `os.tmpdir()`.
|
|
69
76
|
*/
|
|
70
77
|
tempDir?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Function to filter files to copy when using a template path.
|
|
80
|
+
* Return `true` to copy the item, `false` to ignore it.
|
|
81
|
+
*/
|
|
82
|
+
templateFilter?: FilterFunction;
|
|
71
83
|
};
|
|
72
84
|
declare const createFixture: (source?: string | FileTree, options?: CreateFixtureOptions) => Promise<FsFixture>;
|
|
73
85
|
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var y=Object.defineProperty;var o=(
|
|
1
|
+
var y=Object.defineProperty;var o=(i,t)=>y(i,"name",{value:t,configurable:!0});import a from"node:fs/promises";import c from"node:path";import w from"node:fs";import g from"node:os";typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class b{static{o(this,"FsFixture")}path;constructor(t){this.path=t}getPath(...t){return c.join(this.path,...t)}exists(t=""){return a.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return a.rm(this.getPath(t),{recursive:!0,force:!0})}cp(t,r,s){return a.cp(t,this.getPath(r),s)}writeFile(t,r){return a.writeFile(this.getPath(t),r)}writeJson(t,r){return this.writeFile(t,JSON.stringify(r,null,2))}readFile(t,r){return a.readFile(this.getPath(t),r)}async[Symbol.asyncDispose](){await this.rm()}}const P=w.realpathSync(g.tmpdir()),d=`fs-fixture-${Date.now()}`;let u=0;const F=o(()=>(u+=1,u),"getId");class h{static{o(this,"Symlink")}target;type;path;constructor(t,r){this.target=t,this.type=r}}const f=o((i,t,r)=>{const s=[];for(const p in i){if(!Object.hasOwn(i,p))continue;const e=c.join(t,p);let n=i[p];if(typeof n=="function"){const m=Object.assign(Object.create(r),{filePath:e}),l=n(m);if(l instanceof h){l.path=e,s.push(l);continue}else n=l}typeof n=="string"?s.push({path:e,content:n}):s.push(...f(n,e,r))}return s},"flattenFileTree"),j=o(async(i,t)=>{const r=t?.tempDir?c.resolve(t.tempDir):P,s=c.join(r,`${d}-${F()}/`);if(await a.mkdir(s,{recursive:!0}),i){if(typeof i=="string")await a.cp(i,s,{recursive:!0,filter:t?.templateFilter});else if(typeof i=="object"){const p={fixturePath:s,getPath:o((...e)=>c.join(s,...e),"getPath"),symlink:o((e,n)=>new h(e,n),"symlink")};await Promise.all(f(i,s,p).map(async e=>{await a.mkdir(c.dirname(e.path),{recursive:!0}),e instanceof h?await a.symlink(e.target,e.path,e.type):await a.writeFile(e.path,e.content)}))}}return new b(s)},"createFixture");export{j as createFixture};
|