<commit_before>'use strict';

var
  fs = require('fs'),

  extend     = require('extend'),
  defaults   = require('json-schema-defaults'),
  chalk      = require('chalk'),
  chokidar   = require('chokidar'),
  open       = require('open'),
  equal      = require('deep-equal'),
  express    = require('express'),
  cors       = require('cors'),
  bodyParser = require('body-parser'),

  // TODO: Remove gulp dependencies (ex. include and use `less` instead of `gulp-less`, remove `gulp`, etc)
  gulp       = require('gulp'),
  rename     = require('gulp-rename'),
  less       = require('gulp-less'),
  handlebars = require('gulp-compile-handlebars'),
  connect    = require('gulp-connect'),

  helpers = require('../lib/handlebars-helpers.js');

var
  cwd,
  readCustomerConfig,
  html,
  css,
  js,
  hbs,
  server,
  apiServer,
  watch,
  openUrl,
  alphaMessage,
  command;

cwd = process.cwd();

readCustomerConfig = function () {
  var path = './customer.json';

  if (!fs.existsSync(path)) {
    console.log(chalk.red('customer.json not found in current directory!'));
    process.exit(1);
  }

  return JSON.parse(fs.readFileSync(path, 'utf-8'));
};

html = function () {
  gulp.src('*.html')
    .pipe(connect.reload());
};

css = function () {
  gulp.src('assets/css/widget.less')
    .pipe(
      less()
    ).on('error', function (err) {
      console.log(chalk.red(err.message));
      this.emit('end');
    })
    .pipe(gulp.dest('assets/css'))
    .pipe(connect.reload());
};

js = function (path) {
  gulp.src(path)
    .pipe(connect.reload());
};

hbs = function () {
  var
    messagesPath,
    templateData,
    messagesSchema,
    messages,
    options;

  messagesPath = './variables.json';

  templateData = readCustomerConfig();

  if (fs.existsSync(messagesPath)) {
    messagesSchema = JSON.parse(fs.readFileSync(messagesPath));
    messages = {
      variablesSchema: messagesSchema,
      variables: defaults(messagesSchema)
    };

    templateData = extend(templateData, messages);
  }

  options = {
    helpers: helpers,
    batch: ['templates']
  };

  gulp.src('templates/widget.hbs')
    .pipe(handlebars(templateData, options))
    .pipe(rename('index.html'))
    .pipe(gulp.dest(''));
};

watch = function () {
  chokidar.watch('*.html').on('change', html);
  chokidar.watch('assets/css/**/*.less').on('change', css);
  chokidar.watch('assets/javascript/**/*.js').on('change', js);
  chokidar.watch(['*.json', 'templates/**/*.hbs']).on('change', hbs);
};

server = function (livereload) {
  connect.server({
    root: process.cwd(),
    livereload: livereload,
    host: process.env.IP || '0.0.0.0',
    port: process.env.PORT || '8080'
  });
};

