<commit_before>'use strict'

const path = require('path')
const execa = require('execa')
const fs = require('fs-extra')
const globby = require('globby')
const merge = require('merge-options')
const { fromRoot, fromAegir, hasFile, readJson } = require('../utils')
const hasConfig = hasFile('tsconfig.json')
let userConfig = null

module.exports = async (argv) => {
  const forwardOptions = argv['--'] ? argv['--'] : []
  const extraInclude = (argv.include && argv.include.length > 0) ? await globby(argv.include) : []

  if (argv.preset === 'config') {
    const extendsConfig = `{
    "extends": "./${path.relative(
        process.cwd(),
        require.resolve('aegir/src/config/tsconfig.aegir.json')
    )}",
    "compilerOptions": {
        "outDir": "dist"
    },
    "include": [
      "test", // remove this line if you don't want to type-check tests
      "src"
    ]
}`
    // eslint-disable-next-line no-console
    console.log(extendsConfig)

    return
  }

  if (!hasConfig) {
    throw new Error(
      'TS config not found. Try running `aegir ts --preset config > tsconfig.json`'
    )
  }
  userConfig = readJson(fromRoot('tsconfig.json'))

  if (argv.preset === 'check') {
    return check(forwardOptions, extraInclude)
  }

  if (argv.preset === 'types') {
    return types(forwardOptions, extraInclude)
  }

  if (argv.preset === 'docs') {
    return docs(forwardOptions, extraInclude)
  }

  if (!argv.preset) {
    return execa('tsc', ['--build', ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })
  }
}

const check = async (forwardOptions, extraInclude) => {
  const configPath = fromRoot('tsconfig-check.aegir.json')
  try {
    fs.writeJsonSync(
      configPath,
      merge.apply({ concatArrays: true }, [
        userConfig,
        {
          compilerOptions: {
            noEmit: true,
            emitDeclarationOnly: false
          },
          include: extraInclude
        }
      ])
    )
    await execa('tsc', ['--build', configPath, ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })
  } finally {
    fs.removeSync(configPath)
  }
}

const types = async (forwardOptions, extraInclude) => {
  const configPath = fromRoot('tsconfig-types.aegir.json')
  try {
    fs.writeJsonSync(
      configPath,
      merge(userConfig, {
        compilerOptions: {
          noEmit: false,
          emitDeclarationOnly: true
        },
        include: ['src', 'package.json', ...extraInclude]
      })
    )
    await execa('tsc', ['--build', configPath, ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })
  } finally {
    fs.removeSync(configPath)
  }
}

const docs = async (forwardOptions, extraInclude) => {
  const configPath = fromRoot('tsconfig-docs.aegir.json')
  try {
    fs.writeJsonSync(
      configPath,
      merge(userConfig, {
        compilerOptions: {
          noEmit: false,
          emitDeclarationOnly: true,
          outDir: 'types'
        },
        include: ['src/**/*', 'package.json', ...extraInclude]
      })
    )

    // run tsc
    await execa('tsc', ['-b', configPath, ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })

    // run typedoc<commit_msg>fix: remove tsbuildinfo files when building types<commit_after>'use strict'

const path = require('path')
const execa = require('execa')
const fs = require('fs-extra')
const globby = require('globby')
const merge = require('merge-options')
const { fromRoot, fromAegir, hasFile, readJson } = require('../utils')
const hasConfig = hasFile('tsconfig.json')
let userConfig = null

module.exports = async (argv) => {
  const forwardOptions = argv['--'] ? argv['--'] : []
  const extraInclude = (argv.include && argv.include.length > 0) ? await globby(argv.include) : []

  if (argv.preset === 'config') {
    const extendsConfig = `{
    "extends": "./${path.relative(
        process.cwd(),
        require.resolve('aegir/src/config/tsconfig.aegir.json')
    )}",
    "compilerOptions": {
        "outDir": "dist"
    },
    "include": [
      "test", // remove this line if you don't want to type-check tests
      "src"
    ]
}`
    // eslint-disable-next-line no-console
    console.log(extendsConfig)

    return
  }

  if (!hasConfig) {
    throw new Error(
      'TS config not found. Try running `aegir ts --preset config > tsconfig.json`'
    )
  }
  userConfig = readJson(fromRoot('tsconfig.json'))

  if (argv.preset === 'check') {
    return check(forwardOptions, extraInclude)
  }

  if (argv.preset === 'types') {
    return types(forwardOptions, extraInclude)
  }

  if (argv.preset === 'docs') {
    return docs(forwardOptions, extraInclude)
  }

  if (!argv.preset) {
    return execa('tsc', ['--build', ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })
  }
}

const check = async (forwardOptions, extraInclude) => {
  const configPath = fromRoot('tsconfig-check.aegir.json')
  try {
    fs.writeJsonSync(
      configPath,
      merge.apply({ concatArrays: true }, [
        userConfig,
        {
          compilerOptions: {
            noEmit: true,
            emitDeclarationOnly: false
          },
          include: extraInclude
        }
      ])
    )
    await execa('tsc', ['--build', configPath, ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })
  } finally {
    fs.removeSync(configPath)
  }
}

const types = async (forwardOptions, extraInclude) => {
  const configPath = fromRoot('tsconfig-types.aegir.json')
  try {
    fs.writeJsonSync(
      configPath,
      merge(userConfig, {
        compilerOptions: {
          noEmit: false,
          emitDeclarationOnly: true
        },
        include: ['src', 'package.json', ...extraInclude]
      })
    )
    await execa('tsc', ['--build', configPath, ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })
  } finally {
    fs.removeSync(configPath)
    fs.removeSync(fromRoot('dist', 'tsconfig-types.aegir.tsbuildinfo'))
  }
}

const docs = async (forwardOptions, extraInclude) => {
  const configPath = fromRoot('tsconfig-docs.aegir.json')
  try {
    fs.writeJsonSync(
      configPath,
      merge(userConfig, {
        compilerOptions: {
          noEmit: false,
          emitDeclarationOnly: true,
          outDir: 'types'
        },
        include: ['src/**/*', 'package.json', ...extraInclude]
      })
    )

    // run tsc
    await execa('tsc', ['-b', configPath, ...forwardOptions], {
      localDir: path.join(__dirname, '../..'),
      preferLocal: true,
      stdio: 'inherit'
    })

    // run typedoc
{'fraction_non_alphanumeric': 0.15853306381749926, 'fraction_numerical': 0.00028876696505919725, 'mean_word_length': 2.465232616308154, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 15, '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': '6091291', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2177, 'n_words': 461}