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

Commit c70d10e

Browse files
Merge pull request #20 from Nikhil-Vats/error_handling
Error handling
2 parents b2dc998 + e8ff45d commit c70d10e

File tree

1 file changed

+210
-23
lines changed

1 file changed

+210
-23
lines changed

generators/app/index.js

Lines changed: 210 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,32 @@ module.exports = class extends Generator {
6060
validate: async function(props) {
6161
if (props) {
6262
let command = "npm view " + props;
63-
let res = await executeCommand(command, "packageName");
63+
let res = await executeCommand(command, "packageName")
64+
.then(() => {
65+
return true;
66+
})
67+
.catch(err => {
68+
if (err.code === 1) {
69+
return chalk.red(
70+
"There's no package on npm with the name " +
71+
chalk.red.bold(props) +
72+
". Please note that the package name is case sensitive."
73+
);
74+
}
75+
76+
return chalk.red(
77+
"Oops! We encountered an error, please see the log below for more details.\n" +
78+
err
79+
);
80+
});
6481
return res;
6582
/**
6683
* Returns true if command is succesfully executed and hence yeoman proceeds to the next prompt
6784
* returns and logs the error if command execution fails
6885
*/
6986
}
7087

71-
return "This is a mandatory field, please answer."; // Warn user if no input is entered
88+
return chalk.red("This is a mandatory field, please answer."); // Warn user if no input is entered
7289
}
7390
},
7491
{
@@ -106,9 +123,29 @@ module.exports = class extends Generator {
106123
return false; // Don't show this prompt if user says that package description is incorrect
107124
},
108125
validate: async function(props, answers) {
109-
let command = "npm view " + answers.packageName + "@" + props;
110-
var res = await executeCommand(command, "version");
111-
return res;
126+
if (props) {
127+
let command = "npm view " + answers.packageName + "@" + props;
128+
var res = await executeCommand(command, "version")
129+
.then(() => {
130+
return true;
131+
})
132+
.catch(() =>
133+
chalk.red(
134+
"Sorry, the version - " +
135+
chalk.red.bold(props) +
136+
" doesn't exist. Please enter again. Enter " +
137+
chalk.cyan("latest") +
138+
" if you want to import the latest version."
139+
)
140+
);
141+
return res;
142+
}
143+
144+
return chalk.red(
145+
"This is a mandatory field, please answer. Enter " +
146+
chalk.cyan("latest") +
147+
" if you want to import the latest version."
148+
); // Warn user if no input is entered
112149
}
113150
},
114151
{
@@ -134,15 +171,101 @@ module.exports = class extends Generator {
134171
return false; // Don't show this prompt if user says that package description is incorrect
135172
},
136173
validate: async function(props) {
137-
var res = await executeCommand(
138-
"mkdir dist && cd dist && curl -O " + props,
139-
"downloadURL"
140-
); // Import the build file in dist directory from npm
141-
return res;
142-
/**
143-
* Returns true if command execution is successful and proceeds to commonPrompts
144-
* returns and logs the error if execution fails
145-
*/
174+
if (props) {
175+
if (props === "skip") {
176+
return true;
177+
}
178+
179+
var res = await executeCommand(
180+
"mkdir component-dist && cd component-dist && curl -O " + props,
181+
"downloadURL"
182+
)
183+
.then(() => {
184+
return true;
185+
})
186+
.catch(async err => {
187+
if (err.code === 1) {
188+
return chalk.red(
189+
`Sorry, there already seems to be a directory with the same name ${chalk.cyan(
190+
"(component-dist)"
191+
)}, please change it's name or move it.\n If you want to just copy this file into that directory, enter ${chalk.cyan(
192+
"skip"
193+
)}.\n`
194+
);
195+
}
196+
197+
if (err.code === 3 || err.code === 23) {
198+
return chalk.red(
199+
"The URL is malformed. Please ensure the URL is in correct format."
200+
);
201+
}
202+
203+
return chalk.red(
204+
"Oops! We encountered an error, please see the log below for more details.\n" +
205+
err
206+
);
207+
}); // Import the build file in component-dist directory from npm
208+
return res;
209+
/**
210+
* Returns true if command execution is successful and proceeds to commonPrompts
211+
* returns and logs the error if execution fails
212+
*/
213+
}
214+
215+
return chalk.red("This is a mandatory field, please answer.");
216+
}
217+
},
218+
{
219+
type: "input",
220+
name: "downloadBuildFile",
221+
message: function(answers) {
222+
return (
223+
"This URL - " +
224+
chalk.bold.yellow(
225+
"https://www.jsdelivr.com/package/npm/" +
226+
answers.packageName +
227+
"?version=" +
228+
answers.version
229+
) +
230+
" contains the directory of the package, please find the build file (generally in the dist or build folder) and paste the link here, we will download it for you in the existing folder."
231+
);
232+
},
233+
when: function(responses) {
234+
if (responses.downloadURL === "skip") {
235+
return true; // Show this prompt if user says that package description is correct
236+
}
237+
238+
return false; // Don't show this prompt if user says that package description is incorrect
239+
},
240+
validate: async props => {
241+
if (props) {
242+
var res = executeCommand(
243+
"cd component-dist && curl -O " + props,
244+
"downloadBuildFile"
245+
)
246+
.then(() => {
247+
return true;
248+
})
249+
.catch(async err => {
250+
if (err.code === 3 || err.code === 23) {
251+
return chalk.red(
252+
"The URL is malformed. Please ensure the URL is in correct format."
253+
);
254+
}
255+
256+
return chalk.red(
257+
"Oops! We encountered an error, please see the log below for more details.\n" +
258+
err
259+
);
260+
}); // Import the build file in component-dist directory locally from computer
261+
return res;
262+
/**
263+
* Returns true if command execution is successful and proceeds to commonPrompts
264+
* returns and logs the error if execution fails
265+
*/
266+
}
267+
268+
return chalk.red("This is a mandatory field, please answer.");
146269
}
147270
}
148271
];
@@ -154,15 +277,79 @@ module.exports = class extends Generator {
154277
name: "pathOfBuildFile",
155278
message: "Please enter the path of the build file.",
156279
validate: async props => {
157-
var res = executeCommand(
158-
"mkdir dist && cp " + props + " dist",
159-
"local"
160-
); // Import the build file in dist directory locally from computer
161-
return res;
162-
/**
163-
* Returns true if command execution is successful and proceeds to commonPrompts
164-
* returns and logs the error if execution fails
165-
*/
280+
if (props) {
281+
if (props === "skip") {
282+
return true;
283+
}
284+
285+
var res = executeCommand(
286+
"mkdir component-dist && cp " + props + " component-dist",
287+
"pathOfBuildFile"
288+
)
289+
.then(() => {
290+
return true;
291+
})
292+
.catch(async err => {
293+
if (err.code === 1) {
294+
return chalk.red(
295+
`Sorry, there already seems to be a directory with the same name ${chalk.cyan(
296+
"(component-dist)"
297+
)}, please change it's name or move it.\n If you want to just copy this file into that directory, enter ${chalk.cyan(
298+
"skip"
299+
)}.\n`
300+
);
301+
}
302+
303+
return chalk.red(
304+
"Oops! We encountered an error, please see the log below for more details.\n" +
305+
err
306+
);
307+
}); // Import the build file in component-dist directory locally from computer
308+
return res;
309+
/**
310+
* Returns true if command execution is successful and proceeds to commonPrompts
311+
* returns and logs the error if execution fails
312+
*/
313+
}
314+
315+
return chalk.red("This is a mandatory field, please answer.");
316+
}
317+
},
318+
{
319+
type: "input",
320+
name: "copyBuildFile",
321+
message:
322+
"Please enter the path of the build file, we will paste it into the existing directory.",
323+
when: function(responses) {
324+
if (responses.pathOfBuildFile === "skip") {
325+
return true; // Show this prompt if user says that package description is correct
326+
}
327+
328+
return false; // Don't show this prompt if user says that package description is incorrect
329+
},
330+
validate: async props => {
331+
if (props) {
332+
var res = executeCommand(
333+
"cp " + props + " component-dist",
334+
"pathOfBuildFile"
335+
)
336+
.then(() => {
337+
return true;
338+
})
339+
.catch(async err => {
340+
return chalk.red(
341+
"Oops! We encountered an error, please see the log below for more details.\n" +
342+
err
343+
);
344+
}); // Import the build file in component-dist directory locally from computer
345+
return res;
346+
/**
347+
* Returns true if command execution is successful and proceeds to commonPrompts
348+
* returns and logs the error if execution fails
349+
*/
350+
}
351+
352+
return chalk.red("This is a mandatory field, please answer.");
166353
}
167354
}
168355
];

0 commit comments

Comments
 (0)