Skip to content

Commit

Permalink
Merge pull request #23450 from mshima/skip_ci-blueprint
Browse files Browse the repository at this point in the history
Many adjusts
  • Loading branch information
DanielFran committed Sep 8, 2023
2 parents ad45df0 + 2925358 commit f0107f9
Show file tree
Hide file tree
Showing 25 changed files with 301 additions and 52 deletions.
8 changes: 5 additions & 3 deletions cli/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/

import { dirname, relative } from 'path';
import { dirname, join, relative } from 'path';
import { fileURLToPath } from 'url';
import { existsSync } from 'fs';
import semver from 'semver';
Expand Down Expand Up @@ -45,9 +45,11 @@ if (!process.argv.includes('--skip-checks')) {
}
}

const appFolderOrWorkspaceRoot = existsSync('../node_modules') ? join(process.cwd(), '..') : process.cwd();
// If this file is not inside app npm repository and the executable exists inside the repository show warning.
if (
relative(__dirname, process.cwd()).startsWith('..') &&
(existsSync('node_modules/.bin/jhipster') || existsSync('../node_modules/.bin/jhipster'))
relative(appFolderOrWorkspaceRoot, __dirname).startsWith('..') &&
existsSync(join(appFolderOrWorkspaceRoot, 'node_modules/.bin/jhipster'))
) {
logger.warn(`Since JHipster v8, the jhipster command will not use the locally installed generator-jhipster.
If you want to execute the locally installed generator-jhipster, run: ${chalk.yellow('npx jhipster')}`);
Expand Down
2 changes: 1 addition & 1 deletion cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default class EnvironmentBuilder {
const generators = await this.env.lookup({ packagePaths: [localBlueprintPath], lookups: ['.'] });
if (generators.length > 0) {
this.env.alias(/^@jhipster\/jhipster-local(:(.*))?$/, '.blueprint$1');
this.env.sharedOptions.localBlueprint = true;
this.env.sharedOptions.composeWithLocalBlueprint = true;
}
}
return this;
Expand Down
3 changes: 2 additions & 1 deletion generators/base-application/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { SpringBootSourceType } from '../server/types.mjs';
import { ClientSourceType } from '../client/types.mjs';
import { LanguageSourceType } from '../languages/types.js';
import command from './command.mjs';
import { JHipsterGeneratorFeatures, JHipsterGeneratorOptions } from '../base/api.mjs';

const { upperFirst } = _;

Expand Down Expand Up @@ -99,7 +100,7 @@ export default class BaseApplicationGenerator<

static POST_WRITING_ENTITIES = asPriority(POST_WRITING_ENTITIES);

constructor(args, options, features) {
constructor(args: string | string[], options: JHipsterGeneratorOptions, features: JHipsterGeneratorFeatures) {
super(args, options, features);

if (this.options.help) {
Expand Down
36 changes: 36 additions & 0 deletions generators/base-core/generator-core.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ describe('generator - base-core', () => {
Dummy = helpers.createDummyGenerator(Base);
});

it('no argument', async () => {
const base = new Dummy([], { sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
jdlFiles: {
type: String,
},
});
jestExpect(base.jdlFiles).toBe(undefined);
});
it('undefined positional arguments', async () => {
const base = new Dummy({ positionalArguments: [], sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
jdlFiles: {
type: String,
},
});
jestExpect(base.jdlFiles).toBe(undefined);
});
it('undefined argument', async () => {
const base = new Dummy([undefined], { sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
jdlFiles: {
type: String,
},
});
jestExpect(base.jdlFiles).toBe(undefined);
});
it('undefined positional arguments', async () => {
const base = new Dummy({ positionalArguments: [undefined], sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
jdlFiles: {
type: String,
},
});
jestExpect(base.jdlFiles).toBe(undefined);
});
it('string arguments', async () => {
const base = new Dummy(['foo'], { sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
Expand Down
6 changes: 4 additions & 2 deletions generators/base-core/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
argument = positionalArguments;
positionalArguments = [];
}
const convertedValue = !argumentDef.type || argumentDef.type === Array ? argument : argumentDef.type(argument as any);
this[argumentName] = convertedValue;
if (argument !== undefined) {
const convertedValue = !argumentDef.type || argumentDef.type === Array ? argument : argumentDef.type(argument as any);
this[argumentName] = convertedValue;
}
} else {
if (argumentDef.required) {
throw new Error(`Missing required argument ${argumentName}`);
Expand Down
2 changes: 2 additions & 0 deletions generators/base/api.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type JHipsterGeneratorOptions = BaseOptions & {
blueprints?: string;
blueprint?: any;
jhipsterContext?: any;
composeWithLocalBlueprint?: boolean;

/* generate-blueprint options */
localBlueprint?: boolean;
Expand Down Expand Up @@ -63,6 +64,7 @@ export type JHipsterGeneratorFeatures = BaseFeatures & {
priorityArgs?: boolean;
jhipster7Migration?: boolean;
sbsBlueprint?: boolean;
checkBlueprint?: boolean;
jhipsterBootstrap?: boolean;
/**
* Store current version at .yo-rc.json.
Expand Down
8 changes: 7 additions & 1 deletion generators/base/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export default class JHipsterBaseBlueprintGenerator<
// jhipsterContext is the original generator
this.jhipsterContext = jhipsterContext;

if (this.getFeatures().checkBlueprint) {
if (!this.jhipsterContext) {
throw new Error(`This is a JHipster blueprint and should be used only like ${chalk.yellow('jhipster --blueprints ionic')}`);
}
}

try {
// Fallback to the original generator if the file does not exists in the blueprint.
const blueprintedTemplatePath = this.jhipsterTemplatePath();
Expand Down Expand Up @@ -427,7 +433,7 @@ export default class JHipsterBaseBlueprintGenerator<
}

let blueprints = this.jhipsterConfig.blueprints || [];
if (this.options.localBlueprint) {
if (this.options.composeWithLocalBlueprint) {
blueprints = blueprints.concat({ name: '@jhipster/local' });
}
const composedBlueprints: any[] = [];
Expand Down
44 changes: 31 additions & 13 deletions generators/bootstrap/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
createPrettierTransform,
createForceWriteConfigFilesTransform,
autoCrlfTransform,
isPrettierConfigFile,
isPrettierConfigFilePath,
createSortConfigFilesTransform,
createESLintTransform,
} from './support/index.mjs';
Expand All @@ -38,7 +38,7 @@ import { createRemoveUnusedImportsTransform } from './support/java-unused-import
import { loadStoredAppOptions } from '../app/support/index.mjs';

const { MULTISTEP_TRANSFORM, PRE_CONFLICTS } = PRIORITY_NAMES;
const { MULTISTEP_TRANSFORM_QUEUE } = QUEUES;
const { MULTISTEP_TRANSFORM_QUEUE, PRE_CONFLICTS_QUEUE } = QUEUES;

const MULTISTEP_TRANSFORM_PRIORITY = BaseGenerator.asPriority(MULTISTEP_TRANSFORM);
const PRE_CONFLICTS_PRIORITY = BaseGenerator.asPriority(PRE_CONFLICTS);
Expand Down Expand Up @@ -87,6 +87,12 @@ export default class BootstrapGenerator extends BaseGenerator {
return {
queueTransform() {
this.queueMultistepTransform();

this.env.sharedFs.on('change', filePath => {
if (createMultiStepTransform().templateFileFs.isTemplate(filePath)) {
this.queueMultistepTransform();
}
});
},
};
}
Expand All @@ -97,9 +103,14 @@ export default class BootstrapGenerator extends BaseGenerator {

get preConflicts(): GenericTaskGroup<this, BaseGeneratorDefinition['preConflictsTaskParam']> {
return {
async commitPrettierConfig() {
const filter = file => isFilePending(file) && isPrettierConfigFile(file);
await this.commitSharedFs(this.env.sharedFs.stream({ filter }));
async queueCommitPrettierConfig() {
await this.queueCommitPrettierConfig();

this.env.sharedFs.on('change', filePath => {
if (isPrettierConfigFilePath(filePath)) {
this.queueCommitPrettierConfig();
}
});
},
};
}
Expand All @@ -125,16 +136,23 @@ export default class BootstrapGenerator extends BaseGenerator {
queueName: MULTISTEP_TRANSFORM_QUEUE,
once: true,
});
}

const onChangeListener = file => {
if (createMultiStepTransform().templateFileFs.isTemplate(file)) {
this.queueMultistepTransform();
} else {
this.env.sharedFs.once('change', onChangeListener);
}
};
queueCommitPrettierConfig() {
this.queueTask({
method: async () => {
await this.commitPrettierConfig();
},
taskName: 'commitPrettierConfig',
queueName: PRE_CONFLICTS_QUEUE,
once: true,
});
}

this.env.sharedFs.once('change', onChangeListener);
async commitPrettierConfig() {
const filter = file => isFilePending(file) && isPrettierConfigFilePath(file.path);
await this.commitSharedFs(this.env.sharedFs.stream({ filter }));
this.log.ok('committed prettier configuration files');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion generators/bootstrap/support/prettier-support.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import type { MemFsEditorFile, VinylMemFsEditorFile } from 'mem-fs-editor';
import type CoreGenerator from '../../base-core/index.mjs';

const minimatch = new Minimatch('**/{.prettierrc**,.prettierignore}');
export const isPrettierConfigFile = (file: MemFsEditorFile) => minimatch.match(file.path);
export const isPrettierConfigFilePath = (filePath: string) => minimatch.match(filePath);
export const isPrettierConfigFile = (file: MemFsEditorFile) => isPrettierConfigFilePath(file.path);

export const createPrettierTransform = async function (
this: CoreGenerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

exports[`generator - generate-blueprint with all option should match snapshot 1`] = `
{
".blueprint/cli/commands.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/command.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/generator.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/index.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/templates/samples/sample.jdl": {
"stateCleared": "modified",
},
".eslintrc.json": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -883,6 +898,21 @@ exports[`generator - generate-blueprint with all option should match snapshot 1`

exports[`generator - generate-blueprint with default config should write files and match snapshot 1`] = `
{
".blueprint/cli/commands.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/command.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/generator.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/index.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/templates/samples/sample.jdl": {
"stateCleared": "modified",
},
".eslintrc.json": {
"stateCleared": "modified",
},
Expand Down
8 changes: 8 additions & 0 deletions generators/generate-blueprint/files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ export const files = {
'README.md',
'tsconfig.json',
'vitest.config.ts',
'.blueprint/cli/commands.mjs',
'.blueprint/generate-sample/command.mjs',
'.blueprint/generate-sample/generator.mjs',
'.blueprint/generate-sample/index.mjs',
],
},
{
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && !ctx.sampleWritten,
templates: ['.blueprint/generate-sample/templates/samples/sample.jdl'],
},
{
condition: ctx => ctx.cli,
templates: ['cli/cli.cjs'],
Expand Down
7 changes: 3 additions & 4 deletions generators/generate-blueprint/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ export default class extends BaseGenerator {
get writing() {
return {
async writing() {
this.application.sampleWritten = this.jhipsterConfig.sampleWritten;
await this.writeFiles({
sections: files,
context: this.application,
});
this.jhipsterConfig.sampleWritten = true;
},
async writingGenerators() {
if (!this.application[GENERATORS]) return;
Expand Down Expand Up @@ -257,10 +259,7 @@ export default class extends BaseGenerator {
pretest: 'npm run prettier-check && npm run lint',
test: 'vitest run',
'update-snapshot': 'vitest run --update',
vitest: mainDependencies.vitest,
},
dependencies: {
chalk: `${mainDependencies.chalk}`,
vitest: 'vitest',
},
devDependencies: {
'ejs-lint': `${mainDependencies['ejs-lint']}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2013-2023 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const defaultCommands = {
'generate-sample': {
desc: 'Generate a test sample',
blueprint: '@jhipster/jhipster-dev',
},
};

export default defaultCommands;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2013-2023 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { GENERATOR_APP } from 'generator-jhipster/generators';
/**
* @type {import('generator-jhipster').JHipsterCommandDefinition}
*/
const command = {
arguments: {
sampleName: {
type: String,
},
},
options: {},
import: [GENERATOR_APP],
};

export default command;
Loading

0 comments on commit f0107f9

Please sign in to comment.