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

Commit e4df4d1

Browse files
Merge pull request #32 from Nikhil-Vats/add_arg_feature
Added option to run generator in directory mentioned in arg
2 parents 2514a81 + e7ebf67 commit e4df4d1

File tree

3 files changed

+93
-34
lines changed

3 files changed

+93
-34
lines changed

__tests__/app.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ describe("generator-biojs-webcomponents:app - Make a new Web Component", () => {
1313
toolNameHuman: "Biojs test component"
1414
});
1515
});
16-
1716
it("creates expected files", () => {
1817
assert.file([
1918
"examples/index.html",
@@ -79,18 +78,23 @@ describe("generator-biojs-webcomponents:app - Make a new Web Component", () => {
7978
});
8079

8180
describe("generator-biojs-webcomponents:app - Upgrade an existing component by importing build file locally", () => {
81+
it("runs the generator in the directory passed in arguments", async () => {
82+
await validators.storeArg("test-component").then(() => {
83+
assert.file("test-component");
84+
});
85+
});
8286
it("makes a new directory named - component-dist", async () => {
8387
await validators
8488
.directoryName("component-dist")
85-
.then(() => assert.file(["component-dist"]));
89+
.then(() => assert.file(["test-component/component-dist"]));
8690
});
8791
it("imports the build file", async () => {
8892
await validators
8993
.importBuildFileLocally(
9094
path.join(__dirname, "../generators/app/validator.js")
9195
)
9296
.then(() => {
93-
assert.file(["component-dist/validator.js"]);
97+
assert.file(["test-component/component-dist/validator.js"]);
9498
});
9599
});
96100
it("throws an error if user enters an empty string as path of build file", async () => {

generators/app/index.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ 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", {
12+
type: String,
13+
required: false,
14+
default: "web-component",
15+
desc: `${chalk.blue(
16+
"path of the project directory, if you enter the path of a directory which does not exist, the generator will make one for you, otherwise it will use the existing one."
17+
)} Default directory: ${chalk.cyan("web-component")}`
18+
});
19+
}
20+
821
initializing() {
922
this.composeWith(require.resolve("generator-license"), {
1023
defaultLicense: "MIT" // (optional) Select a default license
@@ -23,6 +36,12 @@ module.exports = class extends Generator {
2336

2437
// First prompt
2538
const initialPrompts = [
39+
{
40+
type: "input",
41+
name: "start",
42+
message: "Press Enter key to get going!",
43+
validate: () => validators.storeArg(this.options.projectDirectory)
44+
},
2645
{
2746
type: "list",
2847
name: "upgradeOrMake",
@@ -43,7 +62,7 @@ module.exports = class extends Generator {
4362
message:
4463
"I need the build file (generally index.js, main.js or componentName.js) for this, import it using one of the options -",
4564
choices: [
46-
"Install component from npm package (Recommended - fastest way)",
65+
"Install component from npm package. (Recommended - fastest way)",
4766
"Tell us the path of the build file on your local machine and I will import it in the project.",
4867
"Tell us the npm package name, version, build file URL and I will download the build file."
4968
],
@@ -68,7 +87,7 @@ module.exports = class extends Generator {
6887
name: "changeImportSourceFromNpmPackage",
6988
message: "What do you want to do?",
7089
choices: [
71-
"Enter package name again to install component from npm package.",
90+
"Enter package name again to install component from npm package. (Recommended - fastest way)",
7291
"Import the file locally from your computer.",
7392
"Enter package name, version, build file URL to download the build file."
7493
],
@@ -115,7 +134,7 @@ module.exports = class extends Generator {
115134
name: "changeImportSourceFromNpmBuildFile",
116135
message: "What do you want to do?",
117136
choices: [
118-
"Enter package name again to install component from npm package.",
137+
"Enter package name again to install component from npm package. (Recommended - fastest way)",
119138
"Import the file locally from your computer.",
120139
"Enter package name, version, build file URL to download the build file."
121140
],
@@ -148,6 +167,13 @@ module.exports = class extends Generator {
148167
message:
149168
"The build file will be imported in a separate directory in the project's root. Enter the name of this directory or press Enter if you like to go with default.",
150169
validate: validators.directoryName,
170+
when: function(responses) {
171+
if (responses.confirmPackageName) {
172+
return true; // Show this prompt if user says that package description is correct
173+
}
174+
175+
return false; // Don't show this prompt if user says that package description is incorrect
176+
},
151177
default: "component-dist"
152178
},
153179
{
@@ -181,9 +207,8 @@ module.exports = class extends Generator {
181207
{
182208
type: "input",
183209
name: "directoryName",
184-
message: `The build file will be imported in a separate directory in the project's root. Enter the name of this directory or press Enter if you like to go with default ${chalk.cyan(
185-
"component-dist"
186-
)}.`,
210+
message:
211+
"The build file will be imported in a separate directory in the project's root. Enter the name of this directory or press Enter if you like to go with default.",
187212
validate: validators.directoryName,
188213
default: "component-dist"
189214
},
@@ -236,7 +261,6 @@ module.exports = class extends Generator {
236261
); // Call the function recursively
237262
}
238263

239-
// If user chooses to install component from npm after starting over
240264
return this.prompt(commonPrompts).then(props => {
241265
this.props = props;
242266
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
@@ -268,30 +292,20 @@ module.exports = class extends Generator {
268292
); // Call the function recursively
269293
}
270294

271-
// If user chooses to import from npm after starting over
272295
return this.prompt(commonPrompts).then(props => {
273296
this.props = props;
274297
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
275298
});
276299
});
277300
}
278-
279-
// If user chooses to import file locally when package description is incorrect
280-
return this.prompt(localPrompts).then(() => {
281-
return this.prompt(commonPrompts).then(props => {
282-
this.props = props;
283-
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
284-
});
285-
});
286301
}
287302

288303
// Normal (initial) prompt execution
289304
return this.prompt(initialPrompts).then(props => {
290-
// To access props later use this.props.someAnswer;
291305
// If user chooses to upgrade an existing component
292-
if (props.upgradeOrMake === initialPrompts[0].choices[0]) {
306+
if (props.upgradeOrMake === initialPrompts[1].choices[0]) {
293307
return this.prompt(upgradeComponentPrompts).then(props => {
294-
// If user chooses to import file locally from computer
308+
// If user chooses to install component as npm package
295309
if (props.importFrom === upgradeComponentPrompts[0].choices[0]) {
296310
return this.prompt(installNpmPackagePrompts).then(props => {
297311
// If user chooses to go back and choose source of importing file again
@@ -301,14 +315,14 @@ module.exports = class extends Generator {
301315
); // Call the function recursively
302316
}
303317

304-
// If user chooses to install component from npm initially
305318
return this.prompt(commonPrompts).then(props => {
306319
this.props = props;
307320
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
308321
});
309322
});
310323
}
311324

325+
// If user chooses to import build file locally
312326
if (props.importFrom === upgradeComponentPrompts[0].choices[1]) {
313327
return this.prompt(localPrompts).then(() => {
314328
return this.prompt(commonPrompts).then(props => {
@@ -318,17 +332,16 @@ module.exports = class extends Generator {
318332
});
319333
}
320334

335+
// If user chooses to import build file from npm package
321336
if (props.importFrom === upgradeComponentPrompts[0].choices[2]) {
322-
// If user chooses to import file from npm
323337
return this.prompt(npmPrompts).then(props => {
324-
// If user chooses to go back and choose source of importing file again
338+
// If user chooses to go back and choose source of importing again
325339
if (props.changeImportSourceFromNpmBuildFile) {
326340
return recursivePromptExecution(
327341
props.changeImportSourceFromNpmBuildFile
328342
); // Call the function recursively
329343
}
330344

331-
// If user chooses to import from npm initially
332345
return this.prompt(commonPrompts).then(props => {
333346
this.props = props;
334347
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
@@ -339,7 +352,7 @@ module.exports = class extends Generator {
339352
}
340353

341354
// If user chooses to make a new component
342-
if (props.upgradeOrMake === initialPrompts[0].choices[1]) {
355+
if (props.upgradeOrMake === initialPrompts[1].choices[1]) {
343356
return this.prompt(commonPrompts).then(props => {
344357
this.props = props;
345358
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
@@ -352,6 +365,7 @@ module.exports = class extends Generator {
352365
}
353366

354367
writing() {
368+
this.destinationRoot(`./${this.options.projectDirectory}`);
355369
this.fs.copyTpl(
356370
this.templatePath("examples/index.html"),
357371
this.destinationPath("examples/index.html"),
@@ -436,6 +450,7 @@ module.exports = class extends Generator {
436450
this.templatePath("img/favicon.png"),
437451
this.destinationPath("img/favicon.png")
438452
);
453+
this.destinationRoot("./");
439454
}
440455

441456
install() {

generators/app/validator.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,38 @@ 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+
if (projectDirectory.trim() === ".") {
13+
return true;
14+
}
15+
16+
var res = await executeCommand("mkdir " + projectDirectory)
17+
.then(() => true)
18+
.catch(async () => {
19+
var tryAgain = await executeCommand(
20+
"ls " + projectDirectory,
21+
"checkDirExistence"
22+
)
23+
.then(() => true)
24+
.catch(err => {
25+
return chalk.red(
26+
`Oops! We encountered an error. Please see below for the more details - \n${chalk.yellow(
27+
err
28+
)}\n.Try this - cd into that directory and run ${chalk.cyan(
29+
"`yo @biojs/biojs-webcomponents .`"
30+
)}.`
31+
);
32+
});
33+
return tryAgain;
34+
});
35+
return res;
36+
};
37+
738
validators.packageName = async function(props) {
839
if (props) {
940
let command = "npm view " + props;
@@ -69,7 +100,13 @@ validators.checkVersionAndInstallComponent = async function(props, answers) {
69100
var res = await executeCommand(command, "version")
70101
.then(async () => {
71102
var res = await executeCommand(
72-
"npm i " + answers.packageNameToInstallComponent + "@" + props,
103+
"cd " +
104+
projectDirectory +
105+
" && npm i " +
106+
answers.packageNameToInstallComponent +
107+
"@" +
108+
props +
109+
" --save-exact",
73110
"checkVersionAndInstallComponent"
74111
)
75112
.then(() => true)
@@ -103,8 +140,8 @@ validators.checkVersionAndInstallComponent = async function(props, answers) {
103140

104141
validators.directoryName = async props => {
105142
var res;
106-
if (props.trim() === "o") {
107-
res = await executeCommand(`rm -rf ${buildDirectory}/*`)
143+
if (props.trim() === "o" || props.trim() === "O") {
144+
res = await executeCommand(`rm -rf ${projectDirectory}/${buildDirectory}/*`)
108145
.then(() => true)
109146
.catch(err => {
110147
return chalk.red(
@@ -118,13 +155,13 @@ validators.directoryName = async props => {
118155

119156
if (props) {
120157
buildDirectory = props;
121-
res = await executeCommand("mkdir " + props)
158+
res = await executeCommand("mkdir " + projectDirectory + "/" + props)
122159
.then(() => true)
123160
.catch(err => {
124161
return chalk.red(
125162
"Uh oh! There already exists a directory with the same name. Enter a new name again or enter " +
126163
chalk.cyan("o") +
127-
" if you want to overwrite the contents of this directory and then import the build file. please see the log below for more details.\n" +
164+
" if you want to overwrite the contents of this directory and then import the build file. Please see the log below for more details.\n" +
128165
chalk.yellow(err)
129166
);
130167
});
@@ -137,7 +174,7 @@ validators.directoryName = async props => {
137174
validators.importBuildFileFromNPM = async function(props) {
138175
if (props) {
139176
var res = await executeCommand(
140-
"cd " + buildDirectory + " && curl -O " + props,
177+
"cd " + projectDirectory + "/" + buildDirectory + " && curl -O " + props,
141178
"importBuildFileFromNPM"
142179
)
143180
.then(() => {
@@ -169,7 +206,7 @@ validators.importBuildFileFromNPM = async function(props) {
169206
validators.importBuildFileLocally = async props => {
170207
if (props) {
171208
var res = await executeCommand(
172-
"cp " + props + " " + buildDirectory,
209+
"cp " + props + " " + projectDirectory + "/" + buildDirectory,
173210
"importBuildFileLocally"
174211
)
175212
.then(() => {
@@ -254,6 +291,9 @@ function executeCommand(command, type) {
254291
spinner.stop();
255292
reject(err);
256293
}
294+
} else if (type === "checkDirExistence") {
295+
spinner.stop();
296+
resolve(true);
257297
} else if (stdout) {
258298
process.stdout.write(`\n ${stdout} \n`); // If there is an output display it
259299
spinner.stop();

0 commit comments

Comments
 (0)