Skip to content

Commit

Permalink
Improve api.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 7, 2021
1 parent 30e587e commit ac1d23c
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 138 deletions.
9 changes: 6 additions & 3 deletions generators/gradle/constants.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
* limitations under the License.
*/
const GRADLE_VERSION = '7.0.2';
const GRADLE = 'gradle';
const GRADLE_DESCRIPTION = 'Gradle';
const BUILD_DESTINATION_VALUE = 'build';

module.exports = {
GRADLE: 'gradle',
GRADLE_DESCRIPTION: 'Gradle',
GRADLE_VERSION,
BUILD_DESTINATION_VALUE: 'build',
GRADLE,
GRADLE_DESCRIPTION,
BUILD_DESTINATION_VALUE,
};
2 changes: 1 addition & 1 deletion generators/gradle/generator.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(`JHipster ${generator} generator`, () => {
customPrompts: {},
generatorPath,
});
describe('blueprint support', () => testBlueprintSupport('gradle'));
describe('blueprint support', () => testBlueprintSupport(generator));
describe('with valid configuration', () => {
let runResult;
before(async () => {
Expand Down
12 changes: 6 additions & 6 deletions generators/gradle/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = class extends MixedChain {
}
}

_initializing() {
get initializing() {
return {
validateFromCli() {
this.checkInvocationFromCLI();
Expand All @@ -81,10 +81,10 @@ module.exports = class extends MixedChain {

get [INITIALIZING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._initializing();
return this.initializing;
}

_loading() {
get loading() {
return {
configureChain() {
this.configureChain();
Expand All @@ -101,7 +101,7 @@ module.exports = class extends MixedChain {

get [LOADING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._loading();
return this.loading;
}

get preparing() {
Expand All @@ -117,7 +117,7 @@ module.exports = class extends MixedChain {
return this.preparing;
}

_writing() {
get writing() {
return {
async writeFiles() {
if (this.shouldSkipFiles()) return;
Expand All @@ -128,6 +128,6 @@ module.exports = class extends MixedChain {

get [WRITING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._writing();
return this.writing;
}
};
11 changes: 8 additions & 3 deletions generators/init/config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { PRETTIER_DEFAULT_INDENT, SKIP_COMMIT_HOOK } = require('./constants.cjs');
const {
PRETTIER_DEFAULT_INDENT,
PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE,
SKIP_COMMIT_HOOK,
SKIP_COMMIT_HOOK_DEFAULT_VALUE,
} = require('./constants.cjs');

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

/** Default config for templates */
const defaultConfig = {
...requiredConfig,
[PRETTIER_DEFAULT_INDENT]: 2,
[SKIP_COMMIT_HOOK]: false,
[PRETTIER_DEFAULT_INDENT]: PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE,
[SKIP_COMMIT_HOOK]: SKIP_COMMIT_HOOK_DEFAULT_VALUE,
};

module.exports = { requiredConfig, defaultConfig };
5 changes: 4 additions & 1 deletion generators/init/constants.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
* limitations under the License.
*/
const NODE_VERSION = '14.17.1';

const PRETTIER_DEFAULT_INDENT = 'prettierDefaultIndent';
const PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE = 2;

const SKIP_COMMIT_HOOK = 'skipCommitHook';
const SKIP_COMMIT_HOOK_DESCRIPTION = 'Skip adding husky commit hooks';
const SKIP_COMMIT_HOOK_DEFAULT_VALUE = false;

module.exports = {
NODE_VERSION,
PRETTIER_DEFAULT_INDENT,
PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE,
SKIP_COMMIT_HOOK,
SKIP_COMMIT_HOOK_DESCRIPTION: 'Skip adding husky commit hooks',
SKIP_COMMIT_HOOK_DESCRIPTION,
SKIP_COMMIT_HOOK_DEFAULT_VALUE,
};
4 changes: 2 additions & 2 deletions generators/init/generator.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const expect = require('expect');
const path = require('path');
const { access } = require('fs/promises');

const { basicTests, testBlueprintSupport } = require('../../test/support/index.cjs');
Expand All @@ -42,6 +42,7 @@ describe(`JHipster ${generator} generator`, () => {
},
contextBuilder,
});
describe('blueprint support', () => testBlueprintSupport(generator));
describe('with', () => {
describe('default config', () => {
let runResult;
Expand Down Expand Up @@ -108,5 +109,4 @@ describe(`JHipster ${generator} generator`, () => {
});
});
});
describe('blueprint support', () => testBlueprintSupport('init'));
});
42 changes: 21 additions & 21 deletions generators/init/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const { generateMixedChain } = require('../../lib/support/mixin.cjs');
const {
INITIALIZING_PRIORITY,
PROMPTING_PRIORITY,
CONFIGURING_PRIORITY,
LOADING_PRIORITY,
PREPARING_PRIORITY,
WRITING_PRIORITY,
Expand All @@ -32,7 +33,7 @@ const {
} = require('../../lib/support/priorities.cjs');

const { GENERATOR_INIT } = require('../generator-list');
const { SKIP_COMMIT_HOOK } = require('./constants.cjs');
const { PRETTIER_DEFAULT_INDENT, SKIP_COMMIT_HOOK } = require('./constants.cjs');
const { files, commitHooksFiles } = require('./files.cjs');
const { defaultConfig } = require('./config.cjs');
const { dependencyChain } = require('./mixin.cjs');
Expand Down Expand Up @@ -66,7 +67,7 @@ module.exports = class extends MixedChain {
}
}

_initializing() {
get initializing() {
return {
validateFromCli() {
this.checkInvocationFromCLI();
Expand All @@ -86,19 +87,18 @@ module.exports = class extends MixedChain {

get [INITIALIZING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._initializing();
return this.initializing;
}

_prompting() {
get prompting() {
return {
async showPrompts() {
if (this.shouldSkipPrompts()) return;
await this.prompt(
[
{
name: 'prettierDefaultIndent',
when: () => !this.abort,
type: 'number',
name: PRETTIER_DEFAULT_INDENT,
type: 'input',
message: 'What is the default indentation?',
default: defaultConfig.prettierDefaultIndent,
},
Expand All @@ -111,23 +111,23 @@ module.exports = class extends MixedChain {

get [PROMPTING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._prompting();
return this.prompting;
}

_configuring() {
get configuring() {
return {
configure() {
this.configureInit();
},
};
}

get configuring() {
get [CONFIGURING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._configuring();
return this.configuring;
}

_loading() {
get loading() {
return {
configureChain() {
this.configureChain();
Expand All @@ -146,7 +146,7 @@ module.exports = class extends MixedChain {

get [LOADING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._loading();
return this.loading;
}

get preparing() {
Expand All @@ -162,7 +162,7 @@ module.exports = class extends MixedChain {
return this.preparing;
}

_writing() {
get writing() {
return {
async writeFiles() {
if (this.shouldSkipFiles()) return;
Expand All @@ -177,10 +177,10 @@ module.exports = class extends MixedChain {

get [WRITING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._writing();
return this.writing;
}

_postWriting() {
get postWriting() {
return {
addCommitHookDependencies() {
if (this.shouldSkipFiles() || this[SKIP_COMMIT_HOOK]) return;
Expand All @@ -196,10 +196,10 @@ module.exports = class extends MixedChain {

get [POST_WRITING_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._postWriting();
return this.postWriting;
}

_install() {
get install() {
return {
// Initialize git repository before package manager install for commit hooks
async initGitRepo() {
Expand All @@ -221,10 +221,10 @@ module.exports = class extends MixedChain {

get [INSTALL_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._install();
return this.install;
}

_end() {
get end() {
return {
/** Initial commit to git repository after package manager install for package-lock.json */
async gitCommit() {
Expand Down Expand Up @@ -266,7 +266,7 @@ module.exports = class extends MixedChain {

get [END_PRIORITY]() {
if (this.delegateToBlueprint) return;
return this._end();
return this.end;
}

/*
Expand Down
20 changes: 14 additions & 6 deletions generators/init/mixin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ const { defaults } = require('lodash');

const { requiredConfig, defaultConfig } = require('./config.cjs');
const { options } = require('./options.cjs');
const { PRETTIER_DEFAULT_INDENT, SKIP_COMMIT_HOOK, NODE_VERSION } = require('./constants.cjs');
const {
NODE_VERSION,
PRETTIER_DEFAULT_INDENT,
PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE,
SKIP_COMMIT_HOOK,
SKIP_COMMIT_HOOK_DEFAULT_VALUE,
} = require('./constants.cjs');

const { GENERATOR_PROJECT_NAME } = require('../generator-list');

Expand All @@ -31,7 +37,10 @@ module.exports.mixin = parent =>
/**
* Load init options constants.
*/
loadInitOptionsConstants(into = this) {}
loadInitOptionsConstants(into = this) {
into.PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE = PRETTIER_DEFAULT_INDENT_DEFAULT_VALUE;
into.SKIP_COMMIT_HOOK_DEFAULT_VALUE = SKIP_COMMIT_HOOK_DEFAULT_VALUE;
}

/**
* Register and parse init options.
Expand Down Expand Up @@ -61,17 +70,16 @@ module.exports.mixin = parent =>
}

/**
* Load derived init configs into fromInto.
* Prepare derived init properties into fromInto.
* @param {any} fromInto - source/destination context
*/
// eslint-disable-next-line no-empty-pattern
prepareDerivedInitProperties(fromInto = this) {}

/**
* Load derived init configs into 'into'.
* Load init constants into 'into'.
* @param {Object} into - destination context
*/
loadInitConstants(into = this) {
this.NODE_VERSION = NODE_VERSION;
into.NODE_VERSION = NODE_VERSION;
}
};
12 changes: 9 additions & 3 deletions generators/java/generator.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
const expect = require('expect');
const path = require('path');

const { basicTests, testBlueprintSupport } = require('../../test/support/index.cjs');
const { skipPrettierHelpers: helpers } = require('../../test/utils/utils');
const { requiredConfig, defaultConfig } = require('./config.cjs');

const { basicTests, testBlueprintSupport } = require('../../test/support/index.cjs');
const { defaultConfig, requiredConfig } = require('./config.cjs');
const { GENERATOR_JAVA } = require('../generator-list');

const generatorPath = path.join(__dirname, 'index.cjs');
const generator = path.basename(__dirname);

describe('JHipster java generator', () => {
describe(`JHipster ${generator} generator`, () => {
it('generator-list constant matches folder name', () => {
expect(GENERATOR_JAVA).toBe(generator);
});
basicTests({
requiredConfig,
defaultConfig,
Expand Down
Loading

0 comments on commit ac1d23c

Please sign in to comment.