-
Notifications
You must be signed in to change notification settings - Fork 0
/
argsParser.js
157 lines (133 loc) · 3.73 KB
/
argsParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
const fs = require("fs");
const path = require("path");
const pj = require("./package.json");
//configuration path
const configPath = path.join(
process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,
"/.kickconfig.json"
);
//check for config file. Use default if none found
const repoList = fs.existsSync(configPath)
? require(configPath)
: require("./repoInfo.json");
//check package version
const version = pj.version;
//get repo urls with property name
const rawRepos = Object.keys(repoList.repos)
.map(repo => {
return `${repo}. ${repoList.repos[repo]}`;
})
.toString()
.replace(/,/g, "\n ");
//output for list flag
const repos = `
Starter Repo List:
${rawRepos}
`;
//output for help flag
const help = `
Usage:
kick [repo] [flag] generate the [repo] starter project in the current directory
Options:
-c, --clone specify a random repo to clone
-e, --enterprise use enterprise github instance
-h, --help print help menu
-l, --list print starter repo options
-r, --remote create a remote repo for this project
-v, --version print current version of kick-init package
-V, --verbose print out each command being executed
[repo] specify the repo to clone, defaults to first one
Starter Repo List:
${rawRepos}
`;
const argsParser = args => {
//define the default info object
let info = {
configPath,
clone: repoList.repos[Object.keys(repoList.repos)[0]],
local: process.cwd(),
remote: false,
verbose: false,
enterprise: false
};
//check if there are any arguments
const length = args.length;
//if no arguments passed return the defaults
if (length === 0) {
return info;
}
//parse each argument
args.map((arg, i) => {
// if argument is passed serve up appropriate object
switch (arg) {
//use enterprise instance
case "-e":
case "--enterprise":
info.enterprise = true;
break;
//Log kick-init version
case "-v":
case "--version":
console.log("\x1b[1m\x1b[37m%s\x1b[0m", version);
info = null;
break;
//print out each command being run
case "-V":
case "--verbose":
info.verbose = true;
break;
//Log out repo list
case "-l":
case "--list":
console.log("\x1b[1m\x1b[37m%s\x1b[0m", repos);
info = null;
break;
//user set repo to clone. Expected as next argument
case "-c":
case "--clone":
if (typeof args[i + 1] == "string" && args[i + 1].includes(".git")) {
info.clone = args[i + 1];
} else {
console.error(
"\x1b[31m%s\x1b[0m",
`ERROR: ${args[i + 1]} is not valid argument following ${arg}.`
);
throw new Error();
}
break;
//Log the help menu
case "-h":
case "--help":
console.log(help);
info = null;
break;
//user called for remote repository to be created
case "-r":
case "--remote":
info.remote = true;
break;
default:
//check if argument exist in config file
if (repoList.repos[arg]) {
//since it does set it to be cloned
info.clone = repoList.repos[arg];
}
//check if file path or url
else if (!arg.includes("://")) {
//if you made it to here you should be
//a file path or url and you are not
console.error(
"\x1b[31m%s\x1b[0m",
`ERROR: ${arg} is not valid argument.`
);
throw new Error();
}
}
});
return info;
};
module.exports = {
argsParser,
repoList
};