<commit_before>/*
 * grunt-jasper
 * https://github.com/jasperjs/grunt-jasper
 *
 * Copyright (c) 2015 bukharin
 * Licensed under the MIT license.
 */

'use strict';
var utils = require('./jasper-utils.js');
var templates = require('./jasper-templates.js');

var getRegistrationScript = function (def) {
  var result = '';
  var type = def.type.toUpperCase();

  delete def.type;
  delete def.__path;

  switch (type.toUpperCase()) {
    case 'COMPONENT':
      result = templates.componentRegistration(def);
      break;
    case 'DECORATOR':
      result = templates.decoratorRegistration(def);
      break;
    case 'SERVICE':
      result = templates.serviceRegistration(def);
      break;
    case 'FILTER':
      result = templates.filterRegistration(def);
      break;
    case 'DIRECTIVE':
      result = templates.directiveRegistration(def);
      break;
    case 'TEMPLATE':
      result = templates.templateRegistration(def.url, def.content);
      break;
    case 'PAGE':
      var pageTemplateUrl = utils.getPageTemplateUrl(def);
      var tagName = utils.shakeCase(def.name);
      var pageTemplate = '<' + tagName + '></' + tagName + '>';
      // register page template and the page component
      result = templates.templateRegistration(pageTemplateUrl, pageTemplate);
      result += templates.componentRegistration(def);
      break;
    default:
      grunt.util.error('Unknown definition type "' + def + '" in "' + JSON.stringify(def) + '"');
      break;
  }
  return result;
};

var options, // jasper task options
  processedBootstrapScripts = [], // scripts defined in the task options
  baseScripts = [], //scripts which must included in _base.js
  routesConfigPath, // path to the routes configuration
  areasConfigPath, // path to areas config file
  valuesConfigPath, // path to values config
  pages = [], // found app pages
  areas = [], // set of found areas
  target; // current target of the task

