fs-fixture 2.9.0 → 2.10.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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var v=Object.defineProperty;var a=(i,e)=>v(i,"name",{value:e,configurable:!0});var n=require("node:fs/promises"),c=require("node:path"),F=require("node:fs"),P=require("node:os");typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class b{static{a(this,"FsFixture")}path;constructor(e){this.path=e}getPath(...e){return c.join(this.path,...e)}exists(e=""){return n.access(this.getPath(e)).then(()=>!0,()=>!1)}rm(e=""){return n.rm(this.getPath(e),{recursive:!0,force:!0})}cp(e,t,s){return t?t.endsWith(c.sep)&&(t+=c.basename(e)):t=c.basename(e),n.cp(e,this.getPath(t),s)}mkdir(e){return n.mkdir(this.getPath(e),{recursive:!0})}readFile=a(((e,t)=>n.readFile(this.getPath(e),t)),"readFile");readdir=a(((e,t)=>n.readdir(this.getPath(e||""),t)),"readdir");writeFile=a(((e,t,...s)=>n.writeFile(this.getPath(e),t,...s)),"writeFile");async readJson(e){const t=await this.readFile(e,"utf8");return JSON.parse(t)}writeJson(e,t,s=2){return this.writeFile(e,JSON.stringify(t,null,s))}async[Symbol.asyncDispose](){await this.rm()}}const k=F.realpathSync(P.tmpdir()),D=`fs-fixture-${Date.now()}-${process.pid}`;let f=0;const j=a(()=>(f+=1,f),"getId");class p{static{a(this,"PathBase")}constructor(e){this.path=e}}class y extends p{static{a(this,"Directory")}}class m extends p{static{a(this,"File")}constructor(e,t){super(e),this.content=t}}class h extends p{static{a(this,"Symlink")}constructor(e,t,s){super(s??""),this.target=e,this.type=t}}const d=a((i,e,t)=>{const s=[];for(const u in i){if(!Object.hasOwn(i,u))continue;const r=c.join(e,u);let o=i[u];if(typeof o=="function"){const w=Object.assign(Object.create(t),{filePath:r}),l=o(w);if(l instanceof h){const g=new h(l.target,l.type,r);s.push(g);continue}else o=l}typeof o=="string"||Buffer.isBuffer(o)?s.push(new m(r,o)):s.push(new y(r),...d(o,r,t))}return s},"flattenFileTree"),x=a(async(i,e)=>{const t=e?.tempDir?c.resolve(e.tempDir):k,s=c.join(t,`${D}-${j()}/`);if(await n.mkdir(s,{recursive:!0}),i){if(typeof i=="string")await n.cp(i,s,{recursive:!0,filter:e?.templateFilter});else if(typeof i=="object"){const u={fixturePath:s,getPath:a((...r)=>c.join(s,...r),"getPath"),symlink:a((r,o)=>new h(r,o),"symlink")};await Promise.all(d(i,s,u).map(async r=>{r instanceof y?await n.mkdir(r.path,{recursive:!0}):r instanceof h?(await n.mkdir(c.dirname(r.path),{recursive:!0}),await n.symlink(r.target,r.path,r.type)):r instanceof m&&(await n.mkdir(c.dirname(r.path),{recursive:!0}),await n.writeFile(r.path,r.content))}))}}return new b(s)},"createFixture");exports.createFixture=x;
1
+ "use strict";var g=Object.defineProperty;var a=(s,e)=>g(s,"name",{value:e,configurable:!0});var n=require("node:fs/promises"),o=require("node:path"),F=require("node:url"),v=require("node:fs"),P=require("node:os");typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class b{static{a(this,"FsFixture")}path;constructor(e){this.path=e}getPath(...e){return o.join(this.path,...e)}exists(e=""){return n.access(this.getPath(e)).then(()=>!0,()=>!1)}rm(e=""){return n.rm(this.getPath(e),{recursive:!0,force:!0})}cp(e,r,i){return r?r.endsWith(o.sep)&&(r+=o.basename(e)):r=o.basename(e),n.cp(e,this.getPath(r),i)}mkdir(e){return n.mkdir(this.getPath(e),{recursive:!0})}readFile=a(((e,r)=>n.readFile(this.getPath(e),r)),"readFile");readdir=a(((e,r)=>n.readdir(this.getPath(e||""),r)),"readdir");writeFile=a(((e,r,...i)=>n.writeFile(this.getPath(e),r,...i)),"writeFile");async readJson(e){const r=await this.readFile(e,"utf8");return JSON.parse(r)}writeJson(e,r,i=2){return this.writeFile(e,JSON.stringify(r,null,i))}async[Symbol.asyncDispose](){await this.rm()}}const D=v.realpathSync(P.tmpdir());class p{static{a(this,"PathBase")}constructor(e){this.path=e}}class y extends p{static{a(this,"Directory")}}class m extends p{static{a(this,"File")}constructor(e,r){super(e),this.content=r}}class u extends p{static{a(this,"Symlink")}constructor(e,r,i){super(i??""),this.target=e,this.type=r}}const w=a((s,e,r)=>{const i=[];for(const l in s){if(!Object.hasOwn(s,l))continue;const c=o.join(e,l);let t=s[l];if(typeof t=="function"){const h=Object.assign(Object.create(r),{filePath:c}),f=t(h);if(f instanceof u){const d=new u(f.target,f.type,c);i.push(d);continue}else t=f}if(typeof t=="string"||Buffer.isBuffer(t))i.push(new m(c,t));else if(t&&typeof t=="object"&&!Array.isArray(t))i.push(new y(c),...w(t,c,r));else throw new TypeError(`Invalid file content for path "${c}". Functions must return a string, Buffer, Symlink, or a nested FileTree object. Received: ${String(t)}`)}return i},"flattenFileTree"),j=a(async(s,e)=>{const r=e?.tempDir?o.resolve(typeof e.tempDir=="string"?e.tempDir:F.fileURLToPath(e.tempDir)):D;e?.tempDir&&await n.mkdir(r,{recursive:!0});const i=await n.mkdtemp(o.join(r,"fs-fixture-"));if(s){if(typeof s=="string")await n.cp(s,i,{recursive:!0,filter:e?.templateFilter});else if(typeof s=="object"){const c=w(s,i,{fixturePath:i,getPath:a((...t)=>o.join(i,...t),"getPath"),symlink:a((t,h)=>new u(t,h),"symlink")});await Promise.all(c.filter(t=>t instanceof y).map(t=>n.mkdir(t.path,{recursive:!0}))),await Promise.all(c.map(async t=>{t instanceof u?await n.symlink(t.target,t.path,t.type):t instanceof m&&await n.writeFile(t.path,t.content)}))}}return new b(i)},"createFixture");exports.createFixture=j;
package/dist/index.d.cts CHANGED
@@ -164,8 +164,12 @@ type CreateFixtureOptions = {
164
164
  /**
165
165
  * The temporary directory to create the fixtures in.
166
166
  * Defaults to `os.tmpdir()`.
167
+ *
168
+ * Accepts either a string path or a URL object.
169
+ *
170
+ * Tip: use `new URL('.', import.meta.url)` to the get the file's directory (not the file).
167
171
  */
168
- tempDir?: string;
172
+ tempDir?: string | URL;
169
173
  /**
170
174
  * Function to filter files to copy when using a template path.
171
175
  * Return `true` to copy the item, `false` to ignore it.
package/dist/index.d.mts CHANGED
@@ -164,8 +164,12 @@ type CreateFixtureOptions = {
164
164
  /**
165
165
  * The temporary directory to create the fixtures in.
166
166
  * Defaults to `os.tmpdir()`.
167
+ *
168
+ * Accepts either a string path or a URL object.
169
+ *
170
+ * Tip: use `new URL('.', import.meta.url)` to the get the file's directory (not the file).
167
171
  */
168
- tempDir?: string;
172
+ tempDir?: string | URL;
169
173
  /**
170
174
  * Function to filter files to copy when using a template path.
171
175
  * Return `true` to copy the item, `false` to ignore it.
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var F=Object.defineProperty;var a=(i,t)=>F(i,"name",{value:t,configurable:!0});import n from"node:fs/promises";import c from"node:path";import P from"node:fs";import b from"node:os";typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class k{static{a(this,"FsFixture")}path;constructor(t){this.path=t}getPath(...t){return c.join(this.path,...t)}exists(t=""){return n.access(this.getPath(t)).then(()=>!0,()=>!1)}rm(t=""){return n.rm(this.getPath(t),{recursive:!0,force:!0})}cp(t,e,s){return e?e.endsWith(c.sep)&&(e+=c.basename(t)):e=c.basename(t),n.cp(t,this.getPath(e),s)}mkdir(t){return n.mkdir(this.getPath(t),{recursive:!0})}readFile=a(((t,e)=>n.readFile(this.getPath(t),e)),"readFile");readdir=a(((t,e)=>n.readdir(this.getPath(t||""),e)),"readdir");writeFile=a(((t,e,...s)=>n.writeFile(this.getPath(t),e,...s)),"writeFile");async readJson(t){const e=await this.readFile(t,"utf8");return JSON.parse(e)}writeJson(t,e,s=2){return this.writeFile(t,JSON.stringify(e,null,s))}async[Symbol.asyncDispose](){await this.rm()}}const v=P.realpathSync(b.tmpdir()),D=`fs-fixture-${Date.now()}-${process.pid}`;let h=0;const j=a(()=>(h+=1,h),"getId");class f{static{a(this,"PathBase")}constructor(t){this.path=t}}class m extends f{static{a(this,"Directory")}}class y extends f{static{a(this,"File")}constructor(t,e){super(t),this.content=e}}class u extends f{static{a(this,"Symlink")}constructor(t,e,s){super(s??""),this.target=t,this.type=e}}const d=a((i,t,e)=>{const s=[];for(const p in i){if(!Object.hasOwn(i,p))continue;const r=c.join(t,p);let o=i[p];if(typeof o=="function"){const w=Object.assign(Object.create(e),{filePath:r}),l=o(w);if(l instanceof u){const g=new u(l.target,l.type,r);s.push(g);continue}else o=l}typeof o=="string"||Buffer.isBuffer(o)?s.push(new y(r,o)):s.push(new m(r),...d(o,r,e))}return s},"flattenFileTree"),x=a(async(i,t)=>{const e=t?.tempDir?c.resolve(t.tempDir):v,s=c.join(e,`${D}-${j()}/`);if(await n.mkdir(s,{recursive:!0}),i){if(typeof i=="string")await n.cp(i,s,{recursive:!0,filter:t?.templateFilter});else if(typeof i=="object"){const p={fixturePath:s,getPath:a((...r)=>c.join(s,...r),"getPath"),symlink:a((r,o)=>new u(r,o),"symlink")};await Promise.all(d(i,s,p).map(async r=>{r instanceof m?await n.mkdir(r.path,{recursive:!0}):r instanceof u?(await n.mkdir(c.dirname(r.path),{recursive:!0}),await n.symlink(r.target,r.path,r.type)):r instanceof y&&(await n.mkdir(c.dirname(r.path),{recursive:!0}),await n.writeFile(r.path,r.content))}))}}return new k(s)},"createFixture");export{x as createFixture};
1
+ var d=Object.defineProperty;var a=(s,e)=>d(s,"name",{value:e,configurable:!0});import n from"node:fs/promises";import c from"node:path";import{fileURLToPath as F}from"node:url";import P from"node:fs";import b from"node:os";typeof Symbol.asyncDispose!="symbol"&&Object.defineProperty(Symbol,"asyncDispose",{configurable:!1,enumerable:!1,writable:!1,value:Symbol.for("asyncDispose")});class D{static{a(this,"FsFixture")}path;constructor(e){this.path=e}getPath(...e){return c.join(this.path,...e)}exists(e=""){return n.access(this.getPath(e)).then(()=>!0,()=>!1)}rm(e=""){return n.rm(this.getPath(e),{recursive:!0,force:!0})}cp(e,r,i){return r?r.endsWith(c.sep)&&(r+=c.basename(e)):r=c.basename(e),n.cp(e,this.getPath(r),i)}mkdir(e){return n.mkdir(this.getPath(e),{recursive:!0})}readFile=a(((e,r)=>n.readFile(this.getPath(e),r)),"readFile");readdir=a(((e,r)=>n.readdir(this.getPath(e||""),r)),"readdir");writeFile=a(((e,r,...i)=>n.writeFile(this.getPath(e),r,...i)),"writeFile");async readJson(e){const r=await this.readFile(e,"utf8");return JSON.parse(r)}writeJson(e,r,i=2){return this.writeFile(e,JSON.stringify(r,null,i))}async[Symbol.asyncDispose](){await this.rm()}}const j=P.realpathSync(b.tmpdir());class h{static{a(this,"PathBase")}constructor(e){this.path=e}}class u extends h{static{a(this,"Directory")}}class y extends h{static{a(this,"File")}constructor(e,r){super(e),this.content=r}}class p extends h{static{a(this,"Symlink")}constructor(e,r,i){super(i??""),this.target=e,this.type=r}}const w=a((s,e,r)=>{const i=[];for(const f in s){if(!Object.hasOwn(s,f))continue;const o=c.join(e,f);let t=s[f];if(typeof t=="function"){const m=Object.assign(Object.create(r),{filePath:o}),l=t(m);if(l instanceof p){const g=new p(l.target,l.type,o);i.push(g);continue}else t=l}if(typeof t=="string"||Buffer.isBuffer(t))i.push(new y(o,t));else if(t&&typeof t=="object"&&!Array.isArray(t))i.push(new u(o),...w(t,o,r));else throw new TypeError(`Invalid file content for path "${o}". Functions must return a string, Buffer, Symlink, or a nested FileTree object. Received: ${String(t)}`)}return i},"flattenFileTree"),k=a(async(s,e)=>{const r=e?.tempDir?c.resolve(typeof e.tempDir=="string"?e.tempDir:F(e.tempDir)):j;e?.tempDir&&await n.mkdir(r,{recursive:!0});const i=await n.mkdtemp(c.join(r,"fs-fixture-"));if(s){if(typeof s=="string")await n.cp(s,i,{recursive:!0,filter:e?.templateFilter});else if(typeof s=="object"){const o=w(s,i,{fixturePath:i,getPath:a((...t)=>c.join(i,...t),"getPath"),symlink:a((t,m)=>new p(t,m),"symlink")});await Promise.all(o.filter(t=>t instanceof u).map(t=>n.mkdir(t.path,{recursive:!0}))),await Promise.all(o.map(async t=>{t instanceof p?await n.symlink(t.target,t.path,t.type):t instanceof y&&await n.writeFile(t.path,t.content)}))}}return new D(i)},"createFixture");export{k as createFixture};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fs-fixture",
3
- "version": "2.9.0",
3
+ "version": "2.10.0",
4
4
  "description": "Easily create test fixtures at a temporary file-system path",
5
5
  "keywords": [
6
6
  "test",