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

Improve jhipster cli global/local messages. #17836

Merged
merged 1 commit into from
Feb 14, 2022
Merged
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
30 changes: 19 additions & 11 deletions cli/jhipster.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,52 @@ const { logger } = require('./utils');
const currentNodeVersion = process.versions.node;
const minimumNodeVersion = packageJson.engines.node;

const BUNDLED_VERSION_MESSAGE = 'Using bundled JHipster';
const LOCAL_VERSION_MESSAGE = "Switching to JHipster installed locally in current project's node repository (node_modules)";

if (!semver.satisfies(currentNodeVersion, minimumNodeVersion)) {
logger.fatal(
`You are running Node version ${currentNodeVersion}\nJHipster requires Node version ${minimumNodeVersion}\nPlease update your version of Node.`
);
}

let preferLocal = true;
const preferLocalArg = process.argv.includes('--prefer-local');
const preferGlobalArg = process.argv.includes('--prefer-global') || process.argv.includes('--bundled');

if (preferLocalArg && preferGlobalArg) {
throw new Error('--prefer-local and --prefer-bundled cannot be used together');
}

// Don't use commander for parsing command line to avoid polluting it in cli.js
// --prefer-local: Always resolve node modules locally (useful when using linked module)
if ((process.argv.includes('upgrade') && !process.argv.includes('--prefer-local')) || process.argv.includes('--prefer-global')) {
// Prefer global version for `jhipster upgrade` to get most recent code
preferLocal = false;
}
const preferLocal = preferLocalArg || (!preferGlobalArg && !process.argv.includes('upgrade'));

requireCLI(preferLocal);

/*
* Require cli.js giving priority to local version over global one if it exists.
* Require cli.js giving priority to local version over bundled one if it exists.
*/
function requireCLI(preferLocal) {
let message = BUNDLED_VERSION_MESSAGE;
/* eslint-disable global-require */
if (preferLocal) {
try {
const localCLI = require.resolve(path.join(process.cwd(), 'node_modules', 'generator-jhipster', 'cli', 'cli.js'));
if (__dirname !== path.dirname(localCLI)) {
if (__dirname === path.dirname(localCLI)) {
message = LOCAL_VERSION_MESSAGE;
} else {
// load local version
/* eslint-disable import/no-dynamic-require */
logger.info("Using JHipster version installed locally in current project's node_modules");
logger.info(LOCAL_VERSION_MESSAGE);
require(localCLI);
return;
}
} catch (e) {
// Unable to find local version, so global one will be loaded anyway
// Unable to find local version, so bundled one will be loaded anyway
}
}
// load global version
logger.info('Using JHipster version installed globally');
// load current jhipster
logger.info(message);
require('./cli');
/* eslint-enable */
}
5 changes: 3 additions & 2 deletions cli/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ const createProgram = ({ executableName = CLI_NAME, executableVersion = JHIPSTER
.option('--skip-regenerate', "Don't regenerate identical files", false)
.option('--skip-yo-resolve', 'Ignore .yo-resolve files', false)
.addOption(new Option('--from-jdl', 'Allow every option jdl forwards').default(false).hideHelp())
.addOption(new Option('--prefer-global', 'Run jhipster installed globally').hideHelp())
.addOption(new Option('--prefer-local', 'Run jhipster installed locally').hideHelp())
.addOption(new Option('--bundled', 'Use JHipster generators bundled with current cli'))
.addOption(new Option('--prefer-global', 'Alias for --blundled').hideHelp())
.addOption(new Option('--prefer-local', 'Prefer JHipster generators installed in current folder node repository.').hideHelp())
);
};

Expand Down