<commit_before>var fs             = require('fs')
  , util           = require('util')
  , path           = require('path')
  , rework         = require('rework')
  , ejs            = require('ejs')
  , colors         = require('colors')
  , express        = require('express')
  , sass_functions = require('./sass_functions');

/**
 * JSON parses the manifest.json file if it exists
 */
function parseManifest(manifest_path){
  if (!fs.existsSync(manifest_path)) return console.error(manifest_path + " does not exist");
  var manifest = fs.readFileSync(manifest_path, 'utf8');
  return JSON.parse(manifest);
}

/**
 * Loades all hdsb files in `templates_path` and concatenates them into
 * one string
 *
 * @param templates_path String path to the template directory
 */
function compileTemplates(templates_path) {
  var files = fs.readdirSync(templates_path);
  var templates = {};
  files.forEach(function(file){
    var name = file.split('.')[0];
    var template = fs.readFileSync(path.join(templates_path, file), 'utf8');
    templates[name] = template;
  });

  return templates;
}

/**
 * Creates the app.js file
 *
 * @param app_path String path to the directory containing the Zendesk app
 * @returns appjs String return app.js
 */
function readified_js(app_path, id){
  if (!app_path) return;

  if (!fs.existsSync(app_path)) return console.error(app_path + " does not exist");

  var manifest = parseManifest(path.join(app_path, 'manifest.json'))
    , name = manifest.name || 'Local App'
    , location = manifest.location
    , translations = JSON.parse(fs.readFileSync(path.join(app_path, 'translations', 'en.json'), 'utf8'))
    , framework_version = manifest.frameworkVersion
    , asset_url_prefix = 'http://localhost:4567/'
    // hardcoding for the moment
    , app_id = id || 0
    , app_class_name = util.format("app-%d", app_id)
    , default_styles = fs.readFileSync(path.join(__dirname, 'assets', 'default_styles.css'), 'utf8')
    , css_file = path.join(app_path, 'app.css');

  // TODO: prompt user for settings
  var settings = {'title': name};

  if (manifest.parameters){
    manifest.parameters.forEach(function(param){
      if (param.default) settings[param.name] = param.default;
    });
  }

  if (fs.existsSync(css_file))
    default_styles += fs.readFileSync(css_file);

  var css = rework(default_styles)
    .use(rework.function({
      "app-asset-url": function(asset){
        return sass_functions.app_asset_url(asset_url_prefix, asset);
      }
    }))
    .use(rework.prefixSelectors(util.format(".%s", app_class_name)))
    .toString();
  var templates = compileTemplates(path.join(app_path, 'templates'));
  templates.layout = util.format('<style>\n%s</style>\n%s', css, templates.layout);

  var src_templ = fs.readFileSync(path.join(__dirname, 'assets', 'src.ejs'), 'utf8')
    , appjs = ejs.render(src_templ, {
      name: name,
      source: fs.readFileSync(path.join(app_path, 'app.js'), 'utf8'),
      location: location,
      asset_url_prefix: asset_url_prefix,
      app_class_name: app_class_name,
      author: manifest.author,
      translations: translations,
      framework_version: framework_version,
      templates: templates,
      settings: settings,
      app_id: app_id
    });

    return appjs;

};

module.exports.readified_js = readified_js;


module.exports.command = function(env){
  var app_path = env.path || '.';

  process.title = 'zapt';

  var app = express();
  app.use(express.compress());

  app.get('/app.js', function(req, res){
    console.log((new Date()).toString().grey + ' - GET '.green + req.originalUrl);
    res.set('Content-Type', 'text/javascript');<commit_msg>utf8 please<commit_after>var fs             = require('fs')
  , util           = require('util')
  , path           = require('path')
  , rework         = require('rework')
  , ejs            = require('ejs')
  , colors         = require('colors')
  , express        = require('express')
  , sass_functions = require('./sass_functions');

/**
 * JSON parses the manifest.json file if it exists
 */
function parseManifest(manifest_path){
  if (!fs.existsSync(manifest_path)) return console.error(manifest_path + " does not exist");
  var manifest = fs.readFileSync(manifest_path, 'utf8');
  return JSON.parse(manifest);
}

/**
 * Loades all hdsb files in `templates_path` and concatenates them into
 * one string
 *
 * @param templates_path String path to the template directory
 */
function compileTemplates(templates_path) {
  var files = fs.readdirSync(templates_path);
  var templates = {};
  files.forEach(function(file){
    var name = file.split('.')[0];
    var template = fs.readFileSync(path.join(templates_path, file), 'utf8');
    templates[name] = template;
  });

  return templates;
}

/**
 * Creates the app.js file
 *
 * @param app_path String path to the directory containing the Zendesk app
 * @returns appjs String return app.js
 */
function readified_js(app_path, id){
  if (!app_path) return;

  if (!fs.existsSync(app_path)) return console.error(app_path + " does not exist");

  var manifest = parseManifest(path.join(app_path, 'manifest.json'))
    , name = manifest.name || 'Local App'
    , location = manifest.location
    , translations = JSON.parse(fs.readFileSync(path.join(app_path, 'translations', 'en.json'), 'utf8'))
    , framework_version = manifest.frameworkVersion
    , asset_url_prefix = 'http://localhost:4567/'
    // hardcoding for the moment
    , app_id = id || 0
    , app_class_name = util.format("app-%d", app_id)
    , default_styles = fs.readFileSync(path.join(__dirname, 'assets', 'default_styles.css'), 'utf8')
    , css_file = path.join(app_path, 'app.css');

  // TODO: prompt user for settings
  var settings = {'title': name};

  if (manifest.parameters){
    manifest.parameters.forEach(function(param){
      if (param.default) settings[param.name] = param.default;
    });
  }

  if (fs.existsSync(css_file))
    default_styles += fs.readFileSync(css_file);

  var css = rework(default_styles)
    .use(rework.function({
      "app-asset-url": function(asset){
        return sass_functions.app_asset_url(asset_url_prefix, asset);
      }
    }))
    .use(rework.prefixSelectors(util.format(".%s", app_class_name)))
    .toString();
  var templates = compileTemplates(path.join(app_path, 'templates'));
  templates.layout = util.format('<style>\n%s</style>\n%s', css, templates.layout);

  var src_templ = fs.readFileSync(path.join(__dirname, 'assets', 'src.ejs'), 'utf8')
    , appjs = ejs.render(src_templ, {
      name: name,
      source: fs.readFileSync(path.join(app_path, 'app.js'), 'utf8'),
      location: location,
      asset_url_prefix: asset_url_prefix,
      app_class_name: app_class_name,
      author: manifest.author,
      translations: translations,
      framework_version: framework_version,
      templates: templates,
      settings: settings,
      app_id: app_id
    });

    return appjs;

};

module.exports.readified_js = readified_js;


module.exports.command = function(env){
  var app_path = env.path || '.';

  process.title = 'zapt';

  var app = express();
  app.use(express.compress());

  app.get('/app.js', function(req, res){
    console.log((new Date()).toString().grey + ' - GET '.green + req.originalUrl);
    res.set('Content-Type', 'text/javascript; charset=utf8');
{'fraction_non_alphanumeric': 0.1466411796832332, 'fraction_numerical': 0.00354997269251775, 'mean_word_length': 3.6040226272784412, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '79093', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2303, 'n_words': 495}