@merkur/cli 0.35.0
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/LICENSE +8 -0
- package/README.md +47 -0
- package/bin/merkur.mjs +61 -0
- package/lib/devClient.mjs +1 -0
- package/lib/index.cjs +7 -0
- package/lib/index.mjs +7 -0
- package/package.json +59 -0
- package/src/CLIConfig.mjs +34 -0
- package/src/buildConfig.mjs +108 -0
- package/src/commands/build.mjs +61 -0
- package/src/commands/constant.mjs +6 -0
- package/src/commands/dev.mjs +59 -0
- package/src/commands/start.mjs +21 -0
- package/src/commands/test.mjs +42 -0
- package/src/context.mjs +17 -0
- package/src/devClient/WebSocketClient.mjs +79 -0
- package/src/devClient/hmr.mjs +92 -0
- package/src/devClient/index.mjs +26 -0
- package/src/devClient/reload.mjs +5 -0
- package/src/devServer.mjs +142 -0
- package/src/emitter.mjs +13 -0
- package/src/handleExit.mjs +33 -0
- package/src/index.mjs +62 -0
- package/src/logger.mjs +77 -0
- package/src/merkurConfig.mjs +252 -0
- package/src/plugins/devPlugin.mjs +97 -0
- package/src/plugins/memoryStaticPlugin.mjs +84 -0
- package/src/plugins/metaPlugin.mjs +53 -0
- package/src/runTask.mjs +21 -0
- package/src/taskConfig.mjs +23 -0
- package/src/templates/footer.ejs +0 -0
- package/src/templates/head.ejs +20 -0
- package/src/templates/playground.ejs +29 -0
- package/src/utils.mjs +33 -0
- package/src/websocket.mjs +41 -0
- package/src/widgetServer.mjs +45 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import WebSocket, { WebSocketServer } from 'ws';
|
|
2
|
+
|
|
3
|
+
function broadcastMessage(server, fromClient, data) {
|
|
4
|
+
server.clients.forEach(function each(toClient) {
|
|
5
|
+
sendMessage(fromClient, toClient, data);
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function sendMessage(fromClient, toClient, data) {
|
|
10
|
+
if (toClient !== fromClient && toClient.readyState === WebSocket.OPEN) {
|
|
11
|
+
toClient.send(data);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function runSocketServer({ merkurConfig, context }) {
|
|
16
|
+
try {
|
|
17
|
+
const server = new WebSocketServer({
|
|
18
|
+
port: merkurConfig.socketServer.port,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
server.on('connection', function connection(ws) {
|
|
22
|
+
ws.on('message', function incomingMessage(data, isBinary) {
|
|
23
|
+
data = isBinary ? data : data.toString();
|
|
24
|
+
broadcastMessage(server, ws, data);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
context.server.socketServer = server;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Unable to create socket server on port ${merkurConfig.socketServer.port}.`,
|
|
32
|
+
{ cause: error },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createClient({ merkurConfig }) {
|
|
38
|
+
const { protocol, host } = merkurConfig.socketServer;
|
|
39
|
+
|
|
40
|
+
return new WebSocket(`${protocol}//${host}`);
|
|
41
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
|
|
6
|
+
import { createLogger } from './logger.mjs';
|
|
7
|
+
|
|
8
|
+
export async function runWidgetServer({ merkurConfig, cliConfig, context }) {
|
|
9
|
+
const logger = createLogger('widgetServer', cliConfig);
|
|
10
|
+
const { watch, inspect } = cliConfig;
|
|
11
|
+
const { protocol, host } = merkurConfig.widgetServer;
|
|
12
|
+
|
|
13
|
+
const args = [`./server/server.js`];
|
|
14
|
+
|
|
15
|
+
if (watch) {
|
|
16
|
+
args.unshift(`--watch-path=./server/`);
|
|
17
|
+
args.unshift(`--watch-path=./node_modules/`);
|
|
18
|
+
args.unshift(`--watch-preserve-output`);
|
|
19
|
+
args.unshift('--watch');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (inspect) {
|
|
23
|
+
args.unshift('--inspect');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (watch) logger.debug('Starting widget server with watch mode.');
|
|
27
|
+
const server = spawn('node', args, {
|
|
28
|
+
env: {
|
|
29
|
+
NODE_CONFIG_DIR: './server/config',
|
|
30
|
+
NODE_WATCH: watch,
|
|
31
|
+
...process.env,
|
|
32
|
+
MERKUR_CONFIG: JSON.stringify(merkurConfig),
|
|
33
|
+
CLI_CONFIG: JSON.stringify(cliConfig),
|
|
34
|
+
},
|
|
35
|
+
stdio: 'inherit',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
server.on('spawn', () => {
|
|
39
|
+
logger.info(
|
|
40
|
+
`Widget API endpoint: ${chalk.green(`${protocol}//${host}/widget`)}`,
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
context.process.widgetServer = server;
|
|
45
|
+
}
|