<commit_before>import butler from "../../util/butler";
import { extract } from "../../util/extract";
import deploy from "../../util/deploy";

import { coreOperate, ICoreInstallOpts, IInstallManager } from "./core";
import getInstallerType from "./get-installer-type";

async function install(opts: ICoreInstallOpts) {
  const { ctx, archivePath } = opts;
  const logger = opts.logger.child({ name: "archive/install" });

  const stagePath = opts.archivePath + "-stage";
  await butler.wipe(stagePath, { logger, ctx });
  await butler.mkdir(stagePath, { logger, ctx });

  logger.info(`extracting archive '${archivePath}' to '${stagePath}'`);

  // TODO: use sub-context for progress [0, 0.8]
  await extract({
    ctx,
    archivePath: opts.archivePath,
    destPath: stagePath,
    logger,
  });

  logger.info(`extracted all files ${archivePath} into staging area`);

  const { caveId } = opts;

  // TODO: use sub-context for progress [0.8, 1]
  await deploy({
    ctx,
    caveId,
    logger,
    stagePath,
    destPath: opts.destPath,
    onSingle: async onlyFile => await handleNested(opts, onlyFile),
  });

  logger.info("wiping stage...");
  await butler.wipe(stagePath, { logger, ctx });
  logger.info("done wiping stage");
}

async function uninstall(opts: ICoreInstallOpts) {
  const { ctx, destPath } = opts;
  const logger = opts.logger.child({ name: "archive/install" });

  logger.info(`wiping directory ${destPath}`);
  await butler.wipe(destPath, { logger, ctx });
}

async function handleNested(opts: ICoreInstallOpts, onlyFile: string) {
  const { ctx, logger, runtime } = opts;

  // zipped installers need love too
  const installerName = await getInstallerType({
    ctx,
    logger,
    runtime,
    target: onlyFile,
  });

  if (installerName === "unknown") {
    return null;
  }

  logger.info(`found nested installer '${installerName}': ${onlyFile}`);
  await coreOperate(
    {
      ...opts,
      archivePath: onlyFile,
    },
    "install",
    installerName,
  );

  return { deployed: true };
}

const manager: IInstallManager = { install, uninstall };
export default manager;
<commit_msg>Use subcontext for extract/deploy progress<commit_after>import butler from "../../util/butler";
import { extract } from "../../util/extract";
import deploy from "../../util/deploy";

import { coreOperate, ICoreInstallOpts, IInstallManager } from "./core";
import getInstallerType from "./get-installer-type";

async function install(opts: ICoreInstallOpts) {
  const { ctx, archivePath } = opts;
  const logger = opts.logger.child({ name: "archive/install" });

  const stagePath = opts.archivePath + "-stage";
  await butler.wipe(stagePath, { logger, ctx });
  await butler.mkdir(stagePath, { logger, ctx });

  logger.info(`extracting archive '${archivePath}' to '${stagePath}'`);

  await ctx.withSub(async subCtx => {
    subCtx.on("progress", pi =>
      ctx.emitProgress({
        ...pi,
        progress: pi.progress * 0.8,
      }),
    );
    await extract({
      ctx: subCtx,
      archivePath: opts.archivePath,
      destPath: stagePath,
      logger,
    });
  });

  logger.info(`extracted all files ${archivePath} into staging area`);

  const { caveId } = opts;

  await ctx.withSub(async subCtx => {
    subCtx.on("progress", pi =>
      ctx.emitProgress({
        ...pi,
        progress: 0.8 + pi.progress * 0.2,
      }),
    );
    await deploy({
      ctx: subCtx,
      caveId,
      logger,
      stagePath,
      destPath: opts.destPath,
      onSingle: async onlyFile => await handleNested(opts, onlyFile),
    });
  });

  logger.info("wiping stage...");
  await butler.wipe(stagePath, { logger, ctx });
  logger.info("done wiping stage");
}

async function uninstall(opts: ICoreInstallOpts) {
  const { ctx, destPath } = opts;
  const logger = opts.logger.child({ name: "archive/install" });

  logger.info(`wiping directory ${destPath}`);
  await butler.wipe(destPath, { logger, ctx });
}

async function handleNested(opts: ICoreInstallOpts, onlyFile: string) {
  const { ctx, logger, runtime } = opts;

  // zipped installers need love too
  const installerName = await getInstallerType({
    ctx,
    logger,
    runtime,
    target: onlyFile,
  });

  if (installerName === "unknown") {
    return null;
  }

  logger.info(`found nested installer '${installerName}': ${onlyFile}`);
  await coreOperate(
    {
      ...opts,
      archivePath: onlyFile,
    },
    "install",
    installerName,
  );

  return { deployed: true };
}

const manager: IInstallManager = { install, uninstall };
export default manager;

{'fraction_non_alphanumeric': 0.14810763509078975, 'fraction_numerical': 0.0026252461168234523, 'mean_word_length': 3.26890756302521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6332294', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1443, 'n_words': 373}