Skip to content

Commit 8dedc56

Browse files
committed
feat(cli): add start message with local and network url
1 parent bcbfae3 commit 8dedc56

19 files changed

+215
-46
lines changed

.changeset/kind-bats-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket/engine': patch
3+
---
4+
5+
Add `engine.getVersion()` method

.changeset/thirty-scissors-jam.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@rocket/cli': patch
3+
---
4+
5+
Add start message for `rocket start`
6+
7+
```
8+
🚀 Rocket Engine v0.2.5
9+
10+
🚧 Local: http://localhost:8000/
11+
🌐 Network: http://xxx.xxx.xxx.xxx:8000/
12+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"start:experimental": "NODE_DEBUG=engine:rendering node --no-warnings --experimental-loader ./packages/engine/src/litCssLoader.js packages/cli/src/cli.js start --open",
3737
"start": "NODE_DEBUG=engine:rendering node --trace-warnings packages/cli/src/cli.js start --open",
3838
"test": "yarn test:node && yarn test:web",
39-
"test:integration": "playwright test packages/*/test-node/*.spec.js",
39+
"test:integration": "playwright test packages/*/test-node/*.spec.js --retries=3",
4040
"test:node": "yarn test:unit && yarn test:integration",
4141
"test:unit": "node --trace-warnings ./node_modules/.bin/mocha --require ./scripts/testMochaGlobalHooks.js \"packages/*/test-node/**/*.test.{ts,js,mjs,cjs}\" -- --timeout 8000 --reporter dot --exit",
4242
"test:web": "web-test-runner",

packages/cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@
5959
"commander": "^9.0.0",
6060
"fs-extra": "^9.0.1",
6161
"gray-matter": "^4.0.3",
62+
"ip": "^1.1.5",
6263
"plugins-manager": "^0.3.0",
6364
"puppeteer": "^13.0.0"
6465
},
6566
"devDependencies": {
67+
"@types/ip": "^1.1.0",
6668
"koa-proxy": "^1.0.0-alpha.3"
6769
},
6870
"types": "./dist-types/src/index.d.ts",

packages/cli/src/RocketStart.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fromRollup } from '@web/dev-server-rollup';
22

33
import { Engine } from '@rocket/engine/server';
4+
import { logStartMessage } from './start/logStartMessage.js';
45

56
export class RocketStart {
67
/** @type {Engine | undefined} */
@@ -60,7 +61,14 @@ export class RocketStart {
6061
setupDevServerPlugins: [...this.cli.options.setupDevServerPlugins, ...withWrap],
6162
});
6263
try {
63-
console.log('🚀 Engines online');
64+
this.engine.events.on('devServerStarted', () => {
65+
if (this.engine?.devServer) {
66+
logStartMessage(
67+
{ devServerOptions: this.engine.devServer?.config, engine: this.engine },
68+
console,
69+
);
70+
}
71+
});
6472
await this.engine.start();
6573
} catch (e) {
6674
console.log('Engine start errored');
@@ -71,7 +79,6 @@ export class RocketStart {
7179
async stop({ hard = true } = {}) {
7280
if (this.engine) {
7381
await this.engine.stop({ hard });
74-
console.log('🚀 Engines offline');
7582
}
7683
}
7784
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import ip from 'ip';
2+
import { white, bold, cyan, gray } from 'colorette';
3+
4+
/** @typedef {import('@web/dev-server').DevServerConfig} DevServerConfig */
5+
6+
/**
7+
*
8+
* @param {DevServerConfig} devServerOptions
9+
* @param {string} host
10+
* @param {string} path
11+
* @returns {string}
12+
*/
13+
function createAddress(devServerOptions, host, path) {
14+
return `http${devServerOptions.http2 ? 's' : ''}://${host}:${devServerOptions.port}${path}`;
15+
}
16+
17+
/**
18+
*
19+
* @param {DevServerConfig} devServerOptions
20+
* @param {console} logger
21+
* @param {string} openPath
22+
*/
23+
function logNetworkAddress(devServerOptions, logger, openPath) {
24+
try {
25+
const address = ip.address();
26+
if (typeof address === 'string') {
27+
logger.log(
28+
`${white(' 🌐 Network:')} ${cyan(createAddress(devServerOptions, address, openPath))}`,
29+
);
30+
}
31+
} catch (_a) {
32+
//
33+
}
34+
}
35+
36+
/**
37+
* @param {{ devServerOptions: DevServerConfig, engine: import('@rocket/engine/server').Engine}} options
38+
* @param {console} logger
39+
*/
40+
export function logStartMessage({ devServerOptions, engine }, logger) {
41+
const prettyHost = devServerOptions.hostname ?? 'localhost';
42+
let openPath = typeof devServerOptions.open === 'string' ? devServerOptions.open : '/';
43+
if (!openPath.startsWith('/')) {
44+
openPath = `/${openPath}`;
45+
}
46+
47+
logger.log(`${bold(`🚀 Rocket Engine`)} ${gray(`v${engine.getVersion()}`)}`);
48+
logger.log('');
49+
logger.log(
50+
`${white(' 🚧 Local:')} ${cyan(createAddress(devServerOptions, prettyHost, openPath))}`,
51+
);
52+
logNetworkAddress(devServerOptions, logger, openPath);
53+
logger.log('');
54+
}

