-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
159 lines (139 loc) · 4.3 KB
/
main.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
import { faker } from "npm:@faker-js/faker";
import * as log from "https://deno.land/std@0.167.0/log/mod.ts";
import dayjs from "npm:dayjs";
import Denomander from "https://deno.land/x/denomander@0.9.3/mod.ts";
// deno-lint-ignore no-explicit-any
function validateDate(value: any): string {
if (dayjs(String(value), "YYYYMMDD").format("YYYYMMDD") !== String(value)) {
throw `${program.errors.INVALID_RULE}: ${value}`;
}
return String(value);
}
// deno-lint-ignore no-explicit-any
function validateNumber(value: any): number {
if (Number.isNaN(parseInt(value, 10))) {
throw `${program.errors.INVALID_RULE}: ${value}`;
}
return parseInt(value, 10);
}
const program = new Denomander({
app_name: "kusa",
app_description: "jinko shiba generator",
app_version: "2.0.0",
});
// コマンド定義
program
.command("generate")
.requiredOption(
"--from",
"(required) This flag specifies the start date. [YYYYMMDD]",
validateDate,
)
.requiredOption(
"--to",
"(required) This flag specifies the end date. [YYYYMMDD]",
validateDate,
)
.option(
"--weekday",
"This argument is the percentage to run during the daytime on weekdays. [number]",
validateNumber,
)
.option(
"--holiday",
"This argument is the percentage to run on holidays and at night. [number]",
validateNumber,
)
.parse(Deno.args);
// 変数の定義
const { from, to, weekday: wd = 5, holiday: hd = 50 } = program;
// 開始の日時をセット
let date = dayjs(`${from}`, "YYYYMMDD");
// 終了の日時をセット
const end = dayjs(`${to}`, "YYYYMMDD");
// dateとendのdiffを取る
const diff = () => {
return date.diff(end) < 0;
};
// dateがendの日時を過ぎるまで続ける
while (diff()) {
// // 月-金判定
// const isWeekday = +date.format("d") > 0 && +date.format("d") < 6;
// 21-3時判定
const isWorkTime = +date.format("h") >= 21 && +date.format("h") <= 3;
// 1/n判定
const n = isWorkTime ? wd : hd;
// 月-金で21-4時の間で1/5の確率でgit commitを行う
// 土日もしくは日中帯の間で1/50の確率でgit commitを行う
const canCommit = Math.floor(Math.random() * n) === 0;
if (canCommit) {
await commit();
}
// 時間を進める
date = date.add(15, "minute");
}
// commitする
async function commit() {
Deno.env.set("GIT_AUTHOR_DATE", date.format());
Deno.env.set("GIT_COMMITTER_DATE", date.format());
// フェイクユーザーリストを作成
if (faker.datatype.boolean()) {
const fakePersonList = [...Array(faker.number.int(20))].map(() => {
return {
userId: faker.string.uuid(),
fullName: faker.person.fullName(),
bio: faker.person.bio(),
gender: faker.person.gender(),
birthdate: faker.date.birthdate(),
registeredAt: faker.date.past(),
};
});
await Deno.writeTextFile(
".fake/personList.json",
JSON.stringify(fakePersonList, null, 2),
);
}
// フェイク会社リストを作成
if (faker.datatype.boolean()) {
const fakeCompanyList = [...Array(faker.number.int(20))].map(() => {
return {
companyId: faker.string.uuid(),
companyName: faker.company.name(),
catchPhrase: faker.company.catchPhrase(),
buzzPhrase: faker.company.buzzPhrase(),
established: faker.date.birthdate({ min: 0, max: 100, mode: "age" }),
registeredAt: faker.date.past(),
};
});
await Deno.writeTextFile(
".fake/companyList.json",
JSON.stringify(fakeCompanyList, null, 2),
);
}
// フェイク商品リストを作成
if (faker.datatype.boolean()) {
const fakeProductList = [...Array(faker.number.int(20))].map(() => {
return {
productId: faker.string.uuid(),
productName: faker.commerce.productName(),
price: faker.commerce.price(),
productDescription: faker.commerce.productDescription(),
registeredAt: faker.date.past(),
};
});
await Deno.writeTextFile(
".fake/productList.json",
JSON.stringify(fakeProductList, null, 2),
);
}
const c = new Deno.Command("git", {
args: [
"commit",
"--allow-empty",
"-am",
faker.lorem.sentence({ max: 5, min: 3 }),
],
});
const { stdout, stderr } = await c.outputSync();
log.info(new TextDecoder().decode(stdout || stderr));
}