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

Commit e86b0be

Browse files
committed
Improved error handling
- The generator will run in directory mentioned in argument, whether the directory exists or not. - Changed tests accordingly - Removed bug which showed the "directoryName" question even when user wished to switch the source of importing file
1 parent 1f40de3 commit e86b0be

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

__tests__/app.js

Lines changed: 13 additions & 11 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,21 +78,24 @@ 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 () => {
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-
});
92+
await validators
93+
.importBuildFileLocally(
94+
path.join(__dirname, "../generators/app/validator.js")
95+
)
96+
.then(() => {
97+
assert.file(["test-component/component-dist/validator.js"]);
98+
});
9799
});
98100
it("throws an error if user enters an empty string as path of build file", async () => {
99101
assert.equal(

generators/app/index.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ module.exports = class extends Generator {
163163
message:
164164
"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.",
165165
validate: validators.directoryName,
166+
when: function(responses) {
167+
if (responses.confirmPackageName) {
168+
return true; // Show this prompt if user says that package description is correct
169+
}
170+
171+
return false; // Don't show this prompt if user says that package description is incorrect
172+
},
166173
default: "component-dist"
167174
},
168175
{
@@ -251,7 +258,6 @@ module.exports = class extends Generator {
251258
); // Call the function recursively
252259
}
253260

254-
// If user chooses to install component from npm after starting over
255261
return this.prompt(commonPrompts).then(props => {
256262
this.props = props;
257263
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
@@ -283,30 +289,20 @@ module.exports = class extends Generator {
283289
); // Call the function recursively
284290
}
285291

286-
// If user chooses to import from npm after starting over
287292
return this.prompt(commonPrompts).then(props => {
288293
this.props = props;
289294
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
290295
});
291296
});
292297
}
293-
294-
// If user chooses to import file locally when package description is incorrect
295-
return this.prompt(localPrompts).then(() => {
296-
return this.prompt(commonPrompts).then(props => {
297-
this.props = props;
298-
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
299-
});
300-
});
301298
}
302299

303300
// Normal (initial) prompt execution
304301
return this.prompt(initialPrompts).then(props => {
305-
// To access props later use this.props.someAnswer;
306302
// If user chooses to upgrade an existing component
307303
if (props.upgradeOrMake === initialPrompts[1].choices[0]) {
308304
return this.prompt(upgradeComponentPrompts).then(props => {
309-
// If user chooses to import file locally from computer
305+
// If user chooses to install component as npm package
310306
if (props.importFrom === upgradeComponentPrompts[0].choices[0]) {
311307
return this.prompt(installNpmPackagePrompts).then(props => {
312308
// If user chooses to go back and choose source of importing file again
@@ -316,14 +312,14 @@ module.exports = class extends Generator {
316312
); // Call the function recursively
317313
}
318314

319-
// If user chooses to install component from npm initially
320315
return this.prompt(commonPrompts).then(props => {
321316
this.props = props;
322317
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
323318
});
324319
});
325320
}
326321

322+
// If user chooses to import build file locally
327323
if (props.importFrom === upgradeComponentPrompts[0].choices[1]) {
328324
return this.prompt(localPrompts).then(() => {
329325
return this.prompt(commonPrompts).then(props => {
@@ -333,17 +329,16 @@ module.exports = class extends Generator {
333329
});
334330
}
335331

332+
// If user chooses to import build file from npm package
336333
if (props.importFrom === upgradeComponentPrompts[0].choices[2]) {
337-
// If user chooses to import file from npm
338334
return this.prompt(npmPrompts).then(props => {
339-
// If user chooses to go back and choose source of importing file again
335+
// If user chooses to go back and choose source of importing again
340336
if (props.changeImportSourceFromNpmBuildFile) {
341337
return recursivePromptExecution(
342338
props.changeImportSourceFromNpmBuildFile
343339
); // Call the function recursively
344340
}
345341

346-
// If user chooses to import from npm initially
347342
return this.prompt(commonPrompts).then(props => {
348343
this.props = props;
349344
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);

generators/app/validator.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ validators.storeArg = async function(props) {
1515

1616
var res = await executeCommand("mkdir " + projectDirectory)
1717
.then(() => true)
18-
.catch(err => {
19-
return chalk.red(
20-
`Oops! We encountered an error. Please see below for the more details - \n${chalk.yellow(
21-
err
22-
)}\nIf the directory whose path you entered already exists and you want that to be your project directory, cd into that directory and run ${chalk.cyan("`yo @biojs/biojs-webcomponents .`")}.`
23-
);
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;
2434
});
2535
return res;
2636
};
@@ -90,7 +100,13 @@ validators.checkVersionAndInstallComponent = async function(props, answers) {
90100
var res = await executeCommand(command, "version")
91101
.then(async () => {
92102
var res = await executeCommand(
93-
"cd " + projectDirectory + " && npm i " + answers.packageNameToInstallComponent + "@" + props + " --save-exact",
103+
"cd " +
104+
projectDirectory +
105+
" && npm i " +
106+
answers.packageNameToInstallComponent +
107+
"@" +
108+
props +
109+
" --save-exact",
94110
"checkVersionAndInstallComponent"
95111
)
96112
.then(() => true)
@@ -125,7 +141,7 @@ validators.checkVersionAndInstallComponent = async function(props, answers) {
125141
validators.directoryName = async props => {
126142
var res;
127143
if (props.trim() === "o") {
128-
res = await executeCommand(`rm -rf ${buildDirectory}/*`)
144+
res = await executeCommand(`rm -rf ${projectDirectory}/${buildDirectory}/*`)
129145
.then(() => true)
130146
.catch(err => {
131147
return chalk.red(
@@ -139,13 +155,13 @@ validators.directoryName = async props => {
139155

140156
if (props) {
141157
buildDirectory = props;
142-
res = await executeCommand("mkdir " + props)
158+
res = await executeCommand("mkdir " + projectDirectory + "/" + props)
143159
.then(() => true)
144160
.catch(err => {
145161
return chalk.red(
146162
"Uh oh! There already exists a directory with the same name. Enter a new name again or enter " +
147163
chalk.cyan("o") +
148-
" 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" +
149165
chalk.yellow(err)
150166
);
151167
});
@@ -275,6 +291,9 @@ function executeCommand(command, type) {
275291
spinner.stop();
276292
reject(err);
277293
}
294+
} else if (type === "checkDirExistence") {
295+
spinner.stop();
296+
resolve(true);
278297
} else if (stdout) {
279298
process.stdout.write(`\n ${stdout} \n`); // If there is an output display it
280299
spinner.stop();

0 commit comments

Comments
 (0)