fs-fixture 2.1.0 → 2.2.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
@@ -42,9 +42,10 @@ import { createFixture } from 'fs-fixture'
42
42
  const fixture = await createFixture({
43
43
  // Nested directory syntax
44
44
  'dir-a': {
45
+ 'file-a.txt': 'hello world',
45
46
  'dir-b': {
46
- 'file-a.txt': 'hello world',
47
- 'file-b.txt': ({ fixturePath }) => `Fixture path: ${fixturePath}`
47
+ 'file-b.txt': ({ fixturePath }) => `Fixture path: ${fixturePath}`,
48
+ 'symlink-c': ({ symlink }) => symlink('../file-a.txt')
48
49
  }
49
50
  },
50
51
 
@@ -100,7 +101,21 @@ Path to a template fixture path, or a `FileTree` object that represents the fixt
100
101
 
101
102
  ```ts
102
103
  type FileTree = {
103
- [path: string]: string | FileTree
104
+ [path: string]: string | FileTree | ((api: Api) => string)
105
+ }
106
+
107
+ type Api = {
108
+ // Fixture root path
109
+ fixturePath: string
110
+
111
+ // Current file path
112
+ filePath: string
113
+
114
+ // Get path from the root of the fixture
115
+ getPath: (subpath: string) => string
116
+
117
+ // Create a symlink
118
+ symlink: (target: string) => Symlink
104
119
  }
105
120
  ```
106
121
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
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;
1
+ "use strict";var s=require("fs/promises"),n=require("path"),y=require("fs"),m=require("os");typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class w{path;constructor(t){this.path=t}getPath(t){return n.join(this.path,t)}exists(t=""){return s.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return s.rm(this.getPath(t),{recursive:!0,force:!0})}writeFile(t,i){return s.writeFile(this.getPath(t),i)}writeJson(t,i){return this.writeFile(t,JSON.stringify(i,null,2))}readFile(t,i){return s.readFile(this.getPath(t),i)}async[Symbol.asyncDispose](){await this.rm()}}const g=y.realpathSync(m.tmpdir()),b=`fs-fixture-${Date.now()}`;let l=0;const F=()=>(l+=1,l);class h{target;path;constructor(t){this.target=t}}const p=(r,t,i)=>{const e=[];for(const u in r){if(!Object.hasOwn(r,u))continue;const o=n.join(t,u);let a=r[u];if(typeof a=="function"){const f=Object.assign(Object.create(i),{filePath:o}),c=a(f);if(c instanceof h){c.path=o,e.push(c);continue}else a=c}typeof a=="string"?e.push({path:o,content:a}):e.push(...p(a,o,i))}return e},P=async r=>{const t=n.join(g,`${b}-${F()}/`);if(await s.mkdir(t,{recursive:!0}),r){if(typeof r=="string")await s.cp(r,t,{recursive:!0});else if(typeof r=="object"){const i={fixturePath:t,getPath:e=>n.join(t,e),symlink:e=>new h(e)};await Promise.all(p(r,t,i).map(async e=>{await s.mkdir(n.dirname(e.path),{recursive:!0}),e instanceof h?await s.symlink(e.target,e.path):await s.writeFile(e.path,e.content)}))}}return new w(t)};exports.createFixture=P;
package/dist/index.d.cts CHANGED
@@ -39,11 +39,21 @@ declare class FsFixture {
39
39
  [Symbol.asyncDispose](): Promise<void>;
40
40
  }
41
41
 
42
- type Api = {
42
+ declare class Symlink {
43
+ target: string;
44
+ path?: string;
45
+ constructor(target: string);
46
+ }
47
+ type ApiBase = {
43
48
  fixturePath: string;
49
+ getPath(subpath: string): string;
50
+ symlink(targetPath: string): Symlink;
51
+ };
52
+ type Api = ApiBase & {
53
+ filePath: string;
44
54
  };
45
55
  type FileTree = {
46
- [path: string]: string | FileTree | ((api: Api) => string);
56
+ [path: string]: string | FileTree | ((api: Api) => string | Symlink);
47
57
  };
48
58
  declare const createFixture: (source?: string | FileTree) => Promise<FsFixture>;
49
59
 
package/dist/index.d.mts CHANGED
@@ -39,11 +39,21 @@ declare class FsFixture {
39
39
  [Symbol.asyncDispose](): Promise<void>;
40
40
  }
41
41
 
42
- type Api = {
42
+ declare class Symlink {
43
+ target: string;
44
+ path?: string;
45
+ constructor(target: string);
46
+ }
47
+ type ApiBase = {
43
48
  fixturePath: string;
49
+ getPath(subpath: string): string;
50
+ symlink(targetPath: string): Symlink;
51
+ };
52
+ type Api = ApiBase & {
53
+ filePath: string;
44
54
  };
45
55
  type FileTree = {
46
- [path: string]: string | FileTree | ((api: Api) => string);
56
+ [path: string]: string | FileTree | ((api: Api) => string | Symlink);
47
57
  };
48
58
  declare const createFixture: (source?: string | FileTree) => Promise<FsFixture>;
49
59
 
package/dist/index.mjs CHANGED
@@ -1 +1 @@
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};
1
+ import s from"fs/promises";import n from"path";import m from"fs";import y from"os";typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class w{path;constructor(t){this.path=t}getPath(t){return n.join(this.path,t)}exists(t=""){return s.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return s.rm(this.getPath(t),{recursive:!0,force:!0})}writeFile(t,i){return s.writeFile(this.getPath(t),i)}writeJson(t,i){return this.writeFile(t,JSON.stringify(i,null,2))}readFile(t,i){return s.readFile(this.getPath(t),i)}async[Symbol.asyncDispose](){await this.rm()}}const g=m.realpathSync(y.tmpdir()),b=`fs-fixture-${Date.now()}`;let l=0;const P=()=>(l+=1,l);class h{target;path;constructor(t){this.target=t}}const f=(r,t,i)=>{const e=[];for(const p in r){if(!Object.hasOwn(r,p))continue;const o=n.join(t,p);let a=r[p];if(typeof a=="function"){const u=Object.assign(Object.create(i),{filePath:o}),c=a(u);if(c instanceof h){c.path=o,e.push(c);continue}else a=c}typeof a=="string"?e.push({path:o,content:a}):e.push(...f(a,o,i))}return e},d=async r=>{const t=n.join(g,`${b}-${P()}/`);if(await s.mkdir(t,{recursive:!0}),r){if(typeof r=="string")await s.cp(r,t,{recursive:!0});else if(typeof r=="object"){const i={fixturePath:t,getPath:e=>n.join(t,e),symlink:e=>new h(e)};await Promise.all(f(r,t,i).map(async e=>{await s.mkdir(n.dirname(e.path),{recursive:!0}),e instanceof h?await s.symlink(e.target,e.path):await s.writeFile(e.path,e.content)}))}}return new w(t)};export{d as createFixture};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fs-fixture",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Easily create test fixtures at a temporary file-system path",
5
5
  "keywords": [
6
6
  "test",