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

Commit 2514a81

Browse files
Merge pull request #29 from Nikhil-Vats/overwrite_rename
Added option to overwrite or rename if build directory exists
2 parents a36414d + 6668aa7 commit 2514a81

File tree

3 files changed

+76
-177
lines changed

3 files changed

+76
-177
lines changed

__tests__/app.js

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ describe("generator-biojs-webcomponents:app - Make a new Web Component", () => {
7979
});
8080

8181
describe("generator-biojs-webcomponents:app - Upgrade an existing component by importing build file locally", () => {
82+
it("makes a new directory named - component-dist", async () => {
83+
await validators
84+
.directoryName("component-dist")
85+
.then(() => assert.file(["component-dist"]));
86+
});
8287
it("imports the build file", async () => {
8388
await validators
8489
.importBuildFileLocally(
@@ -88,57 +93,27 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
8893
assert.file(["component-dist/validator.js"]);
8994
});
9095
});
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-
});
99-
it("skips the current question if user enters skip", async () => {
100-
assert.equal(await validators.importBuildFileLocally("skip"), true);
101-
});
10296
it("throws an error if user enters an empty string as path of build file", async () => {
10397
assert.equal(
10498
await validators.importBuildFileFromNPM(""),
10599
chalk.red("This is a mandatory field, please answer.")
106100
);
107101
});
108-
it("throws an error if user enters an empty string as path of build file to be copied", async () => {
109-
assert.equal(
110-
await validators.copyBuildFileLocally(""),
111-
chalk.red("This is a mandatory field, please answer.")
112-
);
113-
});
114102
});
115103

