Hey all,
I’m trying to use the blueprint feature to create an entity generator for Ionic.
I have an index.js that’s as follows:
const chalk = require('chalk');
const packagejs = require('../../package.json');
const jsonfile = require('jsonfile');
const BaseGenerator = require('generator-jhipster/generators/generator-base');
const jhipsterConstants = require('generator-jhipster/generators/generator-constants');
const prompts = require('./prompts');
module.exports = class extends BaseGenerator {
get initializing() {
return {
readConfig() {
this.jhipsterAppConfig = this.getJhipsterAppConfig();
},
displayLogo() {
this.log(chalk.white(`Running ${chalk.bold('JHipster')} ${chalk.blue.bold('Ionic')} Entity Generator! ${chalk.yellow(`v${packagejs.version}\n`)}`));
}
};
}
get prompting() {
return {
askForPath: prompts.askForPath
};
}
get writing() {
const fromPath = `${this.appPath}/.yo-rc.json`;
this.jhipsterAppConfig = this.fs.readJSON(fromPath)['generator-jhipster'];
return {
writeFiles() {
// function to use directly template
this.template = function (source, destination) {
this.fs.copyTpl(
this.templatePath(source),
this.destinationPath(destination),
this
);
};
this.template('dummy.txt', 'dummy.txt', this, {});
}
};
}
end() {
this.log(`\n${chalk.bold.green('Finished!')}`);
}
};
My prompts.js looks like this:
const chalk = require('chalk');
const shelljs = require('shelljs');
module.exports = {
askForPath
};
/**
* Ask For Path
*/
function askForPath(meta) {
if (!meta && this.existingProject) return;
const done = this.async();
const messageAskForPath = 'Enter the directory where your JHipster app is located:';
const prompts = [{
type: 'confirm',
name: 'useAppJson',
message: 'Do you want to generate this entity from an existing app?',
default: true
},
{
type: 'input',
name: 'appPath',
message: messageAskForPath,
default: 'backend',
store: true,
validate: (input) => {
const path = this.destinationPath(input);
if (shelljs.test('-d', path)) {
const appsFolders = getAppFolder.call(this, input);
if (appsFolders.length === 0) {
return `No application found in ${path}`;
}
return true;
}
return `${path} is not a directory or doesn't exist`;
}
}];
this.prompt(prompts).then((props) => {
this.appPath = props.appPath;
done();
});
}
/**
* Get App Folders
* @param input path to join to destination path
* @returns {Array} array of string representing app folders
*/
function getAppFolder(input) {
const destinationPath = this.destinationPath(input);
const appsFolders = [];
if (shelljs.test('-f', `${destinationPath}/.yo-rc.json`)) {
try {
const fileData = this.fs.readJSON(`${destinationPath}/.yo-rc.json`);
if (fileData['generator-jhipster'].baseName !== undefined) {
appsFolders.push(destinationPath);
}
} catch (err) {
this.log(chalk.red(`The .yo-rc.json in ${destinationPath} can't be read!`));
this.debug('Error:', err);
}
}
return appsFolders;
}For some reason, I’m getting the following error and I’m not getting prompted. Any ideas?
/Users/mraible/dev/jhipster/generator-jhipster-ionic/generators/entity/index.js:28
this.jhipsterAppConfig = this.fs.readJSON(fromPath)['generator-jhipster'];
^
TypeError: Cannot read property 'readJSON' of undefined
If I change index.js#writing to the following:
get writing() {
const fromPath = `${this.appPath}/.yo-rc.json`;
console.log('fromPath: ' + fromPath);
//this.jhipsterAppConfig = this.fs.readJSON(fromPath)['generator-jhipster'];
}It prompts me correctly, but it seems to try and read the file path before I enter it.
fromPath: undefined/.yo-rc.json
Running JHipster Ionic Entity Generator! v0.0.0
? Do you want to generate this entity from an existing app? Yes
? Enter the directory where your JHipster app is located: ../ionic-workspace/backend
Any ideas?
Thanks,
Matt