@asyncapi/cli 0.11.2 → 0.12.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.
@@ -8,7 +8,7 @@ const SpecificationFile_1 = require("../../models/SpecificationFile");
8
8
  class StartStudio extends base_1.default {
9
9
  async run() {
10
10
  const { flags } = this.parse(StartStudio);
11
- const filePath = flags.file || (await SpecificationFile_1.load()).getPath();
11
+ const filePath = flags.file || (await SpecificationFile_1.load()).getFilePath();
12
12
  const port = flags.port;
13
13
  Studio_1.start(filePath, port);
14
14
  }
@@ -27,8 +27,14 @@ class Validate extends base_1.default {
27
27
  }
28
28
  }
29
29
  try {
30
- await parser.parse(await specFile.read());
31
- this.log(`File ${specFile.getPath()} successfully validated!`);
30
+ if (specFile.getFilePath()) {
31
+ await parser.parse(specFile.text());
32
+ this.log(`File ${specFile.getFilePath()} successfully validated!`);
33
+ }
34
+ else if (specFile.getFileURL()) {
35
+ await parser.parse(specFile.text());
36
+ this.log(`URL ${specFile.getFileURL()} successfully validated`);
37
+ }
32
38
  }
33
39
  catch (error) {
34
40
  throw new validation_error_1.ValidationError({
@@ -44,5 +50,5 @@ Validate.flags = {
44
50
  help: command_1.flags.help({ char: 'h' })
45
51
  };
46
52
  Validate.args = [
47
- { name: 'spec-file', description: 'spec path or context-name', required: false },
53
+ { name: 'spec-file', description: 'spec path, url, or context-name', required: false },
48
54
  ];
@@ -4,4 +4,7 @@ declare class SpecificationFileError extends Error {
4
4
  export declare class SpecificationFileNotFound extends SpecificationFileError {
5
5
  constructor(filePath?: string);
6
6
  }
7
+ export declare class SpecificationURLNotFound extends SpecificationFileError {
8
+ constructor(URL: string);
9
+ }
7
10
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SpecificationFileNotFound = void 0;
3
+ exports.SpecificationURLNotFound = exports.SpecificationFileNotFound = void 0;
4
4
  class SpecificationFileError extends Error {
5
5
  constructor() {
6
6
  super();
@@ -19,3 +19,10 @@ class SpecificationFileNotFound extends SpecificationFileError {
19
19
  }
20
20
  }
21
21
  exports.SpecificationFileNotFound = SpecificationFileNotFound;
22
+ class SpecificationURLNotFound extends SpecificationFileError {
23
+ constructor(URL) {
24
+ super();
25
+ this.message = `Unable to fetch specification file from url: ${URL}`;
26
+ }
27
+ }
28
+ exports.SpecificationURLNotFound = SpecificationURLNotFound;
@@ -1,9 +1,24 @@
1
+ export declare class Specification {
2
+ private readonly spec;
3
+ private readonly filePath?;
4
+ private readonly fileURL?;
5
+ constructor(spec: string, options?: {
6
+ filepath?: string;
7
+ fileURL?: string;
8
+ });
9
+ text(): string;
10
+ getFilePath(): string | undefined;
11
+ getFileURL(): string | undefined;
12
+ static fromFile(filepath: string): Promise<Specification>;
13
+ static fromURL(URLpath: string): Promise<Specification>;
14
+ }
1
15
  export default class SpecificationFile {
2
16
  private readonly pathToFile;
3
17
  constructor(filePath: string);
4
18
  getPath(): string;
5
19
  read(): Promise<string>;
6
20
  }
7
- export declare function load(filePathOrContextName?: string): Promise<SpecificationFile>;
21
+ export declare function load(filePathOrContextName?: string): Promise<Specification>;
8
22
  export declare function nameType(name: string): Promise<string>;
23
+ export declare function isURL(urlpath: string): Promise<boolean>;
9
24
  export declare function fileExists(name: string): Promise<boolean>;
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fileExists = exports.nameType = exports.load = void 0;
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
6
  const path = tslib_1.__importStar(require("path"));
7
+ const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
7
8
  const Context_1 = require("./Context");
8
9
  const specification_file_1 = require("../errors/specification-file");
9
10
  const { readFile, lstat } = fs_1.promises;
@@ -14,6 +15,37 @@ const allowedFileNames = [
14
15
  ];
15
16
  const TYPE_CONTEXT_NAME = 'context-name';
16
17
  const TYPE_FILE_PATH = 'file-path';
18
+ const TYPE_URL = 'url-path';
19
+ class Specification {
20
+ constructor(spec, options) {
21
+ this.spec = spec;
22
+ this.filePath = options === null || options === void 0 ? void 0 : options.filepath;
23
+ this.fileURL = options === null || options === void 0 ? void 0 : options.fileURL;
24
+ }
25
+ text() {
26
+ return this.spec;
27
+ }
28
+ getFilePath() {
29
+ return this.filePath;
30
+ }
31
+ getFileURL() {
32
+ return this.fileURL;
33
+ }
34
+ static async fromFile(filepath) {
35
+ return new Specification(await readFile(filepath, { encoding: 'utf8' }), { filepath });
36
+ }
37
+ static async fromURL(URLpath) {
38
+ let response;
39
+ try {
40
+ response = await node_fetch_1.default(URLpath, { method: 'GET' });
41
+ }
42
+ catch (error) {
43
+ throw new specification_file_1.SpecificationURLNotFound(URLpath);
44
+ }
45
+ return new Specification(await (response === null || response === void 0 ? void 0 : response.text()), { fileURL: URLpath });
46
+ }
47
+ }
48
+ exports.Specification = Specification;
17
49
  class SpecificationFile {
18
50
  constructor(filePath) {
19
51
  this.pathToFile = filePath;
@@ -32,8 +64,11 @@ async function load(filePathOrContextName) {
32
64
  if (type === TYPE_CONTEXT_NAME) {
33
65
  return loadFromContext(filePathOrContextName);
34
66
  }
67
+ if (type === TYPE_URL) {
68
+ return Specification.fromURL(filePathOrContextName);
69
+ }
35
70
  await fileExists(filePathOrContextName);
36
- return new SpecificationFile(filePathOrContextName);
71
+ return Specification.fromFile(filePathOrContextName);
37
72
  }
38
73
  try {
39
74
  return await loadFromContext();
@@ -41,7 +76,7 @@ async function load(filePathOrContextName) {
41
76
  catch (e) {
42
77
  const autoDetectedSpecFile = await detectSpecFile();
43
78
  if (autoDetectedSpecFile) {
44
- return new SpecificationFile(autoDetectedSpecFile);
79
+ return Specification.fromFile(autoDetectedSpecFile);
45
80
  }
46
81
  if (!filePathOrContextName || !autoDetectedSpecFile) {
47
82
  throw e;
@@ -61,10 +96,23 @@ async function nameType(name) {
61
96
  return TYPE_CONTEXT_NAME;
62
97
  }
63
98
  catch (e) {
99
+ if (await isURL(name)) {
100
+ return TYPE_URL;
101
+ }
64
102
  return TYPE_CONTEXT_NAME;
65
103
  }
66
104
  }
67
105
  exports.nameType = nameType;
106
+ async function isURL(urlpath) {
107
+ try {
108
+ const url = new URL(urlpath);
109
+ return url.protocol === 'http:' || url.protocol === 'https:';
110
+ }
111
+ catch (error) {
112
+ return false;
113
+ }
114
+ }
115
+ exports.isURL = isURL;
68
116
  async function fileExists(name) {
69
117
  try {
70
118
  if ((await lstat(name)).isFile()) {
@@ -79,7 +127,7 @@ async function fileExists(name) {
79
127
  exports.fileExists = fileExists;
80
128
  async function loadFromContext(contextName) {
81
129
  const context = await Context_1.loadContext(contextName);
82
- return new SpecificationFile(context);
130
+ return Specification.fromFile(context);
83
131
  }
84
132
  async function detectSpecFile() {
85
133
  const existingFileNames = await Promise.all(allowedFileNames.map(async (filename) => {
@@ -1 +1 @@
1
- {"version":"0.11.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":[]},"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 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.12.0","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":[]},"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.11.2",
4
+ "version": "0.12.0",
5
5
  "author": "@asyncapi",
6
6
  "bin": {
7
7
  "asyncapi": "./bin/run"
@@ -16,6 +16,7 @@
16
16
  "@oclif/plugin-help": "^3.2.3",
17
17
  "@types/inquirer": "^8.1.3",
18
18
  "@types/ws": "^8.2.0",
19
+ "node-fetch": "^2.0.0",
19
20
  "chalk": "^4.1.0",
20
21
  "chokidar": "^3.5.2",
21
22
  "indent-string": "^4.0.0",
@@ -42,6 +43,7 @@
42
43
  "@types/lodash.template": "^4.4.4",
43
44
  "@types/mocha": "^5.2.7",
44
45
  "@types/node": "^10.17.60",
46
+ "@types/node-fetch": "^2.0.2",
45
47
  "@types/serve-handler": "^6.1.1",
46
48
  "@types/wrap-ansi": "^8.0.1",
47
49
  "@typescript-eslint/eslint-plugin": "^4.28.4",