apiServer = function () {
  var configPath = './api.json';

  if (!fs.existsSync(configPath)) { return false; }

  var
    app            = express(),
    config         = JSON.parse(fs.readFileSync(configPath)),
    customerConfig = readCustomerConfig(),
    endpoint       = {};

  app.use(cors());
  app.use(bodyParser.json());

  if (customerConfig.apiEndpoint) {
    var hostAndPort = customerConfig.apiEndpoint.match(/https?:\/\/([^:/]+):([^/]+)/);
    endpoint.host = hostAndPort[1];
    endpoint.port = hostAndPort[2];
  }

  app.post('/', function (req, res) {
    var response;

    config.exchanges.forEach(function (exchange) {
      if (equal(req.body, exchange.request)) {
        response = exchange.response;
      }
    });

    res.send(response || config.defaultResponse);
  });

  // TODO: Add live reloading
  var server = app.listen(<commit_msg>Set status code for API responses if defined<commit_after>'use strict';

var
  fs = require('fs'),

  extend     = require('extend'),
  defaults   = require('json-schema-defaults'),
  chalk      = require('chalk'),
  chokidar   = require('chokidar'),
  open       = require('open'),
  equal      = require('deep-equal'),
  express    = require('express'),
  cors       = require('cors'),
  bodyParser = require('body-parser'),

  // TODO: Remove gulp dependencies (ex. include and use `less` instead of `gulp-less`, remove `gulp`, etc)
  gulp       = require('gulp'),
  rename     = require('gulp-rename'),
  less       = require('gulp-less'),
  handlebars = require('gulp-compile-handlebars'),
  connect    = require('gulp-connect'),

  helpers = require('../lib/handlebars-helpers.js');

var
  cwd,
  readCustomerConfig,
  html,
  css,
  js,
  hbs,
  server,
  apiServer,
  watch,
  openUrl,
  alphaMessage,
  command;

cwd = process.cwd();

readCustomerConfig = function () {
  var path = './customer.json';

  if (!fs.existsSync(path)) {
    console.log(chalk.red('customer.json not found in current directory!'));
    process.exit(1);
  }

  return JSON.parse(fs.readFileSync(path, 'utf-8'));
};

html = function () {
  gulp.src('*.html')
    .pipe(connect.reload());
};

css = function () {
  gulp.src('assets/css/widget.less')
    .pipe(
      less()
    ).on('error', function (err) {
      console.log(chalk.red(err.message));
      this.emit('end');
    })
    .pipe(gulp.dest('assets/css'))
    .pipe(connect.reload());
};

js = function (path) {
  gulp.src(path)
    .pipe(connect.reload());
};

hbs = function () {
  var
    messagesPath,
    templateData,
    messagesSchema,
    messages,
    options;

  messagesPath = './variables.json';

  templateData = readCustomerConfig();

  if (fs.existsSync(messagesPath)) {
    messagesSchema = JSON.parse(fs.readFileSync(messagesPath));
    messages = {
      variablesSchema: messagesSchema,
      variables: defaults(messagesSchema)
    };

    templateData = extend(templateData, messages);
  }

  options = {
    helpers: helpers,
    batch: ['templates']
  };

  gulp.src('templates/widget.hbs')
    .pipe(handlebars(templateData, options))
    .pipe(rename('index.html'))
    .pipe(gulp.dest(''));
};

watch = function () {
  chokidar.watch('*.html').on('change', html);
  chokidar.watch('assets/css/**/*.less').on('change', css);
  chokidar.watch('assets/javascript/**/*.js').on('change', js);
  chokidar.watch(['*.json', 'templates/**/*.hbs']).on('change', hbs);
};

server = function (livereload) {
  connect.server({
    root: process.cwd(),
    livereload: livereload,
    host: process.env.IP || '0.0.0.0',
    port: process.env.PORT || '8080'
  });
};

apiServer = function () {
  var configPath = './api.json';

  if (!fs.existsSync(configPath)) { return false; }

  var
    app            = express(),
    config         = JSON.parse(fs.readFileSync(configPath)),
    customerConfig = readCustomerConfig(),
    endpoint       = {};

  app.use(cors());
  app.use(bodyParser.json());

  if (customerConfig.apiEndpoint) {
    var hostAndPort = customerConfig.apiEndpoint.match(/https?:\/\/([^:/]+):([^/]+)/);
    endpoint.host = hostAndPort[1];
    endpoint.port = hostAndPort[2];
  }

  app.post('/', function (req, res) {
    var response;

    config.exchanges.forEach(function (exchange) {
      if (equal(req.body, exchange.request)) {
        response = exchange.response;

        if (exchange.status) {
          res.status(exchange.status);
        }
      }
    });

    res.send(response || config.defaultResponse);
  });

  // TODO: Add live reloading
  var server = app.listen(
{'fraction_non_alphanumeric': 0.18136439267886856, 'fraction_numerical': 0.0033277870216306157, 'mean_word_length': 3.0774448841153195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '63195', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2374, 'n_words': 433}