@nitro/app 11.0.0-beta.1 → 11.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/core/config.js +1 -1
- package/app/core/listen.js +33 -13
- package/app/scripts/server.js +6 -1
- package/app/scripts/util/cliOpen.js +35 -0
- package/bin/serve.js +25 -0
- package/package.json +2 -1
package/app/core/config.js
CHANGED
|
@@ -39,6 +39,7 @@ const defaultConfig = {
|
|
|
39
39
|
},
|
|
40
40
|
server: {
|
|
41
41
|
port: 8080,
|
|
42
|
+
host: 'localhost',
|
|
42
43
|
production: !!(process.env.NODE_ENV && process.env.NODE_ENV.replace(/\s/g, '') === 'production'),
|
|
43
44
|
compression: true,
|
|
44
45
|
projectPaths: [
|
|
@@ -46,7 +47,6 @@ const defaultConfig = {
|
|
|
46
47
|
'project/helpers',
|
|
47
48
|
'project/locales',
|
|
48
49
|
'project/routes',
|
|
49
|
-
'project/server',
|
|
50
50
|
'project/viewData',
|
|
51
51
|
'public',
|
|
52
52
|
'src/views',
|
package/app/core/listen.js
CHANGED
|
@@ -5,18 +5,38 @@ const mode = config.get('server.production') ? 'production' : 'development';
|
|
|
5
5
|
const port = config.get('server.port');
|
|
6
6
|
|
|
7
7
|
/* eslint-disable no-console */
|
|
8
|
-
module.exports = function (app) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
console.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
module.exports = function (app, opts = {}) {
|
|
9
|
+
const rawHost = (config.has('server.host')) ? config.get('server.host') : 'localhost';
|
|
10
|
+
const host = ['0.0.0.0', '::', '::0'].includes(String(rawHost)) ? 'localhost' : rawHost;
|
|
11
|
+
const url = `http://${host}:${port}`;
|
|
12
|
+
const openBrowser = opts.open; // true | false | string (URL)
|
|
13
|
+
|
|
14
|
+
const server = app
|
|
15
|
+
.listen(port, () => {
|
|
16
|
+
console.log('-------------------------------------------------------------------');
|
|
17
|
+
console.log('Nitro listening on %s in %s mode', url, mode);
|
|
18
|
+
console.log('-------------------------------------------------------------------');
|
|
19
|
+
|
|
20
|
+
if (openBrowser) {
|
|
21
|
+
const openUrl = typeof openBrowser === 'string' ? openBrowser : url;
|
|
22
|
+
(async () => {
|
|
23
|
+
try {
|
|
24
|
+
const mod = await import('open');
|
|
25
|
+
const open = mod.default;
|
|
26
|
+
await open(openUrl, { wait: false });
|
|
27
|
+
} catch (e) { /* empty */}
|
|
28
|
+
})();
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
.on('error', (err) => {
|
|
32
|
+
if (err && err.errno === 'EADDRINUSE') {
|
|
33
|
+
console.error('Port *:%s already in use.', port);
|
|
34
|
+
} else {
|
|
35
|
+
console.error(err);
|
|
36
|
+
}
|
|
37
|
+
process.exit(1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return server;
|
|
21
41
|
};
|
|
22
42
|
/* eslint-enable no-console */
|
package/app/scripts/server.js
CHANGED
|
@@ -7,9 +7,14 @@ const router = require('../core/router');
|
|
|
7
7
|
const compression = require('compression');
|
|
8
8
|
const bodyParser = require('body-parser');
|
|
9
9
|
|
|
10
|
+
// CLI flags (only --open[=URL] or --open URL)
|
|
11
|
+
const { parseOpenArg } = require('./util/cliOpen');
|
|
12
|
+
const { open } = parseOpenArg(process.argv.slice(2));
|
|
13
|
+
|
|
10
14
|
const isProduction = config.get('server.production');
|
|
11
15
|
const useCompression = config.get('server.compression');
|
|
12
16
|
const isTwig = config.get('nitro.templateEngine') === 'twig';
|
|
17
|
+
|
|
13
18
|
let engine;
|
|
14
19
|
|
|
15
20
|
// webpack
|
|
@@ -47,4 +52,4 @@ if (isTwig) {
|
|
|
47
52
|
app.engine(config.get('nitro.viewFileExtension'), engine.__express);
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
require('../core/listen')(app);
|
|
55
|
+
require('../core/listen')(app, { open });
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function strip(s) {
|
|
4
|
+
return s.replace(/^['"]|['"]$/g, '');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function parseOpenArg(args) {
|
|
8
|
+
for (let i = 0; i < args.length; i++) {
|
|
9
|
+
const a = args[i];
|
|
10
|
+
|
|
11
|
+
// case 1: "--open"
|
|
12
|
+
if (a === '--open') {
|
|
13
|
+
const next = args[i + 1];
|
|
14
|
+
|
|
15
|
+
// case 2: "--open http://somehost"
|
|
16
|
+
if (next && !next.startsWith('-')) {
|
|
17
|
+
return { open: strip(next) };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return { open: true };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// case 3: "--open=http://somehost"
|
|
24
|
+
if (a.startsWith('--open=')) {
|
|
25
|
+
const url = a.slice('--open='.length).trim();
|
|
26
|
+
return { open: strip(url) };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return { open: false };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
parseOpenArg
|
|
35
|
+
};
|
package/bin/serve.js
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* CLI flag `--open` opens a browser after starting the server
|
|
5
|
+
* --open
|
|
6
|
+
* --open <url>
|
|
7
|
+
* --open=<url>
|
|
8
|
+
*
|
|
9
|
+
* If the flag is provided without a URL, the URL to open is computed by `../app/core/listen`.
|
|
10
|
+
* If a URL is provided, it overrides the computed URL.
|
|
11
|
+
*
|
|
12
|
+
* Examples
|
|
13
|
+
*
|
|
14
|
+
* # 1) start nitro server
|
|
15
|
+
* nitro-app-serve
|
|
16
|
+
*
|
|
17
|
+
* # 2) ... and opens computed URL in browser
|
|
18
|
+
* nitro-app-serve --open
|
|
19
|
+
*
|
|
20
|
+
* # 3) ... and opens exact URL in browser
|
|
21
|
+
* nitro-app-serve --open http://localhost:5050/styleguide
|
|
22
|
+
*
|
|
23
|
+
* # 4) also valid
|
|
24
|
+
* nitro-app-serve --open=https://local:3000/documentation
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
|
|
3
28
|
require('../app/scripts/server.js');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nitro/app",
|
|
3
|
-
"version": "11.0.0-beta.
|
|
3
|
+
"version": "11.0.0-beta.2",
|
|
4
4
|
"description": "Nitro server",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"i18next-sprintf-postprocessor": "0.2.2",
|
|
52
52
|
"jasmine": "6.0.0",
|
|
53
53
|
"lodash": "4.17.23",
|
|
54
|
+
"open": "11.0.0",
|
|
54
55
|
"twig": "1.13.3",
|
|
55
56
|
"webpack-dev-middleware": "7.4.5",
|
|
56
57
|
"webpack-hot-middleware": "2.26.1"
|