@asyncapi/cli 0.25.1 → 0.25.3

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.
@@ -6,11 +6,13 @@ const base_1 = tslib_1.__importDefault(require("../../base"));
6
6
  const Studio_1 = require("../../models/Studio");
7
7
  const SpecificationFile_1 = require("../../models/SpecificationFile");
8
8
  class StartStudio extends base_1.default {
9
- async run() {
10
- const { flags } = await this.parse(StartStudio);
11
- const filePath = flags.file || (await (0, SpecificationFile_1.load)()).getFilePath();
12
- const port = flags.port;
13
- (0, Studio_1.start)(filePath, port);
9
+ run() {
10
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
11
+ const { flags } = yield this.parse(StartStudio);
12
+ const filePath = flags.file || (yield (0, SpecificationFile_1.load)()).getFilePath();
13
+ const port = flags.port;
14
+ (0, Studio_1.start)(filePath, port);
15
+ });
14
16
  }
15
17
  }
16
18
  exports.default = StartStudio;
@@ -9,30 +9,32 @@ const SpecificationFile_1 = require("../models/SpecificationFile");
9
9
  const globals_1 = require("../globals");
10
10
  const flags_1 = require("../flags");
11
11
  class Validate extends base_1.default {
12
- async run() {
13
- const { args, flags } = await this.parse(Validate); //NOSONAR
14
- const filePath = args['spec-file'];
15
- const watchMode = flags['watch'];
16
- const specFile = await (0, SpecificationFile_1.load)(filePath);
17
- if (watchMode) {
18
- (0, globals_1.specWatcher)({ spec: specFile, handler: this, handlerName: 'validate' });
19
- }
20
- try {
21
- if (specFile.getFilePath()) {
22
- await parser.parse(specFile.text());
23
- this.log(`File ${specFile.getFilePath()} successfully validated!`);
12
+ run() {
13
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
14
+ const { args, flags } = yield this.parse(Validate); //NOSONAR
15
+ const filePath = args['spec-file'];
16
+ const watchMode = flags['watch'];
17
+ const specFile = yield (0, SpecificationFile_1.load)(filePath);
18
+ if (watchMode) {
19
+ (0, globals_1.specWatcher)({ spec: specFile, handler: this, handlerName: 'validate' });
24
20
  }
25
- else if (specFile.getFileURL()) {
26
- await parser.parse(specFile.text());
27
- this.log(`URL ${specFile.getFileURL()} successfully validated`);
21
+ try {
22
+ if (specFile.getFilePath()) {
23
+ yield parser.parse(specFile.text());
24
+ this.log(`File ${specFile.getFilePath()} successfully validated!`);
25
+ }
26
+ else if (specFile.getFileURL()) {
27
+ yield parser.parse(specFile.text());
28
+ this.log(`URL ${specFile.getFileURL()} successfully validated`);
29
+ }
28
30
  }
29
- }
30
- catch (error) {
31
- throw new validation_error_1.ValidationError({
32
- type: 'parser-error',
33
- err: error
34
- });
35
- }
31
+ catch (error) {
32
+ throw new validation_error_1.ValidationError({
33
+ type: 'parser-error',
34
+ err: error
35
+ });
36
+ }
37
+ });
36
38
  }
37
39
  }
38
40
  exports.default = Validate;
package/lib/flags.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const watchFlag: (description?: string) => import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
1
+ export declare const watchFlag: (description?: string | undefined) => import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
package/lib/globals.js CHANGED
@@ -27,15 +27,15 @@ const specWatcher = (params) => {
27
27
  WATCH_MESSAGES.logOnStart(filePath);
28
28
  chokidar_1.default
29
29
  .watch(filePath, CHOKIDAR_CONFIG)
30
- .on('change', async () => {
30
+ .on('change', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
31
31
  WATCH_MESSAGES.logOnChange(params.handlerName);
32
32
  try {
33
- await params.handler.run();
33
+ yield params.handler.run();
34
34
  }
35
35
  catch (err) {
36
- await params.handler.catch(err);
36
+ yield params.handler.catch(err);
37
37
  }
38
- });
38
+ }));
39
39
  CHOKIDAR_INSTANCE_STORE.set(params.label || '_default', true);
40
40
  }
