All files / libs index.js

63.38% Statements 135/213
46.15% Branches 42/91
72.92% Functions 35/48
65.05% Lines 134/206

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889      9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x                           9x 9x 9x                 9x                                                                                                       9x 9x 9x     9x                       9x               9x                               9x                     9x                     5x 5x                       5x   5x 5x 5x 5x   5x                           5x 2x     5x 2x     5x 2x     5x 2x     5x       5x                       5x 5x 5x                         5x             5x             5x             5x             5x             5x             5x             5x                             2x 2x   2x                     2x 2x   2x                     2x 2x 2x 2x       2x 2x 1x 1x 1x     2x                           2x 2x   2x                       8x 8x 8x       8x 3x       3x     8x 8x 8x   8x                             2x 2x                           2x 2x 2x 2x 2x                                   2x 2x                               1x 1x   1x                                                     1x 1x 1x 1x 1x 1x                                 5x 5x 5x 5x                           18x                                                                                                                                                                 2x                   2x                   2x                   2x                   8x 8x   8x                     2x                     2x                     2x                     2x                     6x                     2x                                                                                       5x                                           6x                     8x                     5x                     5x                       44x                         13x 13x                           13x 13x                     13x 13x   13x                       2x       9x  
'use strict'
// @ts-check
 
const { version } = require('../package.json')
const Util = require('util')
const { promisify } = Util
const _ = require('lodash')
const ReplaceInFiles = require('replace-in-files')
const Fs = require('fs')
const Path = require('path')
const Inflector = require('inflected')
const { Command } = require('commander')
const Process = require('process')
const Colors = require('colors/safe')
const SequelizeAuto = require('sequelize-auto')
const Config = require('config')
 
/**
 *
 *
 * @class Scaffolding
 */
class Scaffolding {
  /**
   *Creates an instance of Scaffolding.
   * @param {IScaffoldingConfig} [options={}]
   * @memberof Scaffolding
   */
  constructor(options = {}) {
    this._commander = new Command()
    this._sequelizeAuto = undefined
    this._logicalFieldTemplate = `\t\tlogical: {
\t\t\ttype: new DataTypes.VIRTUAL(DataTypes.INTEGER(1), ['logical']),
\t\t\tallowNull: true,
\t\t\tfield: 'logical',
\t\t\tget() {
\t\t\t\treturn this.getDataValue('logical')
\t\t\t},
\t\t\tset(val) {}
\t\t}`
    this._attributes = `		id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id'
    },
    status: {
      type: DataTypes.ENUM('active', 'inactive', 'pending'),
      values: ['active', 'inactive', 'pending'],
      allowNull: false,
      defaultValue: 'pending',
      field: 'status',
      validate: {
        isIn: [
          ['active', 'inactive', 'pending']
        ]
      }
    },
    createdId: {
      type: DataTypes.INTEGER(8).UNSIGNED,
      allowNull: true,
      field: 'created_id'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'created_at'
    },
    updatedId: {
      type: DataTypes.INTEGER(8).UNSIGNED,
      allowNull: true,
      field: 'updated_id'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'updated_at'
    },
    deletedId: {
      type: DataTypes.INTEGER(8).UNSIGNED,
      allowNull: true,
      field: 'deleted_id'
    },
    deletedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'deleted_at'
    },
${this._logicalFieldTemplate}`
    this._references = []
    this._files = []
    this._args = {}
 
    /** @type {IScaffoldingConfig} */
    this._options = this._deepExtend(
      {
        path: {
          controllers: './controllers/',
          routes: './routes/',
          models: './models/',
          schemas: './schemas/'
        }
      },
      options
    )
 
    this._init()
  }
 
  /**
   * @private
   * @memberof Scaffolding
   */
  _init() {
    this._commander
      .command('generate <name>')
      .version(process.env.npm_package_version || version, '-v, --version', 'SHOW CURRENT VERSION')
      .alias('g')
      .description('[SCAFFOLDING CLI]: SCAFFOLDING [controller|route]')
      .option('-m, --model', 'SCAFFOLDING modle')
      .option('-c, --controller', 'SCAFFOLDING controller')
      .option('-r, --route', 'SCAFFOLDING route')
      .option('-s, --schema', 'SCAFFOLDING schema')
      .option('-f, --force', 'CREATE FORCIBLY IF FILE ALREADY EXISTS')
      .option('-a, --run-all', 'CREATE [controller|route|modle|schema]')
      .option('-t, --type <items>', 'SCAFFOLDING TYPE', value => (value || []).split(','), [])
      .helpOption('-h, --help', 'READ MORE INFORMATION')
      .on('--help', this._cmdHelp.bind(this))
      .action(this._cmdAction.bind(this))
 
    this._commander.version(process.env.npm_package_version || version, '-v, --version', 'SHOW CURRENT VERSION')
  }
 
