Skip to content

Commit

Permalink
add node:prettier generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jun 23, 2024
1 parent 0dd4511 commit 85dafe1
Show file tree
Hide file tree
Showing 23 changed files with 465 additions and 162 deletions.
3 changes: 1 addition & 2 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Options:
--skip-jhipster-dependencies Don't write jhipster dependencies to package.json.
--creation-timestamp <value> Project creation timestamp (used for reproducible builds)
--jdl-store <value> JDL store
--prettier-tab-width <value> Default tab width for prettier
--skip-commit-hook Skip adding husky commit hooks
--prettier-tab-width <value> Default tab width for prettier
--db <value> Provide DB name for the application when skipping server side generation
--incremental-changelog Creates incremental database changelogs
--skip-user-management Skip the user management module during app generation
Expand Down Expand Up @@ -1345,7 +1345,6 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"reactorBlockOptional": ".blockOptional()",
"rememberMeKey": undefined,
"requiresDeleteAllUsers": true,
"routes": undefined,
"searchEngine": "no",
"searchEngineAny": false,
"searchEngineCouchbase": false,
Expand Down
1 change: 1 addition & 0 deletions generators/base-application/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type BaseApplication = {

skipClient?: boolean;
skipServer?: boolean;
monorepository?: boolean;

/** Customize templates sourceFile and destinationFile */
customizeTemplatePaths: Array<
Expand Down
3 changes: 3 additions & 0 deletions generators/base/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export type JHipsterGeneratorOptions = BaseOptions & {
entities?: string[];
disableBlueprints?: boolean;

/* init related */
fromInit?: boolean;

/* blueprint options */
blueprints?: string;
blueprint?: any;
Expand Down
10 changes: 1 addition & 9 deletions generators/common/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,16 @@ target/
"stateCleared": "modified",
},
".prettierrc": {
"contents": "# Prettier configuration
"contents": "
printWidth: 140
singleQuote: true
tabWidth: 2
useTabs: false
# js and ts rules:
arrowParens: avoid
# jsx and tsx rules:
bracketSameLine: false
plugins:
- prettier-plugin-packagejson
- prettier-plugin-java
# java rules:
overrides:
- files: "*.java"
options:
Expand Down
7 changes: 1 addition & 6 deletions generators/common/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import { GENERATOR_BOOTSTRAP_APPLICATION_BASE } from '../generator-list.js';

const command: JHipsterCommandDefinition = {
options: {
prettierTabWidth: {
description: 'Default tab width for prettier',
type: Number,
scope: 'storage',
},
skipCommitHook: {
description: 'Skip adding husky commit hooks',
type: Boolean,
Expand All @@ -45,7 +40,7 @@ const command: JHipsterCommandDefinition = {
scope: 'storage',
},
},
import: [GENERATOR_BOOTSTRAP_APPLICATION_BASE],
import: [GENERATOR_BOOTSTRAP_APPLICATION_BASE, 'jhipster:javascript:prettier'],
};

export default command;
8 changes: 0 additions & 8 deletions generators/common/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
* The default is to use a file path string. It implies use of the template method.
* For any other config an object { file:.., method:.., template:.. } can be used
*/
export const prettierConfigFiles = {
global: [
{
templates: ['.prettierrc', '.prettierignore.jhi'],
},
],
};

export const commonFiles = {
global: [
{
Expand Down
91 changes: 42 additions & 49 deletions generators/common/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { isFileStateModified } from 'mem-fs-editor/state';
import BaseApplicationGenerator from '../base-application/index.js';

import { writeFiles, prettierConfigFiles } from './files.js';
import { writeFiles } from './files.js';
import {
MAIN_DIR,
TEST_DIR,
Expand Down Expand Up @@ -100,37 +100,16 @@ export default class CommonGenerator extends BaseApplicationGenerator {
return this.delegateTasksToBlueprint(() => this.configuring);
}

get configuringEachEntity() {
return this.asConfiguringEachEntityTaskGroup({
migrateEntity({ entityConfig, entityStorage }) {
for (const field of entityConfig.fields) {
if (field.javadoc) {
field.documentation = field.javadoc;
delete field.javadoc;
}
if (field.fieldTypeJavadoc) {
field.fieldTypeDocumentation = field.fieldTypeJavadoc;
delete field.fieldTypeJavadoc;
}
}
for (const relationship of entityConfig.relationships) {
if (relationship.javadoc) {
relationship.documentation = relationship.javadoc;
delete relationship.javadoc;
}
}
if (entityConfig.javadoc) {
entityConfig.documentation = entityConfig.javadoc;
delete entityConfig.javadoc;
} else {
entityStorage.save();
}
get composing() {
return this.asComposingTaskGroup({
async composePrettier() {
await this.composeWithJHipster('jhipster:javascript:prettier');
},
});
}

get [BaseApplicationGenerator.CONFIGURING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.configuringEachEntity);
get [BaseApplicationGenerator.COMPOSING]() {
return this.delegateTasksToBlueprint(() => this.composing);
}

// Public API method used by the getter and also by Blueprints
Expand All @@ -145,12 +124,6 @@ export default class CommonGenerator extends BaseApplicationGenerator {
this.fetchFromInstalledJHipster(GENERATOR_COMMON, 'resources', 'package.json'),
);
},

loadConfig({ applicationDefaults }) {
applicationDefaults({
prettierTabWidth: this.jhipsterConfig.prettierTabWidth ?? 2,
});
},
});
}

Expand Down Expand Up @@ -185,6 +158,39 @@ export default class CommonGenerator extends BaseApplicationGenerator {
return this.delegateTasksToBlueprint(() => this.preparing);
}

get configuringEachEntity() {
return this.asConfiguringEachEntityTaskGroup({
migrateEntity({ entityConfig, entityStorage }) {
for (const field of entityConfig.fields) {
if (field.javadoc) {
field.documentation = field.javadoc;
delete field.javadoc;
}
if (field.fieldTypeJavadoc) {
field.fieldTypeDocumentation = field.fieldTypeJavadoc;
delete field.fieldTypeJavadoc;
}
}
for (const relationship of entityConfig.relationships) {
if (relationship.javadoc) {
relationship.documentation = relationship.javadoc;
delete relationship.javadoc;
}
}
if (entityConfig.javadoc) {
entityConfig.documentation = entityConfig.javadoc;
delete entityConfig.javadoc;
} else {
entityStorage.save();
}
},
});
}

get [BaseApplicationGenerator.CONFIGURING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.configuringEachEntity);
}

get default() {
return this.asDefaultTaskGroup({
async formatSonarProperties() {
Expand All @@ -207,7 +213,7 @@ export default class CommonGenerator extends BaseApplicationGenerator {

// Public API method used by the getter and also by Blueprints
get writing() {
return {
return this.asWritingTaskGroup({
cleanup({ application }) {
if (this.isJhipsterVersionLessThan('7.1.1')) {
if (!application.skipCommitHook) {
Expand All @@ -226,14 +232,8 @@ export default class CommonGenerator extends BaseApplicationGenerator {
}
}
},
writePrettierConfig({ application }) {
return this.writeFiles({
sections: prettierConfigFiles,
context: application,
});
},
...writeFiles(),
};
});
}

get [BaseApplicationGenerator.WRITING]() {
Expand Down Expand Up @@ -271,13 +271,6 @@ export default class CommonGenerator extends BaseApplicationGenerator {
'prettier-plugin-packagejson': application.nodeDependencies['prettier-plugin-packagejson'],
},
});
if (application.backendTypeJavaAny) {
this.packageJson.merge({
devDependencies: {
'prettier-plugin-java': application.nodeDependencies['prettier-plugin-java'],
},
});
}
},
});
}
Expand Down
24 changes: 0 additions & 24 deletions generators/common/templates/.prettierrc.ejs

This file was deleted.

6 changes: 5 additions & 1 deletion generators/git/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ const command: JHipsterCommandDefinition = {
type: Boolean,
scope: 'generator',
},
},
configs: {
monorepository: {
type: Boolean,
description: 'Use monorepository',
cli: {
type: Boolean,
},
scope: 'storage',
},
},
Expand Down
17 changes: 9 additions & 8 deletions generators/init/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ exports[`generator - init help should print expected information 1`] = `
Init project (alpha)
Options:
--auto-crlf Detect line endings
--skip-commit-hook Skip adding husky commit hooks
--skip-cache Do not remember prompt answers (default: false)
--skip-install Do not automatically install dependencies (default: false)
--force-install Fail on install dependencies error (default: false)
--ask-answered Show prompts for already configured options (default: false)
--base-name <value> Application base name
-h, --help display help for command
--auto-crlf Detect line endings
--skip-commit-hook Skip adding husky commit hooks
--skip-cache Do not remember prompt answers (default: false)
--skip-install Do not automatically install dependencies (default: false)
--force-install Fail on install dependencies error (default: false)
--ask-answered Show prompts for already configured options (default: false)
--base-name <value> Application base name
--prettier-tab-width <value> Default tab width for prettier
-h, --help display help for command
"
`;
Expand Down
2 changes: 1 addition & 1 deletion generators/init/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const command: JHipsterCommandDefinition = {
scope: 'storage',
},
},
import: [GENERATOR_PROJECT_NAME],
import: [GENERATOR_PROJECT_NAME, 'jhipster:javascript:prettier'],
};

export default command;
5 changes: 0 additions & 5 deletions generators/init/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* limitations under the License.
*/
export const files = {
prettier: [
{
templates: ['.prettierrc.yml.jhi', '.prettierignore.jhi'],
},
],
git: [
{
templates: ['.gitignore.jhi', '.gitattributes.jhi'],
Expand Down
34 changes: 14 additions & 20 deletions generators/init/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ export default class InitGenerator extends BaseApplicationGenerator {
return this.delegateTasksToBlueprint(() => this.initializing);
}

get composing() {
return this.asComposingTaskGroup({
async compose() {
await this.composeWithJHipster(GENERATOR_GIT);
const generatorOptions = { fromInit: true };
await this.composeWithJHipster('jhipster:javascript:prettier', { generatorOptions });
},
});
}

get [BaseApplicationGenerator.COMPOSING]() {
return this.delegateTasksToBlueprint(() => this.composing);
}

get loading() {
return this.asLoadingTaskGroup({
loadConfig({ application }) {
Expand All @@ -72,18 +86,6 @@ export default class InitGenerator extends BaseApplicationGenerator {
return this.delegateTasksToBlueprint(() => this.loading);
}

get composing() {
return this.asComposingTaskGroup({
async compose() {
await this.composeWithJHipster(GENERATOR_GIT);
},
});
}

get [BaseApplicationGenerator.COMPOSING]() {
return this.delegateTasksToBlueprint(() => this.composing);
}

get writing() {
return this.asWritingTaskGroup({
cleanup() {
Expand Down Expand Up @@ -114,14 +116,6 @@ export default class InitGenerator extends BaseApplicationGenerator {
return this.asPostWritingTaskGroup({
addPrettierDependencies({ application }) {
this.packageJson.merge({
scripts: {
'prettier-check': 'prettier --check "{,**/}*.{md,json,yml,html,cjs,mjs,js,cts,mts,ts,tsx,css,scss,vue,java}"',
'prettier-format': 'prettier --write "{,**/}*.{md,json,yml,html,cjs,mjs,js,cts,mts,ts,tsx,css,scss,vue,java}"',
},
devDependencies: {
prettier: application.nodeDependencies.prettier,
'prettier-plugin-packagejson': application.nodeDependencies['prettier-plugin-packagejson'],
},
engines: {
node: application.applicationNodeEngine,
},
Expand Down
Loading

0 comments on commit 85dafe1

Please sign in to comment.