41
41
  catch (error) {
@@ -9,94 +9,108 @@ const context_error_1 = require("../errors/context-error");
9
9
  const { readFile, writeFile } = fs_1.promises;
10
10
  const CONTEXT_FILENAME = process.env.CONTEXT_FILENAME || '.asyncapi';
11
11
  exports.DEFAULT_CONTEXT_FILE_PATH = path.resolve(process.env.CONTEXT_FILE_PATH || os.homedir(), CONTEXT_FILENAME);
12
- async function loadContext(contextName) {
13
- const fileContent = await loadContextFile();
14
- if (contextName) {
15
- const context = fileContent.store[String(contextName)];
16
- if (!context) {
17
- throw new context_error_1.ContextNotFound(contextName);
12
+ function loadContext(contextName) {
13
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
14
+ const fileContent = yield loadContextFile();
15
+ if (contextName) {
16
+ const context = fileContent.store[String(contextName)];
17
+ if (!context) {
18
+ throw new context_error_1.ContextNotFound(contextName);
19
+ }
20
+ return context;
18
21
  }
19
- return context;
20
- }
21
- else if (fileContent.current) {
22
- const context = fileContent.store[fileContent.current];
23
- if (!context) {
24
- throw new context_error_1.ContextNotFound(fileContent.current);
22
+ else if (fileContent.current) {
23
+ const context = fileContent.store[fileContent.current];
24
+ if (!context) {
25
+ throw new context_error_1.ContextNotFound(fileContent.current);
26
+ }
27
+ return context;
25
28
  }
26
- return context;
27
- }
28
- throw new context_error_1.MissingCurrentContextError();
29
+ throw new context_error_1.MissingCurrentContextError();
30
+ });
29
31
  }
30
32
  exports.loadContext = loadContext;
31
- async function addContext(contextName, pathToFile) {
32
- let fileContent;
33
- try {
34
- fileContent = await loadContextFile();
35
- }
36
- catch (err) {
37
- if (err instanceof context_error_1.MissingContextFileError) {
38
- fileContent = {
39
- store: {
40
- [contextName]: pathToFile,
41
- }
42
- };
33
+ function addContext(contextName, pathToFile) {
34
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
35
+ let fileContent;
36
+ try {
37
+ fileContent = yield loadContextFile();
43
38
  }
44
- else {
45
- throw err;
39
+ catch (err) {
40
+ if (err instanceof context_error_1.MissingContextFileError) {
41
+ fileContent = {
42
+ store: {
43
+ [contextName]: pathToFile,
44
+ }
45
+ };
46
+ }
47
+ else {
48
+ throw err;
49
+ }
46
50
  }
47
- }
48
- fileContent.store[String(contextName)] = pathToFile;
49
- await saveContextFile(fileContent);
51
+ fileContent.store[String(contextName)] = pathToFile;
52
+ yield saveContextFile(fileContent);
53
+ });
50
54
  }
51
55
  exports.addContext = addContext;
52
- async function removeContext(contextName) {
53
- const fileContent = await loadContextFile();
54
- if (!fileContent.store[String(contextName)]) {
55
- throw new context_error_1.ContextNotFound(contextName);
56
- }
57
- if (fileContent.current === contextName) {
58
- delete fileContent.current;
59
- }
60
- delete fileContent.store[String(contextName)];
61
- await saveContextFile(fileContent);
56
+ function removeContext(contextName) {
57
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
58
+ const fileContent = yield loadContextFile();
59
+ if (!fileContent.store[String(contextName)]) {
60
+ throw new context_error_1.ContextNotFound(contextName);
61
+ }
62
+ if (fileContent.current === contextName) {
63
+ delete fileContent.current;
64
+ }
65
+ delete fileContent.store[String(contextName)];
66
+ yield saveContextFile(fileContent);
67
+ });
62
68
  }
63
69
  exports.removeContext = removeContext;
64
- async function getCurrentContext() {
65
- const fileContent = await loadContextFile();
66
- const context = await loadContext();
67
- return {
68
- current: fileContent.current,
69
- context,
70
- };
70
+ function getCurrentContext() {
71
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
72
+ const fileContent = yield loadContextFile();
73
+ const context = yield loadContext();
74
+ return {
75
+ current: fileContent.current,
76
+ context,
77
+ };
78
+ });
71
79
  }
72
80
  exports.getCurrentContext = getCurrentContext;
73
- async function setCurrentContext(contextName) {
74
- const fileContent = await loadContextFile();
75
- if (!fileContent.store[String(contextName)]) {
76
- throw new context_error_1.ContextNotFound(contextName);
77
- }
78
- fileContent.current = contextName;
79
- await saveContextFile(fileContent);
81
+ function setCurrentContext(contextName) {
82
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
83
+ const fileContent = yield loadContextFile();
84
+ if (!fileContent.store[String(contextName)]) {
85
+ throw new context_error_1.ContextNotFound(contextName);
86
+ }
87
+ fileContent.current = contextName;
88
+ yield saveContextFile(fileContent);
89
+ });
80
90
  }
