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AAQ,GAAK,EAC7D,EAAI,MAAJ,GACC,CAAC,QAAS,OAGf,QAAQ,IAAI,oCAEL,CACR,CAYc,MAAM,uBAcnB,aAAY,UAAC,EAAD,WAAY,IAA4C,gBAAA,KAAA,oBAAA,gBAAA,KAAA,qBAClE,KAAK,UAAY,EACjB,KAAK,WAAa,CACnB,CAGD,MAAM,GACJ,EAAS,MAAM,KAAK,WAAW,0BAA0B,UACvD,MAAM,EAAO,KAAK,gBAAgB,mBAAmB,KAAK,YAE1D,EAAY,OAAO,KAAK,YAAc,CACpC,OAAQ,IAAM,EACd,KAAM,IAAM,EAAK,OAFnB,GAKH,E","file":"/npm/@signalstickers/fetch-sticker-data-webpack-plugin@1.0.0/dist/fetch-sticker-data-webpack-plugin.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@signalstickers/fetch-sticker-data-webpack-plugin@1.0.0/dist/fetch-sticker-data-webpack-plugin.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import path from 'path';\n\nimport findUp from 'find-up';\nimport fs from 'fs-extra';\nimport yaml from 'js-yaml';\nimport pQueue from 'p-queue';\nimport pRetry from 'p-retry';\nimport ProgressBar from 'progress';\nimport * as R from 'ramda';\nimport webpack from 'webpack';\n\nimport {getStickerPackManifest} from '@signalstickers/stickers-client';\nimport {StickerPackPartial, StickerPackYaml} from 'etc/types';\n\n\n// ----- Locals ----------------------------------------------------------------\n\n/**\n * Limits concurrency of requests made to the Signal API to avoid throttling.\n */\nconst requestQueue = new pQueue({concurrency: 6});\n\n\n/**\n * Queries Signal for each sticker pack enumerated in stickers.yml and creates\n * a StickerPackPartial. This object is then cached on disk to improve\n * performance.\n */\nasync function getAllStickerPacks(inputFile: string): Promise<Array<StickerPackPartial>> {\n  const stickerPackPartials: Array<StickerPackPartial> = [];\n  const stickerPackYaml: StickerPackYaml = yaml.safeLoad(await fs.readFile(inputFile, {encoding: 'utf8'}));\n  const stickerPackEntries = Object.entries(stickerPackYaml);\n\n  const gitRoot = await findUp('.git', {type: 'directory', cwd: __dirname});\n\n  if (!gitRoot) {\n    throw new Error('Not in a git repository.');\n  }\n\n  const cacheDir = path.resolve(gitRoot, '..', '.sticker-pack-cache');\n\n  // Create the cache directory if it does not exist.\n  await fs.ensureDir(cacheDir);\n\n  console.log('[FetchStickerDataPlugin] Downloading sticker pack manifests.');\n\n  const bar = new ProgressBar('[FetchStickerDataPlugin] [:bar] :current / :total', {\n    total: stickerPackEntries.length,\n    width: 64,\n    clear: true,\n    head: '>'\n  });\n\n  await requestQueue.addAll(stickerPackEntries.map(([id, meta], index) => {\n    return async () => pRetry(async () => {\n      const cachePath = path.resolve(cacheDir, `${id}.json`);\n      const cacheHasPack = await fs.pathExists(cachePath);\n      let stickerPackPartial: StickerPackPartial;\n\n      if (cacheHasPack) {\n        stickerPackPartial = await fs.readJson(cachePath);\n      } else {\n        const stickerPackManifest = await getStickerPackManifest(id, meta.key);\n\n        stickerPackPartial = {\n          meta: {...meta, id},\n          // To keep the size of the generated JSON file small, only extract\n          // the properties from the manifest that we need to generate a list\n          // of search results.\n          manifest: R.pick(['title', 'author', 'cover'], stickerPackManifest)\n        };\n\n        await fs.writeJson(cachePath, stickerPackPartial);\n      }\n\n      // Order partials in the reverse order of stickers.yml, so the most\n      // recently added pack is first (assuming stickers.yml is only ever\n      // appended to when new packs are added).\n      stickerPackPartials[stickerPackEntries.length - index - 1] = stickerPackPartial;\n      bar.tick();\n    }, {retries: 2});\n  }));\n\n  console.log('[FetchStickerDataPlugin] Done.\\n');\n\n  return stickerPackPartials;\n}\n\n\n/**\n * Options accepted by FetchStickerDataPlugin.\n */\nexport interface FetchStickerDataPluginOptions {\n  inputFile: string;\n  outputFile: string;\n}\n\n\nexport default class FetchStickerDataPlugin {\n  /**\n   * Name of the input file, which should be a YAML document consisting of an\n   * array of StickerPackMetadata objects.\n   */\n  inputFile: string;\n\n  /**\n   * Name of the output file, which will be a JSON document containing an array\n   * of StickerPackPartial objects.\n   */\n  outputFile: string;\n\n\n  constructor({inputFile, outputFile}: FetchStickerDataPluginOptions) {\n    this.inputFile = inputFile;\n    this.outputFile = outputFile;\n  }\n\n\n  apply(compiler: webpack.Compiler) {\n    compiler.hooks.emit.tapPromise('FetchStickerDataPlugin', async compilation => {\n      const json = JSON.stringify(await getAllStickerPacks(this.inputFile));\n\n      compilation.assets[this.outputFile] = {\n        source: () => json,\n        size: () => json.length\n      };\n    });\n  }\n}\n"]}