-
-
Notifications
You must be signed in to change notification settings - Fork 946
/
Copy pathindex.ts
249 lines (230 loc) · 6.31 KB
/
index.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import type { Faker } from '../..';
import { deprecated } from '../../internal/deprecated';
const GIT_DATE_FORMAT_BASE = new Intl.DateTimeFormat('en', {
weekday: 'short',
month: 'short',
day: 'numeric',
hour: '2-digit',
hourCycle: 'h24',
minute: '2-digit',
second: '2-digit',
year: 'numeric',
timeZone: 'UTC',
});
const GIT_TIMEZONE_FORMAT = new Intl.NumberFormat('en', {
minimumIntegerDigits: 4,
maximumFractionDigits: 0,
useGrouping: false,
signDisplay: 'always',
});
/**
* Module to generate git related entries.
*/
export class GitModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(GitModule.prototype) as Array<
keyof GitModule | 'constructor'
>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Generates a random branch name.
*
* @example
* faker.git.branch() // 'feed-parse'
*
* @since 5.0.0
*/
branch(): string {
const noun = this.faker.hacker.noun().replace(' ', '-');
const verb = this.faker.hacker.verb().replace(' ', '-');
return `${noun}-${verb}`;
}
/**
* Generates a random commit entry as printed by `git log`.
*
* @param options Options for the commit entry.
* @param options.merge Set to `true` to generate a merge message line.
* @param options.eol Choose the end of line character to use. Defaults to 'CRLF'.
* 'LF' = '\n',
* 'CRLF' = '\r\n'
*
* @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`.
*
* @example
* faker.git.commitEntry()
* // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
* // Author: Marion Becker <Marion_Becker49@gmail.com>
* // Date: Mon Nov 7 05:38:37 2022 -0600
* //
* // generate open-source system
*
* @since 5.0.0
*/
commitEntry(
options: {
/**
* Set to `true` to generate a merge message line.
*
* @default faker.datatype.boolean({ probability: 0.2 })
*/
merge?: boolean;
/**
* Choose the end of line character to use.
*
* - 'LF' = '\n',
* - 'CRLF' = '\r\n'
*
* @default 'CRLF'
*/
eol?: 'LF' | 'CRLF';
/**
* The date to use as reference point for the commit.
*
* @default new Date()
*/
refDate?: string | Date | number;
} = {}
): string {
const {
merge = this.faker.datatype.boolean({ probability: 0.2 }),
eol = 'CRLF',
refDate,
} = options;
const lines = [`commit ${this.faker.git.commitSha()}`];
if (merge) {
lines.push(
`Merge: ${this.commitSha({ length: 7 })} ${this.commitSha({
length: 7,
})}`
);
}
const firstName = this.faker.person.firstName();
const lastName = this.faker.person.lastName();
const fullName = this.faker.person.fullName({ firstName, lastName });
const username = this.faker.internet.userName(firstName, lastName);
let user = this.faker.helpers.arrayElement([fullName, username]);
const email = this.faker.internet.email(firstName, lastName);
// Normalize user according to https://github.com/libgit2/libgit2/issues/5342
user = user.replace(/^[\.,:;"\\']|[\<\>\n]|[\.,:;"\\']$/g, '');
lines.push(
`Author: ${user} <${email}>`,
`Date: ${this.commitDate({ refDate })}`,
'',
`\xa0\xa0\xa0\xa0${this.commitMessage()}`,
// to end with a eol char
''
);
const eolChar = eol === 'CRLF' ? '\r\n' : '\n';
const entry = lines.join(eolChar);
return entry;
}
/**
* Generates a random commit message.
*
* @example
* faker.git.commitMessage() // 'reboot cross-platform driver'
*
* @since 5.0.0
*/
commitMessage(): string {
return `${this.faker.hacker.verb()} ${this.faker.hacker.adjective()} ${this.faker.hacker.noun()}`;
}
/**
* Generates a date string for a git commit using the same format as `git log`.
*
* @param options The optional options object.
* @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`.
*
* @example
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
* faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
*
* @since 8.0.0
*/
commitDate(
options: {
/**
* The date to use as reference point for the commit.
*
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
} = {}
): string {
const { refDate = this.faker.defaultRefDate() } = options;
const dateParts = GIT_DATE_FORMAT_BASE.format(
this.faker.date.recent({ days: 1, refDate })
)
.replace(/,/g, '')
.split(' ');
[dateParts[3], dateParts[4]] = [dateParts[4], dateParts[3]];
// Timezone offset
dateParts.push(
GIT_TIMEZONE_FORMAT.format(
this.faker.number.int({ min: -11, max: 12 }) * 100
)
);
return dateParts.join(' ');
}
/**
* Generates a random commit sha.
*
* By default, the length of the commit sha is 40 characters.
*
* For a shorter commit sha, use the `length` option.
*
* Usual short commit sha length is:
* - 7 for GitHub
* - 8 for GitLab
*
* @param options Options for the commit sha.
* @param options.length The length of the commit sha. Defaults to 40.
*
* @example
* faker.git.commitSha() // '2c6e3880fd94ddb7ef72d34e683cdc0c47bec6e6'
*
* @since 5.0.0
*/
commitSha(
options: {
/**
* The length of the commit sha.
*
* @default 40
*/
length?: number;
} = {}
): string {
const { length = 40 } = options;
return this.faker.string.hexadecimal({
length,
casing: 'lower',
prefix: '',
});
}
/**
* Generates a random commit sha (short).
*
* @example
* faker.git.shortSha() // '6155732'
*
* @since 5.0.0
*
* @deprecated Use `faker.git.commitSha({ length: 7 })` instead.
*/
shortSha(): string {
deprecated({
deprecated: 'faker.git.shortSha()',
proposed: 'faker.git.commitSha({ length: 7 })',
since: '8.0',
until: '9.0',
});
return this.commitSha({ length: 7 });
}
}