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

Commit 2c60bab

Browse files
committed
Added option to run generator in directory mentioned in arg
1 parent 6668aa7 commit 2c60bab

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

__tests__/app.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
8585
.then(() => assert.file(["component-dist"]));
8686
});
8787
it("imports the build file", async () => {
88-
await validators
89-
.importBuildFileLocally(
90-
path.join(__dirname, "../generators/app/validator.js")
91-
)
92-
.then(() => {
93-
assert.file(["component-dist/validator.js"]);
94-
});
88+
await validators.storeArg("web-component").then(async () => {
89+
await validators
90+
.importBuildFileLocally(
91+
path.join(__dirname, "../generators/app/validator.js")
92+
)
93+
.then(() => {
94+
assert.file(["web-component/component-dist/validator.js"]);
95+
});
96+
});
9597
});
9698
it("throws an error if user enters an empty string as path of build file", async () => {
9799
assert.equal(

generators/app/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ const yosay = require("yosay");
55
const validators = require("./validator");
66

77
module.exports = class extends Generator {
8+
// Note: arguments and options should be defined in the constructor.
9+
constructor(args, opts) {
10+
super(args, opts);
11+
this.argument("projectDirectory", { type: String, required: false });
12+
if (!this.options.projectDirectory) {
13+
this.options.projectDirectory = "web-component";
14+
}
15+
}
16+
817
initializing() {
918
this.composeWith(require.resolve("generator-license"), {
1019
defaultLicense: "MIT" // (optional) Select a default license
@@ -23,6 +32,12 @@ module.exports = class extends Generator {
2332

2433
// First prompt
2534
const initialPrompts = [
35+
{
36+
type: "input",
37+
name: "start",
38+
message: "Press any key to get going!",
39+
validate: () => validators.storeArg(this.options.projectDirectory)
40+
},
2641
{
2742
type: "list",
2843
name: "upgradeOrMake",
@@ -289,7 +304,7 @@ module.exports = class extends Generator {
289304
return this.prompt(initialPrompts).then(props => {
290305
// To access props later use this.props.someAnswer;
291306
// If user chooses to upgrade an existing component
292-
if (props.upgradeOrMake === initialPrompts[0].choices[0]) {
307+
if (props.upgradeOrMake === initialPrompts[1].choices[0]) {
293308
return this.prompt(upgradeComponentPrompts).then(props => {
294309
// If user chooses to import file locally from computer
295310
if (props.importFrom === upgradeComponentPrompts[0].choices[0]) {
@@ -339,7 +354,7 @@ module.exports = class extends Generator {
339354
}
340355

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

354369
writing() {
370+
this.destinationRoot(`./${this.options.projectDirectory}`);
355371
this.fs.copyTpl(
356372
this.templatePath("examples/index.html"),
357373
this.destinationPath("examples/index.html"),
@@ -436,6 +452,7 @@ module.exports = class extends Generator {
436452
this.templatePath("img/favicon.png"),
437453
this.destinationPath("img/favicon.png")
438454
);
455+
this.destinationRoot("./");
439456
}
440457

441458
install() {

generators/app/validator.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@ const chalk = require("chalk");
33
const { exec } = require("child_process");
44
const ora = require("ora");
55
const validators = {};
6+
67
let buildDirectory;
8+
let projectDirectory;
9+
10+
validators.storeArg = async function(props) {
11+
projectDirectory = props;
12+
var res = await executeCommand("mkdir " + projectDirectory)
13+
.then(() => true)
14+
.catch(err => {
15+
return chalk.red(
16+
"Oops! We encountered an error, please see the log below for more details.\n" +
17+
err
18+
);
19+
});
20+
return res;
21+
};
22+
723
validators.packageName = async function(props) {
824
if (props) {
925
let command = "npm view " + props;
@@ -137,7 +153,7 @@ validators.directoryName = async props => {
137153
validators.importBuildFileFromNPM = async function(props) {
138154
if (props) {
139155
var res = await executeCommand(
140-
"cd " + buildDirectory + " && curl -O " + props,
156+
"cd " + projectDirectory + "/" + buildDirectory + " && curl -O " + props,
141157
"importBuildFileFromNPM"
142158
)
143159
.then(() => {
@@ -169,7 +185,7 @@ validators.importBuildFileFromNPM = async function(props) {
169185
validators.importBuildFileLocally = async props => {
170186
if (props) {
171187
var res = await executeCommand(
172-
"cp " + props + " " + buildDirectory,
188+
"cp " + props + " " + projectDirectory + "/" + buildDirectory,
173189
"importBuildFileLocally"
174190
)
175191
.then(() => {
@@ -236,6 +252,14 @@ function executeCommand(command, type) {
236252
spinner: "weather"
237253
});
238254
spinner.start();
255+
if (type === "projectDirectory") {
256+
if (command === "." || command.trim() === "") {
257+
command = "mkdir webcomponent && cd webcomponent";
258+
} else {
259+
command = "mkdir " + command + " && cd " + command;
260+
}
261+
}
262+
239263
return new Promise((resolve, reject) => {
240264
exec(command, (err, stdout) => {
241265
if (err) {

0 commit comments

Comments
 (0)