<commit_before>import chalk from 'chalk';
import execa from 'execa';
import { join } from 'path';
import npa from 'npm-package-arg';
import mkdirp from 'mkdirp-promise';
import { funCacheDir } from '@zeit/fun';
import { readJSON, writeJSON, remove } from 'fs-extra';
import cacheDirectory from 'cache-or-tmp-directory';
import wait from '../../../util/output/wait';
import { Output } from '../../../util/output';
import { devDependencies as nowCliDeps } from '../../../../package.json';
import { Builder, BuilderWithPackage } from './types';
import {
  NoBuilderCacheError,
  BuilderCacheCleanError
} from '../../../util/errors-ts';

import * as staticBuilder from './static-builder';

const localBuilders: { [key: string]: BuilderWithPackage } = {
  '@now/static': {
    builder: Object.freeze(staticBuilder),
    package: { version: '' }
  }
};

export const cacheDirPromise = prepareCacheDir();
export const builderDirPromise = prepareBuilderDir();

/**
 * Prepare cache directory for installing now-builders
 */
export async function prepareCacheDir() {
  const designated = cacheDirectory('co.zeit.now');

  if (!designated) {
    throw new NoBuilderCacheError();
  }

  const cacheDir = join(designated, 'dev');
  await mkdirp(cacheDir);
  return cacheDir;
}

export async function prepareBuilderDir() {
  const builderDir = join(await cacheDirPromise, 'builders');
  await mkdirp(builderDir);

  // Create an empty private `package.json`,
  // but only if one does not already exist
  try {
    const buildersPkg = join(builderDir, 'package.json');
    await writeJSON(buildersPkg, { private: true }, { flag: 'wx' });
  } catch (err) {
    if (err.code !== 'EEXIST') {
      throw err;
    }
  }
<commit_msg>Support env NOW_BUILDER_CACHE_DIR for debugging builders (#2212)<commit_after>import chalk from 'chalk';
import execa from 'execa';
import { join, resolve } from 'path';
import npa from 'npm-package-arg';
import mkdirp from 'mkdirp-promise';
import { funCacheDir } from '@zeit/fun';
import { readJSON, writeJSON, remove } from 'fs-extra';
import cacheDirectory from 'cache-or-tmp-directory';
import wait from '../../../util/output/wait';
import { Output } from '../../../util/output';
import { devDependencies as nowCliDeps } from '../../../../package.json';
import { BuilderWithPackage } from './types';
import {
  NoBuilderCacheError,
  BuilderCacheCleanError
} from '../../../util/errors-ts';

import * as staticBuilder from './static-builder';

const localBuilders: { [key: string]: BuilderWithPackage } = {
  '@now/static': {
    builder: Object.freeze(staticBuilder),
    package: { version: '' }
  }
};

export const cacheDirPromise = prepareCacheDir();
export const builderDirPromise = prepareBuilderDir();

/**
 * Prepare cache directory for installing now-builders
 */
export async function prepareCacheDir() {
  const { NOW_BUILDER_CACHE_DIR } = process.env;
  const designated = NOW_BUILDER_CACHE_DIR
    ? resolve(NOW_BUILDER_CACHE_DIR)
    : cacheDirectory('co.zeit.now');

  if (!designated) {
    throw new NoBuilderCacheError();
  }

  const cacheDir = join(designated, 'dev');
  await mkdirp(cacheDir);
  return cacheDir;
}

export async function prepareBuilderDir() {
  const builderDir = join(await cacheDirPromise, 'builders');
  await mkdirp(builderDir);

  // Create an empty private `package.json`,
  // but only if one does not already exist
  try {
    const buildersPkg = join(builderDir, 'package.json');
    await writeJSON(buildersPkg, { private: true }, { flag: 'wx' });
  } catch (err) {
    if (err.code !== 'EEXIST') {
      throw err;
    }
  }

{'fraction_non_alphanumeric': 0.1463143254520167, 'fraction_numerical': 0.0011126564673157164, 'mean_word_length': 3.926027397260274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2349536', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1086, 'n_words': 309}