{"ast":null,"code":"\"use strict\";\n\nconst pathUtil = require(\"path\");\n\nconst os = require(\"os\");\n\nconst crypto = require(\"crypto\");\n\nconst dir = require(\"./dir\");\n\nconst fs = require(\"./utils/fs\");\n\nconst validate = require(\"./utils/validate\");\n\nconst validateInput = (methodName, options) => {\n  const methodSignature = `${methodName}([options])`;\n  validate.options(methodSignature, \"options\", options, {\n    prefix: [\"string\"],\n    basePath: [\"string\"]\n  });\n};\n\nconst getOptionsDefaults = (passedOptions, cwdPath) => {\n  passedOptions = passedOptions || {};\n  const options = {};\n\n  if (typeof passedOptions.prefix !== \"string\") {\n    options.prefix = \"\";\n  } else {\n    options.prefix = passedOptions.prefix;\n  }\n\n  if (typeof passedOptions.basePath === \"string\") {\n    options.basePath = pathUtil.resolve(cwdPath, passedOptions.basePath);\n  } else {\n    options.basePath = os.tmpdir();\n  }\n\n  return options;\n};\n\nconst randomStringLength = 32; // ---------------------------------------------------------\n// Sync\n// ---------------------------------------------------------\n\nconst tmpDirSync = (cwdPath, passedOptions) => {\n  const options = getOptionsDefaults(passedOptions, cwdPath);\n  const randomString = crypto.randomBytes(randomStringLength / 2).toString(\"hex\");\n  const dirPath = pathUtil.join(options.basePath, options.prefix + randomString); // Let's assume everything will go well, do the directory fastest way possible\n\n  try {\n    fs.mkdirSync(dirPath);\n  } catch (err) {\n    // Something went wrong, try to recover by using more sophisticated approach\n    if (err.code === \"ENOENT\") {\n      dir.sync(dirPath);\n    } else {\n      throw err;\n    }\n  }\n\n  return dirPath;\n}; // ---------------------------------------------------------\n// Async\n// ---------------------------------------------------------\n\n\nconst tmpDirAsync = (cwdPath, passedOptions) => {\n  return new Promise((resolve, reject) => {\n    const options = getOptionsDefaults(passedOptions, cwdPath);\n    crypto.randomBytes(randomStringLength / 2, (err, bytes) => {\n      if (err) {\n        reject(err);\n      } else {\n        const randomString = bytes.toString(\"hex\");\n        const dirPath = pathUtil.join(options.basePath, options.prefix + randomString); // Let's assume everything will go well, do the directory fastest way possible\n\n        fs.mkdir(dirPath, err => {\n          if (err) {\n            // Something went wrong, try to recover by using more sophisticated approach\n            if (err.code === \"ENOENT\") {\n              dir.async(dirPath).then(() => {\n                resolve(dirPath);\n              }, reject);\n            } else {\n              reject(err);\n            }\n          } else {\n            resolve(dirPath);\n          }\n        });\n      }\n    });\n  });\n}; // ---------------------------------------------------------\n// API\n// ---------------------------------------------------------\n\n\nexports.validateInput = validateInput;\nexports.sync = tmpDirSync;\nexports.async = tmpDirAsync;","map":{"version":3,"sources":["D:/Development/Work/CENOS/cenos-ui/node_modules/fs-jetpack/lib/tmp_dir.js"],"names":["pathUtil","require","os","crypto","dir","fs","validate","validateInput","methodName","options","methodSignature","prefix","basePath","getOptionsDefaults","passedOptions","cwdPath","resolve","tmpdir","randomStringLength","tmpDirSync","randomString","randomBytes","toString","dirPath","join","mkdirSync","err","code","sync","tmpDirAsync","Promise","reject","bytes","mkdir","async","then","exports"],"mappings":"AAAA;;AAEA,MAAMA,QAAQ,GAAGC,OAAO,CAAC,MAAD,CAAxB;;AACA,MAAMC,EAAE,GAAGD,OAAO,CAAC,IAAD,CAAlB;;AACA,MAAME,MAAM,GAAGF,OAAO,CAAC,QAAD,CAAtB;;AACA,MAAMG,GAAG,GAAGH,OAAO,CAAC,OAAD,CAAnB;;AACA,MAAMI,EAAE,GAAGJ,OAAO,CAAC,YAAD,CAAlB;;AACA,MAAMK,QAAQ,GAAGL,OAAO,CAAC,kBAAD,CAAxB;;AAEA,MAAMM,aAAa,GAAG,CAACC,UAAD,EAAaC,OAAb,KAAyB;AAC7C,QAAMC,eAAe,GAAI,GAAEF,UAAW,aAAtC;AACAF,EAAAA,QAAQ,CAACG,OAAT,CAAiBC,eAAjB,EAAkC,SAAlC,EAA6CD,OAA7C,EAAsD;AACpDE,IAAAA,MAAM,EAAE,CAAC,QAAD,CAD4C;AAEpDC,IAAAA,QAAQ,EAAE,CAAC,QAAD;AAF0C,GAAtD;AAID,CAND;;AAQA,MAAMC,kBAAkB,GAAG,CAAC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AO,CAACF,KAAR,GAAgBL,WAAhB","sourcesContent":["\"use strict\";\n\nconst pathUtil = require(\"path\");\nconst os = require(\"os\");\nconst crypto = require(\"crypto\");\nconst dir = require(\"./dir\");\nconst fs = require(\"./utils/fs\");\nconst validate = require(\"./utils/validate\");\n\nconst validateInput = (methodName, options) => {\n  const methodSignature = `${methodName}([options])`;\n  validate.options(methodSignature, \"options\", options, {\n    prefix: [\"string\"],\n    basePath: [\"string\"]\n  });\n};\n\nconst getOptionsDefaults = (passedOptions, cwdPath) => {\n  passedOptions = passedOptions || {};\n  const options = {};\n  if (typeof passedOptions.prefix !== \"string\") {\n    options.prefix = \"\";\n  } else {\n    options.prefix = passedOptions.prefix;\n  }\n  if (typeof passedOptions.basePath === \"string\") {\n    options.basePath = pathUtil.resolve(cwdPath, passedOptions.basePath);\n  } else {\n    options.basePath = os.tmpdir();\n  }\n  return options;\n};\n\nconst randomStringLength = 32;\n\n// ---------------------------------------------------------\n// Sync\n// ---------------------------------------------------------\n\nconst tmpDirSync = (cwdPath, passedOptions) => {\n  const options = getOptionsDefaults(passedOptions, cwdPath);\n  const randomString = crypto\n    .randomBytes(randomStringLength / 2)\n    .toString(\"hex\");\n  const dirPath = pathUtil.join(\n    options.basePath,\n    options.prefix + randomString\n  );\n  // Let's assume everything will go well, do the directory fastest way possible\n  try {\n    fs.mkdirSync(dirPath);\n  } catch (err) {\n    // Something went wrong, try to recover by using more sophisticated approach\n    if (err.code === \"ENOENT\") {\n      dir.sync(dirPath);\n    } else {\n      throw err;\n    }\n  }\n  return dirPath;\n};\n\n// ---------------------------------------------------------\n// Async\n// ---------------------------------------------------------\n\nconst tmpDirAsync = (cwdPath, passedOptions) => {\n  return new Promise((resolve, reject) => {\n    const options = getOptionsDefaults(passedOptions, cwdPath);\n    crypto.randomBytes(randomStringLength / 2, (err, bytes) => {\n      if (err) {\n        reject(err);\n      } else {\n        const randomString = bytes.toString(\"hex\");\n        const dirPath = pathUtil.join(\n          options.basePath,\n          options.prefix + randomString\n        );\n        // Let's assume everything will go well, do the directory fastest way possible\n        fs.mkdir(dirPath, err => {\n          if (err) {\n            // Something went wrong, try to recover by using more sophisticated approach\n            if (err.code === \"ENOENT\") {\n              dir.async(dirPath).then(() => {\n                resolve(dirPath);\n              }, reject);\n            } else {\n              reject(err);\n            }\n          } else {\n            resolve(dirPath);\n          }\n        });\n      }\n    });\n  });\n};\n\n// ---------------------------------------------------------\n// API\n// ---------------------------------------------------------\n\nexports.validateInput = validateInput;\nexports.sync = tmpDirSync;\nexports.async = tmpDirAsync;\n"]},"metadata":{},"sourceType":"script"}