export async function signAddon()

in src/util/submit-addon.js [466:543]


export async function signAddon({
  apiKey,
  apiSecret,
  apiProxy,
  amoBaseUrl,
  validationCheckTimeout,
  approvalCheckTimeout,
  id,
  xpiPath,
  downloadDir,
  channel,
  savedIdPath,
  savedUploadUuidPath,
  metaDataJson = {},
  submissionSource,
  userAgentString = 'web-ext-lib',
  SubmitClient = Client,
  ApiAuthClass = JwtApiAuth,
}) {
  try {
    const stats = await fsPromises.stat(xpiPath);

    if (!stats.isFile()) {
      throw new Error('not a file');
    }
  } catch (statError) {
    throw new Error(`error with ${xpiPath}: ${statError}`);
  }

  let baseUrl;
  try {
    baseUrl = new URL(amoBaseUrl);
  } catch (err) {
    throw new Error(`Invalid AMO API base URL: ${amoBaseUrl}`);
  }

  const client = new SubmitClient({
    apiAuth: new ApiAuthClass({ apiKey, apiSecret }),
    apiProxy,
    baseUrl,
    validationCheckTimeout,
    approvalCheckTimeout,
    downloadDir,
    userAgentString,
  });
  const uploadUuid = await client.getPreviousUuidOrUploadXpi(
    xpiPath,
    channel,
    savedUploadUuidPath,
  );
  const patchData = {};
  // if we have a source file we need to upload we patch after the create
  if (submissionSource) {
    try {
      const stats2 = await fsPromises.stat(submissionSource);

      if (!stats2.isFile()) {
        throw new Error('not a file');
      }
    } catch (statError) {
      throw new Error(`error with ${submissionSource}: ${statError}`);
    }
    patchData.version = { source: client.fileFromSync(submissionSource) };
  }

  // We specifically need to know if `id` has not been passed as a parameter because
  // it's the indication that a new add-on should be created, rather than a new version.
  if (id === undefined) {
    return client.postNewAddon(
      uploadUuid,
      savedIdPath,
      metaDataJson,
      patchData,
    );
  }

  return client.putVersion(uploadUuid, id, metaDataJson, patchData);
}