@asyncapi/cli 0.13.2 → 0.13.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,26 +6,11 @@ const parser = tslib_1.__importStar(require("@asyncapi/parser"));
6
6
  const base_1 = tslib_1.__importDefault(require("../base"));
7
7
  const validation_error_1 = require("../errors/validation-error");
8
8
  const SpecificationFile_1 = require("../models/SpecificationFile");
9
- const specification_file_1 = require("../errors/specification-file");
10
9
  class Validate extends base_1.default {
11
10
  async run() {
12
11
  const { args } = this.parse(Validate);
13
12
  const filePath = args['spec-file'];
14
- let specFile;
15
- try {
16
- specFile = await SpecificationFile_1.load(filePath);
17
- }
18
- catch (err) {
19
- if (err instanceof specification_file_1.SpecificationFileNotFound) {
20
- this.error(new validation_error_1.ValidationError({
21
- type: 'invalid-file',
22
- filepath: filePath
23
- }));
24
- }
25
- else {
26
- this.error(err);
27
- }
28
- }
13
+ const specFile = await SpecificationFile_1.load(filePath);
29
14
  try {
30
15
  if (specFile.getFilePath()) {
31
16
  await parser.parse(specFile.text());
@@ -1,3 +1,4 @@
1
+ export declare const NO_CONTEXTS_SAVED = "Specify what AsyncAPI file to be used.\nThese are your options to specify in the CLI what AsyncAPI file should be used:\n - You can provide a path to the AsyncAPI file: asyncapi <command> path/to/file/asyncapi\n - You can also pass a saved context that points to your AsyncAPI file: asyncapi <command> context-name\n - In case you did not specify a context that you want to use, the CLI checks if there is a default context and uses it. To set default context run: asyncapi context use mycontext\n - In case you did not provide any reference to AsyncAPI file and there is no default context, the CLI detects if in your current working directory you have files like asyncapi.json, asyncapi.yaml, asyncapi.yml. Just rename your file accordingly.\n";
1
2
  declare class ContextError extends Error {
2
3
  constructor();
3
4
  }
@@ -1,9 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ContextNotFound = exports.MissingCurrentContextError = exports.MissingContextFileError = void 0;
4
- const NO_CONTEXTS_SAVED = 'No contexts saved yet, run asyncapi --help to learn more';
3
+ exports.ContextNotFound = exports.MissingCurrentContextError = exports.MissingContextFileError = exports.NO_CONTEXTS_SAVED = void 0;
5
4
  const CONTEXT_NOT_FOUND = (contextName) => `Context "${contextName}" does not exists.`;
6
5
  const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set a current context.';
6
+ exports.NO_CONTEXTS_SAVED = `Specify what AsyncAPI file to be used.
7
+ These are your options to specify in the CLI what AsyncAPI file should be used:
8
+ - You can provide a path to the AsyncAPI file: asyncapi <command> path/to/file/asyncapi
9
+ - You can also pass a saved context that points to your AsyncAPI file: asyncapi <command> context-name
10
+ - In case you did not specify a context that you want to use, the CLI checks if there is a default context and uses it. To set default context run: asyncapi context use mycontext
11
+ - In case you did not provide any reference to AsyncAPI file and there is no default context, the CLI detects if in your current working directory you have files like asyncapi.json, asyncapi.yaml, asyncapi.yml. Just rename your file accordingly.
12
+ `;
7
13
  class ContextError extends Error {
8
14
  constructor() {
9
15
  super();
@@ -13,7 +19,7 @@ class ContextError extends Error {
13
19
  class MissingContextFileError extends ContextError {
14
20
  constructor() {
15
21
  super();
16
- this.message = NO_CONTEXTS_SAVED;
22
+ this.message = exports.NO_CONTEXTS_SAVED;
17
23
  }
18
24
  }
19
25
  exports.MissingContextFileError = MissingContextFileError;
@@ -7,4 +7,9 @@ export declare class SpecificationFileNotFound extends SpecificationFileError {
7
7
  export declare class SpecificationURLNotFound extends SpecificationFileError {
8
8
  constructor(URL: string);
9
9
  }
10
+ declare type From = 'file' | 'url' | 'context';
11
+ export declare class ErrorLoadingSpec extends Error {
12
+ private readonly errorMessages;
13
+ constructor(from?: From, param?: string);
14
+ }
10
15
  export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SpecificationURLNotFound = exports.SpecificationFileNotFound = void 0;
3
+ exports.ErrorLoadingSpec = exports.SpecificationURLNotFound = exports.SpecificationFileNotFound = void 0;
4
+ const context_error_1 = require("./context-error");
4
5
  class SpecificationFileError extends Error {
5
6
  constructor() {
6
7
  super();
@@ -26,3 +27,28 @@ class SpecificationURLNotFound extends SpecificationFileError {
26
27
  }
27
28
  }
28
29
  exports.SpecificationURLNotFound = SpecificationURLNotFound;
30
+ class ErrorLoadingSpec extends Error {
31
+ constructor(from, param) {
32
+ super();
33
+ this.errorMessages = {
34
+ default: context_error_1.NO_CONTEXTS_SAVED
35
+ };
36
+ if (from === 'file') {
37
+ this.name = 'error loading AsyncAPI document from file';
38
+ this.message = `${param} is an invalid file path`;
39
+ }
40
+ if (from === 'url') {
41
+ this.name = 'error loading AsyncAPI docuement from url';
42
+ this.message = `${param} is an invalid url`;
43
+ }
44
+ if (from === 'context') {
45
+ this.name = 'error loading AsyncAPI document from context';
46
+ this.message = `${param} is an invalid context name`;
47
+ }
48
+ if (!from) {
49
+ this.name = 'error locating AsyncAPI document';
50
+ this.message = this.errorMessages.default;
51
+ }
52
+ }
53
+ }
54
+ exports.ErrorLoadingSpec = ErrorLoadingSpec;
@@ -18,7 +18,13 @@ export default class SpecificationFile {
18
18
  getPath(): string;
19
19
  read(): Promise<string>;
20
20
  }
21
- export declare function load(filePathOrContextName?: string): Promise<Specification>;
21
+ interface LoadType {
22
+ file?: boolean;
23
+ url?: boolean;
24
+ context?: boolean;
25
+ }
26
+ export declare function load(filePathOrContextName?: string, loadType?: LoadType): Promise<Specification>;
22
27
  export declare function nameType(name: string): Promise<string>;
23
28
  export declare function isURL(urlpath: string): Promise<boolean>;
24
29
  export declare function fileExists(name: string): Promise<boolean>;
30
+ export {};
@@ -7,6 +7,7 @@ const path = tslib_1.__importStar(require("path"));
7
7
  const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
8
8
  const Context_1 = require("./Context");
9
9
  const specification_file_1 = require("../errors/specification-file");
10
+ const context_error_1 = require("../errors/context-error");
10
11
  const { readFile, lstat } = fs_1.promises;
11
12
  const allowedFileNames = [
12
13
  'asyncapi.json',
@@ -32,15 +33,25 @@ class Specification {
32
33
  return this.fileURL;
33
34
  }
34
35
  static async fromFile(filepath) {
35
- return new Specification(await readFile(filepath, { encoding: 'utf8' }), { 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 });
36
44
  }
37
45
  static async fromURL(URLpath) {
38
46
  let response;
39
47
  try {
40
48
  response = await node_fetch_1.default(URLpath, { method: 'GET' });
49
+ if (!response.ok) {
50
+ throw new specification_file_1.ErrorLoadingSpec('url', URLpath);
51
+ }
41
52
  }
42
53
  catch (error) {
43
- throw new specification_file_1.SpecificationURLNotFound(URLpath);
54
+ throw new specification_file_1.ErrorLoadingSpec('url', URLpath);
44
55
  }
45
56
  return new Specification(await (response === null || response === void 0 ? void 0 : response.text()), { fileURL: URLpath });
46
57
  }
@@ -58,8 +69,18 @@ class SpecificationFile {
58
69
  }
59
70
  }
60
71
  exports.default = SpecificationFile;
61
- async function load(filePathOrContextName) {
72
+ /* eslint-disable sonarjs/cognitive-complexity */
73
+ async function load(filePathOrContextName, loadType) {
62
74
  if (filePathOrContextName) {
75
+ if (loadType === null || loadType === void 0 ? void 0 : loadType.file) {
76
+ return Specification.fromFile(filePathOrContextName);
77
+ }
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
+ }
63
84
  const type = await nameType(filePathOrContextName);
64
85
  if (type === TYPE_CONTEXT_NAME) {
65
86
  return loadFromContext(filePathOrContextName);
@@ -78,11 +99,11 @@ async function load(filePathOrContextName) {
78
99
  if (autoDetectedSpecFile) {
79
100
  return Specification.fromFile(autoDetectedSpecFile);
80
101
  }
81
- if (!filePathOrContextName || !autoDetectedSpecFile) {
82
- throw e;
102
+ if (e instanceof context_error_1.MissingContextFileError) {
103
+ throw new specification_file_1.ErrorLoadingSpec();
83
104
  }
105
+ throw e;
84
106
  }
85
- throw new specification_file_1.SpecificationFileNotFound();
86
107
  }
87
108
  exports.load = load;
88
109
  async function nameType(name) {
@@ -118,16 +139,24 @@ async function fileExists(name) {
118
139
  if ((await lstat(name)).isFile()) {
119
140
  return true;
120
141
  }
121
- throw new specification_file_1.SpecificationFileNotFound(name);
142
+ throw new specification_file_1.ErrorLoadingSpec('file', name);
122
143
  }
123
144
  catch (e) {
124
- throw new specification_file_1.SpecificationFileNotFound(name);
145
+ throw new specification_file_1.ErrorLoadingSpec('file', name);
125
146
  }
126
147
  }
127
148
  exports.fileExists = fileExists;
128
149
  async function loadFromContext(contextName) {
129
- const context = await Context_1.loadContext(contextName);
130
- return Specification.fromFile(context);
150
+ try {
151
+ const context = await 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();
157
+ }
158
+ throw error;
159
+ }
131
160
  }
132
161
  async function detectSpecFile() {
133
162
  const existingFileNames = await Promise.all(allowedFileNames.map(async (filename) => {
@@ -1 +1 @@
1
- {"version":"0.13.2","commands":{"config":{"id":"config","description":"access configs","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"diff":{"id":"diff","description":"find diff between two asyncapi files","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"format":{"name":"format","type":"option","char":"f","description":"format of the output","options":["json"],"default":"json"},"type":{"name":"type","type":"option","char":"t","description":"type of the output","options":["breaking","non-breaking","unclassified","all"],"default":"all"},"overrides":{"name":"overrides","type":"option","char":"o","description":"path to JSON file containing the override properties"}},"args":[{"name":"old","description":"old spec path, URL or context-name","required":true},{"name":"new","description":"new spec path, URL or context-name","required":true}]},"new":{"id":"new","description":"creates a new asyncapi file","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"file-name":{"name":"file-name","type":"option","char":"n","description":"name of the file"},"example":{"name":"example","type":"option","char":"e","description":"name of the example to use"},"studio":{"name":"studio","type":"boolean","char":"s","description":"open in Studio","allowNo":false},"port":{"name":"port","type":"option","char":"p","description":"port in which to start Studio"},"no-tty":{"name":"no-tty","type":"boolean","description":"do not use an interactive terminal","allowNo":false}},"args":[]},"start":{"id":"start","description":"starts a new local instance of Studio","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"validate":{"id":"validate","description":"validate asyncapi file","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"spec-file","description":"spec path, url, or context-name","required":false}]},"config:context":{"id":"config:context","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"start:studio":{"id":"start:studio","description":"starts a new local instance of Studio","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"file":{"name":"file","type":"option","char":"f","description":"path to the AsyncAPI file to link with Studio"},"port":{"name":"port","type":"option","char":"p","description":"port in which to start Studio"}},"args":[]},"config:context:add":{"id":"config:context:add","description":"Add or modify a context in the store","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"context-name","description":"context name","required":true},{"name":"spec-file-path","description":"file path of the spec file","required":true}]},"config:context:current":{"id":"config:context:current","description":"Shows the current context that is being used","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"config:context:list":{"id":"config:context:list","description":"List all the stored context in the store","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"config:context:remove":{"id":"config:context:remove","description":"Delete a context from the store","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"context-name","description":"Name of the context to delete","required":true}]},"config:context:use":{"id":"config:context:use","description":"Set a context as current","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"context-name","description":"name of the saved context","required":true}]}}}
1
+ {"version":"0.13.3","commands":{"config":{"id":"config","description":"access configs","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"diff":{"id":"diff","description":"find diff between two asyncapi files","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"format":{"name":"format","type":"option","char":"f","description":"format of the output","options":["json"],"default":"json"},"type":{"name":"type","type":"option","char":"t","description":"type of the output","options":["breaking","non-breaking","unclassified","all"],"default":"all"},"overrides":{"name":"overrides","type":"option","char":"o","description":"path to JSON file containing the override properties"}},"args":[{"name":"old","description":"old spec path, URL or context-name","required":true},{"name":"new","description":"new spec path, URL or context-name","required":true}]},"new":{"id":"new","description":"creates a new asyncapi file","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"file-name":{"name":"file-name","type":"option","char":"n","description":"name of the file"},"example":{"name":"example","type":"option","char":"e","description":"name of the example to use"},"studio":{"name":"studio","type":"boolean","char":"s","description":"open in Studio","allowNo":false},"port":{"name":"port","type":"option","char":"p","description":"port in which to start Studio"},"no-tty":{"name":"no-tty","type":"boolean","description":"do not use an interactive terminal","allowNo":false}},"args":[]},"start":{"id":"start","description":"starts a new local instance of Studio","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"validate":{"id":"validate","description":"validate asyncapi file","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"spec-file","description":"spec path, url, or context-name","required":false}]},"config:context":{"id":"config:context","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"start:studio":{"id":"start:studio","description":"starts a new local instance of Studio","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"file":{"name":"file","type":"option","char":"f","description":"path to the AsyncAPI file to link with Studio"},"port":{"name":"port","type":"option","char":"p","description":"port in which to start Studio"}},"args":[]},"config:context:add":{"id":"config:context:add","description":"Add or modify a context in the store","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"context-name","description":"context name","required":true},{"name":"spec-file-path","description":"file path of the spec file","required":true}]},"config:context:current":{"id":"config:context:current","description":"Shows the current context that is being used","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"config:context:list":{"id":"config:context:list","description":"List all the stored context in the store","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"config:context:remove":{"id":"config:context:remove","description":"Delete a context from the store","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"context-name","description":"Name of the context to delete","required":true}]},"config:context:use":{"id":"config:context:use","description":"Set a context as current","pluginName":"@asyncapi/cli","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"context-name","description":"name of the saved context","required":true}]}}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@asyncapi/cli",
3
3
  "description": "All in one CLI for all AsyncAPI tools",
4
- "version": "0.13.2",
4
+ "version": "0.13.3",
5
5
  "author": "@asyncapi",
6
6
  "bin": {
7
7
  "asyncapi": "./bin/run"