Skip to content

Latest commit

 

History

History
322 lines (260 loc) · 13 KB

api.md

File metadata and controls

322 lines (260 loc) · 13 KB

Generator

Kind: global class

new Generator(templateName, targetDir, options)

Instantiates a new Generator object.

Param Type Default Description
templateName String Name of the template to generate.
targetDir String Path to the directory where the files will be generated.
options Object
[options.templateParams] String Optional parameters to pass to the template. Each template define their own params.
[options.entrypoint] String Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template.
[options.noOverwriteGlobs] Array.<String> List of globs to skip when regenerating the template.
[options.disabledHooks] Object.<String, (Boolean|String|Array.<String>)> Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array.
[options.output] String 'fs' Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set.
[options.forceWrite] Boolean false Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.
[options.install] Boolean false Install the template and its dependencies, even when the template has already been installed.
[options.debug] Boolean false Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.
[options.mapBaseUrlToFolder] Object.<String, String> Optional parameter to map schema references from a base url to a local base folder e.g. url=https://schema.example.com/crm/ folder=./test/docs/ .

Example

const path = require('path');
const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));

Example (Passing custom params to the template)

const path = require('path');
const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'), {
  templateParams: {
    sidebarOrganization: 'byTags'
  }
});

generator.templateName : String

Name of the template to generate.

Kind: instance property of Generator

generator.targetDir : String

Path to the directory where the files will be generated.

Kind: instance property of Generator

generator.entrypoint : String

Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template.

Kind: instance property of Generator

generator.noOverwriteGlobs : Array.<String>

List of globs to skip when regenerating the template.

Kind: instance property of Generator

generator.disabledHooks : Object.<String, (Boolean|String|Array.<String>)>

Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array.

Kind: instance property of Generator

generator.output : String

Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set.

Kind: instance property of Generator

generator.forceWrite : Boolean

Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.

Kind: instance property of Generator

generator.debug : Boolean

Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.

Kind: instance property of Generator

generator.install : Boolean

Install the template and its dependencies, even when the template has already been installed.

Kind: instance property of Generator

generator.templateConfig : Object

The template configuration.

Kind: instance property of Generator

generator.hooks : Object

Hooks object with hooks functions grouped by the hook type.

Kind: instance property of Generator

generator.mapBaseUrlToFolder : Object

Maps schema URL to folder.

Kind: instance property of Generator

generator.templateParams : Object

The template parameters. The structure for this object is based on each individual template.

Kind: instance property of Generator

generator.originalAsyncAPI : String

AsyncAPI string to use as a source.

Kind: instance property of Generator

generator.generate(asyncapiDocument) ⇒ Promise

Generates files from a given template and an AsyncAPIDocument object.

Kind: instance method of Generator

Param Type Description
asyncapiDocument AsyncAPIDocument AsyncAPIDocument object to use as source.

Example

generator
  .generate(myAsyncAPIdocument)
  .then(() => {
    console.log('Done!');
  })
  .catch(console.error);

Example (Using async/await)

try {
  await generator.generate(myAsyncAPIdocument);
  console.log('Done!');
} catch (e) {
  console.error(e);
}

generator.configureTemplate()

Configure the templates based the desired renderer.

Kind: instance method of Generator

generator.generateFromString(asyncapiString, [parserOptions]) ⇒ Promise

Generates files from a given template and AsyncAPI string.

Kind: instance method of Generator

Param Type Default Description
asyncapiString String AsyncAPI string to use as source.
[parserOptions] Object {} AsyncAPI parser options. Check out @asyncapi/parser for more information.

Example

const asyncapiString = `
asyncapi: '2.0.0'
info:
  title: Example
  version: 1.0.0
...
`;
generator
  .generateFromString(asyncapiString)
  .then(() => {
    console.log('Done!');
  })
  .catch(console.error);

Example (Using async/await)

const asyncapiString = `
asyncapi: '2.0.0'
info:
  title: Example
  version: 1.0.0
...
`;

try {
  await generator.generateFromString(asyncapiString);
  console.log('Done!');
} catch (e) {
  console.error(e);
}

generator.generateFromURL(asyncapiURL) ⇒ Promise

Generates files from a given template and AsyncAPI file stored on external server.

Kind: instance method of Generator

Param Type Description
asyncapiURL String Link to AsyncAPI file

Example

generator
  .generateFromURL('https://example.com/asyncapi.yaml')
  .then(() => {
    console.log('Done!');
  })
  .catch(console.error);

Example (Using async/await)

try {
  await generator.generateFromURL('https://example.com/asyncapi.yaml');
  console.log('Done!');
} catch (e) {
  console.error(e);
}

generator.generateFromFile(asyncapiFile) ⇒ Promise

Generates files from a given template and AsyncAPI file.

Kind: instance method of Generator

Param Type Description
asyncapiFile String AsyncAPI file to use as source.

Example

generator
  .generateFromFile('asyncapi.yaml')
  .then(() => {
    console.log('Done!');
  })
  .catch(console.error);

Example (Using async/await)

try {
  await generator.generateFromFile('asyncapi.yaml');
  console.log('Done!');
} catch (e) {
  console.error(e);
}

generator.installTemplate([force])

Downloads and installs a template and its dependencies

Kind: instance method of Generator

Param Type Default Description
[force] Boolean false Whether to force installation (and skip cache) or not.

Generator.getTemplateFile(templateName, filePath, [templatesDir]) ⇒ Promise

Returns the content of a given template file.

Kind: static method of Generator

Param Type Default Description
templateName String Name of the template to generate.
filePath String Path to the file to render. Relative to the template directory.
[templatesDir] String DEFAULT_TEMPLATES_DIR Path to the directory where the templates are installed.

Example

const Generator = require('asyncapi-generator');
const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html');

Example (Using a custom `templatesDir`)

const Generator = require('asyncapi-generator');
const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates');