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AAM,IAAI,KAAK,CAAC,2BAA2B,YAAY,iBAAiB,CAAC,CAAA;SAC1E;aAAM;YACL,MAAM,CAAC,CAAA;SACR;KACF;IAED,OAAO,OAAO,CAAA;AAChB,CAAC;AA9BD,0BA8BC;AAED,SAAS,WAAW,CAAC,OAAkB;IACrC,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IAC7B,IAAI,sBAAO,CAAC,OAAO,EAAE;QACnB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;KACjB;IACD,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["import { path7za } from \"7zip-bin\"\nimport { debug7z, exec } from \"builder-util\"\nimport { exists, unlinkIfExists } from \"builder-util/out/fs\"\nimport { move } from \"fs-extra\"\nimport * as path from \"path\"\nimport { TmpDir } from \"temp-file\"\nimport { CompressionLevel } from \"../core\"\nimport { getLinuxToolsPath } from \"./tools\"\n\n/** @internal */\nexport async function tar(\n  compression: CompressionLevel | any | any,\n  format: string,\n  outFile: string,\n  dirToArchive: string,\n  isMacApp: boolean,\n  tempDirManager: TmpDir\n): Promise<void> {\n  const tarFile = await tempDirManager.getTempFile({ suffix: \".tar\" })\n  const tarArgs = debug7zArgs(\"a\")\n  tarArgs.push(tarFile)\n  tarArgs.push(path.basename(dirToArchive))\n\n  await Promise.all([\n    exec(path7za, tarArgs, { cwd: path.dirname(dirToArchive) }),\n    // remove file before - 7z doesn't overwrite file, but update\n    unlinkIfExists(outFile),\n  ])\n\n  if (!isMacApp) {\n    await exec(path7za, [\"rn\", tarFile, path.basename(dirToArchive), path.basename(outFile, `.${format}`)])\n  }\n\n  if (format === \"tar.lz\") {\n    // noinspection SpellCheckingInspection\n    let lzipPath = \"lzip\"\n    if (process.platform === \"darwin\") {\n      lzipPath = path.join(await getLinuxToolsPath(), \"bin\", lzipPath)\n    }\n    await exec(lzipPath, [compression === \"store\" ? \"-1\" : \"-9\", \"--keep\" /* keep (don't delete) input files */, tarFile])\n    // bloody lzip creates file in the same dir where input file with postfix `.lz`, option --output doesn't work\n    await move(`${tarFile}.lz`, outFile)\n    return\n  }\n\n  const args = compute7zCompressArgs(format === \"tar.xz\" ? \"xz\" : format === \"tar.bz2\" ? \"bzip2\" : \"gzip\", {\n    isRegularFile: true,\n    method: \"DEFAULT\",\n    compression,\n  })\n  args.push(outFile, tarFile)\n  await exec(\n    path7za,\n    args,\n    {\n      cwd: path.dirname(dirToArchive),\n    },\n    debug7z.enabled\n  )\n}\n\nexport interface ArchiveOptions {\n  compression?: CompressionLevel | null\n\n  /**\n   * @default false\n   */\n  withoutDir?: boolean\n\n  /**\n   * @default true\n   */\n  solid?: boolean\n\n  /**\n   * @default true\n   */\n  isArchiveHeaderCompressed?: boolean\n\n  dictSize?: number\n  excluded?: Array<string> | null\n\n  // DEFAULT allows to disable custom logic and do not pass method switch at all\n  method?: \"Copy\" | \"LZMA\" | \"Deflate\" | \"DEFAULT\"\n\n  isRegularFile?: boolean\n}\n\nexport function compute7zCompressArgs(format: string, options: ArchiveOptions = {}) {\n  let storeOnly = options.compression === \"store\"\n  const args = debug7zArgs(\"a\")\n\n  let isLevelSet = false\n  if (process.env.ELECTRON_BUILDER_COMPRESSION_LEVEL != null) {\n    storeOnly = false\n    args.push(`-mx=${process.env.ELECTRON_BUILDER_COMPRESSION_LEVEL}`)\n    isLevelSet = true\n  }\n\n  const isZip = format === \"zip\"\n  if (!storeOnly) {\n    if (isZip && options.compression === \"maximum\") {\n      // http://superuser.com/a/742034\n      args.push(\"-mfb=258\", \"-mpass=15\")\n    }\n\n    if (!isLevelSet) {\n      // https://github.com/electron-userland/electron-builder/pull/3032\n      args.push(\"-mx=\" + (!isZip || options.compression === \"maximum\" ? \"9\" : \"7\"))\n    }\n  }\n\n  if (options.dictSize != null) {\n    args.push(`-md=${options.dictSize}m`)\n  }\n\n  // https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm#7Z\n  // https://stackoverflow.com/questions/27136783/7zip-produces-different-output-from-identical-input\n  // tc and ta are off by default, but to be sure, we explicitly set it to off\n  // disable \"Stores NTFS timestamps for files: Modification time, Creation time, Last access time.\" to produce the same archive for the same data\n  if (!options.isRegularFile) {\n    args.push(\"-mtc=off\")\n  }\n\n  if (format === \"7z\" || format.endsWith(\".7z\")) {\n    if (options.solid === false) {\n      args.push(\"-ms=off\")\n    }\n\n    if (options.isArchiveHeaderCompressed === false) {\n      args.push(\"-mhc=off\")\n    }\n\n    // args valid only for 7z\n    // -mtm=off disable \"Stores last Modified timestamps for files.\"\n    args.push(\"-mtm=off\", \"-mta=off\")\n  }\n\n  if (options.method != null) {\n    if (options.method !== \"DEFAULT\") {\n      args.push(`-mm=${options.method}`)\n    }\n  } else if (isZip || storeOnly) {\n    args.push(`-mm=${storeOnly ? \"Copy\" : \"Deflate\"}`)\n  }\n\n  if (isZip) {\n    // -mcu switch:  7-Zip uses UTF-8, if there are non-ASCII symbols.\n    // because default mode: 7-Zip uses UTF-8, if the local code page doesn't contain required symbols.\n    // but archive should be the same regardless where produced\n    args.push(\"-mcu\")\n  }\n  return args\n}\n\n// 7z is very fast, so, use ultra compression\n/** @internal */\nexport async function archive(format: string, outFile: string, dirToArchive: string, options: ArchiveOptions = {}): Promise<string> {\n  const args = compute7zCompressArgs(format, options)\n  // remove file before - 7z doesn't overwrite file, but update\n  await unlinkIfExists(outFile)\n\n  args.push(outFile, options.withoutDir ? \".\" : path.basename(dirToArchive))\n  if (options.excluded != null) {\n    for (const mask of options.excluded) {\n      args.push(`-xr!${mask}`)\n    }\n  }\n\n  try {\n    await exec(\n      path7za,\n      args,\n      {\n        cwd: options.withoutDir ? dirToArchive : path.dirname(dirToArchive),\n      },\n      debug7z.enabled\n    )\n  } catch (e) {\n    if (e.code === \"ENOENT\" && !(await exists(dirToArchive))) {\n      throw new Error(`Cannot create archive: \"${dirToArchive}\" doesn't exist`)\n    } else {\n      throw e\n    }\n  }\n\n  return outFile\n}\n\nfunction debug7zArgs(command: \"a\" | \"x\"): Array<string> {\n  const args = [command, \"-bd\"]\n  if (debug7z.enabled) {\n    args.push(\"-bb\")\n  }\n  return args\n}\n"]}