Skip to content

Commit

Permalink
🐛💄 Fix bug in generator and server and increase DX.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieutu committed Oct 8, 2018
1 parent 750d5d6 commit 16e0a11
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ module.exports = (api, options, rootOptions) => {
},
});

api.render('./templates/srv');
api.render({[`${options.serverDir}/index.js`]: './templates/srv/index.js'});

if (options.addExamples) {
// TODO
}
};

18 changes: 9 additions & 9 deletions src/generator/templates/srv/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import express from 'express';

export default app => {
app.use(express.json());

app.get('/foo', (req, res) => {
res.json({msg: 'foo'});
});

app.post('/bar', (req, res) => {
res.json(req.body);
});
// app.use(express.json());
//
// app.get('/foo', (req, res) => {
// res.json({msg: 'foo'});
// });
//
// app.post('/bar', (req, res) => {
// res.json(req.body);
// });
}
4 changes: 2 additions & 2 deletions src/prompts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ module.exports = [
{
type: 'confirm',
name: 'shouldServeApp',
message: `Should serve vue app? (default: ${shouldServeApp})`,
message: `Should serve vue app?`,
description: 'This will allow you to serve the vue app via the express server. So only one server for the app and the api.',
default: shouldServeApp,
},
{
type: 'input',
name: 'serverDir',
message: `Where will be located your server? (default: "${serverDir}")`,
message: `Where will be located your server?`,
description: 'The location of your server code, relative to the root of your project.',
default: serverDir,
},
Expand Down
26 changes: 22 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import express from 'express';
import { load } from './utils/misc';
import listEndpoints from 'express-list-endpoints';

import history from 'connect-history-api-fallback';

export default ({
Expand All @@ -19,7 +17,9 @@ export default ({
require('ts-node/register/transpile-only');
}

load(srvPath)(app);
const server = loadServer(srvPath);

server(app);

if (isInProduction && shouldServeApp) {
app.use(history());
Expand All @@ -30,8 +30,26 @@ export default ({
if (err) {
reject(err);
} else {
resolve(listEndpoints(app));
resolve(getAppEndpoints(app));
}
});
});
};

function getAppEndpoints (app) {
try {
return listEndpoints(app);
} catch (e) {
return [];
}
}

function loadServer (file) {
const empty = () => {};
try {
const module = require(file);
return module.default || module || empty;
} catch (e) {
return empty;
}
}
3 changes: 2 additions & 1 deletion src/servicePlugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import config from '../config';
import serveCommand from './serveCommand';
import runCommand from './runCommand';
import webpackConfig from './webpackConfig';
import { coalesce } from '../utils/misc';

module.exports = (api, options) => {
const expressOptions = (options.pluginOptions && options.pluginOptions.express) || {};
const shouldServeApp = expressOptions.shouldServeApp || config.shouldServeApp;
const shouldServeApp = coalesce(expressOptions.shouldServeApp, config.shouldServeApp);
const isInProduction = process.env.NODE_ENV === 'production';
const srvPath = api.resolve(expressOptions.serverDir || config.serverDir);
const distPath = api.resolve(options.outputDir);
Expand Down
16 changes: 13 additions & 3 deletions src/utils/logSuccessLaunch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@ export default ({ urls, routes, isInProduction, shouldServeApp }) => {
console.log();
if (isInProduction) {
console.log(` 📦 You're in ${chalk.bold('production')} mode. To build the application, run ${cmd('build')}.`);
console.log();
if (shouldServeApp) {
console.log(` 🎉 Fallback to the app enabled: ${chalk.bold('your application is served!')}`);
} else {
console.log(` ⚠️ Fallback to the app disabled: ${chalk.bold('your application is not served!')}`);
}
} else {
console.log(` ⚙ You're in ${chalk.bold('development')} mode. to start the application, run ${cmd('serve')}.`);
console.log();
if (shouldServeApp) {
console.log(' 🎉 Fallback to this server enabled: ' + chalk.bold('you can use relative routes in your code!'));
console.log(` 🎉 Fallback to this server enabled: ${chalk.bold('you can use relative routes in your code!')}`);
} else {
console.log(` ⚠️ Fallback to this server disabled: ${chalk.bold('you cannot use relative routes in your code!')}`);
}
}

console.log();
console.log(' 🔀 Express routes found:');
console.log(routesTable(routes));
if (routes.length) {
console.log(' 🔀 api routes found:');
console.log(routesTable(routes));
} else {
console.log(` 🔀 No api routes found${isInProduction ? '' : ' (yet?)'}.`);
}
console.log();
};
6 changes: 0 additions & 6 deletions src/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,3 @@ export const coalesce = (...args) => {

return null;
};

export const load = file => {
const module = require(file);

return module.default || module;
};
4 changes: 3 additions & 1 deletion src/utils/serverUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export default {
fs.writeFileSync(tmpPath, url);
},
deleteFile () {
fs.unlinkSync(tmpPath);
if (this.isSet()) {
fs.unlinkSync(tmpPath);
}
},

async findServerUrl (args, defaults) {
Expand Down

0 comments on commit 16e0a11

Please sign in to comment.