index.js 761 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const uniqueString = require('unique-string');
  5. const tempDir = require('temp-dir');
  6. const getPath = () => path.join(tempDir, uniqueString());
  7. module.exports.file = options => {
  8. options = {
  9. extension: '',
  10. ...options
  11. };
  12. if (options.name) {
  13. if (options.extension) {
  14. throw new Error('The `name` and `extension` options are mutually exclusive');
  15. }
  16. return path.join(module.exports.directory(), options.name);
  17. }
  18. return getPath() + '.' + options.extension.replace(/^\./, '');
  19. };
  20. module.exports.directory = () => {
  21. const directory = getPath();
  22. fs.mkdirSync(directory);
  23. return directory;
  24. };
  25. Object.defineProperty(module.exports, 'root', {
  26. get() {
  27. return tempDir;
  28. }
  29. });