Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement: ES6 modules #50

Merged
merged 3 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
client/favicons
client/public
dist/
node_modules/
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "theatrebase",
"version": "0.0.0",
"description": "A database-driven site that provides listings for theatrical productions, playtexts and associated data.",
"main": "app.js",
"main": "server/app-dev.js",
"scripts": {
"start": "nodemon app",
"test": "find spec -name '*.spec.js' | xargs mocha -R spec"
"start": "nodemon server/app-dev.js",
"test": "find spec -name '*.spec.js' | xargs mocha -R spec",
"pretest": "babel server --out-dir dist --ignore app-dev.js"
},
"author": "https://github.com/andygout",
"license": "MS-RSL",
Expand All @@ -27,6 +28,10 @@
"serve-favicon": "^2.2.0"
},
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.22.0",
"babel-register": "^6.22.0",
"chai": "^3.5.0",
"mocha": "^3.2.0",
"node-mocks-http": "^1.5.4",
Expand Down
6 changes: 6 additions & 0 deletions server/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
"add-module-exports",
"transform-es2015-modules-commonjs"
]
}
2 changes: 2 additions & 0 deletions server/app-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('babel-register');
require('./app');
39 changes: 19 additions & 20 deletions app.js → server/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require('dotenv').config();

const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const express = require('express');
const Handlebars = require('handlebars');
const favicon = require('serve-favicon');
const flash = require('connect-flash');
const http = require('http');
const logger = require('morgan');
const path = require('path');
const sassMiddleware = require('node-sass-middleware');
const session = require('express-session');

const handlebarsHelpers = require('./server/lib/handlebars-helpers');
const router = require('./server/routes');
import _ from './dotenv';
import bodyParser from 'body-parser';
import exphbs from 'express-handlebars';
import express from 'express';
import Handlebars from 'handlebars';
import favicon from 'serve-favicon';
import flash from 'connect-flash';
import http from 'http';
import logger from 'morgan';
import path from 'path';
import sassMiddleware from 'node-sass-middleware';
import session from 'express-session';

import * as handlebarsHelpers from './lib/handlebars-helpers';
import router from './routes';

const app = express();

Expand All @@ -29,7 +28,7 @@ app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));

app.use(favicon(path.join(__dirname, 'client', 'favicons', 'favicon.ico')));
app.use(favicon(path.join(__dirname, '../', 'client', 'favicons', 'favicon.ico')));

app.use(logger('dev'));

Expand All @@ -39,14 +38,14 @@ app.use(flash());

app.use(
sassMiddleware({
src: path.join(__dirname, 'client', 'stylesheets'),
dest: path.join(__dirname, 'client', 'public'),
src: path.join(__dirname, '../', 'client', 'stylesheets'),
dest: path.join(__dirname, '../', 'client', 'public'),
prefix: '/stylesheets',
debug: true
})
);

app.use(express.static(path.join(__dirname, 'client', 'public')));
app.use(express.static(path.join(__dirname, '../', 'client', 'public')));

app.use('/', router);

Expand Down
9 changes: 7 additions & 2 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
exports.productions = require('./productions');
exports.theatres = require('./theatres');
import * as productions from './productions';
import * as theatres from './theatres';

export {
productions,
theatres
};
32 changes: 21 additions & 11 deletions server/controllers/productions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const Production = require('../models/production');
const getAlert = require('../lib/alert').get;
const getPageData = require('../lib/get-page-data');
const handleModelResponse = require('../lib/handle-model-response');
import Production from '../models/production';
import { getAlert } from '../lib/alert';
import getPageData from '../lib/get-page-data';
import handleModelResponse from '../lib/handle-model-response';