  /**
   *
   *
   * @param {string[]} [payload=Process.argv]
   * @return {Promise<Object>}
   * @memberof Scaffolding
   */
  async run(payload = Process.argv) {
    return await Promise.resolve(this._commander.parse(payload))
  }
 
  /**
   *
   * @private
   * @param {string} name
   * @param {Object} options
   * @memberof Scaffolding
   */
  async _cmdAction(name, options) {
    try {
      Iif (_.isEmpty(name)) {
        throw new TypeError('Please specify a parameter: [name]')
      }
 
      let {
        type = [],
        runAll = false,
        force = false,
        controller = false,
        route = false,
        model = false,
        schema = false
      } = options
 
      controller = controller || !_.isUndefined(type.find(t => t === 'controller')) || runAll
      route = route || !_.isUndefined(type.find(t => t === 'route')) || runAll
      model = model || !_.isUndefined(type.find(t => t === 'model')) || runAll
      schema = schema || !_.isUndefined(type.find(t => t === 'schema')) || runAll
 
      this._args = _.assign(
        {},
        {
          type,
          name,
          controller,
          route,
          model,
          schema,
          runAll,
          force
        }
      )
 
      if (controller) {
        await this._createController(name)
      }
 
      if (route) {
        this._createRoute(name)
      }
 
      if (model) {
        await this._createModel(name)
      }
 
      if (schema) {
        await this._createSchema(name)
      }
 
      Iif (!_.size(this._files)) {
        throw new TypeError('Please specify at least 1 type: [controller|route|modle|schema]')
      }
 
      await this._replaceFiles()
    } catch (error) {
      this._log(error, 'error')
      this._showHelp(false, 'red')
    }
  }
 
