Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Migrate generated app to be usable without Babel and Node 5 directly
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Feb 18, 2016
1 parent 4d15ec4 commit 294031e
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 113 deletions.
4 changes: 1 addition & 3 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ module.exports = generators.Base.extend({
'feathers-configuration',
'serve-favicon',
'compression',
'winston',
'babel-core',
'babel-preset-es2015'
'winston'
];
},

Expand Down
47 changes: 25 additions & 22 deletions generators/app/templates/app.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import { join } from 'path';
import { static as serveStatic } from 'feathers';
import favicon from 'serve-favicon';
import compress from 'compression';<% if (cors) { %>
import cors from 'cors';<% } %>
import feathers from 'feathers';
import configuration from 'feathers-configuration';
import hooks from 'feathers-hooks';<% if (providers.indexOf('rest') !== -1) { %>
import rest from 'feathers-rest';
import bodyParser from 'body-parser';
<% } %><% if (providers.indexOf('socket.io') !== -1) { %>import socketio from 'feathers-socketio';<% } %><% if (providers.indexOf('primus') !== -1) { %>import primus from 'feathers-primus';<% } %>
<% if (authentication.length) { %>import authentication from 'feathers-authentication';<% } %>
import middleware from './middleware';
import services from './services';
'use strict';

const path = require('path');
const serveStatic = require('feathers').static;
const favicon = require('serve-favicon');
const compress = require('compression');<% if (cors) { %>
const cors = require('cors');<% } %>
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const hooks = require('feathers-hooks');<% if (providers.indexOf('rest') !== -1) { %>
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
<% } %><% if (providers.indexOf('socket.io') !== -1) { %>const socketio = require('feathers-socketio');<% } %><% if (providers.indexOf('primus') !== -1) { %>const primus = require('feathers-primus');<% } %>
<% if (authentication.length) { %>const authentication = require('feathers-authentication');<% } %>
const middleware = require('./middleware');
const services = require('./services');

<% if (cors === 'whitelisted') { %>
let whitelist = app.get('corsWhitelist');
let corsOptions = {
origin: function(origin, callback){
var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
const whitelist = app.get('corsWhitelist');
const corsOptions = {
origin(origin, callback){
const originIsWhitelisted = whitelist.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
}
};<% } %>
let app = feathers();
const app = feathers();

app.configure(configuration(join(__dirname, '..')))
app.configure(configuration(path.join(__dirname, '..')))
.use(compress())<% if (cors) { %>
.options('*', cors(<% if (cors === 'whitelisted') { %>corsOptions<% } %>))
.use(cors(<% if (cors === 'whitelisted') { %>corsOptions<% } %>))<% } %>
.use(favicon( join(app.get('public'), 'favicon.ico') ))
.use(favicon( path.join(app.get('public'), 'favicon.ico') ))
.use('/', serveStatic( app.get('public') ))<% if(providers.indexOf('rest') !== -1) { %>
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))<% } %>
Expand All @@ -40,4 +43,4 @@ app.configure(configuration(join(__dirname, '..')))
.configure(services)
.configure(middleware);

export default app;
module.exports = app;
15 changes: 8 additions & 7 deletions generators/app/templates/middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import path from 'path';
import notFound from './not-found-handler';
import { handler as error } from 'feathers-errors';
import logger from './logger';
'use strict';

