@asyncapi/cli 3.4.0 → 3.4.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.
@@ -16,111 +16,125 @@ const picocolors_1 = require("picocolors");
16
16
  const { readFile, writeFile } = fs_1.promises;
17
17
  const sockets = [];
18
18
  const messageQueue = [];
19
- exports.DEFAULT_PORT = 3210;
19
+ exports.DEFAULT_PORT = 0;
20
20
  function isValidFilePath(filePath) {
21
21
  return (0, fs_1.existsSync)(filePath);
22
22
  }
23
23
  // eslint-disable-next-line sonarjs/cognitive-complexity
24
- function start(filePath, port = exports.DEFAULT_PORT) {
25
- if (filePath && !isValidFilePath(filePath)) {
26
- throw new specification_file_1.SpecificationFileNotFound(filePath);
27
- }
28
- // Locate @asyncapi/studio package
29
- const studioPath = path_1.default.dirname(require.resolve('@asyncapi/studio/package.json'));
30
- const app = (0, next_1.default)({
31
- dev: false,
32
- dir: studioPath,
33
- conf: {
34
- distDir: 'build',
35
- },
36
- });
37
- const handle = app.getRequestHandler();
38
- const wsServer = new ws_1.WebSocketServer({ noServer: true });
39
- wsServer.on('connection', (socket) => {
40
- sockets.push(socket);
41
- if (filePath) {
42
- getFileContent(filePath).then((code) => {
24
+ function start(filePath_1) {
25
+ return tslib_1.__awaiter(this, arguments, void 0, function* (filePath, port = exports.DEFAULT_PORT) {
26
+ if (filePath && !isValidFilePath(filePath)) {
27
+ throw new specification_file_1.SpecificationFileNotFound(filePath);
28
+ }
29
+ // Locate @asyncapi/studio package
30
+ const studioPath = path_1.default.dirname(require.resolve('@asyncapi/studio/package.json'));
31
+ const app = (0, next_1.default)({
32
+ dev: false,
33
+ dir: studioPath,
34
+ conf: {
35
+ distDir: 'build',
36
+ },
37
+ });
38
+ const handle = app.getRequestHandler();
39
+ const wsServer = new ws_1.WebSocketServer({ noServer: true });
40
+ wsServer.on('connection', (socket) => {
41
+ sockets.push(socket);
42
+ if (filePath) {
43
+ getFileContent(filePath).then((code) => {
44
+ messageQueue.push(JSON.stringify({
45
+ type: 'file:loaded',
46
+ code,
47
+ }));
48
+ sendQueuedMessages();
49
+ });
50
+ }
51
+ else {
43
52
  messageQueue.push(JSON.stringify({
44
53
  type: 'file:loaded',
45
- code,
54
+ code: '',
46
55
  }));
47
56
  sendQueuedMessages();
48
- });
49
- }
50
- else {
51
- messageQueue.push(JSON.stringify({
52
- type: 'file:loaded',
53
- code: '',
54
- }));
55
- sendQueuedMessages();
56
- }
57
- socket.on('message', (event) => {
58
- try {
59
- const json = JSON.parse(event);
60
- if (filePath && json.type === 'file:update') {
61
- saveFileContent(filePath, json.code);
57
+ }
58
+ socket.on('message', (event) => {
59
+ try {
60
+ const json = JSON.parse(event);
61
+ if (filePath && json.type === 'file:update') {
62
+ saveFileContent(filePath, json.code);
63
+ }
64
+ else {
65
+ console.warn('Live Server: An unknown event has been received. See details:');
66
+ console.log(json);
67
+ }
62
68
  }
63
- else {
64
- console.warn('Live Server: An unknown event has been received. See details:');
65
- console.log(json);
69
+ catch (e) {
70
+ console.error(`Live Server: An invalid event has been received. See details:\n${event}`);
66
71
  }
67
- }
68
- catch (e) {
69
- console.error(`Live Server: An invalid event has been received. See details:\n${event}`);
70
- }
72
+ });
71
73
  });
72
- });
73
- wsServer.on('close', (socket) => {
74
- sockets.splice(sockets.findIndex((s) => s === socket));
75
- });
76
- app.prepare().then(() => {
77
- if (filePath) {
78
- chokidar_1.default.watch(filePath).on('all', (event, path) => {
79
- switch (event) {
80
- case 'add':
81
- case 'change':
82
- getFileContent(path).then((code) => {
74
+ wsServer.on('close', (socket) => {
75
+ sockets.splice(sockets.findIndex((s) => s === socket));
76
+ });
77
+ app.prepare().then(() => {
78
+ if (filePath) {
79
+ chokidar_1.default.watch(filePath).on('all', (event, path) => {
80
+ switch (event) {
81
+ case 'add':
82
+ case 'change':
83
+ getFileContent(path).then((code) => {
84
+ messageQueue.push(JSON.stringify({
85
+ type: 'file:changed',
86
+ code,
87
+ }));
88
+ sendQueuedMessages();
89
+ });
90
+ break;
91
+ case 'unlink':
83
92
  messageQueue.push(JSON.stringify({
84
- type: 'file:changed',
85
- code,
93
+ type: 'file:deleted',
94
+ filePath,
86
95
  }));
87
96
  sendQueuedMessages();
88
- });
89
- break;
90
- case 'unlink':
91
- messageQueue.push(JSON.stringify({
92
- type: 'file:deleted',
93
- filePath,
94
- }));
95
- sendQueuedMessages();
96
- break;
97
- }
98
- });
99
- }
100
- const server = (0, http_1.createServer)((req, res) => handle(req, res));
101
- server.on('upgrade', (request, socket, head) => {
102
- if (request.url === '/live-server') {
103
- console.log('🔗 WebSocket connection established.');
104
- wsServer.handleUpgrade(request, socket, head, (sock) => {
105
- wsServer.emit('connection', sock, request);
97
+ break;
98
+ }
106
99
  });
107
100
  }
108
- else {
109
- socket.destroy();
110
- }
111
- });
112
- server.listen(port, () => {
113
- const url = `http://localhost:${port}?liveServer=${port}&studio-version=${package_json_1.version}`;
114
- console.log(`🎉 Connected to Live Server running at ${(0, picocolors_1.blueBright)(url)}.`);
115
- console.log(`🌐 Open this URL in your web browser: ${(0, picocolors_1.blueBright)(url)}`);
116
- console.log(`🛑 If needed, press ${(0, picocolors_1.redBright)('Ctrl + C')} to stop the process.`);
117
- if (filePath) {
118
- console.log(`👁️ Watching changes on file ${(0, picocolors_1.blueBright)(filePath)}`);
119
- }
120
- else {
121
- console.warn('Warning: No file was provided, and we couldn\'t find a default file (like "asyncapi.yaml" or "asyncapi.json") in the current folder. Starting Studio with a blank workspace.');
122
- }
123
- (0, open_1.default)(url);
101
+ const server = (0, http_1.createServer)((req, res) => handle(req, res));
102
+ server.on('upgrade', (request, socket, head) => {
103
+ if (request.url === '/live-server') {
104
+ console.log('🔗 WebSocket connection established.');
105
+ wsServer.handleUpgrade(request, socket, head, (sock) => {
106
+ wsServer.emit('connection', sock, request);
107
+ });
108
+ }
109
+ else {
110
+ socket.destroy();
111
+ }
112
+ });
113
+ server.listen(port, () => {
114
+ const addr = server.address();
115
+ const listenPort = (addr && typeof addr === 'object' && 'port' in addr) ? addr.port : port;
116
+ const url = `http://localhost:${listenPort}?liveServer=${listenPort}&studio-version=${package_json_1.version}`;
117
+ console.log(`🎉 Connected to Live Server running at ${(0, picocolors_1.blueBright)(url)}.`);
118
+ console.log(`🌐 Open this URL in your web browser: ${(0, picocolors_1.blueBright)(url)}`);
119
+ console.log(`🛑 If needed, press ${(0, picocolors_1.redBright)('Ctrl + C')} to stop the process.`);
120
+ if (filePath) {
121
+ console.log(`👁️ Watching changes on file ${(0, picocolors_1.blueBright)(filePath)}`);
122
+ }
123
+ else {
124
+ console.warn('Warning: No file was provided, and we couldn\'t find a default file (like "asyncapi.yaml" or "asyncapi.json") in the current folder. Starting Studio with a blank workspace.');
125
+ }
126
+ (0, open_1.default)(url);
127
+ }).on('error', (error) => {
128
+ if (error.message.includes('EADDRINUSE')) {
129
+ console.log(error);
130
+ console.error((0, picocolors_1.redBright)(`Error: Port ${port} is already in use.`));
131
+ // eslint-disable-next-line no-process-exit
132
+ process.exit(2);
133
+ }
134
+ else {
135
+ console.error(`Failed to start server on port ${port}`);
136
+ }
137
+ });
124
138
  });
125
139
  });
126
140
  }
@@ -6,7 +6,7 @@ const config_1 = tslib_1.__importDefault(require("config"));
6
6
  const fs_1 = tslib_1.__importDefault(require("fs"));
7
7
  const path_1 = tslib_1.__importDefault(require("path"));
8
8
  const winston_1 = tslib_1.__importDefault(require("winston"));
9
- const logDir = path_1.default.join(__dirname, config_1.default.get('log.dir'));
9
+ const logDir = path_1.default.join(__dirname, config_1.default.has('log.dir') ? config_1.default.get('log.dir') : 'logs');
10
10
  if (!fs_1.default.existsSync(logDir)) {
11
11
  fs_1.default.mkdirSync(logDir);
12
12
  }
@@ -14,6 +14,7 @@ function createTempDirectory() {
14
14
  function removeTempDirectory(tmpDir) {
15
15
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
16
16
  try {
17
+ // eslint-disable-next-line no-unused-expressions
17
18
  tmpDir &&
18
19
  fs_1.default.existsSync(tmpDir) &&
19
20
  (yield fs_1.promises.rm(tmpDir, { recursive: true }));
@@ -1978,5 +1978,5 @@
1978
1978
  ]
1979
1979
  }
1980
1980
  },
1981
- "version": "3.4.0"
1981
+ "version": "3.4.2"
1982
1982
  }