@vexify-org/yaggs 6.14.0 → 6.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +8 -0
- package/lib/parser.js +43 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -185,6 +185,14 @@ class Yaggs {
|
|
|
185
185
|
return this.parser.file(path, options);
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
template(str, data = {}) {
|
|
189
|
+
return this.parser.template(str, data);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
formatDate(date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') {
|
|
193
|
+
return this.parser.formatDate(date, format);
|
|
194
|
+
}
|
|
195
|
+
|
|
188
196
|
check(fn) {
|
|
189
197
|
this.middleware(fn);
|
|
190
198
|
return this;
|
package/lib/parser.js
CHANGED
|
@@ -1129,6 +1129,49 @@ complete -c ${this.scriptName} `;
|
|
|
1129
1129
|
};
|
|
1130
1130
|
}
|
|
1131
1131
|
|
|
1132
|
+
template(str, data = {}) {
|
|
1133
|
+
return str.replace(/{{([^}]+)}}/g, (match, key) => {
|
|
1134
|
+
const keys = key.trim().split('.');
|
|
1135
|
+
let value = data;
|
|
1136
|
+
|
|
1137
|
+
for (const k of keys) {
|
|
1138
|
+
if (value && typeof value === 'object') {
|
|
1139
|
+
value = value[k];
|
|
1140
|
+
} else {
|
|
1141
|
+
value = undefined;
|
|
1142
|
+
break;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
return value !== undefined ? value : match;
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
formatDate(date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') {
|
|
1151
|
+
const d = typeof date === 'string' ? new Date(date) : date;
|
|
1152
|
+
|
|
1153
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
1154
|
+
|
|
1155
|
+
const replacements = {
|
|
1156
|
+
YYYY: d.getFullYear(),
|
|
1157
|
+
MM: pad(d.getMonth() + 1),
|
|
1158
|
+
DD: pad(d.getDate()),
|
|
1159
|
+
HH: pad(d.getHours()),
|
|
1160
|
+
mm: pad(d.getMinutes()),
|
|
1161
|
+
ss: pad(d.getSeconds()),
|
|
1162
|
+
ms: d.getMilliseconds().toString().padStart(3, '0'),
|
|
1163
|
+
YYYYMMDD: `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}`,
|
|
1164
|
+
HHmmss: `${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`,
|
|
1165
|
+
day: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()],
|
|
1166
|
+
month: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()],
|
|
1167
|
+
timestamp: Math.floor(d.getTime() / 1000)
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
return format.replace(/(YYYY|MM|DD|HH|mm|ss|ms|YYYYMMDD|HHmmss|day|month|timestamp)/g, (match) => {
|
|
1171
|
+
return replacements[match] !== undefined ? replacements[match] : match;
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1132
1175
|
getHelp() {
|
|
1133
1176
|
let help = `\u001b[36m${this.scriptName}\u001b[0m`;
|
|
1134
1177
|
|