export default function() {
const errors = require('feathers-errors');
const notFound = require('./not-found-handler');
const logger = require('./logger');

module.exports = function() {
const app = this;

// Add your custom middleware here. Remember, that
// just like Express the order matters, so error
// handling middleware should go last.
app.use(notFound())
.use(logger(app))
.use(error());
}
.use(errors.handler());
};
2 changes: 1 addition & 1 deletion generators/app/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"scripts": {
"start": "node src/",
"jshint": "jshint src/. test/. --config",
"mocha": "mocha test/ --compilers js:babel-core/register",
"mocha": "mocha test/ --use_strict",
"test": "npm run jshint && npm run mocha"
}
}
10 changes: 6 additions & 4 deletions generators/app/templates/service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<% for (var i = 0; i < services.length; i++) { %>import <%= services[i] %> from './<%= services[i] %>';
'use strict';

<% for (var i = 0; i < services.length; i++) { %>const <%= services[i] %> = require('./<%= services[i] %>');
<% } %>
<% if (database === 'sqlite') { %>
import path from 'path';
import fs from 'fs-extra';<% } %><% if (database === 'mongodb') { %>import mongoose from 'mongoose';<% } %><% if (database === 'sqlite' || database === 'mssql' || database === 'postgres' || database === 'mysql' || database === 'mariadb') { %>import Sequelize from 'sequelize';<% } %>
const path = require('path');
const fs = require('fs-extra');<% } %><% if (database === 'mongodb') { %>const mongoose = require('mongoose');<% } %><% if (database === 'sqlite' || database === 'mssql' || database === 'postgres' || database === 'mysql' || database === 'mariadb') { %>const Sequelize = require('sequelize');<% } %>

