Skip to content

Commit

Permalink
introduce logguer class
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcharl committed Jan 20, 2023
1 parent fd873a0 commit 4f1f890
Show file tree
Hide file tree
Showing 30 changed files with 178 additions and 150 deletions.
5 changes: 2 additions & 3 deletions generators/angular/needle-api/needle-client-angular.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import needleClientBase from '../../client/needle-api/needle-client.mjs';
import { LINE_LENGTH } from '../../generator-constants.mjs';
import { checkStringInFile, replaceContent, checkRegexInFile } from '../../utils.mjs';
import { stripMargin } from '../../base/support/index.mjs';
import { logDebug } from '../../base/support/index.mjs';

export default class extends needleClientBase {
addGlobalSCSSStyle(style, comment) {
Expand Down Expand Up @@ -145,7 +144,7 @@ export default class extends needleClientBase {
iconImport +
chalk.yellow('.\n')
);
logDebug(this.generator, 'Error:', e);
this.generator.logguer.debug('Error:', e);
}
}
}
Expand Down Expand Up @@ -243,7 +242,7 @@ export default class extends needleClientBase {
this._addRoute(entityUrl, modulePath, undefined, 'jhipster-needle-add-entity-route', entityModulePath, pageTitle);
}
} catch (e) {
logDebug(this.generator, 'Error:', e);
this.generator.logguer.debug('Error:', e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion generators/app/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export default class JHipsterAppGenerator extends BaseGenerator {
if (this.skipChecks) {
return;
}
checkNode(this);
checkNode(this.logguer);
},

checkForNewJHVersion() {
Expand Down
9 changes: 3 additions & 6 deletions generators/app/support/check-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import semver from 'semver';
import chalk from 'chalk';

import { warning } from '../../base/support/index.mjs';
import { packageJson } from '../../../lib/index.mjs';

const isNodeVersionCompliantWithRequirement = (gatheredFromEnvironment, requiredVersion) => {
Expand All @@ -44,20 +43,18 @@ const requiredEngineFromPackageJson = () => {
* @private
* Check if Node is installed, up to date, and in LTS version
*/
const checkNode = yeomanContext => {
const checkNode = logguer => {
const requiredNodeVersionFromPackageJson = requiredEngineFromPackageJson();
const currentNodeVersion = getNodeVersionFromCurrentProcess();
if (isNodeVersionCompliantWithRequirement(currentNodeVersion, requiredNodeVersionFromPackageJson)) {
warning(
yeomanContext,
logguer.warn(
`Your NodeJS version is too old (${currentNodeVersion}). You should use at least NodeJS ${chalk.bold(
requiredNodeVersionFromPackageJson
)}`
);
}
if (!isNodeLTS(getNodeReleaseFromCurrentProcess())) {
warning(
yeomanContext,
logguer.warn(
'Your Node version is not LTS (Long Term Support), use it at your own risk! JHipster does not support non-LTS releases, so if you encounter a bug, please use a LTS version first.'
);
}
Expand Down
22 changes: 11 additions & 11 deletions generators/base-application/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class BaseApplicationGenerator extends BaseGenerator {

/* Add tasks allowing entities priorities to match normal priorities pattern */
this.on('queueOwnTasks', () => {
this.debug('Queueing entity tasks');
this.logguer.debug('Queueing entity tasks');
this.queueEntityTasks();
});

Expand Down Expand Up @@ -383,7 +383,7 @@ export default class BaseApplicationGenerator extends BaseGenerator {
*/
resetEntitiesFakeData(seed) {
seed = `${this.sharedData.getApplication().baseName}-${seed}`;
this.debug(`Reseting entities seed with '${seed}'`);
this.logguer.debug(`Reseting entities seed with '${seed}'`);
this.sharedData.getEntities().forEach(({ entity }) => {
entity.resetFakerSeed(seed);
});
Expand Down Expand Up @@ -575,10 +575,10 @@ export default class BaseApplicationGenerator extends BaseGenerator {
taskName: 'queueConfiguringEachEntity',
cancellable: true,
method: () => {
this.debug(`Queueing entity tasks ${CONFIGURING_EACH_ENTITY}`);
this.logguer.debug(`Queueing entity tasks ${CONFIGURING_EACH_ENTITY}`);
const tasks = this.extractTasksFromPriority(CONFIGURING_EACH_ENTITY, { skip: false });
this.getEntitiesDataToConfigure().forEach(({ entityName, entityStorage, entityConfig }) => {
this.debug(`Queueing entity tasks ${CONFIGURING_EACH_ENTITY} for ${entityName}`);
this.logguer.debug(`Queueing entity tasks ${CONFIGURING_EACH_ENTITY} for ${entityName}`);
const args = this.getArgsForPriority(CONFIGURING_EACH_ENTITY);
tasks.forEach(task => {
this.queueTask({
Expand All @@ -595,9 +595,9 @@ export default class BaseApplicationGenerator extends BaseGenerator {
taskName: 'queueLoadingEntities',
cancellable: true,
method: () => {
this.debug(`Queueing entity tasks ${LOADING_ENTITIES}`);
this.logguer.debug(`Queueing entity tasks ${LOADING_ENTITIES}`);
const tasks = this.extractTasksFromPriority(LOADING_ENTITIES, { skip: false });
this.debug(`Queueing entity tasks ${LOADING_ENTITIES}`);
this.logguer.debug(`Queueing entity tasks ${LOADING_ENTITIES}`);
const args = this.getArgsForPriority(LOADING_ENTITIES);
tasks.forEach(task => {
this.queueTask({
Expand All @@ -613,10 +613,10 @@ export default class BaseApplicationGenerator extends BaseGenerator {
taskName: 'queuePreparingEachEntity',
cancellable: true,
method: () => {
this.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY}`);
this.logguer.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY}`);
const tasks = this.extractTasksFromPriority(PREPARING_EACH_ENTITY, { skip: false });
this.getEntitiesDataToPrepare().forEach(({ description, ...data }) => {
this.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY} for ${description}`);
this.logguer.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY} for ${description}`);
const args = this.getArgsForPriority(PREPARING_EACH_ENTITY);
tasks.forEach(task => {
this.queueTask({
Expand All @@ -635,7 +635,7 @@ export default class BaseApplicationGenerator extends BaseGenerator {
method: () => {
const tasks = this.extractTasksFromPriority(PREPARING_EACH_ENTITY_FIELD, { skip: false });
this.getEntitiesFieldsDataToPrepare().forEach(({ description, ...data }) => {
this.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY_FIELD} for ${description}`);
this.logguer.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY_FIELD} for ${description}`);
const args = this.getArgsForPriority(PREPARING_EACH_ENTITY_FIELD);
tasks.forEach(task => {
this.queueTask({
Expand All @@ -654,7 +654,7 @@ export default class BaseApplicationGenerator extends BaseGenerator {
method: () => {
const tasks = this.extractTasksFromPriority(PREPARING_EACH_ENTITY_RELATIONSHIP, { skip: false });
this.getEntitiesRelationshipsDataToPrepare().forEach(({ description, ...data }) => {
this.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY_RELATIONSHIP} for ${description}`);
this.logguer.debug(`Queueing entity tasks ${PREPARING_EACH_ENTITY_RELATIONSHIP} for ${description}`);
const args = this.getArgsForPriority(PREPARING_EACH_ENTITY_RELATIONSHIP);
tasks.forEach(task => {
this.queueTask({
Expand All @@ -673,7 +673,7 @@ export default class BaseApplicationGenerator extends BaseGenerator {
method: () => {
const tasks = this.extractTasksFromPriority(POST_PREPARING_EACH_ENTITY, { skip: false });
this.getEntitiesDataToPostPrepare().forEach(({ description, ...data }) => {
this.debug(`Queueing entity tasks ${POST_PREPARING_EACH_ENTITY} for ${description}`);
this.logguer.debug(`Queueing entity tasks ${POST_PREPARING_EACH_ENTITY} for ${description}`);
const args = this.getArgsForPriority(POST_PREPARING_EACH_ENTITY);
tasks.forEach(task => {
this.queueTask({
Expand Down
3 changes: 2 additions & 1 deletion generators/base-docker/docker-base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createBase64Secret } from '../../lib/utils/secret-utils.mjs';
import { applicationTypes, buildToolTypes, getConfigWithDefaults } from '../../jdl/jhipster/index.mjs';
import { removeFieldsWithUnsetValues } from '../base/support/index.mjs';
import { handleError, removeFieldsWithUnsetValues } from '../base/support/index.mjs';

const { MAVEN } = buildToolTypes;
const { MONOLITH, MICROSERVICE, GATEWAY } = applicationTypes;

Expand Down Expand Up @@ -97,7 +98,7 @@ export function loadConfigs() {
const serverPort = 8080;

// Loading configs
logDebug(this, `Apps folders: ${this.appsFolders}`);
this.logguer.debug(`Apps folders: ${this.appsFolders}`);
this.appsFolders.forEach((appFolder, index) => {
const path = this.destinationPath(`${this.directoryPath + appFolder}`);
if (this.fs.exists(`${path}/.yo-rc.json`)) {
Expand Down
2 changes: 1 addition & 1 deletion generators/base-docker/docker-prompts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export function getAppFolders(input, deploymentApplicationType) {
}
} catch (err) {
this.log(chalk.red(`${file}: this .yo-rc.json can't be read`));
this.debug('Error:', err);
this.logguer.debug('Error:', err);
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions generators/base/generator-base-blueprint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import { warning } from './support/index.mjs';
import { packageJson } from '../../lib/index.mjs';
import { packageNameToNamespace } from '../utils.mjs';
import JHipsterBaseGenerator from './generator-base.mjs';
import { logDebug } from './support/index.mjs';
import { mergeBlueprints, parseBluePrints, loadBlueprintsFromConfiguration, normalizeBlueprintName } from '../../utils/blueprint.mjs';
import { PRIORITY_NAMES } from './priorities.mjs';

Expand Down Expand Up @@ -66,7 +64,7 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
// Fallback to the original generator if the file does not exists in the blueprint.
this.jhipsterTemplatesFolders.push(this.jhipsterTemplatePath());
} catch (error) {
warning(this, 'Error adding current blueprint templates as alternative for JHipster templates.');
this.logguer.warn('Error adding current blueprint templates as alternative for JHipster templates.');
this.log(error);
}
}
Expand Down Expand Up @@ -460,7 +458,7 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
if (baseGeneratorPriorityName in this) {
const blueprintPriorityName = `${blueprintTaskPrefix}${priorityName}`;
if (!Object.hasOwn(Object.getPrototypeOf(blueprintGenerator), blueprintPriorityName)) {
logDebug(this, `Priority ${blueprintPriorityName} not implemented at ${blueprintGenerator.options.namespace}.`);
this.logguer.debug(`Priority ${blueprintPriorityName} not implemented at ${blueprintGenerator.options.namespace}.`);
}
}
}
Expand All @@ -475,7 +473,7 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
// check for old single blueprint declaration
const blueprint = this.options.blueprint;
if (blueprint) {
warning(this, '--blueprint option is deprecated. Please use --blueprints instead');
this.logguer.warn('--blueprint option is deprecated. Please use --blueprints instead');
if (!argvBlueprints.split(',').includes(blueprint)) {
argvBlueprints = `${blueprint},${argvBlueprints}`;
}
Expand Down Expand Up @@ -528,16 +526,14 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
await this.env.lookup({ filterPaths: true, packagePatterns: blueprint });
}
if (!(await this.env.get(generatorNamespace))) {
logDebug(
this,
this.logguer.debug(
`No blueprint found for blueprint ${chalk.yellow(blueprint)} and ${chalk.yellow(subGen)} with namespace ${chalk.yellow(
generatorNamespace
)} subgenerator: falling back to default generator`
);
return undefined;
}
logDebug(
this,
this.logguer.debug(
`Found blueprint ${chalk.yellow(blueprint)} and ${chalk.yellow(subGen)} with namespace ${chalk.yellow(generatorNamespace)}`
);

Expand Down Expand Up @@ -566,7 +562,7 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
const blueprintGeneratorName = packageNameToNamespace(blueprintPkgName);
const blueprintPackagePath = this.env.getPackagePath(blueprintGeneratorName);
if (!blueprintPackagePath) {
warning(this, `Could not retrieve packagePath of blueprint '${blueprintPkgName}'`);
this.logguer.warn(`Could not retrieve packagePath of blueprint '${blueprintPkgName}'`);
return undefined;
}
const packageJsonFile = path.join(blueprintPackagePath, 'package.json');
Expand All @@ -585,7 +581,7 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
_findBlueprintVersion(blueprintPkgName) {
const blueprintPackageJson = this._findBlueprintPackageJson(blueprintPkgName);
if (!blueprintPackageJson || !blueprintPackageJson.version) {
warning(this, `Could not retrieve version of blueprint '${blueprintPkgName}'`);
this.logguer.warn(`Could not retrieve version of blueprint '${blueprintPkgName}'`);
return undefined;
}
return blueprintPackageJson.version;
Expand All @@ -611,7 +607,7 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
_checkJHipsterBlueprintVersion(blueprintPkgName) {
const blueprintPackageJson = this._findBlueprintPackageJson(blueprintPkgName);
if (!blueprintPackageJson) {
warning(this, `Could not retrieve version of JHipster declared by blueprint '${blueprintPkgName}'`);
this.logguer.warn(`Could not retrieve version of JHipster declared by blueprint '${blueprintPkgName}'`);
return;
}
const mainGeneratorJhipsterVersion = packageJson.version;
Expand Down Expand Up @@ -642,6 +638,6 @@ export default class JHipsterBaseBlueprintGenerator extends JHipsterBaseGenerato
)} blueprint targets JHipster ${blueprintPeerJhipsterVersion} and is not compatible with this JHipster version. Either update the blueprint or JHipster. You can also disable this check using --skip-checks at your own risk`
);
}
warning(this, `Could not retrieve version of JHipster declared by blueprint '${blueprintPkgName}'`);
this.logguer.warn(`Could not retrieve version of JHipster declared by blueprint '${blueprintPkgName}'`);
}
}
2 changes: 2 additions & 0 deletions generators/base/generator-base-private.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Generator from 'yeoman-generator';
import shelljs from 'shelljs';

import { stringify } from '../../utils/index.mjs';
import { Logguer } from './support/logging.mjs';

/**
* @typedef {import('./api.mjs').JHipsterGeneratorFeatures} JHipsterGeneratorFeatures
Expand Down Expand Up @@ -53,6 +54,7 @@ export default class PrivateBase extends Generator {
}
// expose lodash to templates
this._ = _;
this.logguer = new Logguer(this.log, this.configOptions, this.options, this._debug);
}

/**
Expand Down
Loading

0 comments on commit 4f1f890

Please sign in to comment.