/
Options.js
197 lines (197 loc) · 6.54 KB
/
Options.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"use strict";
/**
* Generate Release
* Written by Kevin Gravier <kevin@mrkmg.com>
* MIT License 2018
*/
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var Minimist = require("minimist");
var path_1 = require("path");
var extend = require("xtend");
var options = {
currentVersion: {
default: null,
file_key: false,
switches: ["c", "current-version"],
validate: function (input) { return input === null || typeof input === "string"; },
},
dotReleaseFileLocation: {
default: "./.release.json",
file_key: false,
filter: function (input) { return path_1.resolve(input); },
switches: ["d", "release-file"],
validate: function (input) { return typeof input === "string"; },
},
filesToCommit: {
default: [],
file_key: "files_to_commit",
switches: false,
validate: function (input) { return Array.isArray(input); },
},
filesToVersion: {
default: ["README.md"],
file_key: "files_to_version",
switches: false,
validate: function (input) { return Array.isArray(input); },
},
nextVersion: {
default: null,
file_key: false,
switches: ["v", "next-version"],
validate: function (input) { return input === null || typeof input === "string"; },
},
noConfirm: {
default: false,
file_key: "no_confirm",
switches: ["n", "no-confirm"],
validate: function (input) { return typeof input === "boolean"; },
},
packageFileLocation: {
default: "./package.json",
file_key: "package_file_location",
filter: function (input) { return path_1.resolve(input); },
switches: ["p", "package"],
validate: function (input) { return typeof input === "string" && fs_1.existsSync(input); },
},
postCommitCommands: {
default: [],
file_key: "post_commit_commands",
switches: false,
validate: function (input) { return Array.isArray(input); },
},
postCompleteCommands: {
default: [],
file_key: "post_complete_commands",
switches: false,
validate: function (input) { return Array.isArray(input); },
},
preCommitCommands: {
default: [],
file_key: "pre_commit_commands",
switches: false,
validate: function (input) { return Array.isArray(input); },
},
quiet: {
default: false,
file_key: false,
switches: ["q", "quiet"],
validate: function (input) { return typeof input === "boolean"; },
},
releaseMessage: {
default: "Release {version}",
file_key: "release_message",
filter: function (input) {
if (input === false) {
return "Release {version}";
}
else {
return input;
}
},
switches: ["m", "set-release-message"],
validate: function (input) { return input === true || typeof input === "string"; },
},
releaseType: {
default: null,
file_key: "release_type",
switches: ["t", "release-type"],
validate: function (input) { return input === null ||
(typeof input === "string" && (input === "patch" || input === "minor" || input === "major")); },
},
remote: {
default: "origin",
file_key: "remote",
switches: ["o", "remote"],
validate: function (input) { return typeof input === "string"; },
},
showHelp: {
default: false,
file_key: false,
switches: ["h", "help"],
validate: function (input) { return typeof input === "boolean"; },
},
skipGitFlowFinish: {
default: false,
file_key: "skip_git_flow_finish",
switches: ["f", "skip-git-flow-finish"],
validate: function (input) { return typeof input === "boolean"; },
},
skipGitPull: {
default: false,
file_key: "skip_git_pull",
switches: ["l", "skip-git-pull"],
validate: function (input) { return typeof input === "boolean"; },
},
skipGitPush: {
default: false,
file_key: "skip_git_push",
switches: ["s", "skip-git-push"],
validate: function (input) { return typeof input === "boolean"; },
},
};
var Options = /** @class */ (function () {
function Options(args) {
this.fileData = {};
this.args = Minimist(args.slice(2));
this.getOption("dotReleaseFileLocation", options.dotReleaseFileLocation);
this.getOption("packageFileLocation", options.packageFileLocation);
this.loadPackageConfig();
this.loadFileData();
this.getAllOptions();
}
Options.prototype.getOption = function (key, opts) {
var value;
if (opts.switches) {
value = this.getSwitchValue(opts.switches);
}
if (value === undefined && opts.file_key !== false) {
value = this.getFileValue(opts.file_key);
}
if (value === undefined) {
value = opts.default;
}
if (opts.filter) {
value = opts.filter(value);
}
if (opts.validate && !opts.validate(value)) {
throw new Error("Invalid Value for " + key + ": " + value);
}
this[key] = value;
};
Options.prototype.getAllOptions = function () {
for (var key in options) {
if (options.hasOwnProperty(key)) {
this.getOption(key, options[key]);
}
}
};
Options.prototype.getSwitchValue = function (switches) {
for (var _i = 0, switches_1 = switches; _i < switches_1.length; _i++) {
var s = switches_1[_i];
if (this.args[s]) {
return this.args[s];
}
}
};
Options.prototype.getFileValue = function (fileKey) {
if (this.fileData[fileKey]) {
return this.fileData[fileKey];
}
};
Options.prototype.loadFileData = function () {
if (fs_1.existsSync(this.dotReleaseFileLocation)) {
this.fileData = extend(this.fileData, require(this.dotReleaseFileLocation));
}
};
Options.prototype.loadPackageConfig = function () {
if (fs_1.existsSync(this.packageFileLocation)) {
var packageJson = require(this.packageFileLocation);
if (packageJson.config && packageJson.config.generateRelease) {
this.fileData = extend(this.fileData, packageJson.config.generateRelease);
}
}
};
return Options;
}());
exports.Options = Options;