Skip to content

Commit

Permalink
feat: add temp dir function
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Mar 26, 2020
1 parent 8e365fe commit 7714c66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"src",
"dist"
],
"browser": {
"fs-extra": false,
"./src/text-encoder.js": "./src/text-encoder.browser.js",
"./src/temp-dir.js": "./src/temp-dir.browser.js"
},
"repository": "github:ipfs/js-ipfs-utils",
"scripts": {
"test": "aegir test",
Expand All @@ -34,6 +39,7 @@
"iso-url": "^0.4.7",
"it-glob": "0.0.7",
"merge-options": "^2.0.0",
"nanoid": "^2.1.11",
"node-fetch": "^2.6.0",
"stream-to-it": "^0.2.0"
},
Expand All @@ -52,9 +58,5 @@
"Alex Potsides <[email protected]>",
"Alan Shaw <[email protected]>",
"Marcin Rataj <[email protected]>"
],
"browser": {
"fs-extra": false,
"./src/text-encoder.js": "./src/text-encoder.browser.js"
}
]
}
15 changes: 15 additions & 0 deletions src/temp-dir.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const nanoid = require('nanoid')

/**
* Temporary folder
*
* @param {function(string): string} transform - Transform function to add prefixes or sufixes to the unique id
* @returns {string} - Full real path to a temporary folder
*/
const tempdir = (transform = d => d) => {
return transform(nanoid())
}

module.exports = tempdir
19 changes: 19 additions & 0 deletions src/temp-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const fs = require('fs')
const os = require('os')
const path = require('path')
const nanoid = require('nanoid')

/**
* Temporary folder
*
* @param {function(string): string} transform - Transform function to add prefixes or sufixes to the unique id
* @returns {string} - Full real path to a temporary folder
*/
const tempdir = (transform = d => d) => {
const osTmpDir = fs.realpathSync(os.tmpdir())
return path.join(osTmpDir, transform(nanoid()))
}

module.exports = tempdir

0 comments on commit 7714c66

Please sign in to comment.