index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict'
  2. const checkWindows = require('./checkWindows')
  3. const resolvePath = require('path').resolve
  4. const whichLib = require('./whichLib')
  5. function get (path, callback) {
  6. if (!callback) {
  7. return new Promise((resolve, reject) => {
  8. get(path, (err, result) => {
  9. if (err) {
  10. reject(err)
  11. } else {
  12. resolve(result)
  13. }
  14. })
  15. })
  16. }
  17. if (checkWindows.async(callback) === true) {
  18. return whichLib.run(function () {
  19. return whichLib.current().get(resolvePath(path), callback)
  20. })
  21. }
  22. }
  23. function getSync (path) {
  24. if (checkWindows.sync() === true) {
  25. return whichLib.run(function () {
  26. return whichLib.current().getSync(resolvePath(path))
  27. })
  28. }
  29. }
  30. function set (path, attrs, callback) {
  31. if (!callback) {
  32. return new Promise((resolve, reject) => {
  33. set(path, attrs, (err) => {
  34. if (err) {
  35. reject(err)
  36. } else {
  37. resolve()
  38. }
  39. })
  40. })
  41. }
  42. if (checkWindows.async(callback) === true) {
  43. return whichLib.run(function () {
  44. return whichLib.current().set(resolvePath(path), attrs, callback)
  45. })
  46. }
  47. }
  48. function setSync (path, attrs) {
  49. if (checkWindows.sync() === true) {
  50. return whichLib.run(function () {
  51. return whichLib.current().setSync(resolvePath(path), attrs)
  52. })
  53. }
  54. }
  55. whichLib.change('auto')
  56. module.exports = {
  57. get,
  58. getSync,
  59. set,
  60. setSync,
  61. // Undocumented -- used for testing
  62. change: whichLib.change,
  63. }