diff --git a/src/generator/index.js b/src/generator/index.js index 667b467..d8922a6 100644 --- a/src/generator/index.js +++ b/src/generator/index.js @@ -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 } }; + diff --git a/src/generator/templates/srv/index.js b/src/generator/templates/srv/index.js index 77b3444..f5202f8 100644 --- a/src/generator/templates/srv/index.js +++ b/src/generator/templates/srv/index.js @@ -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); + // }); } diff --git a/src/prompts/index.js b/src/prompts/index.js index 0783d95..0f69531 100644 --- a/src/prompts/index.js +++ b/src/prompts/index.js @@ -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, }, diff --git a/src/server.js b/src/server.js index 8f5e176..d8dbead 100644 --- a/src/server.js +++ b/src/server.js @@ -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 ({ @@ -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()); @@ -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; + } +} diff --git a/src/servicePlugin/index.js b/src/servicePlugin/index.js index d6a6d99..612d7a7 100644 --- a/src/servicePlugin/index.js +++ b/src/servicePlugin/index.js @@ -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); diff --git a/src/utils/logSuccessLaunch.js b/src/utils/logSuccessLaunch.js index 5e29005..3439aab 100644 --- a/src/utils/logSuccessLaunch.js +++ b/src/utils/logSuccessLaunch.js @@ -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(); }; diff --git a/src/utils/misc.js b/src/utils/misc.js index 8581c7a..1187585 100644 --- a/src/utils/misc.js +++ b/src/utils/misc.js @@ -7,9 +7,3 @@ export const coalesce = (...args) => { return null; }; - -export const load = file => { - const module = require(file); - - return module.default || module; -}; diff --git a/src/utils/serverUrl.js b/src/utils/serverUrl.js index 8d0906f..e9ddc8f 100644 --- a/src/utils/serverUrl.js +++ b/src/utils/serverUrl.js @@ -17,7 +17,9 @@ export default { fs.writeFileSync(tmpPath, url); }, deleteFile () { - fs.unlinkSync(tmpPath); + if (this.isSet()) { + fs.unlinkSync(tmpPath); + } }, async findServerUrl (args, defaults) {