/endpoint/index.js
JavaScript | 76 lines | 64 code | 11 blank | 1 comment | 8 complexity | a0222f559cdf5e2a917b45e5b3b13f64 MD5 | raw file
- 'use strict';
- var path = require('path');
- var yeoman = require('yeoman-generator');
- var util = require('util');
- var ngUtil = require('../util');
- var ScriptBase = require('../script-base.js');
- var Generator = module.exports = function Generator() {
- ScriptBase.apply(this, arguments);
- };
- util.inherits(Generator, ScriptBase);
- Generator.prototype.askFor = function askFor() {
- var done = this.async();
- var name = this.name;
- var base = this.config.get('routesBase') || '/api/';
- if(base.charAt(base.length-1) !== '/') {
- base = base + '/';
- }
- // pluralization defaults to true for backwards compat
- if (this.config.get('pluralizeRoutes') !== false) {
- name = name + 's';
- }
- var prompts = [
- {
- name: 'route',
- message: 'What will the url of your endpoint to be?',
- default: base + name
- }
- ];
- this.prompt(prompts, function (props) {
- if(props.route.charAt(0) !== '/') {
- props.route = '/' + props.route;
- }
- this.route = props.route;
- done();
- }.bind(this));
- };
- Generator.prototype.registerEndpoint = function registerEndpoint() {
- if(this.config.get('insertRoutes')) {
- var routeConfig = {
- file: this.config.get('registerRoutesFile'),
- needle: this.config.get('routesNeedle'),
- splicable: [
- "app.use(\'" + this.route +"\', require(\'./api/" + this.name + "\'));"
- ]
- };
- ngUtil.rewriteFile(routeConfig);
- }
- if (this.filters.socketio) {
- if(this.config.get('insertSockets')) {
- var socketConfig = {
- file: this.config.get('registerSocketsFile'),
- needle: this.config.get('socketsNeedle'),
- splicable: [
- "require(\'../api/" + this.name + '/' + this.name + ".socket\').register(socket);"
- ]
- };
- ngUtil.rewriteFile(socketConfig);
- }
- }
- };
- Generator.prototype.createFiles = function createFiles() {
- var dest = this.config.get('endpointDirectory') || 'server/api/' + this.name;
- this.sourceRoot(path.join(__dirname, './templates'));
- ngUtil.processDirectory(this, '.', dest);
- };