Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
Added option to run generator in directory mentioned in arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil-Vats committed Jul 27, 2019
1 parent 6668aa7 commit 2c60bab
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
16 changes: 9 additions & 7 deletions __tests__/app.js
Expand Up @@ -85,13 +85,15 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
.then(() => assert.file(["component-dist"]));
});
it("imports the build file", async () => {
await validators
.importBuildFileLocally(
path.join(__dirname, "../generators/app/validator.js")
)
.then(() => {
assert.file(["component-dist/validator.js"]);
});
await validators.storeArg("web-component").then(async () => {
await validators
.importBuildFileLocally(
path.join(__dirname, "../generators/app/validator.js")
)
.then(() => {
assert.file(["web-component/component-dist/validator.js"]);
});
});
});
it("throws an error if user enters an empty string as path of build file", async () => {
assert.equal(
Expand Down
21 changes: 19 additions & 2 deletions generators/app/index.js
Expand Up @@ -5,6 +5,15 @@ const yosay = require("yosay");
const validators = require("./validator");

module.exports = class extends Generator {
// Note: arguments and options should be defined in the constructor.
constructor(args, opts) {
super(args, opts);
this.argument("projectDirectory", { type: String, required: false });
if (!this.options.projectDirectory) {
this.options.projectDirectory = "web-component";
}
}

initializing() {
this.composeWith(require.resolve("generator-license"), {
defaultLicense: "MIT" // (optional) Select a default license
Expand All @@ -23,6 +32,12 @@ module.exports = class extends Generator {

// First prompt
const initialPrompts = [
{
type: "input",
name: "start",
message: "Press any key to get going!",
validate: () => validators.storeArg(this.options.projectDirectory)
},
{
type: "list",
name: "upgradeOrMake",
Expand Down Expand Up @@ -289,7 +304,7 @@ module.exports = class extends Generator {
return this.prompt(initialPrompts).then(props => {
// To access props later use this.props.someAnswer;
// If user chooses to upgrade an existing component
if (props.upgradeOrMake === initialPrompts[0].choices[0]) {
if (props.upgradeOrMake === initialPrompts[1].choices[0]) {
return this.prompt(upgradeComponentPrompts).then(props => {
// If user chooses to import file locally from computer
if (props.importFrom === upgradeComponentPrompts[0].choices[0]) {
Expand Down Expand Up @@ -339,7 +354,7 @@ module.exports = class extends Generator {
}

// If user chooses to make a new component
if (props.upgradeOrMake === initialPrompts[0].choices[1]) {
if (props.upgradeOrMake === initialPrompts[1].choices[1]) {
return this.prompt(commonPrompts).then(props => {
this.props = props;
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
Expand All @@ -352,6 +367,7 @@ module.exports = class extends Generator {
}

writing() {
this.destinationRoot(`./${this.options.projectDirectory}`);
this.fs.copyTpl(
this.templatePath("examples/index.html"),
this.destinationPath("examples/index.html"),
Expand Down Expand Up @@ -436,6 +452,7 @@ module.exports = class extends Generator {
this.templatePath("img/favicon.png"),
this.destinationPath("img/favicon.png")
);
this.destinationRoot("./");
}

install() {
Expand Down
28 changes: 26 additions & 2 deletions generators/app/validator.js
Expand Up @@ -3,7 +3,23 @@ const chalk = require("chalk");
const { exec } = require("child_process");
const ora = require("ora");
const validators = {};

let buildDirectory;
let projectDirectory;

validators.storeArg = async function(props) {
projectDirectory = props;
var res = await executeCommand("mkdir " + projectDirectory)
.then(() => true)
.catch(err => {
return chalk.red(
"Oops! We encountered an error, please see the log below for more details.\n" +
err
);
});
return res;
};

validators.packageName = async function(props) {
if (props) {
let command = "npm view " + props;
Expand Down Expand Up @@ -137,7 +153,7 @@ validators.directoryName = async props => {
validators.importBuildFileFromNPM = async function(props) {
if (props) {
var res = await executeCommand(
"cd " + buildDirectory + " && curl -O " + props,
"cd " + projectDirectory + "/" + buildDirectory + " && curl -O " + props,
"importBuildFileFromNPM"
)
.then(() => {
Expand Down Expand Up @@ -169,7 +185,7 @@ validators.importBuildFileFromNPM = async function(props) {
validators.importBuildFileLocally = async props => {
if (props) {
var res = await executeCommand(
"cp " + props + " " + buildDirectory,
"cp " + props + " " + projectDirectory + "/" + buildDirectory,
"importBuildFileLocally"
)
.then(() => {
Expand Down Expand Up @@ -236,6 +252,14 @@ function executeCommand(command, type) {
spinner: "weather"
});
spinner.start();
if (type === "projectDirectory") {
if (command === "." || command.trim() === "") {
command = "mkdir webcomponent && cd webcomponent";
} else {
command = "mkdir " + command + " && cd " + command;
}
}

return new Promise((resolve, reject) => {
exec(command, (err, stdout) => {
if (err) {
Expand Down

0 comments on commit 2c60bab

Please sign in to comment.