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

Commit 864e005

Browse files
Merge pull request #30 from Nikhil-Vats/install_npm_package
Added option to install npm component
2 parents adea202 + 06b7715 commit 864e005

File tree

2 files changed

+170
-24
lines changed

2 files changed

+170
-24
lines changed

generators/app/index.js

Lines changed: 129 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,60 @@ module.exports = class extends Generator {
4343
message:
4444
"We need the build file (generally index.js, main.js or componentName.js) for this, import it using one of the options -",
4545
choices: [
46-
"Tell us the path of the file on your local machine and we will import it in the project.",
47-
"Tell us the npm package name, version, etc. and we will import it."
46+
"Install component from npm package (Recommended - fastest way)",
47+
"Tell us the path of the build file on your local machine and we will import it in the project.",
48+
"Tell us the npm package name, version, build file URL and we will download the build file."
4849
],
4950
default: 0
5051
}
5152
];
5253

54+
const installNpmPackagePrompts = [
55+
{
56+
type: "input",
57+
name: "packageNameToInstallComponent",
58+
message: "Enter the package name (case sensitive).",
59+
validate: validators.packageName
60+
},
61+
{
62+
type: "confirm",
63+
name: "confirmPackageNameToInstallComponent",
64+
message: "Press enter if the package description shown is correct."
65+
},
66+
{
67+
type: "rawlist",
68+
name: "changeImportSourceFromNpmPackage",
69+
message: "What do you want to do?",
70+
choices: [
71+
"Enter package name again to install component from npm package.",
72+
"Import the file locally from your computer.",
73+
"Enter package name, version, build file URL to download the build file."
74+
],
75+
when: function(responses) {
76+
if (responses.confirmPackageNameToInstallComponent) {
77+
return false; // Don't show this prompt, if user says that package description is correct.
78+
}
79+
80+
return true; // Show this prompt if user says that package description is not correct.
81+
}
82+
},
83+
{
84+
type: "input",
85+
name: "checkVersionAndInstallComponent",
86+
message:
87+
"Great! We will import the latest version of the npm package, if you don't want this, enter the version.",
88+
default: "latest",
89+
when: function(responses) {
90+
if (responses.confirmPackageNameToInstallComponent) {
91+
return true; // Show this prompt if user says that package description is correct
92+
}
93+
94+
return false; // Don't show this prompt if user says that package description is incorrect
95+
},
96+
validate: validators.checkVersionAndInstallComponent
97+
}
98+
];
99+
53100
// Prompts if user chooses to import file(s) using npm
54101
const npmPrompts = [
55102
{
@@ -65,11 +112,12 @@ module.exports = class extends Generator {
65112
},
66113
{
67114
type: "rawlist",
68-
name: "changeImportSource",
115+
name: "changeImportSourceFromNpmBuildFile",
69116
message: "What do you want to do?",
70117
choices: [
71-
"Enter package name again.",
72-
"Import the file locally from your computer."
118+
"Enter package name again to install component from npm package.",
119+
"Import the file locally from your computer.",
120+
"Enter package name, version, build file URL to download the build file."
73121
],
74122
when: function(responses) {
75123
if (responses.confirmPackageName) {
@@ -190,18 +238,55 @@ module.exports = class extends Generator {
190238

191239
/** Interacts with the user using prompts
192240
* Recursive so that user can go to a previous step and/or change method of importing file
193-
* @param {string} repeatLocalOrNpmPrompts tells the generator which prompts should be shown again (localPrompts or npmPrompts)
241+
* @param {string} repeatPrompts tells the generator which prompts should be shown again (installNpmPackagePrompts or localPrompts or npmPrompts)
194242
* @returns {Promise} to execute prompts and wait for there execution to finish
195243
*/
196-
const recursivePromptExecution = repeatLocalOrNpmPrompts => {
244+
const recursivePromptExecution = repeatPrompts => {
197245
// If user changes the method of importing later, recursive execution
198-
if (repeatLocalOrNpmPrompts) {
246+
if (repeatPrompts) {
199247
// If user chooses to enter package name again when package description is incorrect
200-
if (repeatLocalOrNpmPrompts === npmPrompts[2].choices[0]) {
248+
if (
249+
repeatPrompts === npmPrompts[2].choices[0] ||
250+
repeatPrompts === installNpmPackagePrompts[2].choices[0]
251+
) {
252+
return this.prompt(installNpmPackagePrompts).then(props => {
253+
// If user chooses to go back and choose source of importing file again
254+
if (props.changeImportSourceFromNpmPackage) {
255+
return recursivePromptExecution(
256+
props.changeImportSourceFromNpmPackage
257+
); // Call the function recursively
258+
}
259+
260+
// If user chooses to install component from npm after starting over
261+
return this.prompt(commonPrompts).then(props => {
262+
this.props = props;
263+
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
264+
});
265+
});
266+
}
267+
268+
if (
269+
repeatPrompts === npmPrompts[2].choices[1] ||
270+
repeatPrompts === installNpmPackagePrompts[2].choices[1]
271+
) {
272+
return this.prompt(localPrompts).then(() => {
273+
return this.prompt(commonPrompts).then(props => {
274+
this.props = props;
275+
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
276+
});
277+
});
278+
}
279+
280+
if (
281+
repeatPrompts === npmPrompts[2].choices[2] ||
282+
repeatPrompts === installNpmPackagePrompts[2].choices[2]
283+
) {
201284
return this.prompt(npmPrompts).then(props => {
202285
// If user chooses to go back and choose source of importing file again
203-
if (props.changeImportSource) {
204-
return recursivePromptExecution(props.changeImportSource); // Call the function recursively
286+
if (props.changeImportSourceFromNpmBuildFile) {
287+
return recursivePromptExecution(
288+
props.changeImportSourceFromNpmBuildFile
289+
); // Call the function recursively
205290
}
206291

207292
// If user chooses to import from npm after starting over
@@ -229,6 +314,23 @@ module.exports = class extends Generator {
229314
return this.prompt(upgradeComponentPrompts).then(props => {
230315
// If user chooses to import file locally from computer
231316
if (props.importFrom === upgradeComponentPrompts[0].choices[0]) {
317+
return this.prompt(installNpmPackagePrompts).then(props => {
318+
// If user chooses to go back and choose source of importing file again
319+
if (props.changeImportSourceFromNpmPackage) {
320+
return recursivePromptExecution(
321+
props.changeImportSourceFromNpmPackage
322+
); // Call the function recursively
323+
}
324+
325+
// If user chooses to install component from npm initially
326+
return this.prompt(commonPrompts).then(props => {
327+
this.props = props;
328+
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
329+
});
330+
});
331+
}
332+
333+
if (props.importFrom === upgradeComponentPrompts[0].choices[1]) {
232334
return this.prompt(localPrompts).then(() => {
233335
return this.prompt(commonPrompts).then(props => {
234336
this.props = props;
@@ -237,19 +339,23 @@ module.exports = class extends Generator {
237339
});
238340
}
239341

240-
// If user chooses to import file from npm
241-
return this.prompt(npmPrompts).then(props => {
242-
// If user chooses to go back and choose source of importing file again
243-
if (props.changeImportSource) {
244-
return recursivePromptExecution(props.changeImportSource); // Call the function recursively
245-
}
246-
247-
// If user chooses to import from npm initially
248-
return this.prompt(commonPrompts).then(props => {
249-
this.props = props;
250-
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
342+
if (props.importFrom === upgradeComponentPrompts[0].choices[2]) {
343+
// If user chooses to import file from npm
344+
return this.prompt(npmPrompts).then(props => {
345+
// If user chooses to go back and choose source of importing file again
346+
if (props.changeImportSourceFromNpmBuildFile) {
347+
return recursivePromptExecution(
348+
props.changeImportSourceFromNpmBuildFile
349+
); // Call the function recursively
350+
}
351+
352+
// If user chooses to import from npm initially
353+
return this.prompt(commonPrompts).then(props => {
354+
this.props = props;
355+
this.props.toolNameCamel = toCamelCase(props.toolNameHuman);
356+
});
251357
});
252-
});
358+
}
253359
});
254360
}
255361

generators/app/validator.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,43 @@ validators.version = async function(props, answers) {
6161
); // Warn user if no input is entered
6262
};
6363

64+
validators.checkVersionAndInstallComponent = async function(props, answers) {
65+
if (props) {
66+
let command =
67+
"npm view " + answers.packageNameToInstallComponent + "@" + props;
68+
var res = await executeCommand(command, "version")
69+
.then(async () => {
70+
var res = await executeCommand(
71+
"npm i " + answers.packageNameToInstallComponent + "@" + props
72+
)
73+
.then(() => true)
74+
.catch(err => {
75+
chalk.red(
76+
"Oops! We encountered an error, please see the log below for more details.\n" +
77+
err
78+
);
79+
});
80+
return res;
81+
})
82+
.catch(() =>
83+
chalk.red(
84+
"Sorry, the version - " +
85+
chalk.red.bold(props) +
86+
" doesn't exist. Please enter again. Enter " +
87+
chalk.cyan("latest") +
88+
" if you want to import the latest version."
89+
)
90+
);
91+
return res;
92+
}
93+
94+
return chalk.red(
95+
"This is a mandatory field, please answer. Enter " +
96+
chalk.cyan("latest") +
97+
" if you want to import the latest version."
98+
); // Warn user if no input is entered
99+
};
100+
64101
validators.importBuildFileFromNPM = async function(props) {
65102
if (props) {
66103
if (props === "skip") {
@@ -253,7 +290,10 @@ function executeCommand(command, type) {
253290
// The command couldn't be executed
254291
spinner.stop();
255292
reject(err);
256-
} else if (type === "version") {
293+
} else if (
294+
type === "version" ||
295+
type === "checkVersionAndInstallComponent"
296+
) {
257297
// Command successfully executed
258298
if (stdout) {
259299
spinner.stop();

0 commit comments

Comments
 (0)