Skip to content

Commit

Permalink
Merge branch 'master' into fix-react-e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdns committed Sep 17, 2018
2 parents 7480e0a + 9ff81bb commit 84c9b06
Show file tree
Hide file tree
Showing 100 changed files with 2,920 additions and 1,798 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"semi": [2, "always"],
"eqeqeq": [2, "smart"],
"no-use-before-define": [2, "nofunc"],
"no-confusing-arrow": "off",
"no-unused-vars": [2, {"vars": "local", "args": "none"}],
"no-multi-str": 2,
"no-irregular-whitespace": 2,
Expand Down
47 changes: 21 additions & 26 deletions cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,36 @@
*/
/* eslint-disable no-console */
const program = require('commander');
const yeoman = require('yeoman-environment');
const chalk = require('chalk');

const packageJson = require('../package.json');
const logger = require('./utils').logger;
const initHelp = require('./utils').initHelp;
const toString = require('./utils').toString;
const getCommand = require('./utils').getCommand;
const getCommandOptions = require('./utils').getCommandOptions;
const getArgs = require('./utils').getArgs;
const CLI_NAME = require('./utils').CLI_NAME;
const {
CLI_NAME, initHelp, logger, createYeomanEnv, toString, getCommand, getCommandOptions, getArgs, done
} = require('./utils');
const initAutoCompletion = require('./completion').init;
const SUB_GENERATORS = require('./commands');

const version = packageJson.version;
const env = yeoman.createEnv();
const JHIPSTER_NS = CLI_NAME;
const env = createYeomanEnv();

/* setup debugging */
logger.init(program);

/* Register yeoman generators */
Object.keys(SUB_GENERATORS).forEach((generator) => {
env.register(require.resolve(`../generators/${generator}`), `${JHIPSTER_NS}:${generator}`);
});

const done = () => {
logger.info(chalk.green.bold('Congratulations, JHipster execution is complete!'));
};

