{"ast":null,"code":"\"use strict\";\n\nconst pathUtil = require(\"path\");\n\nconst fs = require(\"./utils/fs\");\n\nconst dir = require(\"./dir\");\n\nconst exists = require(\"./exists\");\n\nconst inspect = require(\"./inspect\");\n\nconst write = require(\"./write\");\n\nconst matcher = require(\"./utils/matcher\");\n\nconst fileMode = require(\"./utils/mode\");\n\nconst treeWalker = require(\"./utils/tree_walker\");\n\nconst validate = require(\"./utils/validate\");\n\nconst validateInput = (methodName, from, to, options) => {\n  const methodSignature = `${methodName}(from, to, [options])`;\n  validate.argument(methodSignature, \"from\", from, [\"string\"]);\n  validate.argument(methodSignature, \"to\", to, [\"string\"]);\n  validate.options(methodSignature, \"options\", options, {\n    overwrite: [\"boolean\", \"function\"],\n    matching: [\"string\", \"array of string\"],\n    ignoreCase: [\"boolean\"]\n  });\n};\n\nconst parseOptions = (options, from) => {\n  const opts = options || {};\n  const parsedOptions = {};\n\n  if (opts.ignoreCase === undefined) {\n    opts.ignoreCase = false;\n  }\n\n  parsedOptions.overwrite = opts.overwrite;\n\n  if (opts.matching) {\n    parsedOptions.allowedToCopy = matcher.create(from, opts.matching, opts.ignoreCase);\n  } else {\n    parsedOptions.allowedToCopy = () => {\n      // Default behaviour - copy everything.\n      return true;\n    };\n  }\n\n  return parsedOptions;\n};\n\nconst generateNoSourceError = path => {\n  const err = new Error(`Path to copy doesn't exist ${path}`);\n  err.code = \"ENOENT\";\n  return err;\n};\n\nconst generateDestinationExistsError = path => {\n  const err = new Error(`Destination path already exists ${path}`);\n  err.code = \"EEXIST\";\n  return err;\n};\n\nconst inspectOptions = {\n  mode: true,\n  symlinks: \"report\",\n  times: true,\n  absolutePath: true\n};\n\nconst shouldThrowDestinationExistsError = context => {\n  return typeof context.opts.overwrite !== \"function\" && context.opts.overwrite !== true;\n}; // ---------------------------------------------------------\n// Sync\n// ---------------------------------------------------------\n\n\nconst checksBeforeCopyingSync = (from, to, opts) => {\n  if (!exists.sync(from)) {\n    throw generateNoSourceError(from);\n  }\n\n  if (exists.sync(to) && !opts.overwrite) {\n    throw generateDestinationExistsError(to);\n  }\n};\n\nconst canOverwriteItSync = context => {\n  if (typeof context.opts.overwrite === \"function\") {\n    const destInspectData = inspect.sync(context.destPath, inspectOptions);\n    return context.opts.overwrite(context.srcInspectData, destInspectData);\n  }\n\n  return context.opts.overwrite === true;\n};\n\nconst copyFileSync = (srcPath, destPath, mode, context) => {\n  const data = fs.readFileSync(srcPath);\n\n  try {\n    fs.writeFileSync(destPath, data, {\n      mode,\n      flag: \"wx\"\n    });\n  } catch (err) {\n    if (err.code === \"ENOENT\") {\n      write.sync(destPath, data, {\n        mode\n      });\n    } else if (err.code === \"EEXIST\") {\n      if (canOverwriteItSync(context)) {\n        fs.writeFileSync(destPath, data, {\n          mode\n        });\n      } else if (shouldThrowDestinationExistsError(context)) {\n        throw generateDestinationExistsError(context.destPath);\n      }\n    } else {\n      throw err;\n    }\n  }\n};\n\nconst copySymlinkSync = (from, to) => {\n  const symlinkPointsAt = fs.readlinkSync(from);\n\n  try {\n    fs.symlinkSync(symlinkPointsAt, to);\n  } catch (err) {\n    // There is already file/symlink with this name on destination location.\n    // Must erase it manually, otherwise system won't allow us to place symlink there.\n    if (err.code === \"EEXIST\") {\n      fs.unlinkSync(to); // Retry...\n\n      fs.symlinkSync(symlinkPointsAt, to);\n    } else {\n      throw err;\n    }\n  }\n};\n\nconst copyItemSync = (srcPath, srcInspectData, destPath, opts) => {\n  const context = {\n    srcPath,\n    destPath,\n    srcInspectData,\n    opts\n  };\n  const mode = fileMode.normalizeFileMode(srcInspectData.mode);\n\n  if (srcInspectData.type === \"dir\") {\n    dir.createSync(destPath, {\n      mode\n    });\n  } else if (srcInspectData.type === \"file\") {\n    copyFileSync(srcPath, destPath, mode, context);\n  } else if (srcInspectData.type === \"symlink\") {\n    copySymlinkSync(srcPath, destPath);\n  }\n};\n\nconst copySync = (from, to, options) => {\n  const opts = parseOptions(options, from);\n  checksBeforeCopyingSync(from, to, opts);\n  treeWalker.sync(from, {\n    inspectOptions\n  }, (srcPath, srcInspectData) => {\n    const rel = pathUtil.relative(from, srcPath);\n    const destPath = pathUtil.resolve(to, rel);\n\n    if (opts.allowedToCopy(srcPath, destPath, srcInspectData)) {\n      copyItemSync(srcPath, srcInspectData, destPath, opts);\n    }\n  });\n}; // ---------------------------------------------------------\n// Async\n// ---------------------------------------------------------\n\n\nconst checksBeforeCopyingAsync = (from, to, opts) => {\n  return exists.async(from).then(srcPathExists => {\n    if (!srcPathExists) {\n      throw generateNoSourceError(from);\n    } else {\n      return exists.async(to);\n    }\n  }).then(destPathExists => {\n    if (destPathExists && !opts.overwrite) {\n      throw generateDestinationExistsError(to);\n    }\n  });\n};\n\nconst canOverwriteItAsync = context => {\n  return new Promise((resolve, reject) => {\n    if (typeof context.opts.overwrite === \"function\") {\n      inspect.async(context.destPath, inspectOptions).then(destInspectData => {\n        resolve(context.opts.overwrite(context.srcInspectData, destInspectData));\n      }).catch(reject);\n    } else {\n      resolve(context.opts.overwrite === true);\n    }\n  });\n};\n\nconst copyFileAsync = (srcPath, destPath, mode, context, runOptions) => {\n  return new Promise((resolve, reject) => {\n    const runOpts = runOptions || {};\n    let flags = \"wx\";\n\n    if (runOpts.overwrite) {\n      flags = \"w\";\n    }\n\n    const readStream = fs.createReadStream(srcPath);\n    const writeStream = fs.createWriteStream(destPath, {\n      mode,\n      flags\n    });\n    readStream.on(\"error\", reject);\n    writeStream.on(\"error\", err => {\n      // Force read stream to close, since write stream errored\n      // read stream serves us no purpose.\n      readStream.resume();\n\n      if (err.code === \"ENOENT\") {\n        // Some parent directory doesn't exits. Create it and retry.\n        dir.createAsync(pathUtil.dirname(destPath)).then(() => {\n          copyFileAsync(srcPath, destPath, mode, context).then(resolve, reject);\n        }).catch(reject);\n      } else if (err.code === \"EEXIST\") {\n        canOverwriteItAsync(context).then(canOverwite => {\n          if (canOverwite) {\n            copyFileAsync(srcPath, destPath, mode, context, {\n              overwrite: true\n            }).then(resolve, reject);\n          } else if (shouldThrowDestinationExistsError(context)) {\n            reject(generateDestinationExistsError(destPath));\n          } else {\n            resolve();\n          }\n        }).catch(reject);\n      } else {\n        reject(err);\n      }\n    });\n    writeStream.on(\"finish\", resolve);\n    readStream.pipe(writeStream);\n  });\n};\n\nconst copySymlinkAsync = (from, to) => {\n  return fs.readlink(from).then(symlinkPointsAt => {\n    return new Promise((resolve, reject) => {\n      fs.symlink(symlinkPointsAt, to).then(resolve).catch(err => {\n        if (err.code === \"EEXIST\") {\n          // There is already file/symlink with this name on destination location.\n          // Must erase it manually, otherwise system won't allow us to place symlink there.\n          fs.unlink(to).then(() => {\n            // Retry...\n            return fs.symlink(symlinkPointsAt, to);\n          }).then(resolve, reject);\n        } else {\n          reject(err);\n        }\n      });\n    });\n  });\n};\n\nconst copyItemAsync = (srcPath, srcInspectData, destPath, opts) => {\n  const context = {\n    srcPath,\n    destPath,\n    srcInspectData,\n    opts\n  };\n  const mode = fileMode.normalizeFileMode(srcInspectData.mode);\n\n  if (srcInspectData.type === \"dir\") {\n    return dir.createAsync(destPath, {\n      mode\n    });\n  } else if (srcInspectData.type === \"file\") {\n    return copyFileAsync(srcPath, destPath, mode, context);\n  } else if (srcInspectData.type === \"symlink\") {\n    return copySymlinkAsync(srcPath, destPath);\n  } // Ha! This is none of supported file system entities. What now?\n  // Just continuing without actually copying sounds sane.\n\n\n  return Promise.resolve();\n};\n\nconst copyAsync = (from, to, options) => {\n  return new Promise((resolve, reject) => {\n    const opts = parseOptions(options, from);\n    checksBeforeCopyingAsync(from, to, opts).then(() => {\n      let allFilesDelivered = false;\n      let filesInProgress = 0;\n      treeWalker.async(from, {\n        inspectOptions\n      }, (srcPath, item) => {\n        if (item) {\n          const rel = pathUtil.relative(from, srcPath);\n          const destPath = pathUtil.resolve(to, rel);\n\n          if (opts.allowedToCopy(srcPath, item, destPath)) {\n            filesInProgress += 1;\n            copyItemAsync(srcPath, item, destPath, opts).then(() => {\n              filesInProgress -= 1;\n\n              if (allFilesDelivered && filesInProgress === 0) {\n                resolve();\n              }\n            }).catch(reject);\n          }\n        }\n      }, err => {\n        if (err) {\n          reject(err);\n        } else {\n          allFilesDelivered = true;\n\n          if (allFilesDelivered && filesInProgress === 0) {\n            resolve();\n          }\n        }\n      });\n    }).catch(reject);\n  });\n}; // ---------------------------------------------------------\n// API\n// ---------------------------------------------------------\n\n\nexports.validateInput = validateInput;\nexports.sync = copySync;\nexports.async = copyAsync;","map":{"version":3,"sources":["D:/Development/Work/CENOS/cenos-ui/node_modules/fs-jetpack/lib/copy.js"],"names":["pathUtil","require","fs","dir","exists","inspect","write","matcher","fileMode","treeWalker","validate","validateInput","methodName","from","to","options","methodSignature","argument","overwrite","matching","ignoreCase","parseOptions","opts","parsedOptions","undefined","allowedToCopy","create","generateNoSourceError","path","err","Error","code","generateDestinationExistsError","inspectOptions","mode","symlinks","times","absolutePath","shouldThrowDestinationExistsError","context","checksBeforeCopyingSync","sync","canOverwriteItSync","destInspectData","destPath","srcInspectData","copyFileSync","srcPath","data","readFileSync","writeFileSync","flag","copySymlinkSync","symlinkPointsAt","readlinkSync","symlinkSync","unlinkSync","copyItemSync","normalizeFileMode","type","createSync","copySync","rel","relative","resolve","checksBeforeCopyingAsync","async","then","srcPathExists","destPathExists","canOverwriteItAsync","Promise","reject","catch","copyFileAsync","runOptions","runOpts","flags","readStream","createReadStream","writeStream","createWriteStream","on","resume","createAsync","dirname","canOverwite","pipe","copySymlinkAsync","readlink","symlink","unlink","copyItemAsync","copyAsync","allFilesDelivered","filesInProgress","item","exports"],"mappings":"AAAA;;AAEA,MAAMA,QAAQ,GAAGC,OAAO,CAAC,MAAD,CAAxB;;AACA,MAAMC,EAAE,GAAGD,OAAO,CAAC,YAAD,CAAlB;;AACA,MAAME,GAAG,GAAGF,OAAO,CAAC,OAAD,CAAnB;;AACA,MAAMG,MAAM,GAAGH,OAAO,CAAC,UAAD,CAAtB;;AACA,MAAMI,OAAO,GAAGJ,OAAO,CAAC,WAAD,CAAvB;;AACA,MAAMK,KAAK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ACQ,MAAM;AACV4B,cAAAA,eAAe,IAAI,CAAnB;;AACA,kBAAID,iBAAiB,IAAIC,eAAe,KAAK,CAA7C,EAAgD;AAC9C/B,gBAAAA,OAAO;AACR;AACF,aANH,EAOGS,KAPH,CAOSD,MAPT;AAQD;AACF;AACF,OAnBH,EAoBE3C,GAAG,IAAI;AACL,YAAIA,GAAJ,EAAS;AACP2C,UAAAA,MAAM,CAAC3C,GAAD,CAAN;AACD,SAFD,MAEO;AACLiE,UAAAA,iBAAiB,GAAG,IAApB;;AACA,cAAIA,iBAAiB,IAAIC,eAAe,KAAK,CAA7C,EAAgD;AAC9C/B,YAAAA,OAAO;AACR;AACF;AACF,OA7BH;AA+BD,KApCH,EAqCGS,KArCH,CAqCSD,MArCT;AAsCD,GAzCM,CAAP;AA0CD,CA3CD,C,CA6CA;AACA;AACA;;;AAEAyB,OAAO,CAACtF,aAAR,GAAwBA,aAAxB;AACAsF,OAAO,CAACxD,IAAR,GAAeoB,QAAf;AACAoC,OAAO,CAAC/B,KAAR,GAAgB2B,SAAhB","sourcesContent":["\"use strict\";\n\nconst pathUtil = require(\"path\");\nconst fs = require(\"./utils/fs\");\nconst dir = require(\"./dir\");\nconst exists = require(\"./exists\");\nconst inspect = require(\"./inspect\");\nconst write = require(\"./write\");\nconst matcher = require(\"./utils/matcher\");\nconst fileMode = require(\"./utils/mode\");\nconst treeWalker = require(\"./utils/tree_walker\");\nconst validate = require(\"./utils/validate\");\n\nconst validateInput = (methodName, from, to, options) => {\n  const methodSignature = `${methodName}(from, to, [options])`;\n  validate.argument(methodSignature, \"from\", from, [\"string\"]);\n  validate.argument(methodSignature, \"to\", to, [\"string\"]);\n  validate.options(methodSignature, \"options\", options, {\n    overwrite: [\"boolean\", \"function\"],\n    matching: [\"string\", \"array of string\"],\n    ignoreCase: [\"boolean\"]\n  });\n};\n\nconst parseOptions = (options, from) => {\n  const opts = options || {};\n  const parsedOptions = {};\n\n  if (opts.ignoreCase === undefined) {\n    opts.ignoreCase = false;\n  }\n\n  parsedOptions.overwrite = opts.overwrite;\n\n  if (opts.matching) {\n    parsedOptions.allowedToCopy = matcher.create(\n      from,\n      opts.matching,\n      opts.ignoreCase\n    );\n  } else {\n    parsedOptions.allowedToCopy = () => {\n      // Default behaviour - copy everything.\n      return true;\n    };\n  }\n\n  return parsedOptions;\n};\n\nconst generateNoSourceError = path => {\n  const err = new Error(`Path to copy doesn't exist ${path}`);\n  err.code = \"ENOENT\";\n  return err;\n};\n\nconst generateDestinationExistsError = path => {\n  const err = new Error(`Destination path already exists ${path}`);\n  err.code = \"EEXIST\";\n  return err;\n};\n\nconst inspectOptions = {\n  mode: true,\n  symlinks: \"report\",\n  times: true,\n  absolutePath: true\n};\n\nconst shouldThrowDestinationExistsError = context => {\n  return (\n    typeof context.opts.overwrite !== \"function\" &&\n    context.opts.overwrite !== true\n  );\n};\n\n// ---------------------------------------------------------\n// Sync\n// ---------------------------------------------------------\n\nconst checksBeforeCopyingSync = (from, to, opts) => {\n  if (!exists.sync(from)) {\n    throw generateNoSourceError(from);\n  }\n\n  if (exists.sync(to) && !opts.overwrite) {\n    throw generateDestinationExistsError(to);\n  }\n};\n\nconst canOverwriteItSync = context => {\n  if (typeof context.opts.overwrite === \"function\") {\n    const destInspectData = inspect.sync(context.destPath, inspectOptions);\n    return context.opts.overwrite(context.srcInspectData, destInspectData);\n  }\n  return context.opts.overwrite === true;\n};\n\nconst copyFileSync = (srcPath, destPath, mode, context) => {\n  const data = fs.readFileSync(srcPath);\n  try {\n    fs.writeFileSync(destPath, data, { mode, flag: \"wx\" });\n  } catch (err) {\n    if (err.code === \"ENOENT\") {\n      write.sync(destPath, data, { mode });\n    } else if (err.code === \"EEXIST\") {\n      if (canOverwriteItSync(context)) {\n        fs.writeFileSync(destPath, data, { mode });\n      } else if (shouldThrowDestinationExistsError(context)) {\n        throw generateDestinationExistsError(context.destPath);\n      }\n    } else {\n      throw err;\n    }\n  }\n};\n\nconst copySymlinkSync = (from, to) => {\n  const symlinkPointsAt = fs.readlinkSync(from);\n  try {\n    fs.symlinkSync(symlinkPointsAt, to);\n  } catch (err) {\n    // There is already file/symlink with this name on destination location.\n    // Must erase it manually, otherwise system won't allow us to place symlink there.\n    if (err.code === \"EEXIST\") {\n      fs.unlinkSync(to);\n      // Retry...\n      fs.symlinkSync(symlinkPointsAt, to);\n    } else {\n      throw err;\n    }\n  }\n};\n\nconst copyItemSync = (srcPath, srcInspectData, destPath, opts) => {\n  const context = { srcPath, destPath, srcInspectData, opts };\n  const mode = fileMode.normalizeFileMode(srcInspectData.mode);\n  if (srcInspectData.type === \"dir\") {\n    dir.createSync(destPath, { mode });\n  } else if (srcInspectData.type === \"file\") {\n    copyFileSync(srcPath, destPath, mode, context);\n  } else if (srcInspectData.type === \"symlink\") {\n    copySymlinkSync(srcPath, destPath);\n  }\n};\n\nconst copySync = (from, to, options) => {\n  const opts = parseOptions(options, from);\n\n  checksBeforeCopyingSync(from, to, opts);\n\n  treeWalker.sync(from, { inspectOptions }, (srcPath, srcInspectData) => {\n    const rel = pathUtil.relative(from, srcPath);\n    const destPath = pathUtil.resolve(to, rel);\n    if (opts.allowedToCopy(srcPath, destPath, srcInspectData)) {\n      copyItemSync(srcPath, srcInspectData, destPath, opts);\n    }\n  });\n};\n\n// ---------------------------------------------------------\n// Async\n// ---------------------------------------------------------\n\nconst checksBeforeCopyingAsync = (from, to, opts) => {\n  return exists\n    .async(from)\n    .then(srcPathExists => {\n      if (!srcPathExists) {\n        throw generateNoSourceError(from);\n      } else {\n        return exists.async(to);\n      }\n    })\n    .then(destPathExists => {\n      if (destPathExists && !opts.overwrite) {\n        throw generateDestinationExistsError(to);\n      }\n    });\n};\n\nconst canOverwriteItAsync = context => {\n  return new Promise((resolve, reject) => {\n    if (typeof context.opts.overwrite === \"function\") {\n      inspect\n        .async(context.destPath, inspectOptions)\n        .then(destInspectData => {\n          resolve(\n            context.opts.overwrite(context.srcInspectData, destInspectData)\n          );\n        })\n        .catch(reject);\n    } else {\n      resolve(context.opts.overwrite === true);\n    }\n  });\n};\n\nconst copyFileAsync = (srcPath, destPath, mode, context, runOptions) => {\n  return new Promise((resolve, reject) => {\n    const runOpts = runOptions || {};\n\n    let flags = \"wx\";\n    if (runOpts.overwrite) {\n      flags = \"w\";\n    }\n\n    const readStream = fs.createReadStream(srcPath);\n    const writeStream = fs.createWriteStream(destPath, { mode, flags });\n\n    readStream.on(\"error\", reject);\n\n    writeStream.on(\"error\", err => {\n      // Force read stream to close, since write stream errored\n      // read stream serves us no purpose.\n      readStream.resume();\n\n      if (err.code === \"ENOENT\") {\n        // Some parent directory doesn't exits. Create it and retry.\n        dir\n          .createAsync(pathUtil.dirname(destPath))\n          .then(() => {\n            copyFileAsync(srcPath, destPath, mode, context).then(\n              resolve,\n              reject\n            );\n          })\n          .catch(reject);\n      } else if (err.code === \"EEXIST\") {\n        canOverwriteItAsync(context)\n          .then(canOverwite => {\n            if (canOverwite) {\n              copyFileAsync(srcPath, destPath, mode, context, {\n                overwrite: true\n              }).then(resolve, reject);\n            } else if (shouldThrowDestinationExistsError(context)) {\n              reject(generateDestinationExistsError(destPath));\n            } else {\n              resolve();\n            }\n          })\n          .catch(reject);\n      } else {\n        reject(err);\n      }\n    });\n\n    writeStream.on(\"finish\", resolve);\n\n    readStream.pipe(writeStream);\n  });\n};\n\nconst copySymlinkAsync = (from, to) => {\n  return fs.readlink(from).then(symlinkPointsAt => {\n    return new Promise((resolve, reject) => {\n      fs.symlink(symlinkPointsAt, to)\n        .then(resolve)\n        .catch(err => {\n          if (err.code === \"EEXIST\") {\n            // There is already file/symlink with this name on destination location.\n            // Must erase it manually, otherwise system won't allow us to place symlink there.\n            fs.unlink(to)\n              .then(() => {\n                // Retry...\n                return fs.symlink(symlinkPointsAt, to);\n              })\n              .then(resolve, reject);\n          } else {\n            reject(err);\n          }\n        });\n    });\n  });\n};\n\nconst copyItemAsync = (srcPath, srcInspectData, destPath, opts) => {\n  const context = { srcPath, destPath, srcInspectData, opts };\n  const mode = fileMode.normalizeFileMode(srcInspectData.mode);\n  if (srcInspectData.type === \"dir\") {\n    return dir.createAsync(destPath, { mode });\n  } else if (srcInspectData.type === \"file\") {\n    return copyFileAsync(srcPath, destPath, mode, context);\n  } else if (srcInspectData.type === \"symlink\") {\n    return copySymlinkAsync(srcPath, destPath);\n  }\n  // Ha! This is none of supported file system entities. What now?\n  // Just continuing without actually copying sounds sane.\n  return Promise.resolve();\n};\n\nconst copyAsync = (from, to, options) => {\n  return new Promise((resolve, reject) => {\n    const opts = parseOptions(options, from);\n\n    checksBeforeCopyingAsync(from, to, opts)\n      .then(() => {\n        let allFilesDelivered = false;\n        let filesInProgress = 0;\n\n        treeWalker.async(\n          from,\n          { inspectOptions },\n          (srcPath, item) => {\n            if (item) {\n              const rel = pathUtil.relative(from, srcPath);\n              const destPath = pathUtil.resolve(to, rel);\n              if (opts.allowedToCopy(srcPath, item, destPath)) {\n                filesInProgress += 1;\n                copyItemAsync(srcPath, item, destPath, opts)\n                  .then(() => {\n                    filesInProgress -= 1;\n                    if (allFilesDelivered && filesInProgress === 0) {\n                      resolve();\n                    }\n                  })\n                  .catch(reject);\n              }\n            }\n          },\n          err => {\n            if (err) {\n              reject(err);\n            } else {\n              allFilesDelivered = true;\n              if (allFilesDelivered && filesInProgress === 0) {\n                resolve();\n              }\n            }\n          }\n        );\n      })\n      .catch(reject);\n  });\n};\n\n// ---------------------------------------------------------\n// API\n// ---------------------------------------------------------\n\nexports.validateInput = validateInput;\nexports.sync = copySync;\nexports.async = copyAsync;\n"]},"metadata":{},"sourceType":"script"}