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

Commit 2b9b383

Browse files
committed
Added option to overwrite or rename if build directory exists
1 parent a36414d commit 2b9b383

File tree

3 files changed

+116
-30
lines changed

3 files changed

+116
-30
lines changed

__tests__/app.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
8888
assert.file(["component-dist/validator.js"]);
8989
});
9090
});
91-
it("only pastes the build file", async () => {
92-
// This will work as the directory component-dist already exists because of the above test
93-
await validators
94-
.copyBuildFileLocally(path.join(__dirname, "../LICENSE"))
95-
.then(() => {
96-
assert.file(["component-dist/LICENSE"]);
97-
});
98-
});
9991
it("skips the current question if user enters skip", async () => {
10092
assert.equal(await validators.importBuildFileLocally("skip"), true);
10193
});
@@ -105,9 +97,9 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
10597
chalk.red("This is a mandatory field, please answer.")
10698
);
10799
});
108-
it("throws an error if user enters an empty string as path of build file to be copied", async () => {
100+
it("throws an error if user enters an empty string as the name for directory in which build file will be imported", async () => {
109101
assert.equal(
110-
await validators.copyBuildFileLocally(""),
102+
await validators.renameDirectory(""),
111103
chalk.red("This is a mandatory field, please answer.")
112104
);
113105
});

generators/app/index.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,73 @@ module.exports = class extends Generator {
201201
validate: validators.importBuildFileLocally
202202
},
203203
{
204-
type: "input",
205-
name: "copyBuildFileLocally",
206-
message:
207-
"Please enter the path of the build file, we will paste it into the existing directory.",
204+
type: "list",
205+
name: "renameOrOverwrite",
206+
message: "What do you want to do?",
207+
choices: [
208+
"Rename the component-dist directory we are making.",
209+
"Overwrite the files in the existing component-dist directory."
210+
],
208211
when: function(responses) {
209212
if (responses.importBuildFileLocally === "skip") {
210213
return true; // Show this prompt if user says that package description is correct
211214
}
212215

213216
return false; // Don't show this prompt if user says that package description is incorrect
217+
}
218+
},
219+
{
220+
type: "input",
221+
name: "renameDirectory",
222+
message: `Enter the name of the directory in which you want to import the build file, make sure it is not ${chalk.cyan(
223+
"dist"
224+
)} or ${chalk.cyan("component-dist")}.`,
225+
when: function(responses) {
226+
if (
227+
responses.renameOrOverwrite ===
228+
"Rename the component-dist directory we are making."
229+
) {
230+
return true;
231+
}
232+
233+
return false;
234+
},
235+
validate: validators.renameDirectory
236+
},
237+
{
238+
type: "input",
239+
name: "importBuildFileInRenamedDirectory",
240+
message:
241+
"Great! Now enter the path of the build file to import it in the renamed directory.",
242+
when: function(responses) {
243+
if (
244+
responses.renameOrOverwrite ===
245+
"Rename the component-dist directory we are making."
246+
) {
247+
return true;
248+
}
249+
250+
return false;
251+
},
252+
validate: validators.importBuildFileInRenamedDirectory
253+
},
254+
{
255+
type: "input",
256+
name: "overwriteDirectoryContent",
257+
message: `Enter the path of the build file. Please note that this will overwrite all the existing content in ${chalk.cyan(
258+
"component-dist"
259+
)}.`,
260+
when: function(responses) {
261+
if (
262+
responses.renameOrOverwrite ===
263+
"Overwrite the files in the existing component-dist directory."
264+
) {
265+
return true;
266+
}
267+
268+
return false;
214269
},
215-
validate: validators.copyBuildFileLocally
270+
validate: validators.overwriteDirectoryContent
216271
}
217272
];
218273

generators/app/validator.js

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ validators.importBuildFileFromNPM = async function(props) {
111111
.then(() => {
112112
return true;
113113
})
114-
.catch(async err => {
114+
.catch(err => {
115115
if (err.code === 1) {
116116
return chalk.red(
117117
`Sorry, there already seems to be a directory with the same name ${chalk.cyan(
@@ -145,14 +145,14 @@ validators.importBuildFileFromNPM = async function(props) {
145145

146146
validators.copyBuildFileFromNPM = async props => {
147147
if (props) {
148-
var res = executeCommand(
148+
var res = await executeCommand(
149149
"cd component-dist && curl -O " + props,
150150
"copyBuildFileFromNPM"
151151
)
152152
.then(() => {
153153
return true;
154154
})
155-
.catch(async err => {
155+
.catch(err => {
156156
if (err.code === 3 || err.code === 23) {
157157
return chalk.red(
158158
"The URL is malformed. Please ensure the URL is in correct format."
@@ -180,21 +180,21 @@ validators.importBuildFileLocally = async props => {
180180
return true;
181181
}
182182

183-
var res = executeCommand(
183+
var res = await executeCommand(
184184
"mkdir component-dist && cp " + props + " component-dist",
185185
"importBuildFileLocally"
186186
)
187187
.then(() => {
188188
return true;
189189
})
190-
.catch(async err => {
190+
.catch(err => {
191191
if (err.code === 1) {
192192
return chalk.red(
193193
`Oops! We encountered an error. This can happen due to one of the following reasons - \n\n1) The path of build file entered is incorrect. \n2) There's already a directory named ${chalk.cyan(
194194
"component-dist"
195195
)}, in this case please input ${chalk.cyan(
196196
"skip"
197-
)} to paste the build file in your existing directory.\n\nPlease see below for the exact error description - \n${chalk.yellow(
197+
)} to rename the new directory or overwrite files in the existing one.\n\nPlease see below for the exact error description - \n${chalk.yellow(
198198
err
199199
)}`
200200
);
@@ -215,21 +215,60 @@ validators.importBuildFileLocally = async props => {
215215
return chalk.red("This is a mandatory field, please answer.");
216216
};
217217

218-
validators.copyBuildFileLocally = async props => {
218+
validators.renameDirectory = async props => {
219+
if (props) {
220+
var res = await executeCommand("mkdir " + props)
221+
.then(() => true)
222+
.catch(err => {
223+
return chalk.red(
224+
"Oops! We encountered an error, please see the log below for more details.\n" +
225+
err
226+
);
227+
});
228+
return res;
229+
/**
230+
* Returns true if command execution is successful and proceeds to commonPrompts
231+
* returns and logs the error if execution fails
232+
*/
233+
}
234+
235+
return chalk.red("This is a mandatory field, please answer.");
236+
};
237+
238+
validators.importBuildFileInRenamedDirectory = async (props, answers) => {
219239
if (props) {
220-
var res = executeCommand(
221-
"cp " + props + " component-dist",
222-
"copyBuildFileLocally"
240+
var res = await executeCommand(
241+
"cp " + props + " " + answers.renameDirectory
223242
)
224-
.then(() => {
225-
return true;
226-
})
227-
.catch(async err => {
243+
.then(() => true)
244+
.catch(err => {
228245
return chalk.red(
229246
"Oops! We encountered an error, please see the log below for more details.\n" +
230247
err
231248
);
232-
}); // Import the build file in component-dist directory locally from computer
249+
});
250+
return res;
251+
/**
252+
* Returns true if command execution is successful and proceeds to commonPrompts
253+
* returns and logs the error if execution fails
254+
*/
255+
}
256+
257+
return chalk.red("This is a mandatory field, please answer.");
258+
};
259+
260+
validators.overwriteDirectoryContent = async props => {
261+
if (props) {
262+
var res = await executeCommand(
263+
"rm -rf component-dist/* && cp " + props + " component-dist"
264+
)
265+
.then(() => true)
266+
.catch(err => {
267+
return chalk.red(
268+
"Oops! We encountered an error, please see the log below for more details.\n" +
269+
err
270+
);
271+
});
233272
return res;
234273
/**
235274
* Returns true if command execution is successful and proceeds to commonPrompts

0 commit comments

Comments
 (0)