generator: The generator does not use templates installed in node_modules
The bug
This installs @asyncapi/html-template twice.
npm i -D @asyncapi/generator @asyncapi/html-template
./node_modules/.bin/ag src/simple.yaml @asyncapi/html-template --force-write
repro: https://github.com/laat/repro-asyncapi-generator
Expected behavior
It should reuse installed templates. This lets me pin templates at a specific version
The repro has a patch
This solves the issue for me. Essentially require.resolve
does the heavy lifting and should support the intended uses of the replaced code.
diff --git a/node_modules/@asyncapi/generator/lib/generator.js b/node_modules/@asyncapi/generator/lib/generator.js
index 841af20..dc62139 100644
--- a/node_modules/@asyncapi/generator/lib/generator.js
+++ b/node_modules/@asyncapi/generator/lib/generator.js
@@ -320,31 +320,12 @@ class Generator {
return new Promise(async (resolve, reject) => {
if (!force) {
try {
- let installedPkg;
-
- if (isFileSystemPath(this.templateName)) {
- const pkg = require(path.resolve(this.templateName, PACKAGE_JSON_FILENAME));
- installedPkg = require(path.resolve(DEFAULT_TEMPLATES_DIR, pkg.name, PACKAGE_JSON_FILENAME));
- } else { // Template is not a filesystem path...
- const templatePath = path.resolve(DEFAULT_TEMPLATES_DIR, this.templateName);
- if (await isLocalTemplate(templatePath)) {
- // This "if" is covering the following workflow:
- // ag asyncapi.yaml ../html-template
- // The previous command installs a template called @asyncapi/html-template
- // And now we run the command again but with the resolved name:
- // ag asyncapi.yaml @asyncapi/html-template
- // The template name doesn't look like a file system path but we find
- // that the package is already installed and it's a symbolic link.
- const { resolvedLink } = await getLocalTemplateDetails(templatePath);
- log.debug(`This template has already been installed and it's pointing to your filesystem at ${resolvedLink}.`);
- }
- installedPkg = require(path.resolve(templatePath, PACKAGE_JSON_FILENAME));
- }
-
+ const pkgJsonPath = require.resolve(path.join(this.templateName, PACKAGE_JSON_FILENAME))
+ const installedPkg = require(pkgJsonPath)
return resolve({
name: installedPkg.name,
version: installedPkg.version,
- path: path.resolve(DEFAULT_TEMPLATES_DIR, installedPkg.name),
+ path: path.dirname(pkgJsonPath),
});
} catch (e) {
// We did our best. Proceed with installation...
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 17 (17 by maintainers)
ok, that makes sense
would you mind opening a PR, I’d love to help with testing different scenarios. I would work on your PR for a couple of days to make sure all works well
come one bot, can’t you see I’m working on it, doh!
@laat hey, it took some time, but it is finally here https://github.com/asyncapi/generator/pull/517 if you want and have time, please have a look at this PR
your use case will be fixed + I’ve added the integration tests that we were always missing
This issue has been automatically marked as stale because it has not had recent activity 😴 It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation. Thank you for your contributions ❤️
you know what, I did some tests locally and yeah, I decided to first write down supported scenarios:
local
html-template
withoutnode_modules
and localgenerator
withouthtml-template
innode_modules
local
html-template
withoutnode_modules
and localgenerator
withhtml-template
innode_modules
local
html-template
withnode_modules
and localgenerator
withouthtml-template
innode_modules
local
html-template
withoutnode_modules
and localgenerator
withouthtml-template
innode_modules
html-template
withoutnode_modules
and localgenerator
withhtml-template
innode_modules
html-template
withnode_modules
and localgenerator
withouthtml-template
innode_modules
The question would be what is wrong with
html-template
symlink ingenerator
innode_modules
that makes scenario 2 not work the same as scenario 1. And how come can this require https://github.com/asyncapi/generator/blob/master/lib/generator.js#L329 trigger scenario 1 to work.now some other question, did you try just this:
Yes.
First setting up the repository https://github.com/laat/repro-asyncapi-generator
Then see that it should install buffer 5.6.0 exactly:
Observe that it actually installs buffer 5.6.1:
@laat yeap, noticed. Will write down all possible test scenarios that need to be checked and starting from that point we will see how to approach testing. I don’t want to throw responsibility for writing missing tests on you, but if you want to work on them, I will not stop you 😄