{"version":3,"file":"loader.cjs","names":[],"sources":["../../../../../node_modules/mock-fs/lib/loader.js"],"sourcesContent":["const {fixWin32Permissions} = require('./item.js');\nconst path = require('path');\nconst FileSystem = require('./filesystem.js');\nconst fs = require('fs');\nconst bypass = require('./bypass.js');\n\nconst createContext = ({output, options = {}, target}, newContext) =>\n  Object.assign(\n    {\n      // Assign options and set defaults if needed\n      options: {\n        recursive: options.recursive !== false,\n        lazy: options.lazy !== false,\n      },\n      output,\n      target,\n    },\n    newContext\n  );\n\nfunction addFile(context, stats, isRoot) {\n  const {output, target} = context;\n  const {lazy} = context.options;\n\n  if (!stats.isFile()) {\n    throw new Error(`${target} is not a valid file!`);\n  }\n\n  const outputPropKey = isRoot ? target : path.basename(target);\n\n  output[outputPropKey] = () => {\n    const content = !lazy ? fs.readFileSync(target) : '';\n    const file = FileSystem.file(Object.assign({}, stats, {content}))();\n\n    if (lazy) {\n      Object.defineProperty(file, '_content', {\n        get() {\n          const res = bypass(() => fs.readFileSync(target));\n          Object.defineProperty(file, '_content', {\n            value: res,\n            writable: true,\n          });\n          return res;\n        },\n        set(data) {\n          Object.defineProperty(file, '_content', {\n            value: data,\n            writable: true,\n          });\n        },\n        configurable: true,\n      });\n    }\n\n    return file;\n  };\n\n  return output[outputPropKey];\n}\n\nfunction addDir(context, stats, isRoot) {\n  const {target, output} = context;\n  const {recursive} = context.options;\n\n  if (!stats.isDirectory()) {\n    throw new Error(`${target} is not a valid directory!`);\n  }\n\n  stats = Object.assign({}, stats);\n  const outputPropKey = isRoot ? target : path.basename(target);\n\n  // On windows platforms, directories do not have the executable flag, which causes FileSystem.prototype.getItem\n  // to think that the directory cannot be traversed. This is a workaround, however, a better solution may be to\n  // re-think the logic in FileSystem.prototype.getItem\n  // This workaround adds executable privileges if read privileges are found\n  stats.mode = fixWin32Permissions(stats.mode);\n\n  // Create directory factory\n  const directoryItems = {};\n  output[outputPropKey] = FileSystem.directory(\n    Object.assign(stats, {items: directoryItems})\n  );\n\n  fs.readdirSync(target).forEach((p) => {\n    const absPath = path.join(target, p);\n    const stats = fs.statSync(absPath);\n    const newContext = createContext(context, {\n      target: absPath,\n      output: directoryItems,\n    });\n\n    if (recursive && stats.isDirectory()) {\n      addDir(newContext, stats);\n    } else if (stats.isFile()) {\n      addFile(newContext, stats);\n    }\n  });\n\n  return output[outputPropKey];\n}\n\n/**\n * Load directory or file from real FS\n * @param {string} p The path.\n * @param {object} options The options.\n * @return {*} The return.\n */\nexports.load = function (p, options) {\n  return bypass(() => {\n    p = path.resolve(p);\n\n    const stats = fs.statSync(p);\n    const context = createContext({output: {}, options, target: p});\n\n    if (stats.isDirectory()) {\n      return addDir(context, stats, true);\n    } else if (stats.isFile()) {\n      return addFile(context, stats, true);\n    }\n  });\n};\n"],"x_google_ignoreList":[0],"mappings":";;;;;;;CAAA,MAAM,EAAC;CACP,MAAM,OAAO,QAAQ,OAAO;CAC5B,MAAM;CACN,MAAM,KAAK,QAAQ,KAAK;CACxB,MAAM;CAEN,MAAM,iBAAiB,EAAC,QAAQ,UAAU,EAAE,EAAE,UAAS,eACrD,OAAO,OACL;EAEE,SAAS;GACP,WAAW,QAAQ,cAAc;GACjC,MAAM,QAAQ,SAAS;GACxB;EACD;EACA;EACD,EACD,WACD;CAEH,SAAS,QAAQ,SAAS,OAAO,QAAQ;EACvC,MAAM,EAAC,QAAQ,WAAU;EACzB,MAAM,EAAC,SAAQ,QAAQ;AAEvB,MAAI,CAAC,MAAM,QAAQ,CACjB,OAAM,IAAI,MAAM,GAAG,OAAO,uBAAuB;EAGnD,MAAM,gBAAgB,SAAS,SAAS,KAAK,SAAS,OAAO;AAE7D,SAAO,uBAAuB;GAC5B,MAAM,UAAU,CAAC,OAAO,GAAG,aAAa,OAAO,GAAG;GAClD,MAAM,OAAO,WAAW,KAAK,OAAO,OAAO,EAAE,EAAE,OAAO,EAAC,SAAQ,CAAC,CAAC,EAAE;AAEnE,OAAI,KACF,QAAO,eAAe,MAAM,YAAY;IACtC,MAAM;KACJ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