<commit_before>#!/usr/bin/env node

import * as path from 'path';
import * as find from 'find-up';
import * as ts from 'typescript';
import * as commander from 'commander';
import * as util from './util';
import * as program from './program';
import { ProgramError } from '../errors';
import { Options, defaultOptions } from '../options';

const pkgFile = find.sync('package.json', {cwd: __dirname});
const pkg = require(pkgFile);
const options: Options = Object.assign({}, defaultOptions);
let compilerOptions: string = '{}';

function defaultAction() {
  const files: string[] = commander.args
    .filter(arg => typeof arg === 'string');

  if (files.length === 0) {
    throw new ProgramError('No entry file(s) passed to transform.');
  }

  const opts = ts.convertCompilerOptionsFromJson(JSON.parse(compilerOptions), '.');

  options.log = false;
  options.compilerOptions = opts.options;

  if (opts.errors.length > 0) {
    program.status.diagnostics(util.formatDiagnostics(opts.errors));
    program.status.error();
    return;
  }

  program.start(files, options, pkg.version);
}

function setNoAnnotate() {
  options.noAnnotate = true;
}

function setCompilerOptions(opts: string) {
  compilerOptions = opts;
}

function setDeclarationFileName(fileName: string) {
  const ext = path.extname(fileName);

  if (ext) {
    fileName = fileName.slice(0, ext.length * -1);
  }

  if (path.basename(fileName) !== fileName) {
    throw new ProgramError('Declaration file name must not be a path.');
  }

  options.declarationFileName = fileName;
}

function setExcludeDeclarationFile() {
  options.excludeDeclarationFile = true;
}

function setExcludeLib() {
  options.excludeLib = true;
}

function setForce() {
  options.force = true;
}

function setKeepTemp() {
  options.keepTemp = true;
}

function setLibIdentifier(identifier: string) {
  options.libIdentifier = identifier;
}

function setLibNamespace(namespace: string) {
  options.libNamespace = namespace;
}

function setDeclarationPrefix(prefix: string) {
  options.declarationPrefix = prefix;
}

function setStackTraceOutput(limit: number) {
  options.stackTraceOutput = limit;
}

function setTempFolderName(name: string) {
  options.tempFolderName = name;
}

function setModuleAlias() {
  options.moduleAlias = true;
}

commander
  .version(pkg.version, '-v, --version')
  .description(`---------  ts-runtime  ---------
  Turns TypeScript type assertions
  into runtime type checks for you
  --------------------------------`)
  .usage('<file...> [options]')
  .option('-a, --noAnnotate', 'do not annotate classes and functions', setNoAnnotate)
  .option('-c, --compilerOptions <compilerOptions>', 'set TypeScript compiler options. defaults to "{}"', setCompilerOptions)
  .option('-d, --declarationFileName <fileName>', 'set file name for global declarations. defaults to "tsr-declarations"', setDeclarationFileName)<commit_msg>Introduced option to specify a tsconfig file to use.<commit_after>#!/usr/bin/env node

import * as path from 'path';
import * as find from 'find-up';
import * as ts from 'typescript';
import * as commander from 'commander';
import * as util from './util';
import * as program from './program';
import { ProgramError } from '../errors';
import { Options, defaultOptions } from '../options';

const pkgFile = find.sync('package.json', {cwd: __dirname});
const pkg = require(pkgFile);
const options: Options = Object.assign({}, defaultOptions);
let compilerOptions: string = '{}';
let tsConfigPath: string;

function defaultAction() {
  const files: string[] = commander.args
    .filter(arg => typeof arg === 'string');

  if (files.length === 0) {
    throw new ProgramError('No entry file(s) passed to transform.');
  }

  compilerOptions = JSON.parse(compilerOptions);

  if (tsConfigPath && ts.sys.fileExists(tsConfigPath)) {
    const tsConfig = require(path.resolve(tsConfigPath));

    if (tsConfig.hasOwnProperty('compilerOptions')) {
      compilerOptions = tsConfig.compilerOptions;
    }
  }

  const opts = ts.convertCompilerOptionsFromJson(compilerOptions, '.');

  options.log = false;
  options.compilerOptions = opts.options;

  if (opts.errors.length > 0) {
    program.status.diagnostics(util.formatDiagnostics(opts.errors));
    program.status.error();
    return;
  }

  program.start(files, options, pkg.version);
}

function useTsConfig(path: string) {
  tsConfigPath = path;
}

function setNoAnnotate() {
  options.noAnnotate = true;
}

function setCompilerOptions(opts: string) {
  compilerOptions = opts;
}

function setDeclarationFileName(fileName: string) {
  const ext = path.extname(fileName);

  if (ext) {
    fileName = fileName.slice(0, ext.length * -1);
  }

  if (path.basename(fileName) !== fileName) {
    throw new ProgramError('Declaration file name must not be a path.');
  }

  options.declarationFileName = fileName;
}

function setExcludeDeclarationFile() {
  options.excludeDeclarationFile = true;
}

function setExcludeLib() {
  options.excludeLib = true;
}

function setForce() {
  options.force = true;
}

function setKeepTemp() {
  options.keepTemp = true;
}

function setLibIdentifier(identifier: string) {
  options.libIdentifier = identifier;
}

function setLibNamespace(namespace: string) {
  options.libNamespace = namespace;
}

function setDeclarationPrefix(prefix: string) {
  options.declarationPrefix = prefix;
}

function setStackTraceOutput(limit: number) {
  options.stackTraceOutput = limit;
}

function setTempFolderName(name: string) {
  options.tempFolderName = name;
}

function setModuleAlias() {
  options.moduleAlias = true;
}

commander
  .version(pkg.version, '-v, --version')
  .description(`---------  ts-runtime  ---------
  Turns TypeScript type assertions
  into runtime type checks for you
  --------------------------------`)
  .usage('<file...> [options]')
  .option('-a, --noAnnotate', 'do not annotate classes and functions', setNoAnnotate)
  .option('-c, --compilerOptions <compilerOptions>', 'set TypeScript compiler options. defaults to "{}"', setCompilerOptions)
  .option('-C, --tsConfig <path>', 'use the compiler options of the given tsconfig.json', useTsConfig)
  .option('-d, --declarationFileName <fileName>', 'set file name for global declarations. defaults to "tsr-declarations"', setDeclarationFileName)
{'fraction_non_alphanumeric': 0.14646946564885496, 'fraction_numerical': 0.001272264631043257, 'mean_word_length': 4.159146841673503, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 14, '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': '1599725', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1844, 'n_words': 489}