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 7f94f8d
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 16 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
58 changes: 46 additions & 12 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 @@ -509,6 +514,7 @@ export default class extends BaseGenerator {

const blueprintInfo = blueprints.length > 0 ? ` and ${blueprints.map(bp => `${bp.name}@${bp.version}`).join(', ')} ` : '';
const message = `JHipster ${jhipsterVersion}${blueprintInfo}`;
let spawnCommandOptions = { ...this.spawnCommandOptions };
if (type === 'target') {
await this.removeJHipsterVersion();
}
Expand All @@ -517,22 +523,29 @@ export default class extends BaseGenerator {
if (jhipsterVersion === 'none') {
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())) {
await this.spawn(cli, cliOptions, spawnCommandOptions);
} else if (jhipsterVersion === 'current' && packageJsonJHipsterVersion) {
if (this.isV7(packageJsonJHipsterVersion)) {
cliOptions = [...cliOptions, ...DEFAULT_CLI_OPTIONS_V7.split(' ')];
}
cliOptions = [cli, ...cliOptions];

Check failure on line 531 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 531 in generators/migrate/generator.js

View workflow job for this annotation

GitHub Actions / npm-test

Expected blank line before this statement
cli = 'npx';

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);
spawnCommandOptions = { ...spawnCommandOptions, execPath: nodePath, preferLocal: true };
}

await this.spawn(cli, cliOptions, spawnCommandOptions);
} else if (jhipsterVersion === 'bundled') {
const bundledCli = join(fileURLToPath(new URL('../../cli/cli.cjs', import.meta.url)));
cli = join(fileURLToPath(new URL('../../cli/cli.cjs', import.meta.url)));
cliOptions = ['app', ...cliOptions];
this.log.info(`Running bundled ${bundledCli} ${cliOptions.join(' ')}`);

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

View workflow job for this annotation

GitHub Actions / npm-test

'bundledCli' is not defined

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

View workflow job for this annotation

GitHub Actions / npm-test

'bundledCli' is not defined
spinner?.start?.();
await this.spawn(bundledCli, cliOptions, this.spawnCommandOptions);
await this.spawn(cli, cliOptions, spawnCommandOptions);
} else {
if (jhipsterVersion === 'current') {
jhipsterVersion = this.getCurrentSourceVersion();
Expand All @@ -542,6 +555,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 +594,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 7f94f8d

Please sign in to comment.