Skip to content

Commit 16e0a11

Browse files
committed
🐛💄 Fix bug in generator and server and increase DX.
1 parent 750d5d6 commit 16e0a11

File tree

8 files changed

+53
-27
lines changed

8 files changed

+53
-27
lines changed

src/generator/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ module.exports = (api, options, rootOptions) => {
1414
},
1515
});
1616

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

1919
if (options.addExamples) {
2020
// TODO
2121
}
2222
};
23+
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import express from 'express';
22

33
export default app => {
4-
app.use(express.json());
5-
6-
app.get('/foo', (req, res) => {
7-
res.json({msg: 'foo'});
8-
});
9-
10-
app.post('/bar', (req, res) => {
11-
res.json(req.body);
12-
});
4+
// app.use(express.json());
5+
//
6+
// app.get('/foo', (req, res) => {
7+
// res.json({msg: 'foo'});
8+
// });
9+
//
10+
// app.post('/bar', (req, res) => {
11+
// res.json(req.body);
12+
// });
1313
}

src/prompts/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ module.exports = [
44
{
55
type: 'confirm',
66
name: 'shouldServeApp',
7-
message: `Should serve vue app? (default: ${shouldServeApp})`,
7+
message: `Should serve vue app?`,
88
description: 'This will allow you to serve the vue app via the express server. So only one server for the app and the api.',
99
default: shouldServeApp,
1010
},
1111
{
1212
type: 'input',
1313
name: 'serverDir',
14-
message: `Where will be located your server? (default: "${serverDir}")`,
14+
message: `Where will be located your server?`,
1515
description: 'The location of your server code, relative to the root of your project.',
1616
default: serverDir,
1717
},

src/server.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import express from 'express';
2-
import { load } from './utils/misc';
32
import listEndpoints from 'express-list-endpoints';
4-
53
import history from 'connect-history-api-fallback';
64

75
export default ({
@@ -19,7 +17,9 @@ export default ({
1917
require('ts-node/register/transpile-only');
2018
}
2119

22-
load(srvPath)(app);
20+
const server = loadServer(srvPath);
21+
22+
server(app);
2323

2424
if (isInProduction && shouldServeApp) {
2525
app.use(history());
@@ -30,8 +30,26 @@ export default ({
3030
if (err) {
3131
reject(err);
3232
} else {
33-
resolve(listEndpoints(app));
33+
resolve(getAppEndpoints(app));
3434
}
3535
});
3636
});
3737
};
38+
39+
function getAppEndpoints (app) {
40+
try {
41+
return listEndpoints(app);
42+
} catch (e) {
43+
return [];
44+
}
45+
}
46+
47+
function loadServer (file) {
48+
const empty = () => {};
49+
try {
50+
const module = require(file);
51+
return module.default || module || empty;
52+
} catch (e) {
53+
return empty;
54+
}
55+
}

src/servicePlugin/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import config from '../config';
22
import serveCommand from './serveCommand';
33
import runCommand from './runCommand';
44
import webpackConfig from './webpackConfig';
5+
import { coalesce } from '../utils/misc';
56

67
module.exports = (api, options) => {
78
const expressOptions = (options.pluginOptions && options.pluginOptions.express) || {};
8-
const shouldServeApp = expressOptions.shouldServeApp || config.shouldServeApp;
9+
const shouldServeApp = coalesce(expressOptions.shouldServeApp, config.shouldServeApp);
910
const isInProduction = process.env.NODE_ENV === 'production';
1011
const srvPath = api.resolve(expressOptions.serverDir || config.serverDir);
1112
const distPath = api.resolve(options.outputDir);

src/utils/logSuccessLaunch.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@ export default ({ urls, routes, isInProduction, shouldServeApp }) => {
1717
console.log();
1818
if (isInProduction) {
1919
console.log(` 📦 You're in ${chalk.bold('production')} mode. To build the application, run ${cmd('build')}.`);
20+
console.log();
2021
if (shouldServeApp) {
2122
console.log(` 🎉 Fallback to the app enabled: ${chalk.bold('your application is served!')}`);
23+
} else {
24+
console.log(` ⚠️ Fallback to the app disabled: ${chalk.bold('your application is not served!')}`);
2225
}
2326
} else {
2427
console.log(` ⚙ You're in ${chalk.bold('development')} mode. to start the application, run ${cmd('serve')}.`);
28+
console.log();
2529
if (shouldServeApp) {
26-
console.log(' 🎉 Fallback to this server enabled: ' + chalk.bold('you can use relative routes in your code!'));
30+
console.log(` 🎉 Fallback to this server enabled: ${chalk.bold('you can use relative routes in your code!')}`);
31+
} else {
32+
console.log(` ⚠️ Fallback to this server disabled: ${chalk.bold('you cannot use relative routes in your code!')}`);
2733
}
2834
}
2935

3036
console.log();
31-
console.log(' 🔀 Express routes found:');
32-
console.log(routesTable(routes));
37+
if (routes.length) {
38+
console.log(' 🔀 api routes found:');
39+
console.log(routesTable(routes));
40+
} else {
41+
console.log(` 🔀 No api routes found${isInProduction ? '' : ' (yet?)'}.`);
42+
}
3343
console.log();
3444
};

src/utils/misc.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,3 @@ export const coalesce = (...args) => {
77

88
return null;
99
};
10-
11-
export const load = file => {
12-
const module = require(file);
13-
14-
return module.default || module;
15-
};

src/utils/serverUrl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default {
1717
fs.writeFileSync(tmpPath, url);
1818
},
1919
deleteFile () {
20-
fs.unlinkSync(tmpPath);
20+
if (this.isSet()) {
21+
fs.unlinkSync(tmpPath);
22+
}
2123
},
2224

2325
async findServerUrl (args, defaults) {

0 commit comments

Comments
 (0)