@unshared/fs 0.0.15 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/createTemporaryDirectory.cjs +1 -1
- package/dist/createTemporaryDirectory.cjs.map +1 -1
- package/dist/createTemporaryDirectory.js +2 -2
- package/dist/createTemporaryDirectory.js.map +1 -1
- package/dist/createTemporaryFile.cjs +1 -1
- package/dist/createTemporaryFile.cjs.map +1 -1
- package/dist/createTemporaryFile.js +2 -2
- package/dist/createTemporaryFile.js.map +1 -1
- package/dist/findAncestor.cjs +1 -1
- package/dist/findAncestor.cjs.map +1 -1
- package/dist/findAncestor.js +2 -2
- package/dist/findAncestor.js.map +1 -1
- package/dist/findAncestors.cjs +1 -1
- package/dist/findAncestors.cjs.map +1 -1
- package/dist/findAncestors.js +3 -3
- package/dist/findAncestors.js.map +1 -1
- package/dist/glob.cjs +1 -1
- package/dist/glob.cjs.map +1 -1
- package/dist/glob.d.ts +2 -2
- package/dist/glob.js +4 -4
- package/dist/glob.js.map +1 -1
- package/dist/index.cjs +6 -6
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -6
- package/dist/loadObject.cjs +1 -1
- package/dist/loadObject.cjs.map +1 -1
- package/dist/loadObject.d.ts +2 -2
- package/dist/loadObject.js +7 -7
- package/dist/loadObject.js.map +1 -1
- package/dist/touch.cjs +1 -1
- package/dist/touch.cjs.map +1 -1
- package/dist/touch.js +1 -1
- package/dist/touch.js.map +1 -1
- package/dist/updateFile.cjs.map +1 -1
- package/dist/updateFile.d.ts +1 -1
- package/dist/updateFile.js.map +1 -1
- package/dist/withTemporaryDirectories.cjs +2 -2
- package/dist/withTemporaryDirectories.cjs.map +1 -1
- package/dist/withTemporaryDirectories.js +2 -2
- package/dist/withTemporaryDirectories.js.map +1 -1
- package/dist/withTemporaryFiles.cjs +2 -2
- package/dist/withTemporaryFiles.cjs.map +1 -1
- package/dist/withTemporaryFiles.js +2 -2
- package/dist/withTemporaryFiles.js.map +1 -1
- package/package.json +6 -6
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"withTemporaryFiles.js","sources":["../withTemporaryFiles.ts"],"sourcesContent":["import { Function, MaybeArray, Tuple, TupleLength } from '@unshared/types'\nimport { CreateTemporaryFileOptions
|
1
|
+
{"version":3,"file":"withTemporaryFiles.js","sources":["../withTemporaryFiles.ts"],"sourcesContent":["import type { Function, MaybeArray, Tuple, TupleLength } from '@unshared/types'\nimport type { CreateTemporaryFileOptions } from './createTemporaryFile'\nimport { createTemporaryFile } from './createTemporaryFile'\n\ntype Callback<U, N extends number> = (...paths: Tuple<N, string>) => Promise<U> | U\n\n/**\n * Wrap a function that will create one or more temporary files and\n * remove them after the function has been executed, regardless of\n * whether the function throws an error or not.\n *\n * @param count The number of temporary files to create.\n * @param fn The function to wrap that takes the temporary directory path(s) as arguments.\n * @returns A promise that resolves to the result of the function.\n */\nexport async function withTemporaryFiles<U, N extends number>(count: N, fn: Callback<U, N>): Promise<U>\nexport async function withTemporaryFiles<U, T extends CreateTemporaryFileOptions[]>(options: T, fn: Callback<U, TupleLength<T>>): Promise<U>\nexport async function withTemporaryFiles<U, T extends CreateTemporaryFileOptions>(option: T, fn: Callback<U, 1>): Promise<U>\nexport async function withTemporaryFiles(options: MaybeArray<CreateTemporaryFileOptions> | number, fn: Function<unknown>): Promise<unknown> {\n\n // --- Normalize the arguments.\n if (typeof options === 'number') options = Array.from({ length: options }, () => ({}))\n if (!Array.isArray(options)) options = [options]\n\n // --- Create temporary files.\n const pathsPromises = options.map(option => createTemporaryFile(undefined, option))\n const pathsInstances = await Promise.all(pathsPromises)\n const paths = pathsInstances.map(x => x[0])\n\n try {\n return await fn(...paths)\n }\n finally {\n const promises = pathsInstances.map(x => x[1]())\n await Promise.all(promises)\n }\n}\n\n/* v8 ignore start */\nif (import.meta.vitest) {\n const { existsSync } = await import('node:fs')\n\n test('should call a function with one temporary file', async() => {\n await withTemporaryFiles(1, (path) => {\n const exists = existsSync(path)\n expect(exists).toBe(true)\n })\n })\n\n test('should call a function with two temporary files', async() => {\n await withTemporaryFiles(2, (path1, path2) => {\n const exists1 = existsSync(path1)\n const exists2 = existsSync(path2)\n expect(exists1).toBe(true)\n expect(exists2).toBe(true)\n })\n })\n\n test('should remove the temporary files after calling the function', async() => {\n let temporaryPath1: string\n let temporaryPath2: string\n await withTemporaryFiles(2, (path1, path2) => {\n temporaryPath1 = path1\n temporaryPath2 = path2\n })\n const exists1 = existsSync(temporaryPath1!)\n const exists2 = existsSync(temporaryPath2!)\n expect(exists1).toBe(false)\n expect(exists2).toBe(false)\n })\n\n test('should remove the temporary files after the function throws an error', async() => {\n let temporaryPath1: string\n let temporaryPath2: string\n await withTemporaryFiles(2, (path1, path2) => {\n temporaryPath1 = path1\n temporaryPath2 = path2\n throw new Error('Test error')\n }).catch(() => {})\n const exists1 = existsSync(temporaryPath1!)\n const exists2 = existsSync(temporaryPath2!)\n expect(exists1).toBe(false)\n expect(exists2).toBe(false)\n })\n\n test('should call a function with a temporary file in the specified directory', async() => {\n await withTemporaryFiles({ directory: '/cache' }, (path) => {\n expect(path).toMatch(/^\\/cache\\/[\\da-z]+$/)\n })\n })\n\n test('should call a function with a temporary file with the specified extension', async() => {\n await withTemporaryFiles({ extension: 'txt' }, (path) => {\n expect(path).toMatch(/^\\/tmp\\/[\\da-z]+\\.txt$/)\n })\n })\n\n test('should call a function with a temporary file with the given random function', async() => {\n await withTemporaryFiles({ random: () => 'foo' }, (path) => {\n expect(path).toMatch(/^\\/tmp\\/foo$/)\n })\n })\n\n test('should call a function with multiple temporary files with different options', async() => {\n await withTemporaryFiles([{ directory: '/cache' }, { extension: 'txt' }, { random: () => 'foo' }], (path1, path2, path3) => {\n expect(path1).toMatch(/^\\/cache\\/[\\da-z]+$/)\n expect(path2).toMatch(/^\\/tmp\\/[\\da-z]+\\.txt$/)\n expect(path3).toMatch(/^\\/tmp\\/foo$/)\n })\n })\n\n test('should return the result of the function', async() => {\n const result = await withTemporaryFiles(1, () => 42)\n expect(result).toBe(42)\n })\n\n test('should throw an error if the function throws an error', async() => {\n const shouldReject = withTemporaryFiles(1, () => { throw new Error('Test error') })\n await expect(shouldReject).rejects.toThrow('Test error')\n })\n}\n"],"names":[],"mappings":";;;;AAkBsB,eAAA,mBAAmB,SAA0D,IAAyC;AAGtI,SAAO,WAAY,aAAU,UAAU,MAAM,KAAK,EAAE,QAAQ,WAAW,OAAO,CAAC,EAAE,IAChF,MAAM,QAAQ,OAAO,MAAG,UAAU,CAAC,OAAO;AAGzC,QAAA,gBAAgB,QAAQ,IAAI,CAAA,WAAU,oBAAoB,QAAW,MAAM,CAAC,GAC5E,iBAAiB,MAAM,QAAQ,IAAI,aAAa,GAChD,QAAQ,eAAe,IAAI,CAAA,MAAK,EAAE,CAAC,CAAC;AAEtC,MAAA;AACK,WAAA,MAAM,GAAG,GAAG,KAAK;AAAA,EAAA,UAE1B;AACE,UAAM,WAAW,eAAe,IAAI,OAAK,EAAE,CAAC,GAAG;AACzC,UAAA,QAAQ,IAAI,QAAQ;AAAA,EAC5B;AACF;"}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@unshared/fs",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.
|
4
|
+
"version": "0.0.17",
|
5
5
|
"license": "MIT",
|
6
6
|
"sideEffects": false,
|
7
7
|
"author": "Stanley Horwood <stanley@hsjm.io>",
|
@@ -78,11 +78,11 @@
|
|
78
78
|
"LICENSE.md"
|
79
79
|
],
|
80
80
|
"dependencies": {
|
81
|
-
"@unshared/collection": "0.0.
|
82
|
-
"@unshared/functions": "0.0.
|
83
|
-
"@unshared/reactivity": "0.0.
|
84
|
-
"@unshared/string": "0.0.
|
85
|
-
"@unshared/types": "0.0.
|
81
|
+
"@unshared/collection": "0.0.17",
|
82
|
+
"@unshared/functions": "0.0.17",
|
83
|
+
"@unshared/reactivity": "0.0.17",
|
84
|
+
"@unshared/string": "0.0.17",
|
85
|
+
"@unshared/types": "0.0.17"
|
86
86
|
},
|
87
87
|
"devDependencies": {
|
88
88
|
"memfs": "4.8.2"
|