  /**
   *
   * @memberof Scaffolding
   */
  async _replaceFiles() {
    try {
      const name = _.get(this._args, 'name')
      const base = {
        files: this._files,
        optionsForFiles: {
          ignore: ['**/node_modules/**']
        },
        allowEmptyPaths: false,
        saveOldFile: false,
        encoding: 'utf8',
        onlyFindPathsWithoutReplace: false,
        returnPaths: true,
        returnCountOfMatchesByPaths: true
      }
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{CLAZZ\}\}/, 'g'),
          to: this._toModelizeAndPluralize(name)
        })
      )
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{PLURAL\}\}/, 'g'),
          to: this._toResource(name)
        })
      )
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{SINGULAR\}\}/, 'g'),
          to: this._toUnderscore(name)
        })
      )
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{FIELDS\}\}/, 'g'),
          to: this._attributes
        })
      )
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{RELATIONS\}\}/, 'g'),
          to: this._getModelReferencesAsString()
        })
      )
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{TABLE\}\}/, 'g'),
          to: this._toTableize(name)
        })
      )
 
      await ReplaceInFiles(
        this._deepExtend(base, {
          from: new RegExp(/\{\{MODEL\}\}/, 'g'),
          to: this._toModelClassify(name)
        })
      )
 
      this._log(`SCAFFOLDING DONE... ${JSON.stringify(this._files)}`, 'log')
    } catch (error) {
      this._log(error, 'error')
      throw error
    }
  }
 
  /**
   * @private
   * @description create controller for `express`
   * @param {string} [name='']
   * @returns {string}
   * @memberof Scaffolding
   */
  async _createController(name = '') {
    const src = this._getTemplatePath('controllers')
    const dist = this._getControllerPath(name)
 
    return this._copyTemplate(src, dist)
  }
 
  /**
   * @private
   * @description create route definition for `express`
   * @param {string} [name='']
   * @returns {string}
   * @memberof Scaffolding
   */
  async _createRoute(name = '') {
    const src = this._getTemplatePath('routes')
    const dist = this._getRoutePath(name)
 
    return this._copyTemplate(src, dist)
  }
 
  /**
   * @private
   * @description create model class for `Sequelize` with typescript using `SequelizeAuto`
   * @param {string} [name='']
   * @returns {string}
   * @memberof Scaffolding
   */
  async _createModel(name = '') {
    try {
      const src = this._getTemplatePath('models')
      const dist = this._getModelPath(name)
      const options = {
        encoding: 'utf-8'
      }
 
      await this._runSequelizeAuto(name)
      if (this._fileExists(dist)) {
        this._setModelAttributes(name, Fs.readFileSync(dist, options))
        this._setModelReferences()
        Fs.unlinkSync(dist)
      }
 
      return this._copyTemplate(src, dist)
    } catch (error) {
      this._log(error, 'error')
    }
  }
 
  /**
   * @private
   * @description create empty schema for `MongoDB`
   * @param {string} [name='']
   * @returns {string}
   * @memberof Scaffolding
   */
  async _createSchema(name = '') {
    const src = this._getTemplatePath('schemas')
    const dist = this._getSchemaPath(name)
 
    return this._copyTemplate(src, dist)
  }
 
  /**
   * @private
   * @description copy from template to be SCAFFOLDING
   * @param {string} src
   * @param {string} dist
   * @returns {string}
   * @memberof Scaffolding
   */
  _copyTemplate(src, dist) {
    try {
      const force = _.get(this._args, 'force')
      Iif (!this._fileExists(src)) {
        throw new TypeError(`Specified template does not exists!: ${src}`)
      }
 
      if (this._fileExists(dist)) {
        Iif (!force) {
          throw new TypeError(`Specified file already exists!: ${dist}`)
        }
 
        Fs.unlinkSync(dist)
      }
 
      Fs.copyFileSync(src, dist)
      this._log(`Generated file: ${dist}`, 'log')
      this._files.push(dist)
 
      return dist
    } catch (error) {
      this._log(error, 'error')
      throw error
    }
  }
 
  /**
   *
   * @private
   * @param {string} [name='']
   * @returns {Promise<any>}
   * @memberof Scaffolding
   */
  async _runSequelizeAuto(name = '') {
    try {
      const options = _.cloneDeep(Config.util.toObject(Config.db[Process.env.NODE_ENV || 'development']))
      const {
        user,
        username,
        password,
        host,
        database,
        port,
        dialect,
        dialectOptions,
        pool,
        timezone,
        define,
        benchmark
      } = options
      const tables = [this._toTableize(name)]
      const camelCase = true
      const indentation = 1
      this._sequelizeAuto = new SequelizeAuto(database, username, password, {
        user,
        username,
        password,
        database,
        host,
        port,
        dialect,
        dialectOptions,
        pool,
        timezone,
        define,
        benchmark,
        tables,
        camelCase,
        indentation
      })
 
      await promisify(this._sequelizeAuto.run).bind(this._sequelizeAuto)()
      return await this._sleep(1000)
    } catch (error) {
      this._log(error, 'error')
      throw error
    }
  }
 
  /**
   *
   * @private
   * @param {string} [name='']
   * @param {string} [str='']
   * @returns {void}
   * @memberof Scaffolding
   */
  _setModelAttributes(name = '', str = '') {
    const table = this._toTableize(name)
    const clazz = this._toModelClassify(name)
 
    this._attributes = str
      // eslint-disable-next-line no-useless-escape
      .replace(new RegExp(/\/\*\sjshint\sindent\:\s1\s\*\//, 'g'), '')
      // eslint-disable-next-line no-useless-escape
      .replace(new RegExp(/module\.exports\s\=\sfunction\(sequelize,\sDataTypes\)\s\{/, 'g'), '')
      .replace(
        new RegExp(
          /\t{1,10}logical:\s\{\n\t{1,10}type:\sDataTypes\.INTEGER\(1\),\n\t{1,10}allowNull:\strue,\n\t{1,10}field:\s'logical'\n\t{1,10}\}/,
          'g'
        ),
        this._logicalFieldTemplate
      )
      .replace(new RegExp('\\s\\sreturn\\ssequelize\\.define\\(\'' + clazz + '\',\\s\\{', 'g'), '')
      .replace(new RegExp('\\treturn\\ssequelize\\.define\\(\'' + clazz + '\',\\s\\{', 'g'), '')
      .replace(new RegExp('\\},\\s\\{\\n\\t\\ttableName:\\s\'' + table + '\'\\n\t\\}\\);\\n\\};', 'g'), '')
      .replace(new RegExp(/\n{2,3}/, 'g'), '')
      .replace(new RegExp(/\n\t\n/, 'g'), '')
      .replace(new RegExp(/\t{3}/, 'g'), '        ')
      .replace(new RegExp(/\t{2}/, 'g'), '      ')
  }
 
  /**
   *
   * @private
   * @memberof Scaffolding
   */
  _setModelReferences() {
    const regexp = new RegExp(/references:\s\{\n\s{1,16}model:\s'(\w{1,128})',/, 'g')
    while (true) {
      const result = regexp.exec(this._attributes)
      const model = RegExp.$1
      Eif (_.isEmpty(result)) {
        break
      }
 
      if (!_.isEmpty(model)) {
        this._references.push(model)
        continue
      }
    }
  }
 
  /**
   *
   * @private
   * @returns {string}
   * @memberof Scaffolding
   */
  _getModelReferencesAsString() {
    const tab = '    '
    const br = '\n'
    Eif (_.isEmpty(this._references)) {
      return '\t\t// TODO: implement your relations'
    }
 
    return this._references.map(ref => `${tab}this.belongsTo(models.${this._toModelClassify(ref)})`).join(br)
  }
 
  /**
   *
   * @private
   * @param {string} [filename='']
   * @returns {boolean}
   * @memberof Scaffolding
   */
  _fileExists(filename = '') {
    return Fs.existsSync(filename)
  }
 
  /**
   *
   * @private
   * @param {boolean} [withExit=false]
   * @param {TScaffoldingLogColor} [color='green']
   * @returns {void}
   * @memberof Scaffolding
   */
  _showHelp(withExit = false, color = 'green') {
    const handler = txt => Colors[color].bind(Colors)(txt)
    if (withExit) {
      return this._commander.help(handler)
    }
 
    return this._commander.outputHelp(handler)
  }
 
  /**
   * @private
   * @description show command help
   * @returns {void}
   * @memberof Scaffolding
   */
  _cmdHelp() {
    this._log('', 'log')
 
    this._log('  Generate Template via CLI:', 'debug')
    this._log('    $ node cli generate <name> -c -m', 'log')
    this._log('    $ node cli g <name>', 'log')
    this._log('', 'log')
 
    this._log('  Examples:', 'debug')
 
    this._log('    # Create a new controller', 'debug')
    this._log('    $ node cli generate your_class_name --controller ', 'log')
    this._log('    $ node cli g your_class_name -c ', 'log')
    this._log('', 'log')
 
    this._log('    # Create a new route', 'debug')
    this._log('    $ node cli generate your_class_name --route ', 'log')
    this._log('    $ node cli g your_class_name -r ', 'log')
    this._log('', 'log')
 
    this._log('    # Create a new model ', 'debug')
    this._log('    # [!!!IMPORTANT!!!]: you must create a table on corresponded database before running', 'error')
    this._log('    $ node cli generate your_class_name --model ', 'log')
    this._log('    $ node cli g your_class_name -m ', 'log')
    this._log('', 'log')
 
    this._log('    # Create a new schema for mongoDB', 'debug')
    this._log('    $ node cli generate your_class_name --schema ', 'log')
    this._log('    $ node cli g your_class_name -s ', 'log')
    this._log('', 'log')
 
    this._log('    # Create all-in-one(controller/route/model/schema)', 'debug')
    this._log('    $ node cli generate your_class_name --run-all ', 'log')
    this._log('    $ node cli g your_class_name -a ', 'log')
    this._log('', 'log')
 
    this._log('    # Or you can specify with the type parameter as a list (comma separated)', 'debug')
    this._log('    $ node cli generate your_class_name --type "controller,route,model"', 'log')
    this._log('    $ node cli g your_class_name -t "controller,route,model"', 'log')
    this._log('', 'log')
 
    this._log('    # Run with force option ', 'debug')
    this._log('    # [!!!IMPORTANT!!!]: running with this options will overwrite the exists file!', 'error')
    this._log('    $ node cli generate your_class_name --force --controller ', 'log')
    this._log('    $ node cli g your_class_name -f -c ', 'log')
    this._log('', 'log')
  }
 
  /**
   *
   * @private
   * @returns {string}
   * @memberof Scaffolding
   */
  _getControllerDir() {
    return Path.resolve(Process.cwd(), this._options.path.controllers)
  }
 
  /**
   *
   * @private
   * @returns {string}
   * @memberof Scaffolding
   */
  _getRouteDir() {
    return Path.resolve(Process.cwd(), this._options.path.routes)
  }
 
  /**
   *
   * @private
   * @returns {string}
   * @memberof Scaffolding
   */
  _getModelDir() {
    return Path.resolve(Process.cwd(), this._options.path.models)
  }
 
  /**
   *
   * @private
   * @returns {string}
   * @memberof Scaffolding
   */
  _getSchemaDir() {
    return Path.resolve(Process.cwd(), this._options.path.schemas)
  }
 
  /**
   * @private
   * @param {string} [type='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getTemplatePath(type = '') {
    const filename = `${type}.tpl`
    const dir = Path.resolve(__dirname, '../templates/')
 
    return Path.join(dir, filename)
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getRoutePath(str = '') {
    return Path.join(this._getRouteDir(), `${this._getSingularFileName(str)}.js`)
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getControllerPath(str = '') {
    return Path.join(this._getControllerDir(), `${this._getSingularFileName(str)}.js`)
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getModelPath(str = '') {
    return Path.join(this._getModelDir(), `${this._getPluralFileName(str)}.js`)
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getSchemaPath(str = '') {
    return Path.join(this._getSchemaDir(), `${this._getSingularFileName(str)}.js`)
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getSingularFileName(str = '') {
    return _.toLower(Inflector.singularize(Inflector.underscore(str)))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _getPluralFileName(str = '') {
    return _.toLower(Inflector.pluralize(Inflector.underscore(str)))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toSingular(str = '') {
    return Inflector.singularize(Inflector.camelize(Inflector.underscore(str), false))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toPluralize(str = '') {
    return Inflector.pluralize(Inflector.camelize(Inflector.underscore(str), false))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toClassify(str = '') {
    return Inflector.classify(Inflector.camelize(Inflector.underscore(str)))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toModelizeAndPluralize(str = '') {
    return Inflector.pluralize(Inflector.classify(Inflector.camelize(Inflector.underscore(str), true)))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toModelize(str = '') {
    return Inflector.singularize(Inflector.camelize(Inflector.underscore(str), false))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toModelClassify(str = '') {
    return Inflector.pluralize(Inflector.camelize(Inflector.underscore(str), false))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toTableize(str = '') {
    return Inflector.tableize(Inflector.classify(Inflector.camelize(Inflector.underscore(str))))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toResource(str = '') {
    return Inflector.underscore(Inflector.pluralize(Inflector.camelize(Inflector.underscore(str), false)))
  }
 
  /**
   *
   * @private
   * @param {string} [str='']
   * @returns {string}
   * @memberof Scaffolding
   */
  _toUnderscore(str = '') {
    return Inflector.underscore(Inflector.singularize(Inflector.camelize(Inflector.underscore(str), false)))
  }
 
  /**
   *
   * @private
   * @param {Object} src
   * @param {Object} extend
   * @returns {Object}
   * @memberof Scaffolding
   */
  _deepExtend(src, extend) {
    return _.assign(_.cloneDeep(src), extend)
  }
 
  /**
   *
   * @private
   * @param {*} val
   * @param {TScaffoldingLogLevel} [level='error']
   * @returns {void}
   * @memberof Scaffolding
   */
  _log(val, level = 'error') {
    /** @type {TScaffoldingLogColor} */
    let color = 'white'
    switch (level) {
      case 'error':
        color = 'red'
        break
 
      case 'warn':
        color = 'yellow'
        break
 
      case 'info':
        color = 'cyan'
        break
 
      case 'log':
        color = 'green'
        break
 
      case 'debug':
        color = 'white'
        break
 
      default:
        color = 'gray'
        break
    }
 
    const logger = _.bind(_.get(console, level), console)
    const args = _.bind(_.get(Colors, color))(val)
 
    return logger(args)
  }
 
  /**
   *
   * @private
   * @static
   * @param {number} [ms=1000]
   * @returns {Promise<void>}
   * @memberof Scaffolding
   */
  async _sleep(ms = 100) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }
}
 
module.exports = new Scaffolding()