<commit_before>var path = require('path');
var clap = require.main.require('clap');

var targets = ['output-graph', 'fs']; // last is default
var handlers = {
  target: function(target){
    target = String(target).toLowerCase();

    if (targets.indexOf(target) == -1)
      return 'fs';

    return target;
  }
};

function resolveCwd(value){
  return path.resolve(process.env.PWD || process.cwd(), value);
}

function applyConfig(command, config, configPath){
  command._configPath = configPath;

  if (config)
  {
    for (var name in config)
    {
      if (command.hasOption(name))
      {
        if (name == 'file' || name == 'output' || name == 'base')
          config[name] = path.resolve(configPath, config[name]);

        command.setOption(name, config[name]);
      }
      else
      {
        if (name == 'preprocess' || name == 'extFileTypes')
          command.values[name] = config[name];
        else
          console.warn('Unknown option `' + name + '` in config (ignored)');
      }
    }

    // if no base in config, set config path as base
    if ('base' in config == false)
      command.setOption('base', configPath);
  }

  return command;
}

function normOptions(options){

  function addPreprocessors(type, handlerList){
    if (!Array.isArray(handlerList))
    {
      if (typeof handlerList == 'string')
        handlerList = [handlerList];
      else
        handlerList = [];
    }

    handlerList = handlerList.map(function(fn){
      return path.normalize(path.resolve(module.exports._configPath || '.', fn));
    });

    var processors = options.preprocess[type];
    if (processors)
      processors.push.apply(processors, handlerList);
    else
      options.preprocess[type] = handlerList;
  }

  // locations
  options.file = path.normalize(path.resolve(options.file));
  options.base = path.normalize((options.base ? path.resolve(options.base) : path.dirname(options.file)) + '/');
  options.output = path.normalize(path.resolve(options.output) + '/');

  // process preprocessing handlers
  if (!options.preprocess)
    options.preprocess = {};

  if (options.pack)
  {
    this.jsBuildMode = true;
    this.jsCutDev = true;
    this.jsPack = true;
    this.cssPack = true;
  }

  var configExtFileTypes = options.extFileTypes;
  var extFileTypes = {};
  var configPreprocess = options.preprocess;

  options.extFileTypes = extFileTypes;
  options.preprocess = {};

  for (var type in configExtFileTypes)
  {
    var cfg = configExtFileTypes[type];
    extFileTypes[type] = cfg.type;
    if (cfg.preprocess)
      addPreprocessors(type, cfg.preprocess);
  }

  for (var type in configPreprocess)
    addPreprocessors(type, configPreprocess[type]);

  /*if (options.jsCutDev)
  {
    if (!options.preprocess['script'])
      options.preprocess['script'] = [];
    options.preprocess['script'].push('./misc/preprocess/js-cut-dev.js');
  }*/

  return options;
}

module.exports = clap.create('build', '[file]')
  .description('Build an app')

  .init(function(){
    var config = this.context.config = this.root.getConfig(this.values);
    if (config)
      applyConfig(this, config.data['build'] || {}, config.path);
  })
  .args(function(filename){
    this.setOption('file', filename);
  })

  .option('-b, --base <path>',
    'Base input path for path resolving (current path by default)',
    resolveCwd
  )
  .option('-f, --file <filename>',
    'File name of file to build, resolve from base path (index.html by default)',
    resolveCwd,
    'index.html'
  )
  .option('-o, --output <path>',
    'Path for output, resolve from file path (build by default)',
    resolveCwd,
    'build'
  )
  .option('--verbose', 'makes output more verbose')
  .option('--no-color', 'suppress color output')
  .option('--warnings', 'list warning messages in summary')

  .option('--same-filenames', 'give script and style files name like basename of index file')
  .option('-t, --target <target>',
    'Define what build should produce. Target could be: ' + targets.join(', ') + ' (file system by default).',
    handlers.target,
    targets[targets.length - 1]
  )

  // bulk flags
  .shortcut('-p, --pack',
    'Pack sources. It equals to: --js-build-mode --js-cut-dev --js-pack --css-pack',
    function(value){
      return {
        jsBuildMode: value,
        jsCutDev: value,
        jsPack: value,
        cssPack: value
      };
    }
  )
  .shortcut('--no-single-file',
    'Avoid merge sources into one file. It equals to: --js-no-single-file --css-no-single-file',
    function(value){
      return {
        jsSingleFile: value,
        cssSingleFile: value
      };
    }
  )

  // javascript<commit_msg>small fix for command description<commit_after>var path = require('path');
var clap = require.main.require('clap');