packages/cli/test-helpers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { setupTestCli } from './test-helpers.js';
55

66
export function prepareTestCli(importMetaUrl) {
77
const dir = path.dirname(fileURLToPath(importMetaUrl));
8-
return (cwd, cliOptions = ['build'], options = {}) => setupTestCli(cwd, cliOptions, options, dir);
8+
return fullOptions => setupTestCli({ dir, ...fullOptions });
99
}
1010

1111
const { expect } = chai;

packages/cli/test-helpers/test-helpers.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ function cleanupLitMarkersFn(text) {
6060
return newText;
6161
}
6262

63-
export async function setupTestCli(cwd, cliOptions = ['build'], options = {}, dir) {
63+
export async function setupTestCli({
64+
cwd,
65+
cliOptions = ['build'],
66+
options = {},
67+
testOptions = {},
68+
dir,
69+
}) {
6470
const resolvedCwd = path.join(dir, cwd.split('/').join(path.sep));
6571
const useOptions = { buildOptimize: false, buildAutoStop: false, ...options, cwd: resolvedCwd };
6672
if (useOptions.inputDir) {
@@ -69,6 +75,18 @@ export async function setupTestCli(cwd, cliOptions = ['build'], options = {}, di
6975
useOptions.outputDir = path.join(resolvedCwd, '__output');
7076
useOptions.outputDevDir = path.join(resolvedCwd, '__output-dev');
7177

78+
const capturedLogs = [];
79+
const origLog = console.log;
80+
const origError = console.error;
81+
if (testOptions.captureLogs) {
82+
console.log = msg => {
83+
capturedLogs.push(msg);
84+
};
85+
console.error = msg => {
86+
capturedLogs.push(msg);
87+
};
88+
}
89+
7290
const cli = new RocketCli({
7391
argv: [process.argv[0], new URL('../src/cli.js', import.meta.url).pathname, ...cliOptions],
7492
});
@@ -184,6 +202,10 @@ export async function setupTestCli(cwd, cliOptions = ['build'], options = {}, di
184202

185203
async function cleanup() {
186204
await cli.stop({ hard: false });
205+
if (testOptions.captureLogs) {
206+
console.log = origLog;
207+
console.error = origError;
208+
}
187209
}
188210

189211
async function build() {
@@ -243,5 +265,6 @@ export async function setupTestCli(cwd, cliOptions = ['build'], options = {}, di
243265
renameSource,
244266
backupOrRestoreSource,
245267
restoreSource,
268+
capturedLogs,
246269
};
247270
}

packages/cli/test-node/01-config.test.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const { expect } = chai;
88

99
describe('Config', () => {
1010
it('01: no config file', async () => {
11-
const { build, readOutput, readDevOutput } = await setupTestCli(
12-
'fixtures/01-config/01-no-config/',
13-
undefined,
14-
{ buildOptimize: true },
15-
);
11+
const { build, readOutput, readDevOutput } = await setupTestCli({
12+
cwd: 'fixtures/01-config/01-no-config/',
13+
options: { buildOptimize: true },
14+
testOptions: { captureLogs: true },
15+
});
1616
await build();
1717

1818
expect(readOutput('index.html')).to.equal(
@@ -31,14 +31,21 @@ describe('Config', () => {
3131
});
3232

3333
it('02: change input dir', async () => {
34-
const { build, readDevOutput } = await setupTestCli('fixtures/01-config/02-change-input-dir/');
34+
const { build, readDevOutput } = await setupTestCli({
35+
cwd: 'fixtures/01-config/02-change-input-dir/',
36+
testOptions: { captureLogs: true },
37+
});
3538
await build();
3639

3740
expect(readDevOutput('index.html')).to.equal(['Hello World!'].join('\n'));
3841
});
3942

4043
it('03: can add a middleware (api proxy) to the dev server', async () => {
41-
const { cleanup, cli } = await setupTestCli('fixtures/01-config/03-add-middleware/', ['start']);
44+
const { cleanup, cli } = await setupTestCli({
45+
cwd: 'fixtures/01-config/03-add-middleware/',
46+
cliOptions: ['start'],
47+
testOptions: { captureLogs: true },
48+
});
4249
const apiServer = http.createServer((request, response) => {
4350
if (request.url === '/api/message') {
4451
response.writeHead(200);
@@ -61,20 +68,22 @@ describe('Config', () => {
6168
});
6269

6370
it('04: can add a rollup plugin via setupDevServerAndBuildPlugins to build', async () => {
64-
const { build, readOutput } = await setupTestCli(
65-
'fixtures/01-config/04-add-rollup-plugin/',
66-
undefined,
67-
{ buildOptimize: true },
68-
);
71+
const { build, readOutput } = await setupTestCli({
72+
cwd: 'fixtures/01-config/04-add-rollup-plugin/',
73+
options: { buildOptimize: true },
74+
testOptions: { captureLogs: true },
75+
});
6976
await build();
7077
const inlineModule = await readOutput('e97af63d.js', { format: false });
7178
expect(inlineModule).to.equal('var a={test:"data"};console.log(a);\n');
7279
});
7380

7481
it('04a: can add a rollup plugin via setupDevServerAndBuildPlugins to start', async () => {
75-
const { cli, cleanup } = await setupTestCli('fixtures/01-config/04-add-rollup-plugin/', [
76-
'start',
77-
]);
82+
const { cli, cleanup } = await setupTestCli({
83+
cwd: 'fixtures/01-config/04-add-rollup-plugin/',
84+
cliOptions: ['start'],
85+
testOptions: { captureLogs: true },
86+
});
7887
await cli.start();
7988
const { port } = cli?.activePlugin?.engine.devServer.config;
8089

@@ -88,9 +97,10 @@ describe('Config', () => {
8897
});
8998

9099
it('05: long file header comments', async () => {
91-
const { build, readSource } = await setupTestCli(
92-
'fixtures/01-config/05-long-file-header-comment/',
93-
);
100+
const { build, readSource } = await setupTestCli({
101+
cwd: 'fixtures/01-config/05-long-file-header-comment/',
102+
testOptions: { captureLogs: true },
103+
});
94104
await build();
95105

96106
expect(readSource('index.rocket.js', { format: false })).to.equal(

packages/cli/test-node/02-build.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const { expect } = chai;
55

66
describe('Build', () => {
77
it('01: copy public files', async () => {
8-
const { build, readOutput, outputExists, readDevOutput } = await setupTestCli(
9-
'fixtures/02-build/01-copy-public-files/',
10-
undefined,
11-
{
8+
const { build, readOutput, outputExists, readDevOutput } = await setupTestCli({
9+
cwd: 'fixtures/02-build/01-copy-public-files/',
10+
options: {
1211
buildOptimize: true,
1312
},
14-
);
13+
testOptions: { captureLogs: true },
14+
});
1515
await build();
1616

1717
expect(readOutput('index.html')).to.equal(

packages/cli/test-node/03-upgrade.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const { expect } = chai;
66
describe('Upgrade System', () => {
77
it('2021-09-menu', async () => {
88
const { build, sourceExists, readSource, backupOrRestoreSource, restoreSource } =
9-
await setupTestCli('fixtures/03-upgrade/2022-03-menu', ['upgrade']);
9+
await setupTestCli({
10+
cwd: 'fixtures/03-upgrade/2022-03-menu',
11+
cliOptions: ['upgrade'],
12+
testOptions: { captureLogs: true },
13+
});
1014
await backupOrRestoreSource();
1115

1216
await build();

packages/cli/test-node/04-open-graph.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const { expect } = chai;
55

66
describe('Open Graph', () => {
77
it('generates the image and adds the meta tags', async () => {
8-
const { build, readOutput, outputExists } = await setupTestCli(
9-
'fixtures/04-open-graph/01-generate-image-and-inject-meta',
10-
undefined,
11-
{
8+
const { build, readOutput, outputExists } = await setupTestCli({
9+
cwd: 'fixtures/04-open-graph/01-generate-image-and-inject-meta',
10+
options: {
1211
buildOptimize: true,
1312
},
14-
);
13+
testOptions: { captureLogs: true },
14+
});
1515
await build();
1616

1717
expect(readOutput('index.html', { replaceImageHashes: true })).to.equal(
@@ -35,13 +35,13 @@ describe('Open Graph', () => {
3535
});
3636

3737
it('handles multiple pages', async () => {
38-
const { build, readOutput } = await setupTestCli(
39-
'fixtures/04-open-graph/02-multiple-pages',
40-
undefined,
41-
{
38+
const { build, readOutput } = await setupTestCli({
39+
cwd: 'fixtures/04-open-graph/02-multiple-pages',
40+
options: {
4241
buildOptimize: true,
4342
},
44-
);
43+
testOptions: { captureLogs: true },
44+
});
4545
await build();
4646

4747
expect(readOutput('index.html', { replaceImageHashes: true })).to.equal(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import chai from 'chai';
2+
import { white, bold } from 'colorette';
3+
4+
import { setupTestCli } from './test-helpers.js';
5+
6+
const { expect } = chai;
7+
8+
describe('Start', () => {
9+
it('Start Message', async () => {
10+
const { cli, capturedLogs, cleanup } = await setupTestCli({
11+
cwd: 'fixtures/05-start/01-start-message',
12+
cliOptions: ['start'],
13+
testOptions: { captureLogs: true },
14+
});
15+
16+
await cli.start();
17+
await cleanup();
18+
19+
expect(capturedLogs[0].startsWith(`${bold(`🚀 Rocket Engine`)} `)).to.be.true;
20+
expect(capturedLogs[1]).to.equal('');
21+
expect(capturedLogs[2].startsWith(`${white(' 🚧 Local:')}`)).to.be.true;
22+
expect(capturedLogs[3].startsWith(`${white(' 🌐 Network:')}`)).to.be.true;
23+
expect(capturedLogs[4]).to.equal('');
24+
});
25+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* START - Rocket auto generated - do not touch */
2+
export const sourceRelativeFilePath = 'index.rocket.js';
3+
/* END - Rocket auto generated - do not touch */
4+
5+
export default () => 'Hello World!';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "index.rocket.js",
3+
"menuLinkText": "index.rocket.js",
4+
"url": "/",
5+
"outputRelativeFilePath": "index.html",
6+
"sourceRelativeFilePath": "index.rocket.js",
7+
"level": 0
8+
}

packages/engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"debug": "DEBUG=engine:rendering yarn test",
3737
"debug:integration": "PWDEBUG=1 yarn test:integration",
3838
"test": "mocha --require ../../scripts/testMochaGlobalHooks.js --timeout 8000 test-node/**/*.test.js test-node/*.test.js",
39-
"test:integration": "playwright test test-node/*.spec.js",
39+
"test:integration": "playwright test test-node/*.spec.js --retries=3",
4040
"test:watch": "onchange 'src/**/*.js' 'test-node/**/*.js' -- npm test",
4141
"types:copy": "copyfiles \"./types/**/*.d.ts\" dist-types/"
4242
},

0 commit comments

Comments
 (0)