Skip to content

Commit

Permalink
converted ask functions as private
Browse files Browse the repository at this point in the history
  • Loading branch information
ameyakoshti authored and a0k00e3 committed Jul 17, 2017
1 parent dc76f93 commit d4dc390
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 313 deletions.
89 changes: 41 additions & 48 deletions packages/generator-electrode/component-add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ module.exports = class extends Generator {
let homeComponentPath = "";
this.demoAppName = "";

//This function checks if all required pieces are present
const checkError = () => {
if (_.isEmpty(this.demoAppName)) {
this.env.error(
Expand All @@ -58,7 +57,7 @@ module.exports = class extends Generator {
}
};

//check if the command is being run from within an existing app
// Check if the command is being run from within an existing app
if (this.fs.exists(this.destinationPath("package.json"))) {
var appPkg = this.fs.readJSON(this.destinationPath("package.json"));
if (
Expand All @@ -72,15 +71,15 @@ module.exports = class extends Generator {
}
}

//check is the command is run from the correct directory
// Check is the command is run from the correct directory
if (path.basename(this.destinationPath()) != "packages") {
this.env.error(
"You need to run this command from the 'packages' folder generated by running 'yo electrode:component'."
);
}

try {
//fetch the demo App Name, which is "demo-app" by default
// Fetch the demo App Name, which is "demo-app" by default
this.demoAppName = _.first(glob.sync("**demo-app", { cwd: path.resolve("..") }));
this.pkg = this.fs.exists(
this.destinationPath(path.join("..", this.demoAppName, "package.json"))
Expand All @@ -94,44 +93,43 @@ module.exports = class extends Generator {
checkError();
}

// check for missing
checkError();
this.props = this.options.props || {};
}

_askFor() {
var prompts = [
{
type: "input",
name: "name",
message: "Component Name",
when: !this.props.name,
default: "wysiwyg-component"
},
{
type: "input",
name: "componentName",
message: "What is the ClassName for your component?",
default: this.props.name,
when: !this.props.componentName
}
];

return this.prompt(prompts).then(props => {
this.props = extend(this.props, props);
this.packageName = this.props.name;
this.componentName = _.kebabCase(_.deburr(this.props.componentName || this.props.name))
.replace(/^\s+|\s+$/g, "")
.replace(/(^|[-_ ])+(.)/g, function(match, first, second) {
return second.toUpperCase();
});
});
}

prompting() {
greeting: {
this.log(yosay("Welcome to the " + chalk.red("Electrode Add Component") + " generator!"));
}
this.log(yosay("Welcome to the " + chalk.red("Electrode Add Component") + " generator!"));

askFor: {
var prompts = [
{
type: "input",
name: "name",
message: "Component Name",
when: !this.props.name,
default: "wysiwyg-component"
},
{
type: "input",
name: "componentName",
message: "What is the ClassName for your component?",
default: this.props.name,
when: !this.props.componentName
}
];

return this.prompt(prompts).then(props => {
this.props = extend(this.props, props);
this.packageName = this.props.name;
this.componentName = _.kebabCase(_.deburr(this.props.componentName || this.props.name))
.replace(/^\s+|\s+$/g, "")
.replace(/(^|[-_ ])+(.)/g, function(match, first, second) {
return second.toUpperCase();
});
});
}
return this._askFor();
}

default() {
Expand All @@ -143,13 +141,7 @@ module.exports = class extends Generator {
quotes: this.props.quotes
};

this.composeWith(
"electrode:component",
{ options },
{
local: require.resolve("../component")
}
);
this.composeWith(require.resolve('../component'), options);
}

writing() {
Expand All @@ -162,7 +154,7 @@ module.exports = class extends Generator {
}
};

//overwrite the Demo App package.json
// Overwrite the Demo App package.json
let existingPkg = this.fs.readJSON(
this.destinationPath(path.join("..", "..", this.demoAppName, "package.json"))
);
Expand All @@ -180,21 +172,22 @@ module.exports = class extends Generator {
);

let homeArray = homeFile.split("\n");
// add import at the top
// Add import at the top
let rx = "import {" + this.componentName + '} from "' + this.packageName + '";';
let rxTag = "<" + this.componentName + " />";
homeArray.unshift(rx);
//get the first closing div, home shoud have one closing div.
// Get the first closing div, home shoud have one closing div.
let splitPoint = homeArray.findIndex((value, index, array) => {
if (value.match("</div>")) {
return index;
}
});
// insert the new class before the div
//splice the array into 2, before closing div and after
// Insert the new class before the div
// Splice the array into 2, before closing div and after
let topHalf = homeArray.splice(0, splitPoint);
topHalf.push(rxTag);
let newHomeString = topHalf.concat(homeArray).join("\n");

this.fs.write(
this.destinationPath(
path.join("..", "..", this.demoAppName, "src", "client", "components", "home.jsx")
Expand Down
215 changes: 107 additions & 108 deletions packages/generator-electrode/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,117 +51,120 @@ module.exports = class extends Generator {
var info = parseAuthor(this.pkg.author);
this.props.developerName = info.name;
}

this.props.quoteType = this.quotes;
}

_askFor() {
if (this.pkg.name || this.options.name) {
this.props.name = this.pkg.name || _.kebabCase(this.options.name);
}

prompting() {
greeting: {
if (!this.isAddon) {
this.log(
"\n" +
chalk.bold.underline("Welcome to the Electrode Component Generator") +
"\n" +
"\nWe're going to set up a new " +
chalk.bold("Electrode") +
" component, ready for development with" +
"\n" +
chalk.bold("react, webpack, demo, electrode component archetype, and live-reload")
);
}
if (this.options.componentName) {
this.props.componentName = this.options.componentName;
}

askFor: {
if (this.pkg.name || this.options.name) {
this.props.name = this.pkg.name || _.kebabCase(this.options.name);
var prompts = [
{
type: "input",
name: "projectName",
message: "What is your Package/GitHub project name? (e.g., 'wysiwyg-component')",
default: "wysiwyg-component",
when: !this.props.name
},
{
type: "input",
name: "packageName",
message: "What is the ClassName for your component?",
default: this.props.componentName || this.props.projectName,
when: !this.props.componentName
},
{
type: "input",
name: "packageName",
message: "What will be the npm package name?",
default: this.props.packageName
},
{
type: "input",
name: "packageGitHubOrg",
message: "What will be the GitHub organization username (e.g., 'walmartlabs')?",
default: this.props.packageGitHubOrg
},
{
type: "input",
name: "developerName",
message: "What is your name? (for copyright notice, etc.)",
default: this.props.developerName
},
{
type: "input",
name: "ghUser",
message: "What is your GitHub Username?",
default: this.props.developerName
},
{
type: "list",
name: "quoteType",
message: "Use double quotes or single quotes?",
choices: ['"', "'"],
default: '"',
when: !this.props.quoteType
},
{
type: "input",
name: "ghRepo",
message: "What is the name of the GitHub repo where this will be published?",
default: this.packageName
},
{
type: "confirm",
name: "createDirectory",
message: "Would you like to create a new directory for your project?",
default: true
}
if (this.options.componentName) {
this.props.componentName = this.options.componentName;
];

return this.optionOrPrompt(prompts).then(props => {
this.props = extend(this.props, props);
this.projectName = this.props.projectName.split(" ").map(_.toLower).join("");
this.packageName = this.props.projectName.split(" ").map(_.toLower).join("");
this.developerName = this.props.developerName;
this.quoteType = this.props.quoteType;
this.ghUser = this.props.ghUser;
this.ghRepo = this.props.ghRepo;
this.packageGitHubOrg = this.props.packageGitHubOrg;
this.createDirectory = this.props.createDirectory;
this.componentName = _.kebabCase(_.deburr(this.props.projectName))
.replace(/^\s+|\s+$/g, "")
.replace(/(^|[-_ ])+(.)/g, function(match, first, second) {
return second.toUpperCase();
});
this.currentYear = new Date().getFullYear();
if (this.props.createDirectory) {
var newRoot = this.destinationPath() + "/" + this.packageName;
this.destinationRoot(newRoot);
}
this.rootPath = this.isAddon ? "" : "packages/" + this.projectName + "/";
});
}

var prompts = [
{
type: "input",
name: "projectName",
message: "What is your Package/GitHub project name? (e.g., 'wysiwyg-component')",
default: "wysiwyg-component",
when: !this.props.name
},
{
type: "input",
name: "packageName",
message: "What is the ClassName for your component?",
default: this.props.componentName || this.props.projectName,
when: !this.props.componentName
},
{
type: "input",
name: "packageName",
message: "What will be the npm package name?",
default: this.props.packageName
},
{
type: "input",
name: "packageGitHubOrg",
message: "What will be the GitHub organization username (e.g., 'walmartlabs')?",
default: this.props.packageGitHubOrg
},
{
type: "input",
name: "developerName",
message: "What is your name? (for copyright notice, etc.)",
default: this.props.developerName
},
{
type: "input",
name: "ghUser",
message: "What is your GitHub Username?",
default: this.props.developerName
},
{
type: "list",
name: "quoteType",
message: "Use double quotes or single quotes?",
choices: ['"', "'"],
default: '"',
when: !this.props.quoteType
},
{
type: "input",
name: "ghRepo",
message: "What is the name of the GitHub repo where this will be published?",
default: this.packageName
},
{
type: "confirm",
name: "createDirectory",
message: "Would you like to create a new directory for your project?",
default: true
}
];
return this.optionOrPrompt(prompts).then(props => {
this.props = extend(this.props, props);
this.projectName = this.props.projectName.split(" ").map(_.toLower).join("");
this.packageName = this.props.projectName.split(" ").map(_.toLower).join("");
this.developerName = this.props.developerName;
this.quoteType = this.props.quoteType;
this.ghUser = this.props.ghUser;
this.ghRepo = this.props.ghRepo;
this.packageGitHubOrg = this.props.packageGitHubOrg;
this.createDirectory = this.props.createDirectory;
this.componentName = _.kebabCase(_.deburr(this.props.projectName))
.replace(/^\s+|\s+$/g, "")
.replace(/(^|[-_ ])+(.)/g, function(match, first, second) {
return second.toUpperCase();
});
this.currentYear = new Date().getFullYear();
if (this.props.createDirectory) {
var newRoot = this.destinationPath() + "/" + this.packageName;
this.destinationRoot(newRoot);
}
this.rootPath = this.isAddon ? "" : "packages/" + this.projectName + "/";
});
prompting() {
if (!this.isAddon) {
this.log(
"\n" +
chalk.bold.underline("Welcome to the Electrode Component Generator") +
"\n" +
"\nWe're going to set up a new " +
chalk.bold("Electrode") +
" component, ready for development with" +
"\n" +
chalk.bold("react, webpack, demo, electrode component archetype, and live-reload")
);
}
},

return this._askFor();
}

writing() {
lernaStructure: {
Expand Down Expand Up @@ -285,12 +288,8 @@ module.exports = class extends Generator {
this.oldRoot = this.destinationRoot();
var newRoot = this.destinationPath() + "/" + this.originalDemoAppName;
this.destinationRoot(newRoot);

this.composeWith('electrode:app', {
options
}, {
local: require.resolve('../generators/app')
});

this.composeWith(require.resolve('../generators/app'), options);
}
}
}
Expand Down
Loading

0 comments on commit d4dc390

Please sign in to comment.