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

Commit 8d63e31

Browse files
committed
Added overwrite or rename directory option in npm flow
1 parent 0a0aa23 commit 8d63e31

File tree

3 files changed

+89
-77
lines changed

3 files changed

+89
-77
lines changed

__tests__/app.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,16 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
9999
it("pastes the build file in renamed directory", async () => {
100100
await validators
101101
.importBuildFileInRenamedDirectory(path.join(__dirname, "../LICENSE"), {
102-
renameDirectory: "test-directory"
102+
renameDirectoryLocal: "test-directory"
103103
})
104104
.then(() => assert.file(["test-directory/LICENSE"]));
105105
});
106106
it("overwrites the directory content", async () => {
107107
await validators
108-
.overwriteDirectoryContent(path.join(__dirname, "../README.md"))
108+
.overwriteDirectoryContent(path.join(__dirname, "../README.md"), {
109+
renameOrOverwriteLocal:
110+
"Overwrite the files in the existing component-dist directory."
111+
})
109112
.then(() => {
110113
assert.file(["component-dist/README.md"]);
111114
});
@@ -125,7 +128,7 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
125128
});
126129

127130
describe("generator-biojs-webcomponents:app - Upgrade an existing component by importing build file using npm", () => {
128-
it("works successfully if the package name and version entered is correct", async () => {
131+
it("throws an error if version entered does not exist", async () => {
129132
assert.equal(
130133
await validators.version("fkdk", { packageName: "node" }),
131134
chalk.red(
@@ -140,16 +143,6 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
140143
it("skips the current question if user enters skip", async () => {
141144
assert.equal(await validators.importBuildFileFromNPM("skip"), true);
142145
});
143-
it("downloads the file if URL entered is correct", async () => {
144-
// This will work as the directory component-dist is already created above
145-
await validators
146-
.copyBuildFileFromNPM(
147-
"https://cdn.jsdelivr.net/npm/[email protected]/build/protvista.min.gz.js"
148-
)
149-
.then(() => {
150-
assert.file(["component-dist/protvista.min.gz.js"]);
151-
});
152-
});
153146
it("throws an error if an npm package doesn't exist", async () => {
154147
assert.equal(
155148
await validators.packageName("cbaxyz"),
@@ -182,10 +175,4 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
182175
chalk.red("This is a mandatory field, please answer.")
183176
);
184177
});
185-
it("throws an error if downloadURL for build file to copied entered is empty", async () => {
186-
assert.equal(
187-
await validators.copyBuildFileFromNPM(""),
188-
chalk.red("This is a mandatory field, please answer.")
189-
);
190-
});
191178
});

generators/app/index.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,73 @@ module.exports = class extends Generator {
167167
validate: validators.importBuildFileFromNPM
168168
},
169169
{
170-
type: "input",
171-
name: "copyBuildFileFromNPM",
172-
message: function(answers) {
173-
return (
174-
"This URL - " +
175-
chalk.bold.yellow(
176-
"https://www.jsdelivr.com/package/npm/" +
177-
answers.packageName +
178-
"?version=" +
179-
answers.version
180-
) +
181-
" 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."
182-
);
183-
},
170+
type: "list",
171+
name: "renameOrOverwriteNpm",
172+
message: "What do you want to do?",
173+
choices: [
174+
"Rename the component-dist directory we are making.",
175+
"Overwrite the files in the existing component-dist directory."
176+
],
184177
when: function(responses) {
185178
if (responses.importBuildFileFromNPM === "skip") {
186179
return true; // Show this prompt if user says that package description is correct
187180
}
188181

189182
return false; // Don't show this prompt if user says that package description is incorrect
183+
}
184+
},
185+
{
186+
type: "input",
187+
name: "renameDirectoryNpm",
188+
message: `Enter the name of the directory in which you want to import the build file, make sure it is not ${chalk.cyan(
189+
"dist"
190+
)} or ${chalk.cyan("component-dist")}.`,
191+
when: function(responses) {
192+
if (
193+
responses.renameOrOverwriteNpm ===
194+
"Rename the component-dist directory we are making."
195+
) {
196+
return true;
197+
}
198+
199+
return false;
190200
},
191-
validate: validators.copyBuildFileFromNPM
201+
validate: validators.renameDirectory
202+
},
203+
{
204+
type: "input",
205+
name: "importBuildFileInRenamedDirectoryNpm",
206+
message:
207+
"Great! Now enter the path of the build file to import it in the renamed directory.",
208+
when: function(responses) {
209+
if (
210+
responses.renameOrOverwriteNpm ===
211+
"Rename the component-dist directory we are making."
212+
) {
213+
return true;
214+
}
215+
216+
return false;
217+
},
218+
validate: validators.importBuildFileInRenamedDirectory
219+
},
220+
{
221+
type: "input",
222+
name: "overwriteDirectoryContentNpm",
223+
message: `Enter the path of the build file. Please note that this will overwrite all the existing content in ${chalk.cyan(
224+
"component-dist"
225+
)}.`,
226+
when: function(responses) {
227+
if (
228+
responses.renameOrOverwriteNpm ===
229+
"Overwrite the files in the existing component-dist directory."
230+
) {
231+
return true;
232+
}
233+
234+
return false;
235+
},
236+
validate: validators.overwriteDirectoryContent
192237
}
193238
];
194239

@@ -202,7 +247,7 @@ module.exports = class extends Generator {
202247
},
203248
{
204249
type: "list",
205-
name: "renameOrOverwrite",
250+
name: "renameOrOverwriteLocal",
206251
message: "What do you want to do?",
207252
choices: [
208253
"Rename the component-dist directory we are making.",
@@ -218,13 +263,13 @@ module.exports = class extends Generator {
218263
},
219264
{
220265
type: "input",
221-
name: "renameDirectory",
266+
name: "renameDirectoryLocal",
222267
message: `Enter the name of the directory in which you want to import the build file, make sure it is not ${chalk.cyan(
223268
"dist"
224269
)} or ${chalk.cyan("component-dist")}.`,
225270
when: function(responses) {
226271
if (
227-
responses.renameOrOverwrite ===
272+
responses.renameOrOverwriteLocal ===
228273
"Rename the component-dist directory we are making."
229274
) {
230275
return true;
@@ -241,7 +286,7 @@ module.exports = class extends Generator {
241286
"Great! Now enter the path of the build file to import it in the renamed directory.",
242287
when: function(responses) {
243288
if (
244-
responses.renameOrOverwrite ===
289+
responses.renameOrOverwriteLocal ===
245290
"Rename the component-dist directory we are making."
246291
) {
247292
return true;
@@ -259,7 +304,7 @@ module.exports = class extends Generator {
259304
)}.`,
260305
when: function(responses) {
261306
if (
262-
responses.renameOrOverwrite ===
307+
responses.renameOrOverwriteLocal ===
263308
"Overwrite the files in the existing component-dist directory."
264309
) {
265310
return true;

generators/app/validator.js

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,6 @@ validators.importBuildFileFromNPM = async function(props) {
143143
return chalk.red("This is a mandatory field, please answer.");
144144
};
145145

146-
validators.copyBuildFileFromNPM = async props => {
147-
if (props) {
148-
var res = await executeCommand(
149-
"cd component-dist && curl -O " + props,
150-
"copyBuildFileFromNPM"
151-
)
152-
.then(() => {
153-
return true;
154-
})
155-
.catch(err => {
156-
if (err.code === 3 || err.code === 23) {
157-
return chalk.red(
158-
"The URL is malformed. Please ensure the URL is in correct format."
159-
);
160-
}
161-
162-
return chalk.red(
163-
"Oops! We encountered an error, please see the log below for more details.\n" +
164-
err
165-
);
166-
}); // Import the build file in component-dist directory locally from computer
167-
return res;
168-
/**
169-
* Returns true if command execution is successful and proceeds to commonPrompts
170-
* returns and logs the error if execution fails
171-
*/
172-
}
173-
174-
return chalk.red("This is a mandatory field, please answer.");
175-
};
176-
177146
validators.importBuildFileLocally = async props => {
178147
if (props) {
179148
if (props === "skip") {
@@ -237,9 +206,14 @@ validators.renameDirectory = async props => {
237206

238207
validators.importBuildFileInRenamedDirectory = async (props, answers) => {
239208
if (props) {
240-
var res = await executeCommand(
241-
"cp " + props + " " + answers.renameDirectory
242-
)
209+
let command;
210+
if (answers.renameDirectoryLocal) {
211+
command = "cp " + props + " " + answers.renameDirectoryLocal;
212+
} else if (answers.renameDirectoryNpm) {
213+
command = "cd " + answers.renameDirectoryNpm + " && curl -O " + props;
214+
}
215+
216+
var res = await executeCommand(command)
243217
.then(() => true)
244218
.catch(err => {
245219
return chalk.red(
@@ -257,11 +231,17 @@ validators.importBuildFileInRenamedDirectory = async (props, answers) => {
257231
return chalk.red("This is a mandatory field, please answer.");
258232
};
259233

260-
validators.overwriteDirectoryContent = async props => {
234+
validators.overwriteDirectoryContent = async (props, answers) => {
261235
if (props) {
262-
var res = await executeCommand(
263-
"rm -rf component-dist/* && cp " + props + " component-dist"
264-
)
236+
let command;
237+
if (answers.renameOrOverwriteLocal) {
238+
command = "rm -rf component-dist/* && cp " + props + " component-dist";
239+
} else if (answers.renameOrOverwriteNpm) {
240+
command =
241+
"rm -rf component-dist/* && cd component-dist && curl -O " + props;
242+
}
243+
244+
var res = await executeCommand(command)
265245
.then(() => true)
266246
.catch(err => {
267247
return chalk.red(

0 commit comments

Comments
 (0)