module.exports = function (grunt) {
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-typescript');

  grunt.registerMultiTask('jasper', 'Build jasper application', function () {
    // Merge task-specific and/or target-specific options with these defaults.
    options = this.options({
      singlePage: 'index.html',
      appPath: 'app',
      package: false,
      packageOutput: 'dist',

      defaultRoutePath: '/',
      bootstrapScripts: [],

      baseCss: [],
      /**
       * Include MD5 hash of file content after package
       */
      fileVersion: false
    });

    // pipeline for building jasper application:
    var pipeline = [
    /**
     *  At first we need to find all areas definitions (_area.json) in the project
     */
      'jasperCollectAreas',
    /**
     * Then on each found area, collect all component definitions (_definition.json)
     */
      'jasperCollectDefs',
    /**
     * Then we find all area scripts and build area client-side initialization (_init.js),
     * which tells client what kind of components places in each area
     */
      'jasperAreaInit',
    /**
     * Now we need to concatenate and minify all areas scripts, templates
     * Executes only for package process (options.package == true)
     */
      'jasperPackageAreas',
    /**
     * Now create a client-side areas configuration script (_areas.js) that tell client which areas exists in application,
     * dependencies between areas, and their script files
     */
      'jasperAreasConfig',
    /**
     * Now create a client-side values configuration script (_values.js) that tell client which config values exists in application
     * Executes only if options.values set to true
     */
      'jasperValuesConfig',
    /**
     * Now create a client-side routes configuration script (_routes.js) that tell client which routes exists in application
     */
      'jasperRoutesConfig',
    /**
     * After areas, routes and values config was built we need to create bootstrap script (_base.js) that will loaded at
     * first time and bootstrap the application.
     * Bootstrap script includes:
     * - Scripts that marks as bootstrap in jasper.json file: angular.js, jasper.js, any custom user's scripts.
     * - Areas, routes and values configuration scripts that was built in previous steps
     * - Areas scripts, which marked as 'bootstrap' (in _area.json)
     */
      'jasperPackageBase',
    /**
     * Modify single page, include reference to bootstrap scripts and styles.
     */
      'jasperPatchIndex'
    ];

    target = this.target;

    grunt.task.run(pipeline);

  });

  /**
   * Find all area's definitions (_area.json) files in the app folder
   */
  grunt.registerTask('jasperCollectAreas', 'Find all areas definitions (_area.json) files in the app folder', function () {
    var areasDefinitionFiles = grunt.file.expand(options.appPath + '/**/_area.json');
    areas = [];
    areasDefinitionFiles.forEach(function (configFile) {
      var area = grunt.file.readJSON(configFile);
      if (!area.dependencies) {
        area.dependencies = [];
      }
      if (!area.name) {
        area.name = utils.getParentFolderName(configFile);
      }
      area.__path = utils.getPath(configFile);
      areas.push(area);
    });
  });

  /**
   * Find all application component's definitions (_definition.json) files
   */
  grunt.registerTask('jasperCollectDefs', 'Find all application components definitions (_definition.json) files', function () {


    areas.forEach(function (area) {
      var areaDefinitions = [];
      var configurations = grunt.file.expand(area.__path + '/**/_definition.json');

      var registerDefinition = function(config, def){
        if (!def.name) {
          var tagName = utils.getParentFolderName(config);
          def.name = utils.camelCaseTagName(tagName);
        }
        if (!def.type) {
          def.type = 'component';
        }

        if (def.attributes) {
          def.attributes = utils.getJasperAttributes(def.attributes);
        }

        if (def.type.toUpperCase() === 'PAGE') {
          def.area = area.name;
          pages.push(def);
        }

        def.__path = utils.getPath(config);

        if (def.templateFile) {
          def.templateUrl = def.__path + '/' + def.templateFile;
          delete def.templateFile;
        }

        if(def.type.toUpperCase() === 'TEMPLATE'){
          var htmlContent = grunt.file.read(def.templateUrl);
          def.content = utils.minifyHtml(htmlContent, {
            removeComments: true,
            preserveLineBreaks: true
          });
          delete def.templateUrl;
          delete def.templateFile;
        }
<commit_msg>Emit error if templateFile not found in _definition.json<commit_after>/*
 * grunt-jasper
 * https://github.com/jasperjs/grunt-jasper
 *
 * Copyright (c) 2015 bukharin
 * Licensed under the MIT license.
 */

'use strict';
var utils = require('./jasper-utils.js');
var templates = require('./jasper-templates.js');

var getRegistrationScript = function (def) {
  var result = '';
  var type = def.type.toUpperCase();

  delete def.type;
  delete def.__path;

  switch (type.toUpperCase()) {
    case 'COMPONENT':
      result = templates.componentRegistration(def);
      break;
    case 'DECORATOR':
      result = templates.decoratorRegistration(def);
      break;
    case 'SERVICE':
      result = templates.serviceRegistration(def);
      break;
    case 'FILTER':
      result = templates.filterRegistration(def);
      break;
    case 'DIRECTIVE':
      result = templates.directiveRegistration(def);
      break;
    case 'TEMPLATE':
      result = templates.templateRegistration(def.url, def.content);
      break;
    case 'PAGE':
      var pageTemplateUrl = utils.getPageTemplateUrl(def);
      var tagName = utils.shakeCase(def.name);
      var pageTemplate = '<' + tagName + '></' + tagName + '>';
      // register page template and the page component
      result = templates.templateRegistration(pageTemplateUrl, pageTemplate);
      result += templates.componentRegistration(def);
      break;
    default:
      grunt.util.error('Unknown definition type "' + def + '" in "' + JSON.stringify(def) + '"');
      break;
  }
  return result;
};

var options, // jasper task options
  processedBootstrapScripts = [], // scripts defined in the task options
  baseScripts = [], //scripts which must included in _base.js
  routesConfigPath, // path to the routes configuration
  areasConfigPath, // path to areas config file
  valuesConfigPath, // path to values config
  pages = [], // found app pages
  areas = [], // set of found areas
  target; // current target of the task

module.exports = function (grunt) {
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-typescript');

  grunt.registerMultiTask('jasper', 'Build jasper application', function () {
    // Merge task-specific and/or target-specific options with these defaults.
    options = this.options({
      singlePage: 'index.html',
      appPath: 'app',
      package: false,
      packageOutput: 'dist',

      defaultRoutePath: '/',
      bootstrapScripts: [],

      baseCss: [],
      /**
       * Include MD5 hash of file content after package
       */
      fileVersion: false
    });

    // pipeline for building jasper application:
    var pipeline = [
    /**
     *  At first we need to find all areas definitions (_area.json) in the project
     */
      'jasperCollectAreas',
    /**
     * Then on each found area, collect all component definitions (_definition.json)
     */
      'jasperCollectDefs',
    /**
     * Then we find all area scripts and build area client-side initialization (_init.js),
     * which tells client what kind of components places in each area
     */
      'jasperAreaInit',
    /**
     * Now we need to concatenate and minify all areas scripts, templates
     * Executes only for package process (options.package == true)
     */
      'jasperPackageAreas',
    /**
     * Now create a client-side areas configuration script (_areas.js) that tell client which areas exists in application,
     * dependencies between areas, and their script files
     */
      'jasperAreasConfig',
    /**
     * Now create a client-side values configuration script (_values.js) that tell client which config values exists in application
     * Executes only if options.values set to true
     */
      'jasperValuesConfig',
    /**
     * Now create a client-side routes configuration script (_routes.js) that tell client which routes exists in application
     */
      'jasperRoutesConfig',
    /**
     * After areas, routes and values config was built we need to create bootstrap script (_base.js) that will loaded at
     * first time and bootstrap the application.
     * Bootstrap script includes:
     * - Scripts that marks as bootstrap in jasper.json file: angular.js, jasper.js, any custom user's scripts.
     * - Areas, routes and values configuration scripts that was built in previous steps
     * - Areas scripts, which marked as 'bootstrap' (in _area.json)
     */
      'jasperPackageBase',
    /**
     * Modify single page, include reference to bootstrap scripts and styles.
     */
      'jasperPatchIndex'
    ];

    target = this.target;

    grunt.task.run(pipeline);

  });

  /**
   * Find all area's definitions (_area.json) files in the app folder
   */
  grunt.registerTask('jasperCollectAreas', 'Find all areas definitions (_area.json) files in the app folder', function () {
    var areasDefinitionFiles = grunt.file.expand(options.appPath + '/**/_area.json');
    areas = [];
    areasDefinitionFiles.forEach(function (configFile) {
      var area = grunt.file.readJSON(configFile);
      if (!area.dependencies) {
        area.dependencies = [];
      }
      if (!area.name) {
        area.name = utils.getParentFolderName(configFile);
      }
      area.__path = utils.getPath(configFile);
      areas.push(area);
    });
  });

  /**
   * Find all application component's definitions (_definition.json) files
   */
  grunt.registerTask('jasperCollectDefs', 'Find all application components definitions (_definition.json) files', function () {


    areas.forEach(function (area) {
      var areaDefinitions = [];
      var configurations = grunt.file.expand(area.__path + '/**/_definition.json');

      var registerDefinition = function(config, def){
        if (!def.name) {
          var tagName = utils.getParentFolderName(config);
          def.name = utils.camelCaseTagName(tagName);
        }
        if (!def.type) {
          def.type = 'component';
        }

        if (def.attributes) {
          def.attributes = utils.getJasperAttributes(def.attributes);
        }

        if (def.type.toUpperCase() === 'PAGE') {
          def.area = area.name;
          pages.push(def);
        }

        def.__path = utils.getPath(config);

        if (def.templateFile) {
          def.templateUrl = def.__path + '/' + def.templateFile;
          delete def.templateFile;
        }

        if(def.type.toUpperCase() === 'TEMPLATE'){
          if(!def.templateUrl){
            grunt.log.error('TemplateFile not defined in ' + def.__path);
            return;
          }
          var htmlContent = grunt.file.read(def.templateUrl);
          def.content = utils.minifyHtml(htmlContent, {
            removeComments: true,
            preserveLineBreaks: true
          });
          delete def.templateUrl;
          delete def.templateFile;
        }

{'fraction_non_alphanumeric': 0.11373123398022703, 'fraction_numerical': 0.0007323324789454412, 'mean_word_length': 2.658183766407715, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3691880', 'n_tokens_mistral': 3792, 'n_tokens_neox': 3752, 'n_words': 1070}