fs-fixture 1.2.0 → 2.0.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 CHANGED
@@ -1,45 +1,54 @@
1
- # fs-fixture
1
+ # fs-fixture [![Latest version](https://badgen.net/npm/v/fs-fixture)](https://npm.im/fs-fixture) [![npm downloads](https://badgen.net/npm/dm/fs-fixture)](https://npm.im/fs-fixture)
2
2
 
3
- Easily create test fixtures at a temporary file-system path.
3
+ Simple API to create disposable test fixtures on disk.
4
4
 
5
- <sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
6
-
7
- ## Usage
8
-
9
- ### JSON input
10
-
11
- Pass in an object representing the test fixture.
5
+ Tiny (`560 B` gzipped) and no dependencies!
12
6
 
7
+ ### Example
13
8
  ```ts
9
+ import fs from 'fs/promises'
14
10
  import { createFixture } from 'fs-fixture'
15
11
 
16
- test('my test using json fixture', async () => {
17
- // Pass in a JSON representing the test fixture
18
- const fixture = await createFixture({
19
- // Nested directory syntax
20
- directoryA: {
21
- directoryB: {
22
- fileNameA: 'fileContent'
23
- }
24
- },
12
+ const fixture = await createFixture({
13
+ 'dir-a': {
14
+ 'file-b': 'hello world'
15
+ }
16
+ })
17
+
18
+ const content = await fs.readFile(fixture.getPath('dir-a/file-b'))
19
+ console.log(content)
20
+ ```
25
21
 
26
- // Directory path syntax - Same as above
27
- 'directoryA/directoryB/fileNameB': 'fileContent'
28
- })
22
+ <p align="center">
23
+ <a href="https://github.com/sponsors/privatenumber/sponsorships?tier_id=398771"><img width="412" src="https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/donate.webp"></a>
24
+ <a href="https://github.com/sponsors/privatenumber/sponsorships?tier_id=397608"><img width="412" src="https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/sponsor.webp"></a>
25
+ </p>
26
+ <p align="center"><sup><i>Already a sponsor?</i> Join the discussion in the <a href="https://github.com/pvtnbr/fs-fixture">Development repo</a>!</sup></p>
29
27
 
30
- /*
31
- Your test code here...
28
+ ## Usage
32
29
 
33
- // Log fixture path
34
- console.log(fixture.path)
30
+ Pass in an object representing the file structure:
35
31
 
36
- // Check if relative path exists
37
- console.log(await fixture.exists('./file'))
38
- */
32
+ ```ts
33
+ import { createFixture } from 'fs-fixture'
34
+
35
+ const fixture = await createFixture({
36
+ // Nested directory syntax
37
+ 'dir-a': {
38
+ 'dir-b': {
39
+ 'file-a.txt': 'hello world'
40
+ }
41
+ },
39
42
 
40
- // Cleanup fixture
41
- await fixture.rm()
43
+ // Alternatively, use the directory path syntax - Same as above
44
+ 'dir-a/dir-b/file-b.txt': 'goodbye world'
42
45
  })
46
+
47
+ // Interact with the fixture
48
+ console.log(fixture.path)
49
+
50
+ // Cleanup fixture
51
+ await fixture.rm()
43
52
  ```
44
53
 
45
54
  ### Template path input