export default function() {
module.exports = function() {
const app = this;
<% if (database === 'sqlite') { %>
fs.ensureDirSync( path.dirname(app.get('sqlite')) );
Expand Down
3 changes: 0 additions & 3 deletions generators/app/templates/static/.babelrc

This file was deleted.

6 changes: 3 additions & 3 deletions generators/app/templates/static/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

// Add any common hooks you want to share across services in here.
//
// Below is an example of how a hook is written and exported. Please
// see http://docs.feathersjs.com/hooks/readme.html for more details
// on hooks.

var myHook = function(options) {
exports.myHook = function(options) {
return function(hook) {
console.log('My custom global hook ran. Feathers is awesome!');
};
};

export default { myHook };
14 changes: 7 additions & 7 deletions generators/app/templates/static/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require('babel-core/register');
'use strict';

var app = require('./app').default;
var port = app.get('port');
var server = app.listen(port);
const app = require('./app');
const port = app.get('port');
const server = app.listen(port);

server.on('listening', function() {
console.log(`Feathers application started on ${app.get('host')}:${port}`);
});
server.on('listening', () =>
console.log(`Feathers application started on ${app.get('host')}:${port}`)
);
8 changes: 5 additions & 3 deletions generators/app/templates/static/src/middleware/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import winston from 'winston';
'use strict';

export default function(app) {
const winston = require('winston');

module.exports = function(app) {
// Add a logger to our app object for convenience
app.logger = winston;

Expand All @@ -19,4 +21,4 @@ export default function(app) {

next(error);
};
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import errors from 'feathers-errors';
'use strict';

export default function() {
const errors = require('feathers-errors');

module.exports = function() {
return function(req, res, next) {
next(new errors.NotFound('Page not found'));
};
}

};
8 changes: 5 additions & 3 deletions generators/app/templates/static/test/app.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import assert from 'assert';
import request from 'request';
import app from '../src/app';
'use strict';

const assert = require('assert');
const request = require('request');
const app = require('../src/app');

describe('Feathers application tests', () => {
before(function(done) {
Expand Down
2 changes: 1 addition & 1 deletion generators/hook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function importHook(filename, name, module, type, method) {
// Lookup existing services/<service-name>/hooks/index.js file
if (fs.existsSync(filename)) {
var content = fs.readFileSync(filename).toString();
var statement = 'import ' + name + ' from \'' + module + '\';';
var statement = 'const ' + name + ' = require(\'' + module + '\');';
var expression = new RegExp( '(' + type + '(.|\n)+?' + method + '.+?)(\]{1})' );

// Also add if it is not already there
Expand Down
6 changes: 4 additions & 2 deletions generators/hook/templates/after-hook.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

// <%= hookPath %>.js
//
// Use this hook to manipulate data after it has been fetched
// from the database and before it gets sent to the user. For more
// information on hooks see: http://docs.feathersjs.com/hooks/readme.html

export default function(options = {}) {
module.exports = function(options = {}) {
const defaults = {};
options = Object.assign({}, defaults, options);

Expand All @@ -14,4 +16,4 @@ export default function(options = {}) {
console.log('My before hook ran after');
}
};
}
};
8 changes: 5 additions & 3 deletions generators/hook/templates/after-hook.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import assert from 'assert';
import <%= codeName %> from '<%= hookPath %>';
'use strict';

const assert = require('assert');
const <%= codeName %> = require('<%= hookPath %>');

const mockHook = {
type: 'after',
Expand All @@ -11,7 +13,7 @@ const mockHook = {

describe('<%= service %> <%= codeName %> hook', () => {
it('can be used', () => {
let hook = <%= codeName %>()(mockHook);
const hook = <%= codeName %>()(mockHook);
assert.equal(hook.type, 'after');
});
});
6 changes: 4 additions & 2 deletions generators/hook/templates/before-hook.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

// <%= hookPath %>.js
//
// Use this hook to manipulate incoming data or params before it is sent to the database.
// For more information on hooks see: http://docs.feathersjs.com/hooks/readme.html

export default function(options = {}) {
module.exports = function(options = {}) {
const defaults = {};
options = Object.assign({}, defaults, options);

Expand All @@ -12,4 +14,4 @@ export default function(options = {}) {
console.log('My before hook ran before');
}
};
}
};
8 changes: 5 additions & 3 deletions generators/hook/templates/before-hook.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import assert from 'assert';
import <%= codeName %> from '<%= hookPath %>';
'use strict';

const assert = require('assert');
const <%= codeName %> = require('<%= hookPath %>');

const mockHook = {
type: 'before',
Expand All @@ -11,7 +13,7 @@ const mockHook = {

describe('<%= service %> <%= codeName %> hook', () => {
it('can be used', () => {
let hook = <%= codeName %>()(mockHook);
const hook = <%= codeName %>()(mockHook);
assert.equal(hook.type, 'before');
});
});
6 changes: 4 additions & 2 deletions generators/model/templates/generic.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

// <%= name %>-model.js - A just a generic object literal model

let <%= name %>Model = {
const <%= name %>Model = {
text: {type: String, required: true, index: true},
createdAt: {type: Date, 'default': Date.now},
updatedAt: {type: Date, 'default': Date.now}
};

export default <%= name %>Model;
module.exports = <%= name %>Model;
10 changes: 6 additions & 4 deletions generators/model/templates/mongoose.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict';

// <%= name %>-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.

import mongoose from 'mongoose';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let <%= name %>Schema = new Schema({
const <%= name %>Schema = new Schema({
text: {type: String, required: true, index: true},
createdAt: {type: Date, 'default': Date.now},
updatedAt: {type: Date, 'default': Date.now}
});

let <%= name %>Model = mongoose.model('<%= name %>', <%= name %>Schema);
const <%= name %>Model = mongoose.model('<%= name %>', <%= name %>Schema);

export default <%= name %>Model;
module.exports = <%= name %>Model;
10 changes: 6 additions & 4 deletions generators/model/templates/sequelize.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

// <%= name %>-model.js - A sequelize model
//
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.

import Sequelize from 'sequelize';
const Sequelize = require('sequelize');

export default function(sequelize) {
let <%= name %> = sequelize.define('<%= name %>', {
module.exports = function(sequelize) {
const <%= name %> = sequelize.define('<%= name %>', {
text: {
type: Sequelize.STRING,
allowNull: false,
Expand All @@ -22,4 +24,4 @@ export default function(sequelize) {
<%= name %>.sync({ force: true });

return <%= name %>;
}
};
4 changes: 1 addition & 3 deletions generators/service/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

var generators = require('yeoman-generator');
var fs = require('fs');
var inflect = require('i')();
Expand All @@ -8,7 +6,7 @@ function importService(filename, name, module) {
// Lookup existing service/index.js file
if (fs.existsSync(filename)) {
var content = fs.readFileSync(filename).toString();
var statement = 'import ' + name + ' from \'' + module + '\';';
var statement = 'const ' + name + ' = require(\'' + module + '\');';
var configure = ' app.configure(' + name + ');\n}';

// Also add if it is not already there
Expand Down
Loading

0 comments on commit 294031e

Please sign in to comment.