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 9, 2024
1 parent d199a46 commit d7bb04c
Show file tree
Hide file tree
Showing 5 changed files with 523 additions and 13 deletions.
2 changes: 2 additions & 0 deletions generators/migrate/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { dirname } from 'path';
import untildify from 'untildify';
import { fileURLToPath } from 'url';

export const V7_NODE = '16.20.2';

export const BASE_APPLICATION = 'source';
export const ACTUAL_APPLICATION = 'actual';

Expand Down
49 changes: 40 additions & 9 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 getNode from 'get-node';
import {
createPrettierTransform,
createESLintTransform,
Expand All @@ -35,6 +36,7 @@ import {
GIT_DRIVER_PACKAGEJSON_REF,
ACTUAL_APPLICATION,
BASE_APPLICATION,
V7_NODE,
} from './constants.js';
import { GENERATOR_JHIPSTER } from 'generator-jhipster';
import command from './command.js';
Expand Down Expand Up @@ -472,7 +474,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 +499,12 @@ 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);
const packageJsonJHipsterVersion = this.getPackageJsonVersion();
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 @@ -518,15 +523,20 @@ export default class extends BaseGenerator {
this.log.info(`Running command ${cli} ${cliOptions.join(' ')}`);
spinner?.start?.();
await this.spawn(cli, cliOptions, this.spawnCommandOptions);
} else if (jhipsterVersion === 'current' && this.getPackageJsonVersion()) {
if (this.isV7(this.getPackageJsonVersion())) {
} else if (jhipsterVersion === 'current' && packageJsonJHipsterVersion) {
if (this.isV7(packageJsonJHipsterVersion)) {
cliOptions = [...cliOptions, ...DEFAULT_CLI_OPTIONS_V7.split(' ')];
}

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(packageJsonJHipsterVersion)) {
const { path: nodePath } = await getNode(V7_NODE);
await this.spawn('npx', [cli, ...cliOptions], { ...this.spawnCommandOptions, execPath: nodePath, preferLocal: true });
} 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,6 +552,17 @@ 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?.();
await libexec({
yes: true,
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
8 changes: 4 additions & 4 deletions generators/migrate/generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe('SubGenerator migrate of migrate JHipster blueprint', () => {

it(
'generated git commits to match snapshot',
{
// Git order is not always the same.
retry: 5,
},
async () => {
const git = createGit();
const log = await git.log();
Expand All @@ -58,10 +62,6 @@ describe('SubGenerator migrate of migrate JHipster blueprint', () => {
Initial version of upgradeTest generated by generator-jhipster@undefined"
`);
},
{
// Git order is not always the same.
retry: 5,
},
);

it('generated branches to match snapshot', async () => {
Expand Down
Loading

0 comments on commit d7bb04c

Please sign in to comment.