Skip to content

Commit

Permalink
feat(useTemporaryDirectory): Make writeFile return the absolute path …
Browse files Browse the repository at this point in the history
…to the file
  • Loading branch information
vinsonchuong committed Nov 3, 2022
1 parent e701b38 commit e33ab22
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ Returns an object with the following members:

- `path`: The absolute path to the temporary directory.
- `writeFile(filePath, fileContents)`: Write a file with path relative to the
temporary directory. Any leading whitespace in the file contents is stripped.
If the file starts with a shebang, it is given executable permissions.
temporary directory, returning the absolute path to the file. Any leading
whitespace in the file contents is stripped. If the file starts with a
shebang, it is given executable permissions.

```js
import process from 'node:process'
Expand Down
4 changes: 3 additions & 1 deletion use-temporary-directory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import stripIndent from 'strip-indent'
*
* @return {Promise<{
* path: string,
* writeFile(filePath: string, fileContents: string): Promise<void>
* writeFile(filePath: string, fileContents: string): Promise<string>
* }>} an object allowing manipulation of files within the directory.
*/
export default async function (t, options) {
Expand All @@ -49,6 +49,8 @@ export default async function (t, options) {
if (contents.startsWith('#!')) {
await fs.chmod(absolutePath, 0o755)
}

return absolutePath
},
}
}
13 changes: 8 additions & 5 deletions use-temporary-directory/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ test.serial('creating a directory', async (t) => {
const stats = await fs.stat(directory.path)
t.true(stats.isDirectory())

await directory.writeFile(
'file.txt',
`
Hello World!
`,
t.is(
await directory.writeFile(
'file.txt',
`
Hello World!
`,
),
path.join(directory.path, 'file.txt'),
)

t.is(
Expand Down

0 comments on commit e33ab22

Please sign in to comment.