Skip to content

Commit 05494f2

Browse files
committed
added regex for link validation
1 parent 6c8be41 commit 05494f2

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

Readme.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ _(Note: The name must match exactly.)_
100100
**Templates can also be added from GitHub with:**
101101

102102
```bash
103-
init -c "https://github.com/user/repoitoryName"
103+
init -c "https://github.com/user/repoitoryName.git"
104104
```
105105

106106
##### Example
@@ -109,6 +109,13 @@ init -c "https://github.com/user/repoitoryName"
109109

110110
This process involves cloning the repository into the current terminal directory, removing the .git folder from the cloned repository, executing the init -a "repoName" command to create a copy in the templates folder, and then deleting the cloned repository folder from the current terminal location.
111111

112+
I have used this Regex to verify if the given string is a link or not.
113+
114+
```js
115+
const urlRegex =
116+
/^(https?|ftp):\/\/(([a-z\d]([a-z\d-]*[a-z\d])?\.)+[a-z]{2,}|localhost)(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/i;
117+
```
118+
112119
The reason it performs all these steps is because I attempted to accomplish it in a simpler manner but couldn't find one.
113120

114121
**To see all the Commands (Help)**
@@ -232,7 +239,7 @@ const [, , ...args] = process.argv;
232239

233240
const options = {
234241
condition: null,
235-
folderPath: null,
242+
folderPathOrNameOrLink: null,
236243
nodeFlagParam: "",
237244
};
238245

@@ -247,7 +254,7 @@ for (let i = 0; i < args.length; i++) {
247254
break;
248255
case "-h":
249256
options.condition = arg;
250-
options.folderPath = "";
257+
options.folderPathOrNameOrLink = "";
251258
break;
252259
case "-y":
253260
// Check if the previous option was '-a'
@@ -263,7 +270,7 @@ for (let i = 0; i < args.length; i++) {
263270
console.error(`Invalid option: ${arg}`);
264271
process.exit(1);
265272
} else {
266-
options.folderPath = arg;
273+
options.folderPathOrNameOrLink = arg;
267274
}
268275
break;
269276
}
@@ -422,7 +429,17 @@ if (!condition) {
422429
});
423430
};
424431

432+
const urlRegex =
433+
/^(https?|ftp):\/\/(([a-z\d]([a-z\d-]*[a-z\d])?\.)+[a-z]{2,}|localhost)(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/i;
434+
425435
const repoLink = process.argv[3];
436+
437+
if (!urlRegex.test(repoLink)) {
438+
console.log("Invalid repository link provided.");
439+
process.exit(1);
440+
} else {
441+
console.log("Repository link is valid.");
442+
}
426443
const gitCommand = `git clone --depth 1 ${repoLink}`;
427444
let projectName;
428445

@@ -541,7 +558,7 @@ ${chalk.bold.underline.white("Package Commands:")}
541558
```json
542559
{
543560
"name": "@s54a/init",
544-
"version": "4.0.0",
561+
"version": "4.2.0",
545562
"description": "Project Initializer",
546563
"main": "./index.js",
547564
"type": "module",

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const [, , ...args] = process.argv;
4040

4141
const options = {
4242
condition: null,
43-
folderPath: null,
43+
folderPathOrNameOrLink: null,
4444
nodeFlagParam: "",
4545
};
4646

@@ -55,7 +55,7 @@ for (let i = 0; i < args.length; i++) {
5555
break;
5656
case "-h":
5757
options.condition = arg;
58-
options.folderPath = "";
58+
options.folderPathOrNameOrLink = "";
5959
break;
6060
case "-y":
6161
// Check if the previous option was '-a'
@@ -71,7 +71,7 @@ for (let i = 0; i < args.length; i++) {
7171
console.error(`Invalid option: ${arg}`);
7272
process.exit(1);
7373
} else {
74-
options.folderPath = arg;
74+
options.folderPathOrNameOrLink = arg;
7575
}
7676
break;
7777
}
@@ -230,7 +230,17 @@ if (!condition) {
230230
});
231231
};
232232

233+
const urlRegex =
234+
/^(https?|ftp):\/\/(([a-z\d]([a-z\d-]*[a-z\d])?\.)+[a-z]{2,}|localhost)(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/i;
235+
233236
const repoLink = process.argv[3];
237+
238+
if (!urlRegex.test(repoLink)) {
239+
console.log("Invalid repository link provided.");
240+
process.exit(1);
241+
} else {
242+
console.log("Repository link is valid.");
243+
}
234244
const gitCommand = `git clone --depth 1 ${repoLink}`;
235245
let projectName;
236246

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@s54a/init",
3-
"version": "4.0.0",
3+
"version": "4.2.0",
44
"description": "Project Initializer",
55
"main": "./index.js",
66
"type": "module",

templates/cliTest/Text File - Copy (2).txt

Whitespace-only changes.

templates/cliTest/Text File - Copy.txt

Whitespace-only changes.

templates/cliTest/Text File.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)