<commit_before>import fs from 'fs';
import path from 'path';
import webpack from 'webpack';
import shelljs from 'shelljs';

import { logger } from '@storybook/node-logger';

import { getProdCli } from './cli';
import loadConfig from './config';
import loadManagerConfig from './manager/manager-config';

const defaultFavIcon = require.resolve('./public/favicon.ico');

export async function buildStaticStandalone(options) {
  const { staticDir, watch, configDir, packageJson } = options;

  const configType = 'PRODUCTION';
  const outputDir = path.join(process.cwd(), options.outputDir);

  // create output directory if not exists
  shelljs.mkdir('-p', outputDir);
  // clear the static dir
  shelljs.rm('-rf', path.join(outputDir, 'static'));
  shelljs.cp(defaultFavIcon, outputDir);

  logger.info('building manager..');
  const managerStartTime = process.hrtime();

  const managerConfig = await loadManagerConfig({
    configType,
    outputDir,
    configDir,
    corePresets: [require.resolve('./manager/manager-preset.js')],
  });

  await new Promise((res, rej) => {
    webpack(managerConfig).run((err, stats) => {
      const managerTotalTime = process.hrtime(managerStartTime);
      logger.trace({ message: 'manager built', time: managerTotalTime });

      if (stats.hasErrors()) {
        rej(stats);
      } else {
        res(stats);
      }
    });
  });

  // Build the webpack configuration using the `baseConfig`
  // custom `.babelrc` file and `webpack.config.js` files
  // NOTE changes to env should be done before calling `getBaseConfig`
  const config = await loadConfig({
    configType,
    outputDir,
    packageJson,
    corePresets: [require.resolve('./preview/preview-preset.js')],
    overridePresets: [require.resolve('./preview/custom-webpack-preset.js')],
    ...options,
  });

  // config.output.path = path.resolve(outputDir);

  // copy all static files
  if (staticDir) {
    staticDir.forEach(dir => {
      if (!fs.existsSync(dir)) {
        logger.error(`Error: no such directory to load static files: ${dir}`);
        process.exit(-1);
      }
      logger.info(`=> Copying static files from: ${dir}`);
      shelljs.cp('-r', `${dir}/*`, outputDir);
    });
  }

  // compile all resources with webpack and write them to the disk.
  return new Promise((resolve, reject) => {
    const previewStartTime = process.hrtime();

    const webpackCb = (err, stats) => {
      if (err || stats.hasErrors()) {
        logger.error('Failed to build the storybook');
        // eslint-disable-next-line no-unused-expressions
        err && logger.error(err.message);
        // eslint-disable-next-line no-unused-expressions
        stats && stats.hasErrors() && stats.toJson().errors.forEach(e => logger.error(e));
        process.exitCode = 1;
        return reject(err);
      }

      const previewTotalTime = process.hrtime(previewStartTime);
      logger.trace({ message: 'preview built', time: previewTotalTime });
<commit_msg>FIX index.html being copied into public folder (breaks our chromatic setup)<commit_after>import fs from 'fs';
import path from 'path';
import webpack from 'webpack';
import shelljs from 'shelljs';

import { logger } from '@storybook/node-logger';

import { getProdCli } from './cli';
import loadConfig from './config';
import loadManagerConfig from './manager/manager-config';

const defaultFavIcon = require.resolve('./public/favicon.ico');

export async function buildStaticStandalone(options) {
  const { staticDir, watch, configDir, packageJson } = options;

  const configType = 'PRODUCTION';
  const outputDir = path.join(process.cwd(), options.outputDir);

  // create output directory if not exists
  shelljs.mkdir('-p', outputDir);
  // clear the static dir
  shelljs.rm('-rf', path.join(outputDir, 'static'));
  shelljs.cp(defaultFavIcon, outputDir);

  logger.info('building manager..');
  const managerStartTime = process.hrtime();

  const managerConfig = await loadManagerConfig({
    configType,
    outputDir,
    configDir,
    corePresets: [require.resolve('./manager/manager-preset.js')],
  });

  await new Promise((res, rej) => {
    webpack(managerConfig).run((err, stats) => {
      const managerTotalTime = process.hrtime(managerStartTime);
      logger.trace({ message: 'manager built', time: managerTotalTime });

      if (stats.hasErrors()) {
        rej(stats);
      } else {
        res(stats);
      }
    });
  });

  // Build the webpack configuration using the `baseConfig`
  // custom `.babelrc` file and `webpack.config.js` files
  // NOTE changes to env should be done before calling `getBaseConfig`
  const config = await loadConfig({
    configType,
    outputDir,
    packageJson,
    corePresets: [require.resolve('./preview/preview-preset.js')],
    overridePresets: [require.resolve('./preview/custom-webpack-preset.js')],
    ...options,
  });

  // config.output.path = path.resolve(outputDir);

  // copy all static files
  if (staticDir) {
    staticDir.forEach(dir => {
      if (!fs.existsSync(dir)) {
        logger.error(`Error: no such directory to load static files: ${dir}`);
        process.exit(-1);
      }
      logger.info(`=> Copying static files from: ${dir}`);
      shelljs.cp('-r', `${dir}/!(index.html)`, outputDir);
    });
  }

  // compile all resources with webpack and write them to the disk.
  return new Promise((resolve, reject) => {
    const previewStartTime = process.hrtime();

    const webpackCb = (err, stats) => {
      if (err || stats.hasErrors()) {
        logger.error('Failed to build the storybook');
        // eslint-disable-next-line no-unused-expressions
        err && logger.error(err.message);
        // eslint-disable-next-line no-unused-expressions
        stats && stats.hasErrors() && stats.toJson().errors.forEach(e => logger.error(e));
        process.exitCode = 1;
        return reject(err);
      }

      const previewTotalTime = process.hrtime(previewStartTime);
      logger.trace({ message: 'preview built', time: previewTotalTime });

{'fraction_non_alphanumeric': 0.14466245427336216, 'fraction_numerical': 0.0006651147322913202, 'mean_word_length': 3.3056549749463136, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2214055', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1761, 'n_words': 452}