exports.new = (req, res, next) => {
function newRoute (req, res, next) {

const production = new Production();

Expand All @@ -13,7 +13,7 @@ exports.new = (req, res, next) => {

};

exports.create = (req, res, next) => {
function createRoute (req, res, next) {

const production = new Production(req.body);

Expand All @@ -29,7 +29,7 @@ exports.create = (req, res, next) => {

};

exports.edit = (req, res, next) => {
function editRoute (req, res, next) {

const production = new Production(req.params);

Expand All @@ -45,7 +45,7 @@ exports.edit = (req, res, next) => {

};

exports.update = (req, res, next) => {
function updateRoute (req, res, next) {

const production = new Production(req.body);

Expand All @@ -61,7 +61,7 @@ exports.update = (req, res, next) => {

};

exports.delete = (req, res, next) => {
function deleteRoute (req, res, next) {

const production = new Production(req.body);

Expand All @@ -77,7 +77,7 @@ exports.delete = (req, res, next) => {

};

exports.show = (req, res, next) => {
function showRoute (req, res, next) {

const production = new Production(req.params);

Expand All @@ -93,7 +93,7 @@ exports.show = (req, res, next) => {

};

exports.list = (req, res, next) => {
function listRoute (req, res, next) {

return Production.list()
.then(productions => {
Expand All @@ -106,3 +106,13 @@ exports.list = (req, res, next) => {
.catch(err => next(err));

};

export {
newRoute,
createRoute,
editRoute,
updateRoute,
deleteRoute,
showRoute,
listRoute
};
26 changes: 17 additions & 9 deletions server/controllers/theatres.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const Theatre = require('../models/theatre');
const getAlert = require('../lib/alert').get;
const getPageData = require('../lib/get-page-data');
const handleModelResponse = require('../lib/handle-model-response');
import Theatre from '../models/theatre';
import { getAlert } from '../lib/alert';
import getPageData from '../lib/get-page-data';
import handleModelResponse from '../lib/handle-model-response';

exports.edit = (req, res, next) => {
function editRoute (req, res, next) {

const theatre = new Theatre(req.params);

Expand All @@ -19,7 +19,7 @@ exports.edit = (req, res, next) => {

};

exports.update = (req, res, next) => {
function updateRoute (req, res, next) {

const theatre = new Theatre(req.body);

Expand All @@ -35,7 +35,7 @@ exports.update = (req, res, next) => {

};

exports.delete = (req, res, next) => {
function deleteRoute (req, res, next) {

const theatre = new Theatre(req.body);

Expand All @@ -51,7 +51,7 @@ exports.delete = (req, res, next) => {

};

exports.show = (req, res, next) => {
function showRoute (req, res, next) {

const theatre = new Theatre(req.params);

Expand All @@ -67,7 +67,7 @@ exports.show = (req, res, next) => {

};

exports.list = (req, res, next) => {
function listRoute (req, res, next) {

return Theatre.list()
.then(theatres => {
Expand All @@ -82,3 +82,11 @@ exports.list = (req, res, next) => {
.catch(err => next(err));

};

export {
editRoute,
updateRoute,
deleteRoute,
showRoute,
listRoute
};
4 changes: 2 additions & 2 deletions database/pool.js → server/database/pool.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const pg = require('pg');
import pg from 'pg';

const config = {
database: process.env.DEV_DATABASE_NAME
};

const pool = new pg.Pool(config);

module.exports = pool;
export default pool;
4 changes: 2 additions & 2 deletions database/query.js → server/database/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const pool = require('./pool');
import pool from './pool';

module.exports = function (queryData) {
export default function (queryData) {

return new Promise(function (resolve, reject) {

Expand Down
3 changes: 3 additions & 0 deletions server/dotenv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dotenv from 'dotenv';

dotenv.config({ silent: true });
10 changes: 8 additions & 2 deletions server/lib/alert.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
exports.set = (req, pageData) => {
function setAlert (req, pageData) {

req.flash('text', pageData.alertText);
req.flash('type', pageData.alertType);

};

exports.get = req => ({ text: req.flash('text'), type: req.flash('type') });
function getAlert (req) {

return { text: req.flash('text'), type: req.flash('type') };

};

export { setAlert, getAlert };
2 changes: 1 addition & 1 deletion server/lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {

STRING_MIN_LENGTH: 1,
STRING_MAX_LENGTH: 255
Expand Down
4 changes: 2 additions & 2 deletions server/lib/get-page-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const modelNamingPropMap = require('./model-naming-prop-map');
import modelNamingPropMap from './model-naming-prop-map';

const getModelName = instance => instance.constructor.name.toLowerCase();

Expand Down Expand Up @@ -43,7 +43,7 @@ const getAlertText = (model, instance, action) => {

const getAlertType = instance => instance.hasError ? 'error' : 'success';

module.exports = function (instance, action) {
export default function (instance, action) {

const model = getModelName(instance);

Expand Down
8 changes: 4 additions & 4 deletions server/lib/handle-model-response.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const alert = require('./alert');
import { getAlert, setAlert } from './alert';

module.exports = function (req, res, data) {
export default function (req, res, data) {

const page = data.page;

alert.set(req, page);
setAlert(req, page);

data[page.modelName].hasError ?
(page.action === 'create' || page.action === 'update') ?
res.render(`${page.modelRoute}/form`, Object.assign(data, { alert: alert.get(req) })) :
res.render(`${page.modelRoute}/form`, Object.assign(data, { alert: getAlert(req) })) :
res.redirect(page.instanceRoute)
:
res.redirect(page.action !== 'delete' ? page.instanceRoute : '/');
Expand Down
2 changes: 1 addition & 1 deletion server/lib/handlebars-helpers/capitalise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function (string) {
export default function (string) {

return string.charAt(0).toUpperCase() + string.substring(1);

Expand Down
4 changes: 2 additions & 2 deletions server/lib/handlebars-helpers/date-format-now.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const dateFormat = require('dateformat');
import dateFormat from 'dateformat';

module.exports = function(format) {
export default function(format) {

return dateFormat(new Date(), format);

Expand Down
24 changes: 15 additions & 9 deletions server/lib/handlebars-helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module.exports = {

capitalise: require('./capitalise'),
dateFormatNow: require('./date-format-now'),
instanceNamingValue: require('./instance-naming-value'),
instanceRoute: require('./instance-route'),
model: require('./model'),
modelLowerCase: require('./model-lower-case'),
upperCase: require('./upper-case')
import capitalise from './capitalise';
import dateFormatNow from './date-format-now';
import instanceNamingValue from './instance-naming-value';
import instanceRoute from './instance-route';
import model from './model';
import modelLowerCase from './model-lower-case';
import upperCase from './upper-case';

export {
capitalise,
dateFormatNow,
instanceNamingValue,
instanceRoute,
model,
modelLowerCase,
upperCase
};
4 changes: 2 additions & 2 deletions server/lib/handlebars-helpers/instance-naming-value.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const modelNamingPropMap = require('../model-naming-prop-map');
import modelNamingPropMap from '../model-naming-prop-map';

module.exports = function (instance) {
export default function (instance) {

const model = instance.constructor.name.toLowerCase();

Expand Down
2 changes: 1 addition & 1 deletion server/lib/handlebars-helpers/instance-route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function (instance) {
export default function (instance) {

return `/${instance.constructor.name.toLowerCase()}s/${instance.id}`;

Expand Down
Loading