fs-fixture 2.0.0 → 2.1.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,4 +1,11 @@
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)
1
+ <p align="center">
2
+ <img width="160" src=".github/logo.webp">
3
+ </p>
4
+ <h1 align="center">
5
+ <sup>fs-fixture</sup>
6
+ <br>
7
+ <a href="https://npm.im/fs-fixture"><img src="https://badgen.net/npm/v/fs-fixture"></a> <a href="https://npm.im/fs-fixture"><img src="https://badgen.net/npm/dm/fs-fixture"></a>
8
+ </h1>
2
9
 
3
10
  Simple API to create disposable test fixtures on disk.
4
11
 
@@ -36,7 +43,8 @@ const fixture = await createFixture({
36
43
  // Nested directory syntax
37
44
  'dir-a': {
38
45
  'dir-b': {
39
- 'file-a.txt': 'hello world'
46
+ 'file-a.txt': 'hello world',
47
+ 'file-b.txt': ({ fixturePath }) => `Fixture path: ${fixturePath}`
40
48
  }
41
49
  },
42
50
 
@@ -103,7 +111,7 @@ class FsFixture {
103
111
  /**
104
112
  Path to the fixture directory.
105
113
  */
106
- path: string
114
+ readonly path: string
107
115
 
108
116
  /**
109
117
  Create a Fixture instance from a path. Does not create the fixture directory.
@@ -111,8 +119,8 @@ class FsFixture {
111
119
  constructor(fixturePath: string)
112
120
 
113
121
  /**
114
- Get the full path to a subpath in the fixture directory.
115
- */
122
+ Get the full path to a subpath in the fixture directory.
123
+ */
116
124
  getPath(subpath: string): string
117
125
 
118
126
  /**
package/dist/index.cjs CHANGED
@@ -1 +1 @@
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;
1
+ "use strict";var i=require("fs/promises"),a=require("path"),l=require("fs"),f=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 a.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 p=l.realpathSync(f.tmpdir()),y=`fs-fixture-${Date.now()}`;let c=0;const m=()=>(c+=1,c),u=(r,t,e)=>{const o=[];for(const n in r){if(!Object.hasOwn(r,n))continue;let s=r[n];typeof s=="function"&&(s=s(e)),typeof s=="string"?o.push({path:a.join(t,n),content:s}):o.push(...u(s,a.join(t,n),e))}return o},w=async r=>{const t=a.join(p,`${y}-${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(u(r,t,{fixturePath:t}).map(async e=>{await i.mkdir(a.dirname(e.path),{recursive:!0}),await i.writeFile(e.path,e.content)}))),new h(t)};exports.createFixture=w;
package/dist/index.d.cts CHANGED
@@ -2,7 +2,7 @@ declare class FsFixture {
2
2
  /**
3
3
  Path to the fixture directory.
4
4
  */
5
- path: string;
5
+ readonly path: string;
6
6
  /**
7
7
  Create a Fixture instance from a path. Does not create the fixture directory.
8
8
  */
@@ -30,7 +30,8 @@ declare class FsFixture {
30
30
  /**
31
31
  Read a file from the fixture directory.
32
32
  */
33
- readFile(filePath: string, encoding?: BufferEncoding): Promise<string | Buffer>;
33
+ readFile(filePath: string, encoding?: null): Promise<Buffer>;
34
+ readFile(filePath: string, encoding: BufferEncoding): Promise<string>;
34
35
  /**
35
36
  * Resource management cleanup
36
37
  * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
@@ -38,8 +39,11 @@ declare class FsFixture {
38
39
  [Symbol.asyncDispose](): Promise<void>;
39
40
  }
40
41
 
42
+ type Api = {
43
+ fixturePath: string;
44
+ };
41
45
  type FileTree = {
42
- [path: string]: string | FileTree;
46
+ [path: string]: string | FileTree | ((api: Api) => string);
43
47
  };
44
48
  declare const createFixture: (source?: string | FileTree) => Promise<FsFixture>;
45
49
 
package/dist/index.d.mts CHANGED
@@ -2,7 +2,7 @@ declare class FsFixture {
2
2
  /**
3
3
  Path to the fixture directory.
4
4
  */
5
- path: string;
5
+ readonly path: string;
6
6
  /**
7
7
  Create a Fixture instance from a path. Does not create the fixture directory.
8
8
  */
@@ -30,7 +30,8 @@ declare class FsFixture {
30
30
  /**
31
31
  Read a file from the fixture directory.
32
32
  */
33
- readFile(filePath: string, encoding?: BufferEncoding): Promise<string | Buffer>;
33
+ readFile(filePath: string, encoding?: null): Promise<Buffer>;
34
+ readFile(filePath: string, encoding: BufferEncoding): Promise<string>;
34
35
  /**
35
36
  * Resource management cleanup
36
37
  * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
@@ -38,8 +39,11 @@ declare class FsFixture {
38
39
  [Symbol.asyncDispose](): Promise<void>;
39
40
  }
40
41
 
42
+ type Api = {
43
+ fixturePath: string;
44
+ };
41
45
  type FileTree = {
42
- [path: string]: string | FileTree;
46
+ [path: string]: string | FileTree | ((api: Api) => string);
43
47
  };
44
48
  declare const createFixture: (source?: string | FileTree) => Promise<FsFixture>;
45
49
 
package/dist/index.mjs CHANGED
@@ -1 +1 @@
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};
1
+ import i from"fs/promises";import a from"path";import l from"fs";import p from"os";typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class u{path;constructor(t){this.path=t}getPath(t){return a.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 h=l.realpathSync(p.tmpdir()),m=`fs-fixture-${Date.now()}`;let c=0;const y=()=>(c+=1,c),f=(r,t,e)=>{const o=[];for(const n in r){if(!Object.hasOwn(r,n))continue;let s=r[n];typeof s=="function"&&(s=s(e)),typeof s=="string"?o.push({path:a.join(t,n),content:s}):o.push(...f(s,a.join(t,n),e))}return o},w=async r=>{const t=a.join(h,`${m}-${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(f(r,t,{fixturePath:t}).map(async e=>{await i.mkdir(a.dirname(e.path),{recursive:!0}),await i.writeFile(e.path,e.content)}))),new u(t)};export{w as createFixture};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fs-fixture",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Easily create test fixtures at a temporary file-system path",
5
5
  "keywords": [
6
6
  "test",