Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add javascript:prettier and javascript:husky generators. #26567

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ updates:
- 'theme: dependencies'
- 'skip-changelog'

- package-ecosystem: 'npm'
directory: '/generators/javascript/resources/'
rebase-strategy: 'auto'
schedule:
interval: 'daily'
time: '00:00'
open-pull-requests-limit: 10
versioning-strategy: increase
labels:
- 'theme: dependencies'
- 'skip-changelog'

- package-ecosystem: 'npm'
directory: '/generators/react/resources/'
rebase-strategy: 'auto'
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
5 changes: 4 additions & 1 deletion 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 based application */
fromInit?: boolean;

/* blueprint options */
blueprints?: string;
blueprint?: any;
Expand Down Expand Up @@ -233,7 +236,7 @@ export type ConfigSpec = {
* The callback receives jhipsterConfigWithDefaults as input for 'storage' (default) scope.
* The callback receives blueprintStorage contents as input for 'blueprint' scope.
*/
default?: string | boolean | string[] | ((this: CoreGenerator | void, ctx: any) => string | boolean | string[]);
default?: string | boolean | number | string[] | ((this: CoreGenerator | void, ctx: any) => string | boolean | number | string[]);
/**
* Configure the generator according to the selected configuration.
*/
Expand Down
3 changes: 2 additions & 1 deletion generators/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { addIconImport, addItemToMenu, addRoute } from '../angular/support/
import type { AngularApplication } from '../angular/types.js';
import type { OptionWithDerivedProperties } from '../base-application/application-options.js';
import type { CypressApplication } from '../cypress/types.js';
import { JavaScriptSourceType } from '../javascript/types.js';

type ClientFrameworkType = ['no', 'angular', 'react', 'vue', 'svelte'];

Expand All @@ -25,7 +26,7 @@ export type ClientResources = {
*/
comment?: string;
};
export type ClientSourceType = {
export type ClientSourceType = JavaScriptSourceType & {
/**
* Add external resources to root file(index.html).
*/
Expand Down
2 changes: 0 additions & 2 deletions generators/generate-blueprint/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { PRIORITY_NAMES_LIST as BASE_PRIORITY_NAMES_LIST } from '../base/priorit

import * as GENERATOR_LIST from '../generator-list.js';
import { packageJson } from '../../lib/index.js';
import { SKIP_COMMIT_HOOK } from '../init/constants.js';
import { BLUEPRINT_API_VERSION, NODE_VERSION } from '../generator-constants.js';
import { files, generatorFiles } from './files.js';
import {
Expand Down Expand Up @@ -128,7 +127,6 @@ export default class extends BaseGenerator {
conditionalConfig() {
if (this.jhipsterConfig[LOCAL_BLUEPRINT_OPTION]) {
this.config.defaults({
[SKIP_COMMIT_HOOK]: true,
[DYNAMIC]: true,
js: false,
});
Expand Down
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
18 changes: 10 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,16 @@ 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-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 (default: 2)
--monorepository Use monorepository
--skip-commit-hook Skip adding husky commit hooks
-h, --help display help for command
"
`;

Expand Down
11 changes: 2 additions & 9 deletions generators/init/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@
*/
import { JHipsterCommandDefinition } from '../base/api.js';
import { GENERATOR_PROJECT_NAME } from '../generator-list.js';
import { SKIP_COMMIT_HOOK, SKIP_COMMIT_HOOK_DESCRIPTION } from './constants.js';

const command: JHipsterCommandDefinition = {
options: {
[SKIP_COMMIT_HOOK]: {
description: SKIP_COMMIT_HOOK_DESCRIPTION,
type: Boolean,
scope: 'storage',
},
},
import: [GENERATOR_PROJECT_NAME],
options: {},
import: [GENERATOR_PROJECT_NAME, 'jhipster:javascript:prettier', 'jhipster:javascript:husky'],
};

export default command;
3 changes: 0 additions & 3 deletions generators/init/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SKIP_COMMIT_HOOK, SKIP_COMMIT_HOOK_DEFAULT_VALUE } from './constants.js';

/** Config required at .yo-rc.json */
export const requiredConfig = {};

/** Default config for templates */
export const defaultConfig = {
...requiredConfig,
[SKIP_COMMIT_HOOK]: SKIP_COMMIT_HOOK_DEFAULT_VALUE,
};
19 changes: 0 additions & 19 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 All @@ -47,17 +42,3 @@ export const readme = {
},
],
};

export const commitHooksFiles = {
commitHooks: [
{
templates: [
'.lintstagedrc.cjs',
{
file: '.husky/pre-commit',
noEjs: true,
},
],
},
],
};
65 changes: 17 additions & 48 deletions generators/init/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
* limitations under the License.
*/
import BaseApplicationGenerator from '../base-application/index.js';
import { GENERATOR_INIT, GENERATOR_GIT, GENERATOR_PROJECT_NAME } from '../generator-list.js';
import { GENERATOR_GIT, GENERATOR_PROJECT_NAME } from '../generator-list.js';
import { packageJson } from '../../lib/index.js';
import { defaultConfig } from './config.js';
import { SKIP_COMMIT_HOOK } from './constants.js';
import { files, commitHooksFiles, readme } from './files.js';
import { files, readme } from './files.js';

/**
* @class
Expand Down Expand Up @@ -52,30 +50,13 @@ export default class InitGenerator extends BaseApplicationGenerator {
return this.delegateTasksToBlueprint(() => this.initializing);
}

get loading() {
return this.asLoadingTaskGroup({
loadConfig({ application }) {
const config = { ...defaultConfig, ...this.config.getAll() };
application.applicationNodeEngine = packageJson.engines.node;
application[SKIP_COMMIT_HOOK] = config[SKIP_COMMIT_HOOK];
},
loadNodeDependencies({ application }) {
this.loadNodeDependenciesFromPackageJson(
application.nodeDependencies,
this.fetchFromInstalledJHipster(GENERATOR_INIT, 'resources', 'package.json'),
);
},
});
}

get [BaseApplicationGenerator.LOADING]() {
return this.delegateTasksToBlueprint(() => this.loading);
}

get composing() {
return this.asComposingTaskGroup({
async compose() {
await this.composeWithJHipster(GENERATOR_GIT);
const generatorOptions = { fromInit: true };
await this.composeWithJHipster('jhipster:javascript:prettier', { generatorOptions });
await this.composeWithJHipster('jhipster:javascript:husky', { generatorOptions });
},
});
}
Expand All @@ -84,6 +65,18 @@ export default class InitGenerator extends BaseApplicationGenerator {
return this.delegateTasksToBlueprint(() => this.composing);
}

get loading() {
return this.asLoadingTaskGroup({
loadConfig({ application }) {
application.applicationNodeEngine = packageJson.engines.node;
},
});
}

get [BaseApplicationGenerator.LOADING]() {
return this.delegateTasksToBlueprint(() => this.loading);
}

get writing() {
return this.asWritingTaskGroup({
cleanup() {
Expand All @@ -99,10 +92,6 @@ export default class InitGenerator extends BaseApplicationGenerator {
await this.writeFiles({ sections: readme, context: application });
}
},
async writeCommitHookFiles({ application }) {
if (application.skipCommitHook) return;
await this.writeFiles({ sections: commitHooksFiles, context: application });
},
});
}

Expand All @@ -114,31 +103,11 @@ 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,
},
});
},
addCommitHookDependencies({ application }) {
if (application.skipCommitHook) return;
this.packageJson.merge({
scripts: {
prepare: 'husky',
},
devDependencies: {
husky: application.nodeDependencies.husky,
'lint-staged': application.nodeDependencies['lint-staged'],
},
});
},
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generator - javascript:husky skipCommitHook(false) should call source snapshot 1`] = `{}`;

exports[`generator - javascript:husky skipCommitHook(false) should match files snapshot 1`] = `
{
".husky/pre-commit": {
"contents": "#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install lint-staged",
"stateCleared": "modified",
},
".lintstagedrc.cjs": {
"contents": "module.exports = {
'{,src/**/,webpack/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java}': ['prettier --write'],
};
",
"stateCleared": "modified",
},
".yo-rc.json": ObjectContaining {
"contents": Any<String>,
},
"package.json": {
"contents": "{
"scripts": {
"prepare": "husky"
},
"devDependencies": {
"husky": "HUSKY_VERSION",
"lint-staged": "LINT_STAGED_VERSION"
}
}
",
"stateCleared": "modified",
},
}
`;

exports[`generator - javascript:husky skipCommitHook(true) should call source snapshot 1`] = `{}`;

exports[`generator - javascript:husky skipCommitHook(true) should match files snapshot 1`] = `
{
".yo-rc.json": ObjectContaining {
"contents": Any<String>,
},
}
`;
34 changes: 34 additions & 0 deletions generators/javascript/generators/husky/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2013-2024 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 type { JHipsterCommandDefinition } from '../../../base/api.js';

const command: JHipsterCommandDefinition = {
configs: {
skipCommitHook: {
description: 'Skip adding husky commit hooks',
cli: {
type: Boolean,
},
scope: 'storage',
},
},
import: [],
};

export default command;
Loading
Loading