Skip to content

Commit

Permalink
improve jhipster v7 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Apr 8, 2024
1 parent d199a46 commit 2e3e2e3
Show file tree
Hide file tree
Showing 3 changed files with 604 additions and 8 deletions.
47 changes: 39 additions & 8 deletions generators/migrate/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { transform } from 'p-transform';
import { globby } from 'globby';
import semver from 'semver';
import gitignore from 'parse-gitignore';
import { join, resolve } from 'path';
import { join } from 'path';
import latestVersion from 'latest-version';
import { loadFile } from 'mem-fs';
import { setModifiedFileState } from 'mem-fs-editor/state';
import { createCommitTransform } from 'mem-fs-editor/transform';
import ora from 'ora';
import { ResetMode } from 'simple-git';
import BaseGenerator from 'generator-jhipster/generators/base-application';
import nvexeca from 'nvexeca';
import {
createPrettierTransform,
createESLintTransform,
Expand Down Expand Up @@ -41,6 +42,8 @@ import command from './command.js';
import { GENERATOR_BOOTSTRAP } from 'generator-jhipster/generators';
import { fileURLToPath } from 'url';

const V7_NODE = '16.20.2';

export default class extends BaseGenerator {
/** @type {boolean} */
verbose;
Expand Down Expand Up @@ -100,7 +103,8 @@ export default class extends BaseGenerator {
if (this.verbose) {
this.options.askAnswered = true;
} else {
this.spawnCommandOptions = { stdio: 'ignore' };
// Set cwd since nvexeca does not provide a default cwd.
this.spawnCommandOptions = { stdio: 'ignore', cwd: this.destinationPath() };
}
},

Expand Down Expand Up @@ -472,7 +476,7 @@ export default class extends BaseGenerator {
}

async rmRf(file) {
const absolutePath = resolve(file);
const absolutePath = this.destinationPath(file);
if (this.verbose) {
this.log.verboseInfo(`Removing ${absolutePath}`);
}
Expand All @@ -497,9 +501,11 @@ export default class extends BaseGenerator {
}

async regenerate({ cli, jhipsterVersion, blueprints, type, cliOptions }) {
const spinner = this.verbose ? undefined : ora(`regenerating ${chalk.yellow(type)} application`);
const regenerateMessage = `regenerating ${chalk.yellow(type)} application using JHipster ${jhipsterVersion}`;
const spinner = this.verbose ? undefined : ora(regenerateMessage);
let requiresManualNode16;
if (this.verbose) {
this.log.info(`regenerating ${chalk.yellow(type)} application`);
this.log.info(regenerateMessage);
}

cliOptions = [...cliOptions, ...DEFAULT_CLI_OPTIONS.split(' ')];
Expand All @@ -526,7 +532,11 @@ export default class extends BaseGenerator {
this.log.info(`Running local npx ${cli} ${cliOptions.join(' ')}`);
spinner?.start?.();
await this.spawnCommand('npm install', this.spawnCommandOptions);
await this.spawn('npx', [cli, ...cliOptions], this.spawnCommandOptions);
if (this.isV7(this.getPackageJsonVersion())) {
await nvexeca(V7_NODE, cli, cliOptions, this.spawnCommandOptions);
} else {
await this.spawn('npx', [cli, ...cliOptions], this.spawnCommandOptions);
}
} else if (jhipsterVersion === 'bundled') {
const bundledCli = join(fileURLToPath(new URL('../../cli/cli.cjs', import.meta.url)));
cliOptions = ['app', ...cliOptions];
Expand All @@ -542,7 +552,18 @@ export default class extends BaseGenerator {
}

this.log.info(`Running npx ${cli} ${cliOptions.join(' ')}`);
if (this.isV7(jhipsterVersion)) {
requiresManualNode16 = true;
await this.prompt([
{
type: 'confirm',
name: 'installNode16',
message: `To generate the application using JHipster ${jhipsterVersion}, node 16 is required, install it globally now.`,
},
]);
}
spinner?.start?.();

Check failure on line 565 in generators/migrate/generator.js

View workflow job for this annotation

GitHub Actions / npm-test

Expected blank line before this statement

Check failure on line 565 in generators/migrate/generator.js

View workflow job for this annotation

GitHub Actions / npm-test

Expected blank line before this statement

await libexec({
yes: true,
npxCache: `${MIGRATE_TMP_FOLDER}/npx/${type}`,
Expand Down Expand Up @@ -570,13 +591,23 @@ export default class extends BaseGenerator {
}
} catch (error) {
if (spinner) {
spinner.fail(`successfully regenerated ${chalk.yellow(type)} application using ${message}`);
spinner.fail(`failed to regenerate ${chalk.yellow(type)} application using ${message}`);
} else {
this.log.error(`successfully regenerated ${chalk.yellow(type)} application using ${message}`);
this.log.error(`failed to regenerate ${chalk.yellow(type)} application using ${message}`);
}

throw error;
}

if (requiresManualNode16) {
await this.prompt([
{
type: 'confirm',
name: 'revertNode16',
message: 'Revert node version to the previous one.',
},
]);
}
}

/**
Expand Down
Loading

0 comments on commit 2e3e2e3

Please sign in to comment.