@@ -47,15 +56,23 @@ test('my test using json fixture', async () => {
47
56
  Pass in a path to a test fixture template directory to make a copy of it.
48
57
 
49
58
  ```ts
50
- test('my test using template path', async () => {
51
- // Pass in a path to a fixture template path, and it will make a copy of it
52
- const fixture = await createFixture('./fixtures/template-a')
59
+ // Pass in a path to a fixture template path, and it will make a copy of it
60
+ const fixture = await createFixture('./fixtures/template-a')
53
61
 
54
- /* Your test code here... */
62
+ /* Your test code here... */
55
63
 
56
- // Cleanup fixture
57
- await fixture.rm()
58
- })
64
+ // Cleanup fixture
65
+ await fixture.rm()
66
+ ```
67
+
68
+ ### `using` keyword (Explicit Resource Management)
69
+
70
+ [TypeScript 5.2](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html) supports the [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) feature, which allows you to instantiate the fixture via `using`. When the fixture is declared this way, it gets automatically cleaned up when exiting the scope.
71
+
72
+ ```ts
73
+ await using fixture = await createFixture({ file: 'hello' })
74
+
75
+ // No need to run fixture.rm()
59
76
  ```
60
77
 
61
78
  ## API
@@ -93,6 +110,11 @@ class FsFixture {
93
110
  */
94
111
  constructor(fixturePath: string)
95
112
 
113
+ /**
114
+ Get the full path to a subpath in the fixture directory.
115
+ */
116
+ getPath(subpath: string): string
117
+
96
118
  /**
97
119
  Check if the fixture exists. Pass in a subpath to check if it exists.
98
120
  */
@@ -111,7 +133,7 @@ class FsFixture {
111
133
  /**
112
134
  Create a JSON file in the fixture directory.
113
135
  */
114
- writeJson(filePath: string, json: any): Promise<void>
136
+ writeJson(filePath: string, json: unknown): Promise<void>
115
137
 
116
138
  /**
117
139
  Read a file from the fixture directory.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var i=require("fs"),c=require("path"),l=require("os");function o(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var p=o(i),a=o(c),d=o(l);class h{path;constructor(t){this.path=t}exists(t=""){return i.promises.access(a.default.join(this.path,t)).then(()=>!0,()=>!1)}rm(t=""){return i.promises.rm(a.default.join(this.path,t),{recursive:!0,force:!0})}writeFile(t,r){return i.promises.writeFile(a.default.join(this.path,t),r)}writeJson(t,r){return this.writeFile(t,JSON.stringify(r,null,2))}readFile(t,r){return i.promises.readFile(a.default.join(this.path,t),r)}}const m=p.default.realpathSync(d.default.tmpdir()),y=`fs-fixture-${Date.now()}`;let u=0;function w(){return u+=1,u}const{hasOwnProperty:j}=Object.prototype,v=(e,t)=>j.call(e,t);function f(e,t){const r=[];for(const s in e){if(!v(e,s))continue;const n=e[s];typeof n=="string"?r.push({path:a.default.join(t,s),content:n}):r.push(...f(n,a.default.join(t,s)))}return r}async function F(e){const t=a.default.join(m,`${y}-${w()}`);return await i.promises.mkdir(t,{recursive:!0}),e&&(typeof e=="string"?await i.promises.cp(e,t,{recursive:!0}):typeof e=="object"&&await Promise.all(f(e,t).map(async r=>{await i.promises.mkdir(a.default.dirname(r.path),{recursive:!0}),await i.promises.writeFile(r.path,r.content)}))),new h(t)}exports.createFixture=F;
1
+ "use strict";var i=require("fs/promises"),s=require("path"),u=require("fs"),l=require("os");typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class h{path;constructor(t){this.path=t}getPath(t){return s.join(this.path,t)}exists(t=""){return i.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return i.rm(this.getPath(t),{recursive:!0,force:!0})}writeFile(t,e){return i.writeFile(this.getPath(t),e)}writeJson(t,e){return this.writeFile(t,JSON.stringify(e,null,2))}readFile(t,e){return i.readFile(this.getPath(t),e)}async[Symbol.asyncDispose](){await this.rm()}}const f=u.realpathSync(l.tmpdir()),p=`fs-fixture-${Date.now()}`;let o=0;const y=()=>(o+=1,o),c=(r,t)=>{const e=[];for(const a in r){if(!Object.hasOwn(r,a))continue;const n=r[a];typeof n=="string"?e.push({path:s.join(t,a),content:n}):e.push(...c(n,s.join(t,a)))}return e},m=async r=>{const t=s.join(f,`${p}-${y()}`);return await i.mkdir(t,{recursive:!0}),r&&(typeof r=="string"?await i.cp(r,t,{recursive:!0}):typeof r=="object"&&await Promise.all(c(r,t).map(async e=>{await i.mkdir(s.dirname(e.path),{recursive:!0}),await i.writeFile(e.path,e.content)}))),new h(t)};exports.createFixture=m;
package/dist/index.d.cts CHANGED
@@ -8,6 +8,10 @@ declare class FsFixture {
8
8
  */
9
9
  constructor(fixturePath: string);
10
10
  /**
11
+ Get the full path to a subpath in the fixture directory.
12
+ */
13
+ getPath(subpath: string): string;
14
+ /**
11
15
  Check if the fixture exists. Pass in a subpath to check if it exists.
12
16
  */
13
17
  exists(subpath?: string): Promise<boolean>;
@@ -22,16 +26,21 @@ declare class FsFixture {
22
26
  /**
23
27
  Create a JSON file in the fixture directory.
24
28
  */
25
- writeJson(filePath: string, json: any): Promise<void>;
29
+ writeJson(filePath: string, json: unknown): Promise<void>;
26
30
  /**
27
31
  Read a file from the fixture directory.
28
32
  */
29
33
  readFile(filePath: string, encoding?: BufferEncoding): Promise<string | Buffer>;
34
+ /**
35
+ * Resource management cleanup
36
+ * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
37
+ */
38
+ [Symbol.asyncDispose](): Promise<void>;
30
39
  }
31
40
 
32
41
  type FileTree = {
33
42
  [path: string]: string | FileTree;
34
43
  };
35
- declare function createFixture(source?: string | FileTree): Promise<FsFixture>;
44
+ declare const createFixture: (source?: string | FileTree) => Promise<FsFixture>;
36
45
 
37
- export { FileTree, FsFixture, createFixture };
46
+ export { type FileTree, FsFixture, createFixture };
package/dist/index.d.mts CHANGED
@@ -8,6 +8,10 @@ declare class FsFixture {
8
8
  */
9
9
  constructor(fixturePath: string);
10
10
  /**
11
+ Get the full path to a subpath in the fixture directory.
12
+ */
13
+ getPath(subpath: string): string;
14
+ /**
11
15
  Check if the fixture exists. Pass in a subpath to check if it exists.
12
16
  */
13
17
  exists(subpath?: string): Promise<boolean>;
@@ -22,16 +26,21 @@ declare class FsFixture {
22
26
  /**
23
27
  Create a JSON file in the fixture directory.
24
28
  */
25
- writeJson(filePath: string, json: any): Promise<void>;
29
+ writeJson(filePath: string, json: unknown): Promise<void>;
26
30
  /**
27
31
  Read a file from the fixture directory.
28
32
  */
29
33
  readFile(filePath: string, encoding?: BufferEncoding): Promise<string | Buffer>;
34
+ /**
35
+ * Resource management cleanup
36
+ * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
37
+ */
38
+ [Symbol.asyncDispose](): Promise<void>;
30
39
  }
31
40
 
32
41
  type FileTree = {
33
42
  [path: string]: string | FileTree;
34
43
  };
35
- declare function createFixture(source?: string | FileTree): Promise<FsFixture>;
44
+ declare const createFixture: (source?: string | FileTree) => Promise<FsFixture>;
36
45
 
37
- export { FileTree, FsFixture, createFixture };
46
+ export { type FileTree, FsFixture, createFixture };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import p,{promises as i}from"fs";import n from"path";import u from"os";class h{path;constructor(t){this.path=t}exists(t=""){return i.access(n.join(this.path,t)).then(()=>!0,()=>!1)}rm(t=""){return i.rm(n.join(this.path,t),{recursive:!0,force:!0})}writeFile(t,e){return i.writeFile(n.join(this.path,t),e)}writeJson(t,e){return this.writeFile(t,JSON.stringify(e,null,2))}readFile(t,e){return i.readFile(n.join(this.path,t),e)}}const f=p.realpathSync(u.tmpdir()),l=`fs-fixture-${Date.now()}`;let a=0;function m(){return a+=1,a}const{hasOwnProperty:w}=Object.prototype,y=(r,t)=>w.call(r,t);function c(r,t){const e=[];for(const o in r){if(!y(r,o))continue;const s=r[o];typeof s=="string"?e.push({path:n.join(t,o),content:s}):e.push(...c(s,n.join(t,o)))}return e}async function F(r){const t=n.join(f,`${l}-${m()}`);return await i.mkdir(t,{recursive:!0}),r&&(typeof r=="string"?await i.cp(r,t,{recursive:!0}):typeof r=="object"&&await Promise.all(c(r,t).map(async e=>{await i.mkdir(n.dirname(e.path),{recursive:!0}),await i.writeFile(e.path,e.content)}))),new h(t)}export{F as createFixture};
1
+ import i from"fs/promises";import s from"path";import l from"fs";import f from"os";typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class p{path;constructor(t){this.path=t}getPath(t){return s.join(this.path,t)}exists(t=""){return i.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return i.rm(this.getPath(t),{recursive:!0,force:!0})}writeFile(t,e){return i.writeFile(this.getPath(t),e)}writeJson(t,e){return this.writeFile(t,JSON.stringify(e,null,2))}readFile(t,e){return i.readFile(this.getPath(t),e)}async[Symbol.asyncDispose](){await this.rm()}}const u=l.realpathSync(f.tmpdir()),h=`fs-fixture-${Date.now()}`;let o=0;const m=()=>(o+=1,o),c=(r,t)=>{const e=[];for(const a in r){if(!Object.hasOwn(r,a))continue;const n=r[a];typeof n=="string"?e.push({path:s.join(t,a),content:n}):e.push(...c(n,s.join(t,a)))}return e},y=async r=>{const t=s.join(u,`${h}-${m()}`);return await i.mkdir(t,{recursive:!0}),r&&(typeof r=="string"?await i.cp(r,t,{recursive:!0}):typeof r=="object"&&await Promise.all(c(r,t).map(async e=>{await i.mkdir(s.dirname(e.path),{recursive:!0}),await i.writeFile(e.path,e.content)}))),new p(t)};export{y as createFixture};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fs-fixture",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "Easily create test fixtures at a temporary file-system path",
5
5
  "keywords": [
6
6
  "test",
@@ -43,6 +43,6 @@
43
43
  }
44
44
  },
45
45
  "engines": {
46
- "node": ">=16.7.0"
46
+ "node": ">=18.0.0"
47
47
  }
48
48
  }