81
91
  exports.setCurrentContext = setCurrentContext;
82
- async function loadContextFile() {
83
- try {
84
- return JSON.parse(await readFile(exports.DEFAULT_CONTEXT_FILE_PATH, { encoding: 'utf8' }));
85
- }
86
- catch (e) {
87
- throw new context_error_1.MissingContextFileError();
88
- }
92
+ function loadContextFile() {
93
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
94
+ try {
95
+ return JSON.parse(yield readFile(exports.DEFAULT_CONTEXT_FILE_PATH, { encoding: 'utf8' }));
96
+ }
97
+ catch (e) {
98
+ throw new context_error_1.MissingContextFileError();
99
+ }
100
+ });
89
101
  }
90
102
  exports.loadContextFile = loadContextFile;
91
- async function saveContextFile(fileContent) {
92
- try {
93
- writeFile(exports.DEFAULT_CONTEXT_FILE_PATH, JSON.stringify({
94
- current: fileContent.current,
95
- store: fileContent.store
96
- }), { encoding: 'utf8' });
97
- return fileContent;
98
- }
99
- catch (error) {
100
- return;
101
- }
103
+ function saveContextFile(fileContent) {
104
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
105
+ try {
106
+ writeFile(exports.DEFAULT_CONTEXT_FILE_PATH, JSON.stringify({
107
+ current: fileContent.current,
108
+ store: fileContent.store
109
+ }), { encoding: 'utf8' });
110
+ return fileContent;
111
+ }
112
+ catch (error) {
113
+ return;
114
+ }
115
+ });
102
116
  }
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fileExists = exports.isURL = exports.nameType = exports.load = exports.Specification = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fs_1 = require("fs");
6
- const path = tslib_1.__importStar(require("path"));
6
+ const path_1 = tslib_1.__importDefault(require("path"));
7
+ const url_1 = require("url");
7
8
  const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
8
9
  const Context_1 = require("./Context");
9
10
  const specification_file_1 = require("../errors/specification-file");
@@ -32,28 +33,32 @@ class Specification {
32
33
  getFileURL() {
33
34
  return this.fileURL;
34
35
  }
35
- static async fromFile(filepath) {
36
- let spec;
37
- try {
38
- spec = await readFile(filepath, { encoding: 'utf8' });
39
- }
40
- catch (error) {
41
- throw new specification_file_1.ErrorLoadingSpec('file', filepath);
42
- }
43
- return new Specification(spec, { filepath });
44
- }
45
- static async fromURL(URLpath) {
46
- let response;
47
- try {
48
- response = await (0, node_fetch_1.default)(URLpath, { method: 'GET' });
49
- if (!response.ok) {
36
+ static fromFile(filepath) {
37
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
38
+ let spec;
39
+ try {
40
+ spec = yield readFile(filepath, { encoding: 'utf8' });
41
+ }
42
+ catch (error) {
43
+ throw new specification_file_1.ErrorLoadingSpec('file', filepath);
44
+ }
45
+ return new Specification(spec, { filepath });
46
+ });
47
+ }
48
+ static fromURL(URLpath) {
49
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
50
+ let response;
51
+ try {
52
+ response = yield (0, node_fetch_1.default)(URLpath, { method: 'GET' });
53
+ if (!response.ok) {
54
+ throw new specification_file_1.ErrorLoadingSpec('url', URLpath);
55
+ }
56
+ }
57
+ catch (error) {
50
58
  throw new specification_file_1.ErrorLoadingSpec('url', URLpath);
51
59
  }
52
- }
53
- catch (error) {
54
- throw new specification_file_1.ErrorLoadingSpec('url', URLpath);
55
- }
56
- return new Specification(await (response === null || response === void 0 ? void 0 : response.text()), { fileURL: URLpath });
60
+ return new Specification(yield (response === null || response === void 0 ? void 0 : response.text()), { fileURL: URLpath });
61
+ });
57
62
  }
58
63
  }
59
64
  exports.Specification = Specification;
@@ -64,109 +69,123 @@ class SpecificationFile {
64
69
  getPath() {
65
70
  return this.pathToFile;
66
71
  }
67
- async read() {
68
- return readFile(this.pathToFile, { encoding: 'utf8' });
72
+ read() {
73
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
74
+ return readFile(this.pathToFile, { encoding: 'utf8' });
75
+ });
69
76
  }
70
77
  }
71
78
  exports.default = SpecificationFile;
72
79
  /* eslint-disable sonarjs/cognitive-complexity */
