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

Commit b9ca2c5

Browse files
committed
Fixed bug - npm package not installing
If user wanted to upgrade an existing package by installing an npm package. The npm package installed was removed when other dependencies were installed. This fixes that by removing the installing logic from validator.js and using a default yeoman function.
1 parent 165e98f commit b9ca2c5

File tree

3 files changed

+20
-46
lines changed

3 files changed

+20
-46
lines changed

__tests__/app.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
158158
});
159159

160160
describe("generator-biojs-webcomponents:app - Upgrade an existing component by installing component from npm package", () => {
161-
it("installs the latest component from its npm package if user enters a valid version", async () => {
162-
jest.setTimeout(10000); // Increases timeout for this test
161+
it("throws an error if version entered does not exist", async () => {
163162
let res = await validators.checkVersionAndInstallComponent("latest", {
164163
packageNameToInstallComponent: "is-odd"
165164
});

generators/app/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ module.exports = class extends Generator {
321321
if (props.importFrom === upgradeComponentPrompts[0].choices[0]) {
322322
return this.prompt(installNpmPackagePrompts).then(props => {
323323
// If user chooses to go back and choose source of importing file again
324+
this.npmProps = props;
324325
if (props.changeImportSourceFromNpmPackage) {
325326
return recursivePromptExecution(
326327
props.changeImportSourceFromNpmPackage
@@ -469,6 +470,21 @@ module.exports = class extends Generator {
469470
this.destinationRoot("./");
470471
}
471472

473+
installingNPMPackage() {
474+
if (
475+
this.npmProps &&
476+
this.npmProps.packageNameToInstallComponent &&
477+
this.npmProps.checkVersionAndInstallComponent
478+
) {
479+
this.npmInstall(
480+
this.npmProps.packageNameToInstallComponent +
481+
"@" +
482+
this.npmProps.checkVersionAndInstallComponent,
483+
{ "save-dev": true }
484+
);
485+
}
486+
}
487+
472488
install() {
473489
this.installDependencies({
474490
npm: true,

generators/app/validator.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -100,51 +100,13 @@ validators.version = async function(props, answers) {
100100
validators.checkVersionAndInstallComponent = async function(props, answers) {
101101
if (props) {
102102
if (props === "latest") {
103-
let res = await executeCommand(
104-
"cd " +
105-
projectDirectory +
106-
" && npm i " +
107-
answers.packageNameToInstallComponent +
108-
"@" +
109-
props +
110-
" --save-exact",
111-
"checkVersionAndInstallComponent"
112-
)
113-
.then(() => true)
114-
.catch(err => {
115-
return chalk.red(
116-
`Oops! We encountered an error. Please see below for the more details - \n${chalk.yellow(
117-
err
118-
)}`
119-
);
120-
});
121-
return res;
103+
return true;
122104
}
123105

124106
let command =
125107
"npm view " + answers.packageNameToInstallComponent + "@" + props;
126108
let res = await executeCommand(command, "version")
127-
.then(async () => {
128-
let res = await executeCommand(
129-
"cd " +
130-
projectDirectory +
131-
" && npm i " +
132-
answers.packageNameToInstallComponent +
133-
"@" +
134-
props +
135-
" --save-exact",
136-
"checkVersionAndInstallComponent"
137-
)
138-
.then(() => true)
139-
.catch(err => {
140-
return chalk.red(
141-
`Oops! We encountered an error. Please see below for the more details - \n${chalk.yellow(
142-
err
143-
)}`
144-
);
145-
});
146-
return res;
147-
})
109+
.then(() => true)
148110
.catch(() =>
149111
chalk.red(
150112
"Sorry, the version - " +
@@ -305,10 +267,7 @@ function executeCommand(command, type) {
305267
// The command couldn't be executed
306268
spinner.stop();
307269
reject(err);
308-
} else if (
309-
type === "version" ||
310-
type === "checkVersionAndInstallComponent"
311-
) {
270+
} else if (type === "version") {
312271
// Command successfully executed
313272
if (stdout) {
314273
spinner.stop();

0 commit comments

Comments
 (0)