Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build : Added pacote dependency with error handling #1320

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions apps/generator/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ title: "Introduction"
weight: 10
---

The AsyncAPI generator is a tool that generates anything you want using the **[AsyncAPI Document](generator/asyncapi-document)** and **[Template](generator/template)** that are supplied as inputs to the AsyncAPI CLI. The generator was built with extensibility in mind; you can use the generator to generate anything you want, provided that it can be defined in a template, such as code, diagrams, markdown files, microservices, and applications. A number of [community-maintained templates](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) are now available for immediate usage.
The AsyncAPI generator is a tool that generates anything you want using the **[AsyncAPI Document](asyncapi-document.md)** and **[Template](template.md)** that are supplied as inputs to the AsyncAPI CLI. The generator was built with extensibility in mind; you can use the generator to generate anything you want, provided that it can be defined in a template, such as code, diagrams, markdown files, microservices, and applications. A number of [community-maintained templates](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) are now available for immediate usage.

> **Note:**
> If your primary objective is to generate models/classes for your event-driven architecture apps, use [AsyncAPI Modelina](/docs/tools/generator/model-generation), which is supported in the AsyncAPI CLI, instead of using the AsyncAPI Generator. Modelina is specifically designed for model generation and provides utilities for working with the AsyncAPI document.
> If your primary objective is to generate models/classes for your event-driven architecture apps, use [AsyncAPI Modelina](model-generation.md), which is supported in the AsyncAPI CLI, instead of using the AsyncAPI Generator. Modelina is specifically designed for model generation and provides utilities for working with the AsyncAPI document.

### Generator use cases
- Generation of interactive and understandable API documentation
Expand All @@ -19,12 +19,12 @@ The AsyncAPI generator is a tool that generates anything you want using the **[A
- Number of community maintained AsyncAPI templates

### Generation process
1. The **Generator** receives the **[Template](generator/template)** and **[AsyncAPI Document](generator/asyncapi-document)** as inputs.
2. The **Generator** sends to the **[Parser](generator/parser)** the **asyncapiString** which is a stringified version of the original **AsyncAPI Document**.
1. The **Generator** receives the **[Template](template.md)** and **[AsyncAPI Document](asyncapi-document.md)** as inputs.
2. The **Generator** sends to the **[Parser](parser.md)** the **asyncapiString** which is a stringified version of the original **AsyncAPI Document**.
3. The **Parser** uses additional plugins such as the OpenAPI, RAML, or Avro schemas to validate custom schemas of message payloads defined in the **AsyncAPI Document**.
4. If the **Parser** determines that the original **AsyncAPI Document** is valid, it manipulates the document and returns a set of helper functions and properties and bundles them together into an **asyncapi** variable that is an instance of [**AsyncAPIDocument**](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#asyncapidocument). The **asyncapi** helper functions make it easier to access the contents of the AsyncAPI Document.
5. At this point, the **Generator** passes the **[asyncapi](generator/asyncapi-document#method-2-asyncapi-and-template)**, the **[originalAsyncAPI](generator/asyncapi-document#method-1-originalasyncapi-and-template)**, and the **params** which collectively make up the **[Template Context](generator/template-context)** to the **Render Engine**.
6. AsyncAPI has two **Render Engines**([react](generator/react-render-engine) and [nunjucks](generator/nunjucks-render-engine)). Depending on which one you've specified in your `package.json`, the **Generator** knows the right **Render Engine** to pass both the **Template Files** and the **Template Context**.
5. At this point, the **Generator** passes the **[asyncapi](asyncapi-document.md)**, the **[originalAsyncAPI](asyncapi-document.md#method-1-originalasyncapi-and-template)**, and the **params** which collectively make up the **[Template Context](template-context.md)** to the **Render Engine**.
6. AsyncAPI has two **Render Engines**([react](react-render-engine.md) and [nunjucks](nunjucks-render-engine.md)). Depending on which one you've specified in your `package.json`, the **Generator** knows the right **Render Engine** to pass both the **Template Files** and the **Template Context**.
7. Once the **Render Engine** receives the **Template Files** and the **Template Context**, it injects all the dynamic values in your react or nunjucks based **Template Files** using the **Template Context**. As a result, the **Render Engine** generates **markdown**, **pdf**, **boilerplate code**, and **anything else** you specified to be generated as output.

> You can generate anything you want using the generator as long as it can be defined in a **Template**.
Expand Down
13 changes: 11 additions & 2 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const filenamify = require('filenamify');
const git = require('simple-git');
const log = require('loglevel');
const Arborist = require('@npmcli/arborist');
const pacote=require('pacote');
const Config = require('@npmcli/config');
const requireg = require('requireg');
const npmPath = requireg.resolve('npm').replace('index.js','');
Expand Down Expand Up @@ -605,8 +606,16 @@ class Generator {
save: false
});

const addResult = arb[Symbol.for('resolvedAdd')];
if (!addResult) throw new Error('Unable to resolve the name of the added package. It was most probably not added to node_modules successfully');
const manifest= await pacote.manifest(this.templateName);
const packageName= manifest.name;

try{
const manifest=await pacote.manifest(this.templateName);
const packageName=manifest.name;
} catch(error){
console.error('Error fetching package manifest:', error);
throw new Error('Failed to fetch the package manifest. Please check the package name and try again.');
}

const packageName = addResult[0].name;
const packageVersion = installResult.children.get(packageName).version;
Expand Down
1 change: 1 addition & 0 deletions apps/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"minimatch": "^3.0.4",
"node-fetch": "^2.6.0",
"nunjucks": "^3.2.0",
"pacote":"^20.0.0",
"requireg": "^0.2.2",
"resolve-from": "^5.0.0",
"resolve-pkg": "^2.0.0",
Expand Down
7 changes: 7 additions & 0 deletions apps/generator/test/__mocks__/pacote/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const pacote=jest.genMockFromModule('pacote');
pacote.manifest=jest.fn(async (templateName)=>{
return{
name:'test'
};
});
module.exports=pacote;
Loading