async function saveArtifacts(artifacts, basePath, options = {}) {
  const status = {msg: 'Saving artifacts', id: 'lh:assetSaver:saveArtifacts'};
  log.time(status);
  fs.mkdirSync(basePath, {recursive: true});

  // Delete any previous artifacts in this directory.
  const filenames = fs.readdirSync(basePath);
  for (const filename of filenames) {
    const isPreviousFile =
      filename.endsWith(traceSuffix) || filename.endsWith(devtoolsLogSuffix) ||
      filename.endsWith(traceSuffix + '.gz') || filename.endsWith(devtoolsLogSuffix + '.gz') ||
      filename === artifactsFilename || filename === artifactsFilename + '.gz';
    if (isPreviousFile) {
      fs.unlinkSync(`${basePath}/${filename}`);
    }
  }

  // `devtoolsLogs` and `traces` are duplicate compat artifacts.
  // We don't need to save them twice, so extract them here. TODO(v13): remove
  const {
    // eslint-disable-next-line no-unused-vars
    traces,
    // eslint-disable-next-line no-unused-vars
    devtoolsLogs,
    DevtoolsLog,
    Trace,
    DevtoolsLogError,
    TraceError,
    ...restArtifacts
  } = artifacts;

  if (Trace) {
    await saveTrace(Trace, `${basePath}/${defaultPrefix}${traceSuffix}`, options);
  }

  if (TraceError) {
    await saveTrace(TraceError, `${basePath}/${errorPrefix}${traceSuffix}`, options);
  }

  if (DevtoolsLog) {
    await saveDevtoolsLog(
      DevtoolsLog, `${basePath}/${defaultPrefix}${devtoolsLogSuffix}`, options);
  }

  if (DevtoolsLogError) {
    await saveDevtoolsLog(
      DevtoolsLogError, `${basePath}/${errorPrefix}${devtoolsLogSuffix}`, options);
  }

  // save everything else, using a replacer to serialize LighthouseErrors in the artifacts.
  const restArtifactsString = JSON.stringify(restArtifacts, stringifyReplacer, 2);
  await writeJson(function* () {
    yield restArtifactsString;
    yield '\n';
  }, `${basePath}/${artifactsFilename}`, !!options.gzip);

  log.log('Artifacts saved to disk in folder:', basePath);
  log.timeEnd(status);
}
Save artifacts object mostly to single file located at basePath/artifacts.json.
Also save the traces & devtoolsLogs to their own files, with optional compression.
@param {LH.Artifacts} artifacts
@param {string} basePath
@param {{gzip?: boolean}} options
@return {Promise<void>}