var targets = ['output-graph', 'fs']; // last is default
var handlers = {
  target: function(target){
    target = String(target).toLowerCase();

    if (targets.indexOf(target) == -1)
      return 'fs';

    return target;
  }
};

function resolveCwd(value){
  return path.resolve(process.env.PWD || process.cwd(), value);
}

function applyConfig(command, config, configPath){
  command._configPath = configPath;

  if (config)
  {
    for (var name in config)
    {
      if (command.hasOption(name))
      {
        if (name == 'file' || name == 'output' || name == 'base')
          config[name] = path.resolve(configPath, config[name]);

        command.setOption(name, config[name]);
      }
      else
      {
        if (name == 'preprocess' || name == 'extFileTypes')
          command.values[name] = config[name];
        else
          console.warn('Unknown option `' + name + '` in config (ignored)');
      }
    }

    // if no base in config, set config path as base
    if ('base' in config == false)
      command.setOption('base', configPath);
  }

  return command;
}

function normOptions(options){

  function addPreprocessors(type, handlerList){
    if (!Array.isArray(handlerList))
    {
      if (typeof handlerList == 'string')
        handlerList = [handlerList];
      else
        handlerList = [];
    }

    handlerList = handlerList.map(function(fn){
      return path.normalize(path.resolve(module.exports._configPath || '.', fn));
    });

    var processors = options.preprocess[type];
    if (processors)
      processors.push.apply(processors, handlerList);
    else
      options.preprocess[type] = handlerList;
  }

  // locations
  options.file = path.normalize(path.resolve(options.file));
  options.base = path.normalize((options.base ? path.resolve(options.base) : path.dirname(options.file)) + '/');
  options.output = path.normalize(path.resolve(options.output) + '/');

  // process preprocessing handlers
  if (!options.preprocess)
    options.preprocess = {};

  if (options.pack)
  {
    this.jsBuildMode = true;
    this.jsCutDev = true;
    this.jsPack = true;
    this.cssPack = true;
  }

  var configExtFileTypes = options.extFileTypes;
  var extFileTypes = {};
  var configPreprocess = options.preprocess;

  options.extFileTypes = extFileTypes;
  options.preprocess = {};

  for (var type in configExtFileTypes)
  {
    var cfg = configExtFileTypes[type];
    extFileTypes[type] = cfg.type;
    if (cfg.preprocess)
      addPreprocessors(type, cfg.preprocess);
  }

  for (var type in configPreprocess)
    addPreprocessors(type, configPreprocess[type]);

  /*if (options.jsCutDev)
  {
    if (!options.preprocess['script'])
      options.preprocess['script'] = [];
    options.preprocess['script'].push('./misc/preprocess/js-cut-dev.js');
  }*/

  return options;
}

module.exports = clap.create('build', '[file]')
  .description('Build an app')

  .init(function(){
    var config = this.context.config = this.root.getConfig(this.values);
    if (config)
      applyConfig(this, config.data['build'] || {}, config.path);
  })
  .args(function(filename){
    this.setOption('file', filename);
  })

  .option('-b, --base <path>',
    'Base input path for path resolving (current path by default)',
    resolveCwd
  )
  .option('-f, --file <filename>',
    'File name of file to build, resolve from base path (index.html by default)',
    resolveCwd,
    'index.html'
  )
  .option('-o, --output <path>',
    'Path for output, resolve from file path (build by default)',
    resolveCwd,
    'build'
  )
  .option('--verbose', 'Makes output more verbose')
  .option('--no-color', 'Suppress color output')
  .option('--warnings', 'List warning messages in summary')

  .option('--same-filenames', 'Give script and style files name like basename of index file')
  .option('-t, --target <target>',
    'Define what build should produce. Target could be: ' + targets.join(', ') + ' (file system by default).',
    handlers.target,
    targets[targets.length - 1]
  )

  // bulk flags
  .shortcut('-p, --pack',
    'Pack sources. It equals to: --js-build-mode --js-cut-dev --js-pack --css-pack',
    function(value){
      return {
        jsBuildMode: value,
        jsCutDev: value,
        jsPack: value,
        cssPack: value
      };
    }
  )
  .shortcut('--no-single-file',
    'Avoid merge sources into one file. It equals to: --js-no-single-file --css-no-single-file',
    function(value){
      return {
        jsSingleFile: value,
        cssSingleFile: value
      };
    }
  )

  // javascript
{'fraction_non_alphanumeric': 0.15265866209262435, 'fraction_numerical': 0.0004288164665523156, 'mean_word_length': 2.888703626511046, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3352702', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2900, 'n_words': 687}