73
- async function load(filePathOrContextName, loadType) {
74
- if (filePathOrContextName) {
75
- if (loadType === null || loadType === void 0 ? void 0 : loadType.file) {
80
+ function load(filePathOrContextName, loadType) {
81
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
82
+ if (filePathOrContextName) {
83
+ if (loadType === null || loadType === void 0 ? void 0 : loadType.file) {
84
+ return Specification.fromFile(filePathOrContextName);
85
+ }
86
+ if (loadType === null || loadType === void 0 ? void 0 : loadType.context) {
87
+ return loadFromContext(filePathOrContextName);
88
+ }
89
+ if (loadType === null || loadType === void 0 ? void 0 : loadType.url) {
90
+ return Specification.fromURL(filePathOrContextName);
91
+ }
92
+ const type = yield nameType(filePathOrContextName);
93
+ if (type === TYPE_CONTEXT_NAME) {
94
+ return loadFromContext(filePathOrContextName);
95
+ }
96
+ if (type === TYPE_URL) {
97
+ return Specification.fromURL(filePathOrContextName);
98
+ }
99
+ yield fileExists(filePathOrContextName);
76
100
  return Specification.fromFile(filePathOrContextName);
77
101
  }
78
- if (loadType === null || loadType === void 0 ? void 0 : loadType.context) {
79
- return loadFromContext(filePathOrContextName);
80
- }
81
- if (loadType === null || loadType === void 0 ? void 0 : loadType.url) {
82
- return Specification.fromURL(filePathOrContextName);
83
- }
84
- const type = await nameType(filePathOrContextName);
85
- if (type === TYPE_CONTEXT_NAME) {
86
- return loadFromContext(filePathOrContextName);
87
- }
88
- if (type === TYPE_URL) {
89
- return Specification.fromURL(filePathOrContextName);
90
- }
91
- await fileExists(filePathOrContextName);
92
- return Specification.fromFile(filePathOrContextName);
93
- }
94
- try {
95
- return await loadFromContext();
96
- }
97
- catch (e) {
98
- const autoDetectedSpecFile = await detectSpecFile();
99
- if (autoDetectedSpecFile) {
100
- return Specification.fromFile(autoDetectedSpecFile);
102
+ try {
103
+ return yield loadFromContext();
101
104
  }
102
- if (e instanceof context_error_1.MissingContextFileError) {
103
- throw new specification_file_1.ErrorLoadingSpec();
105
+ catch (e) {
106
+ const autoDetectedSpecFile = yield detectSpecFile();
107
+ if (autoDetectedSpecFile) {
108
+ return Specification.fromFile(autoDetectedSpecFile);
109
+ }
110
+ if (e instanceof context_error_1.MissingContextFileError) {
111
+ throw new specification_file_1.ErrorLoadingSpec();
112
+ }
113
+ throw e;
104
114
  }
105
- throw e;
106
- }
115
+ });
107
116
  }
108
117
  exports.load = load;
109
- async function nameType(name) {
110
- if (name.startsWith('.')) {
111
- return TYPE_FILE_PATH;
112
- }
113
- try {
114
- if (await fileExists(name)) {
118
+ function nameType(name) {
119
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
120
+ if (name.startsWith('.')) {
115
121
  return TYPE_FILE_PATH;
116
122
  }
117
- return TYPE_CONTEXT_NAME;
118
- }
119
- catch (e) {
120
- if (await isURL(name)) {
121
- return TYPE_URL;
123
+ try {
124
+ if (yield fileExists(name)) {
125
+ return TYPE_FILE_PATH;
126
+ }
127
+ return TYPE_CONTEXT_NAME;
122
128
  }
123
- return TYPE_CONTEXT_NAME;
124
- }
129
+ catch (e) {
130
+ if (yield isURL(name)) {
131
+ return TYPE_URL;
132
+ }
133
+ return TYPE_CONTEXT_NAME;
134
+ }
135
+ });
125
136
  }
126
137
  exports.nameType = nameType;
127
- async function isURL(urlpath) {
128
- try {
129
- const url = new URL(urlpath);
130
- return url.protocol === 'http:' || url.protocol === 'https:';
131
- }
132
- catch (error) {
133
- return false;
134
- }
138
+ function isURL(urlpath) {
139
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
140
+ try {
141
+ const url = new url_1.URL(urlpath);
142
+ return url.protocol === 'http:' || url.protocol === 'https:';
143
+ }
144
+ catch (error) {
145
+ return false;
146
+ }
147
+ });
135
148
  }
136
149
  exports.isURL = isURL;
137
- async function fileExists(name) {
138
- try {
139
- if ((await lstat(name)).isFile()) {
140
- return true;
150
+ function fileExists(name) {
151
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
152
+ try {
153
+ if ((yield lstat(name)).isFile()) {
154
+ return true;
155
+ }
156
+ throw new specification_file_1.ErrorLoadingSpec('file', name);
141
157
  }
142
- throw new specification_file_1.ErrorLoadingSpec('file', name);
143
- }
144
- catch (e) {
145
- throw new specification_file_1.ErrorLoadingSpec('file', name);
146
- }
147
- }
148
- exports.fileExists = fileExists;
149
- async function loadFromContext(contextName) {
150
- try {
151
- const context = await (0, Context_1.loadContext)(contextName);
152
- return Specification.fromFile(context);
153
- }
154
- catch (error) {
155
- if (error instanceof context_error_1.MissingContextFileError) {
156
- throw new specification_file_1.ErrorLoadingSpec();
158
+ catch (e) {
159
+ throw new specification_file_1.ErrorLoadingSpec('file', name);
157
160
  }
158
- throw error;
159
- }
161
+ });
160
162
  }
161
- async function detectSpecFile() {
162
- const existingFileNames = await Promise.all(allowedFileNames.map(async (filename) => {
163
+ exports.fileExists = fileExists;
164
+ function loadFromContext(contextName) {
165
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
163
166
  try {
164
- const exists = await fileExists(path.resolve(process.cwd(), filename));
165
- return exists ? filename : undefined;
167
+ const context = yield (0, Context_1.loadContext)(contextName);
168
+ return Specification.fromFile(context);
166
169
  }
167
- catch (e) {
168
- // We did our best...
170
+ catch (error) {
171
+ if (error instanceof context_error_1.MissingContextFileError) {
172
+ throw new specification_file_1.ErrorLoadingSpec();
173
+ }
174
+ throw error;
169
175
  }
170
- }));
171
- return existingFileNames.find(filename => filename !== undefined);
176
+ });
177
+ }
178
+ function detectSpecFile() {
179
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
180
+ const existingFileNames = yield Promise.all(allowedFileNames.map((filename) => tslib_1.__awaiter(this, void 0, void 0, function* () {
181
+ try {
182
+ const exists = yield fileExists(path_1.default.resolve(process.cwd(), filename));
183
+ return exists ? filename : undefined;
184
+ }
185
+ catch (e) {
186
+ // We did our best...
187
+ }
188
+ })));
189
+ return existingFileNames.find(filename => filename !== undefined);
190
+ });
172
191
  }
@@ -7,9 +7,11 @@ const fs = tslib_1.__importStar(require("fs"));
7
7
  const util_1 = require("util");
8
8
  const chokidar_1 = tslib_1.__importDefault(require("chokidar"));
9
9
  const lstat = (0, util_1.promisify)(fs.lstat);
10
- async function isLocalTemplate(templatePath) {
11
- const stats = await lstat(templatePath);
12
- return stats.isSymbolicLink();
10
+ function isLocalTemplate(templatePath) {
11
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
12
+ const stats = yield lstat(templatePath);
13
+ return stats.isSymbolicLink();
14
+ });
13
15
  }
