PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/endpoint/index.js

https://gitlab.com/javajamesb08/generator-angular-fullstack
JavaScript | 76 lines | 64 code | 11 blank | 1 comment | 8 complexity | a0222f559cdf5e2a917b45e5b3b13f64 MD5 | raw file
  1. 'use strict';
  2. var path = require('path');
  3. var yeoman = require('yeoman-generator');
  4. var util = require('util');
  5. var ngUtil = require('../util');
  6. var ScriptBase = require('../script-base.js');
  7. var Generator = module.exports = function Generator() {
  8. ScriptBase.apply(this, arguments);
  9. };
  10. util.inherits(Generator, ScriptBase);
  11. Generator.prototype.askFor = function askFor() {
  12. var done = this.async();
  13. var name = this.name;
  14. var base = this.config.get('routesBase') || '/api/';
  15. if(base.charAt(base.length-1) !== '/') {
  16. base = base + '/';
  17. }
  18. // pluralization defaults to true for backwards compat
  19. if (this.config.get('pluralizeRoutes') !== false) {
  20. name = name + 's';
  21. }
  22. var prompts = [
  23. {
  24. name: 'route',
  25. message: 'What will the url of your endpoint to be?',
  26. default: base + name
  27. }
  28. ];
  29. this.prompt(prompts, function (props) {
  30. if(props.route.charAt(0) !== '/') {
  31. props.route = '/' + props.route;
  32. }
  33. this.route = props.route;
  34. done();
  35. }.bind(this));
  36. };
  37. Generator.prototype.registerEndpoint = function registerEndpoint() {
  38. if(this.config.get('insertRoutes')) {
  39. var routeConfig = {
  40. file: this.config.get('registerRoutesFile'),
  41. needle: this.config.get('routesNeedle'),
  42. splicable: [
  43. "app.use(\'" + this.route +"\', require(\'./api/" + this.name + "\'));"
  44. ]
  45. };
  46. ngUtil.rewriteFile(routeConfig);
  47. }
  48. if (this.filters.socketio) {
  49. if(this.config.get('insertSockets')) {
  50. var socketConfig = {
  51. file: this.config.get('registerSocketsFile'),
  52. needle: this.config.get('socketsNeedle'),
  53. splicable: [
  54. "require(\'../api/" + this.name + '/' + this.name + ".socket\').register(socket);"
  55. ]
  56. };
  57. ngUtil.rewriteFile(socketConfig);
  58. }
  59. }
  60. };
  61. Generator.prototype.createFiles = function createFiles() {
  62. var dest = this.config.get('endpointDirectory') || 'server/api/' + this.name;
  63. this.sourceRoot(path.join(__dirname, './templates'));
  64. ngUtil.processDirectory(this, '.', dest);
  65. };