Skip to content

Commit

Permalink
feat: Use npmcli config in asyncapi generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierrick BOULÉ committed Feb 6, 2024
1 parent 7629c16 commit 35eb66c
Show file tree
Hide file tree
Showing 5 changed files with 830 additions and 36 deletions.
48 changes: 31 additions & 17 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const filenamify = require('filenamify');
const git = require('simple-git');
const log = require('loglevel');
const Arborist = require('@npmcli/arborist');
const Config = require('@npmcli/config');

const { isAsyncAPIDocument } = require('@asyncapi/parser/cjs/document');

const { configureReact, renderReact, saveRenderedReactContent } = require('./renderer/react');
Expand All @@ -31,6 +33,7 @@ const {
const { parse, usesNewAPI, getProperApiDocument } = require('./parser');
const { registerFilters } = require('./filtersRegistry');
const { registerHooks } = require('./hooksRegistry');
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')

Check failure on line 36 in lib/generator.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing semicolon

const FILTERS_DIRNAME = 'filters';
const HOOKS_DIRNAME = 'hooks';
Expand Down Expand Up @@ -532,7 +535,7 @@ class Generator {
arbOptions.registry = providedRegistry;
registryUrl = providedRegistry;
}

const domainName = registryUrl.replace(/^https?:\/\//, '');
//doing basic if/else so basically only one auth type is used and token as more secure is primary
if (this.registry.token) {
Expand All @@ -541,11 +544,13 @@ class Generator {
} else if (this.registry.auth) {
authorizationName = `//${domainName}:_auth`;
arbOptions[authorizationName] = this.registry.auth;
}
}

//not sharing in logs neither token nor auth for security reasons
log.debug(`Using npm registry ${registryUrl} and authorization type ${authorizationName} to handle template installation.`);
}


Check failure on line 553 in lib/generator.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

More than 1 blank line not allowed
/**
* Downloads and installs a template and its dependencies
*
Expand All @@ -556,14 +561,14 @@ class Generator {
let pkgPath;
let installedPkg;
let packageVersion;

try {
installedPkg = getTemplateDetails(this.templateName, PACKAGE_JSON_FILENAME);
pkgPath = installedPkg && installedPkg.pkgPath;
packageVersion = installedPkg && installedPkg.version;
log.debug(logMessage.templateSource(pkgPath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));

return {
name: installedPkg.name,
path: pkgPath
Expand All @@ -573,38 +578,47 @@ class Generator {
// We did our best. Proceed with installation...
}
}

const debugMessage = force ? logMessage.TEMPLATE_INSTALL_FLAG_MSG : logMessage.TEMPLATE_INSTALL_DISK_MSG;
log.debug(logMessage.installationDebugMessage(debugMessage));

if (isFileSystemPath(this.templateName)) log.debug(logMessage.NPM_INSTALL_TRIGGER);

const arbOptions = {
path: ROOT_DIR,
};
if (this.registry) {

const npmPath = process.execPath.replace('/bin/node','/lib/node_modules/npm/');

const config = new Config({
definitions,
flatten,
shorthands,
npmPath
})

Check failure on line 594 in lib/generator.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing semicolon

await config.load()

Check failure on line 596 in lib/generator.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing semicolon

const arbOptions = {...{path: ROOT_DIR}, ...config.flat};

if (!(Object.keys(this.registry).length === 0)) {

Check failure on line 600 in lib/generator.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Use the opposite operator (!==) instead
this.initialiseArbOptions(arbOptions);
}

const arb = new Arborist(arbOptions);

try {
const installResult = await arb.reify({
add: [this.templateName],
saveType: 'prod',
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 packageName = addResult[0].name;
const packageVersion = installResult.children.get(packageName).version;
const packagePath = installResult.children.get(packageName).path;

if (!isFileSystemPath(this.templateName)) log.debug(logMessage.templateSuccessfullyInstalled(packageName, packagePath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));

return {
name: packageName,
path: packagePath,
Expand Down
Loading

0 comments on commit 35eb66c

Please sign in to comment.