116104
describe("generator-biojs-webcomponents:app - Upgrade an existing component by importing build file using npm", () => {
117-
it("works successfully if the package name and version entered is correct", async () => {
105+
it("throws an error if version entered does not exist", async () => {
118106
assert.equal(
119107
await validators.version("fkdk", { packageName: "node" }),
120108
chalk.red(
121109
"Sorry, the version - " +
122-
chalk.red.bold("fkdk") +
110+
chalk.yellow("fkdk") +
123111
" doesn't exist. Please enter again. Enter " +
124112
chalk.cyan("latest") +
125113
" if you want to import the latest version."
126114
)
127115
);
128116
});
129-
it("skips the current question if user enters skip", async () => {
130-
assert.equal(await validators.importBuildFileFromNPM("skip"), true);
131-
});
132-
it("downloads the file if URL entered is correct", async () => {
133-
// This will work as the directory component-dist is already created above
134-
await validators
135-
.copyBuildFileFromNPM(
136-
"https://cdn.jsdelivr.net/npm/[email protected]/build/protvista.min.gz.js"
137-
)
138-
.then(() => {
139-
assert.file(["component-dist/protvista.min.gz.js"]);
140-
});
141-
});
142117
it("throws an error if an npm package doesn't exist", async () => {
143118
assert.equal(
144119
await validators.packageName("cbaxyz"),
@@ -171,10 +146,4 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
171146
chalk.red("This is a mandatory field, please answer.")
172147
);
173148
});
174-
it("throws an error if downloadURL for build file to copied entered is empty", async () => {
175-
assert.equal(
176-
await validators.copyBuildFileFromNPM(""),
177-
chalk.red("This is a mandatory field, please answer.")
178-
);
179-
});
180149
});

generators/app/index.js

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ module.exports = class extends Generator {
4141
type: "rawlist",
4242
name: "importFrom",
4343
message:
44-
"We need the build file (generally index.js, main.js or componentName.js) for this, import it using one of the options -",
44+
"I need the build file (generally index.js, main.js or componentName.js) for this, import it using one of the options -",
4545
choices: [
4646
"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."
47+
"Tell us the path of the build file on your local machine and I will import it in the project.",
48+
"Tell us the npm package name, version, build file URL and I will download the build file."
4949
],
5050
default: 0
5151
}
@@ -84,7 +84,7 @@ module.exports = class extends Generator {
8484
type: "input",
8585
name: "checkVersionAndInstallComponent",
8686
message:
87-
"Great! We will import the latest version of the npm package, if you don't want this, enter the version.",
87+
"Great! I will import the latest version of the npm package, if you don't want this, enter the version.",
8888
default: "latest",
8989
when: function(responses) {
9090
if (responses.confirmPackageNameToInstallComponent) {
@@ -131,7 +131,7 @@ module.exports = class extends Generator {
131131
type: "input",
132132
name: "version",
133133
message:
134-
"Great! We will import the latest version of your file from the npm package, if you don't want this, enter the version.",
134+
"Great! I will import the latest version of your file from the npm package, if you don't want this, enter the version.",
135135
default: "latest",
136136
when: function(responses) {
137137
if (responses.confirmPackageName) {
@@ -144,31 +144,15 @@ module.exports = class extends Generator {
144144
},
145145
{
146146
type: "input",
147-
name: "importBuildFileFromNPM",
148-
message: function(answers) {
149-
return (
150-
"This URL - " +
151-
chalk.bold.yellow(
152-
"https://www.jsdelivr.com/package/npm/" +
153-
answers.packageName +
154-
"?version=" +
155-
answers.version
156-
) +
157-
" 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."
158-
);
159-
},
160-
when: function(responses) {
161-
if (responses.confirmPackageName) {
162-
return true; // Show this prompt if user says that package description is correct
163-
}
164-
165-
return false; // Don't show this prompt if user says that package description is incorrect
166-
},
167-
validate: validators.importBuildFileFromNPM
147+
name: "directoryName",
148+
message:
149+
"The build file will be imported in a separate directory in the project's root. Enter the name of this directory or press Enter if you like to go with default.",
150+
validate: validators.directoryName,
151+
default: "component-dist"
168152
},
169153
{
170154
type: "input",
171-
name: "copyBuildFileFromNPM",
155+
name: "importBuildFileFromNPM",
172156
message: function(answers) {
173157
return (
174158
"This URL - " +
@@ -178,41 +162,36 @@ module.exports = class extends Generator {
178162
"?version=" +
179163
answers.version
180164
) +
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."
165+
" contains the directory of the package, please find the build file (generally in the dist or build folder) and paste the link here, I will download it for you."
182166
);
183167
},
184168
when: function(responses) {
185-
if (responses.importBuildFileFromNPM === "skip") {
169+
if (responses.confirmPackageName) {
186170
return true; // Show this prompt if user says that package description is correct
187171
}
188172

189173
return false; // Don't show this prompt if user says that package description is incorrect
190174
},
191-
validate: validators.copyBuildFileFromNPM
175+
validate: validators.importBuildFileFromNPM
192176
}
193177
];
194178

195179
// Prompt(s) if user chooses to import files locally from computer
196180
const localPrompts = [
197181
{
198182
type: "input",
199-
name: "importBuildFileLocally",
200-
message: "Please enter the path of the build file.",
201-
validate: validators.importBuildFileLocally
183+
name: "directoryName",
184+
message: `The build file will be imported in a separate directory in the project's root. Enter the name of this directory or press Enter if you like to go with default ${chalk.cyan(
185+
"component-dist"
186+
)}.`,
187+
validate: validators.directoryName,
188+
default: "component-dist"
202189
},
203190
{
204191
type: "input",
205-
name: "copyBuildFileLocally",
206-
message:
207-
"Please enter the path of the build file, we will paste it into the existing directory.",
208-
when: function(responses) {
209-
if (responses.importBuildFileLocally === "skip") {
210-
return true; // Show this prompt if user says that package description is correct
211-
}
212-
213-
return false; // Don't show this prompt if user says that package description is incorrect
214-
},
215-
validate: validators.copyBuildFileLocally
192+
name: "importBuildFileLocally",
193+
message: "Please enter the path of the build file.",
194+
validate: validators.importBuildFileLocally
216195
}
217196
];
218197

@@ -466,7 +445,7 @@ module.exports = class extends Generator {
466445
yarn: false
467446
});
468447
this.log(
469-
`While we install the dependencies, you can read the next steps - \n1. Write the code for your component, instructions are in ${chalk.yellow(
448+
`While I install the dependencies, you can read the next steps - \n1. Write the code for your component, instructions are in ${chalk.yellow(
470449
"src/index.js"
471450
)}.\n2. Add css styles to your component in ${chalk.yellow(
472451
"src/style"

0 commit comments

Comments
 (0)