14
16
  exports.isLocalTemplate = isLocalTemplate;
15
17
  class Watcher {
@@ -45,11 +47,13 @@ class Watcher {
45
47
  * This method initiate the watch for change in all files
46
48
  * @param {*} callback called when the file(s) change
47
49
  */
48
- async watch(changeCallback, errorCallback) {
49
- for (const index in this.paths) {
50
- const path = this.paths[String(index)];
51
- this.initiateWatchOnPath(path, changeCallback, errorCallback);
52
- }
50
+ watch(changeCallback, errorCallback) {
51
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
52
+ for (const index in this.paths) {
53
+ const path = this.paths[String(index)];
54
+ this.initiateWatchOnPath(path, changeCallback, errorCallback);
55
+ }
56
+ });
53
57
  }
54
58
  /**
55
59
  * Should be called when a file has changed one way or another.
@@ -68,11 +72,11 @@ class Watcher {
68
72
  if (this.fsWait) {
69
73
  return;
70
74
  }
71
- this.fsWait = setTimeout(async () => {
72
- await changeCallback(this.filesChanged);
75
+ this.fsWait = setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
76
+ yield changeCallback(this.filesChanged);
73
77
  this.filesChanged = {};
74
78
  this.fsWait = false;
75
- }, 500);
79
+ }), 500);
76
80
  }
77
81
  }
78
82
  catch (e) {