All files / platform/packages/validator/commands MakeValidator.js

100% Statements 13/13
100% Branches 1/1
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                      1x 1x                   4x 4x                                                                                                 5x         5x               5x 5x 5x             5x 5x                                 1x  
'use strict';
 
/*
 * adonis-validator
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
 
const path = require('path');
const { Command } = require('@adonisjs/ace');
 
/**
 * Command to create a validator
 *
 * @class MakeValidator
 * @constructor
 */
class MakeValidator extends Command {
  constructor(Helpers) {
    super();
    this.Helpers = Helpers;
  }
 
  /* istanbul ignore next */
  /**
   * IoC container injections
   *
   * @method inject
   *
   * @return {Array}
   */
  static get inject() {
    return ['Adonis/Src/Helpers'];
  }
 
  /* istanbul ignore next */
  /**
   * The command signature
   *
   * @method signature
   *
   * @return {String}
   */
  static get signature() {
    return 'make:validator {name : Name of the validator}';
  }
 
  /* istanbul ignore next */
  /**
   * The command description
   *
   * @method description
   *
   * @return {String}
   */
  static get description() {
    return 'Make route validator';
  }
 
  /**
   * Method called when command is executed
   *
   * @method handle
   *
   * @param  {String} options.name
   *
   * @return {void}
   */
  async handle({ name }) {
    name = name.replace(/Validator$/i, '');
 
    /**
     * Reading template as a string form the mustache file
     */
    const template = await this.readFile(
      path.join(__dirname, './templates/validator.mustache'),
      'utf8',
    );
 
    /**
     * Directory paths
     */
    const relativePath = path.join('app/Validators', `${name}.js`);
    const validatorPath = path.join(this.Helpers.appRoot(), relativePath);
    const className = name.replace(/[-/_](\w)/g, (match, group) => group.toUpperCase());
 
    /**
     * If command is not executed via command line, then return
     * the response
     */
    /* istanbul ignore else */
    if (!this.viaAce) {
      return this.generateFile(validatorPath, template, { name: className });
    }
 
    /* istanbul ignore next */
    /**
     * Otherwise wrap in try/catch and show appropriate messages
     * to the end user.
     */
    try {
      await this.generateFile(validatorPath, template, { name: className });
      this.completed('create', relativePath);
    } catch (error) {
      this.error(`${relativePath} validator already exists`);
    }
  }
}
 
module.exports = MakeValidator;