/**
* Run a yeoman command
*/
const runYoCommand = (cmd, args, opts) => {
const runYoCommand = (cmd, args, options, opts) => {
logger.debug(`cmd: ${toString(cmd)}`);
logger.debug(`args: ${toString(args)}`);
logger.debug(`opts: ${toString(opts)}`);
const command = getCommand(cmd, args, opts);
const options = getCommandOptions(packageJson, process.argv.slice(2));
logger.info(chalk.yellow(`Executing ${command}`));
logger.info(chalk.yellow(`Options: ${toString(options)}`));
try {
env.run(command, options, done);
} catch (e) {
logger.error(e.message);
logger.log(e);
process.exit(1);
logger.error(e.message, e);
}
};

Expand All @@ -80,11 +63,23 @@ Object.keys(SUB_GENERATORS).forEach((key) => {
command.allowUnknownOption()
.description(opts.desc)
.action((args) => {
runYoCommand(key, program.args, opts);
const options = getCommandOptions(packageJson, process.argv.slice(2));
if (opts.cliOnly) {
logger.debug('Executing CLI only script');
/* eslint-disable global-require, import/no-dynamic-require */
require(`./${key}`)(program.args, options, env);
/* eslint-enable */
} else {
runYoCommand(key, program.args, options, opts);
}
})
.on('--help', () => {
logger.debug('Adding additional help info');
env.run(`${JHIPSTER_NS}:${key} --help`, done);
if (opts.help) {
logger.info(opts.help);
} else {
logger.debug('Adding additional help info');
env.run(`${JHIPSTER_NS}:${key} --help`, done);
}
});
});

Expand Down
17 changes: 16 additions & 1 deletion cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ module.exports = {
},
'import-jdl': {
argument: ['jdlFiles...'],
desc: 'Create entities from the JDL file passed in argument'
cliOnly: true,
desc: 'Create entities from the JDL file passed in argument',
help: `
--skip-install # Do not automatically install dependencies Default: false
--db # Provide DB option for the application when using skip-server flag
--json-only # Generate only the JSON files and skip entity regeneration Default: false
--ignore-application # Ignores application generation Default: false
--skip-ui-grouping # Disable the UI grouping behaviour for entity client side code Default: false
Arguments:
jdlFiles # The JDL file names Type: String[] Required: true
Example:
jhipster import-jdl myfile.jdl
jhipster import-jdl myfile1.jdl myfile2.jdl
`
},
info: {
desc: 'Display information about your current project and system'
Expand Down
223 changes: 223 additions & 0 deletions cli/import-jdl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
const chalk = require('chalk');
const _ = require('lodash');
const path = require('path');
const shelljs = require('shelljs');
const jhiCore = require('jhipster-core');
const pretty = require('js-object-pretty-print').pretty;
const pluralize = require('pluralize');
const { fork } = require('child_process');

const {
CLI_NAME, GENERATOR_NAME, logger, toString, getOptionsFromArgs, done, getOptionAsArgs
} = require('./utils');
const jhipsterUtils = require('../generators/utils');

const packagejs = require('../package.json');
const statistics = require('../generators/statistics');

const runYeomanProcess = require.resolve('./run-yeoman-process.js');

function importJDL() {
logger.info('The JDL is being parsed.');
const jdlImporter = new jhiCore.JDLImporter(this.jdlFiles, {
databaseType: this.prodDatabaseType,
applicationType: this.applicationType,
applicationName: this.baseName,
generatorVersion: packagejs.version,
forceNoFiltering: this.options.force
});
let importState = {
exportedEntities: [],
exportedApplications: []
};
try {
importState = jdlImporter.import();
logger.debug(`importState exportedEntities: ${importState.exportedEntities.length}`);
logger.debug(`importState exportedApplications: ${importState.exportedApplications.length}`);
if (importState.exportedEntities.length > 0) {
const entityNames = _.uniq(importState.exportedEntities
.map(exportedEntity => exportedEntity.name))
.join(', ');
logger.log(`Found entities: ${chalk.yellow(entityNames)}.`);
} else {
logger.log(chalk.yellow('No change in entity configurations, no entities were updated.'));
}
logger.log('The JDL has been successfully parsed');
} catch (error) {
logger.debug('Error:', error);
if (error) {
const errorName = `${error.name}:` || '';
const errorMessage = error.message || '';
logger.log(chalk.red(`${errorName} ${errorMessage}`));
}
logger.error(`Error while parsing applications and entities from the JDL ${error}`);
}
return importState;
}

const shouldGenerateApplications = generator => !generator.options['ignore-application'] && generator.importState.exportedApplications.length !== 0;

const generateApplicationFiles = ({
generator, application, withEntities, inAppFolder
}) => {
const baseName = application[GENERATOR_NAME].baseName;
logger.info(`Generating application ${baseName} in a new parallel process`);
logger.debug(`Generating application: ${pretty(application[GENERATOR_NAME])}`);

const cwd = inAppFolder ? path.join(generator.pwd, baseName) : generator.pwd;
logger.debug(`Child process will be triggered for ${runYeomanProcess} with cwd: ${cwd}`);

const command = `${CLI_NAME}:app`;
fork(runYeomanProcess, [command, ...getOptionAsArgs(generator.options, withEntities)], {
cwd
});
};

const generateEntityFiles = (generator, entity, inAppFolder, env) => {
const options = {
...generator.options,
regenerate: true,
'from-cli': true,
'skip-install': true,
'skip-client': entity.skipClient,
'skip-server': entity.skipServer,
'no-fluent-methods': entity.noFluentMethod,
'skip-user-management': entity.skipUserManagement,
'skip-ui-grouping': generator.options['skip-ui-grouping']
};
const command = `${CLI_NAME}:entity ${entity.name}`;
if (inAppFolder) {
const baseNames = entity.applications;
baseNames.forEach((baseName) => {
logger.info(`Generating entities for application ${baseName} in a new parallel process`);
const cwd = path.join(generator.pwd, baseName);
logger.debug(`Child process will be triggered for ${runYeomanProcess} with cwd: ${cwd}`);

fork(runYeomanProcess, [command, ...getOptionAsArgs(options)], { cwd });
});
} else {
/* Traditional entity only generation */
env.run(command, options, done);
}
};

class JDLProcessor {
constructor(jdlFiles, options) {
logger.debug(`JDLProcessor started with jdlFiles: ${jdlFiles} and options: ${toString(options)}`);
this.jdlFiles = jdlFiles;
this.options = options;
this.pwd = process.cwd();
}

validate() {
if (this.jdlFiles) {
this.jdlFiles.forEach((key) => {
if (!shelljs.test('-f', key)) {
logger.error(chalk.red(`\nCould not find ${key}, make sure the path is correct.\n`));
}
});
}
}

getConfig() {
if (jhiCore.FileUtils.doesFileExist('.yo-rc.json')) {
logger.info('Found .yo-rc.json on path. This is an existing app');
const configuration = jhipsterUtils.getAllJhipsterConfig(null, true);
this.applicationType = configuration.applicationType;
this.baseName = configuration.baseName;
this.databaseType = configuration.databaseType || jhipsterUtils.getDBTypeFromDBValue(this.options.db);
this.prodDatabaseType = configuration.prodDatabaseType || this.options.db;
this.devDatabaseType = configuration.devDatabaseType || this.options.db;
this.skipClient = configuration.skipClient;
this.clientFramework = configuration.clientFramework;
this.clientFramework = this.clientFramework || 'angularX';
this.clientPackageManager = configuration.clientPackageManager;
if (!this.clientPackageManager) {
if (this.useNpm) {
this.clientPackageManager = 'npm';
} else {
this.clientPackageManager = 'yarn';
}
}
}
}

importJDL() {
this.importState = importJDL.call(this);
}

sendInsight() {
statistics.sendSubGenEvent('generator', 'import-jdl');
}

generateApplications() {
if (!shouldGenerateApplications(this)) {
logger.debug('Applications not generated');
return;
}
logger.log(`Generating ${this.importState.exportedApplications.length} `
+ `${pluralize('application', this.importState.exportedApplications.length)}.`);

this.importState.exportedApplications.forEach((application) => {
try {
generateApplicationFiles({
generator: this,
application,
withEntities: this.importState.exportedEntities.length !== 0,
inAppFolder: this.importState.exportedApplications.length > 1
});
} catch (error) {
logger.error(`Error while generating applications from the parsed JDL\n${error}`);
}
});
}

generateEntities(env) {
if (this.importState.exportedEntities.length === 0 || shouldGenerateApplications(this)) {
return;
}
if (this.options['json-only']) {
logger.log('Entity JSON files created. Entity generation skipped.');
return;
}
try {
this.importState.exportedEntities.forEach((exportedEntity) => {
logger.log(`Generating ${this.importState.exportedEntities.length} `
+ `${pluralize('entity', this.importState.exportedEntities.length)}.`);

generateEntityFiles(this, exportedEntity, this.importState.exportedApplications.length > 1, env);
});
} catch (error) {
logger.error(`Error while generating entities from the parsed JDL\n${error}`);
}
}

end() {
if (!this.options['skip-install'] && !this.skipClient && !this.options['json-only']
&& !shouldGenerateApplications(this)) {
logger.debug('Building client');
// TODO figure out a way to do this nicely
// this.rebuildClient();
}
}
}

module.exports = (args, options, env) => {
logger.debug('cmd: import-jdl from ./import-jdl');
logger.debug(`args: ${toString(args)}`);
const jdlFiles = getOptionsFromArgs(args);
logger.info(chalk.yellow(`Executing import-jdl ${jdlFiles.join(' ')}`));
logger.info(chalk.yellow(`Options: ${toString(options)}`));
try {
const jdlImporter = new JDLProcessor(jdlFiles, options);
jdlImporter.validate();
jdlImporter.getConfig();
jdlImporter.importJDL();
jdlImporter.sendInsight();
jdlImporter.generateApplications();
jdlImporter.generateEntities(env);
jdlImporter.end();
} catch (e) {
logger.error(`Error during import-jdl: ${e.message}`, e);
}
};
3 changes: 1 addition & 2 deletions cli/jhipster.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
const semver = require('semver');
const path = require('path');
const packageJson = require('../package.json');
const logger = require('./utils').logger;
const { logger } = require('./utils');

const currentNodeVersion = process.versions.node;
const minimumNodeVersion = packageJson.engines.node;
Expand All @@ -31,7 +31,6 @@ if (!semver.satisfies(currentNodeVersion, minimumNodeVersion)) {
}\nJHipster requires Node version ${minimumNodeVersion
}\nPlease update your version of Node.`);
/* eslint-enable */
process.exit(1);
}

let preferLocal = true;
Expand Down
18 changes: 18 additions & 0 deletions cli/run-yeoman-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const chalk = require('chalk');

const packageJson = require('../package.json');
const {
logger, createYeomanEnv, toString, getCommandOptions, done
} = require('./utils');

const env = createYeomanEnv();

const command = process.argv[2];
const options = getCommandOptions(packageJson, process.argv.slice(3));
logger.info(chalk.yellow(`Executing ${command} on ${process.cwd()}`));
logger.info(chalk.yellow(`Options: ${toString(options)}`));
try {
env.run(command, options, done);
} catch (e) {
logger.error(e.message, e);
}
Loading

0 comments on commit 84c9b06

Please sign in to comment.