diff --git a/docs/api.md b/docs/api.md index 076289df9..e6f219b5c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -11,7 +11,7 @@ * [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ Promise * [.installTemplate([force])](#Generator+installTemplate) * _static_ - * [.getTemplateFile(templateName, filePath, options)](#Generator.getTemplateFile) ⇒ Promise + * [.getTemplateFile(templateName, filePath, [templatesDir])](#Generator.getTemplateFile) ⇒ Promise @@ -30,7 +30,7 @@ Instantiates a new Generator object. | [options.disabledHooks] | Array.<String> | | List of hooks to disable. | | [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.forceInstall] | Boolean | false | Force the installation of the template and its dependencies. | +| [options.install] | Boolean | false | Install the template and its dependencies, even when the template has already been installed. | **Example** ```js @@ -162,19 +162,24 @@ Downloads and installs a template and its dependencies. -### Generator.getTemplateFile(templateName, filePath, options) ⇒ Promise +### Generator.getTemplateFile(templateName, filePath, [templatesDir]) ⇒ Promise Returns the content of a given template file. **Kind**: static method of [Generator](#Generator) -| Param | Type | Description | -| --- | --- | --- | -| templateName | String | Name of the template to generate. | -| filePath | String | Path to the file to render. Relative to the template directory. | -| options | Object | | +| 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** ```js const Generator = require('asyncapi-generator'); -const content = await Generator.getTemplateFile('html', '.partials/content.html'); +const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html'); +``` +**Example** *(Using a custom `templatesDir`)* +```js +const Generator = require('asyncapi-generator'); +const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates'); ``` diff --git a/lib/generator.js b/lib/generator.js index 8058460b4..8f20e9368 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -256,16 +256,20 @@ class Generator { * * @example * const Generator = require('asyncapi-generator'); - * const content = await Generator.getTemplateFile('html', 'partials/content.html'); + * 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'); * * @static * @param {String} templateName Name of the template to generate. * @param {String} filePath Path to the file to render. Relative to the template directory. - * @param {Object} options + * @param {String} [templatesDir=DEFAULT_TEMPLATES_DIR] Path to the directory where the templates are installed. * @return {Promise} */ - static async getTemplateFile(templateName, filePath) { - return await readFile(path.resolve(DEFAULT_TEMPLATES_DIR, templateName, filePath), 'utf8'); + static async getTemplateFile(templateName, filePath, templatesDir = DEFAULT_TEMPLATES_DIR) { + return await readFile(path.resolve(templatesDir, templateName, filePath), 'utf8'); } /**