@asagiri-design/labels-config 0.2.2 → 0.3.1

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.
@@ -1,285 +0,0 @@
1
- import {
2
- __publicField
3
- } from "./chunk-QZ7TP4HQ.mjs";
4
-
5
- // src/github/client.ts
6
- import { execSync } from "child_process";
7
- var GitHubClient = class {
8
- constructor(options) {
9
- __publicField(this, "owner");
10
- __publicField(this, "repo");
11
- __publicField(this, "repoPath");
12
- this.owner = options.owner;
13
- this.repo = options.repo;
14
- this.repoPath = `${this.owner}/${this.repo}`;
15
- }
16
- /**
17
- * Execute gh CLI command
18
- */
19
- exec(command) {
20
- try {
21
- return execSync(command, {
22
- encoding: "utf-8",
23
- stdio: ["pipe", "pipe", "pipe"]
24
- }).trim();
25
- } catch (error) {
26
- throw new Error(`gh CLI command failed: ${error.message}`);
27
- }
28
- }
29
- /**
30
- * Fetch all labels from repository
31
- */
32
- async fetchLabels() {
33
- try {
34
- const output = this.exec(
35
- `gh label list --repo ${this.repoPath} --json name,color,description --limit 1000`
36
- );
37
- if (!output) {
38
- return [];
39
- }
40
- const labels = JSON.parse(output);
41
- return labels.map((label) => ({
42
- name: label.name,
43
- color: label.color,
44
- description: label.description || ""
45
- }));
46
- } catch (error) {
47
- throw new Error(`Failed to fetch labels from ${this.repoPath}: ${error}`);
48
- }
49
- }
50
- /**
51
- * Create a new label
52
- */
53
- async createLabel(label) {
54
- try {
55
- const name = label.name.replace(/"/g, '\\"');
56
- const description = label.description.replace(/"/g, '\\"');
57
- const color = label.color.replace("#", "");
58
- this.exec(
59
- `gh label create "${name}" --color "${color}" --description "${description}" --repo ${this.repoPath}`
60
- );
61
- return {
62
- name: label.name,
63
- color: label.color,
64
- description: label.description
65
- };
66
- } catch (error) {
67
- throw new Error(`Failed to create label "${label.name}": ${error}`);
68
- }
69
- }
70
- /**
71
- * Update an existing label
72
- */
73
- async updateLabel(currentName, label) {
74
- try {
75
- const escapedCurrentName = currentName.replace(/"/g, '\\"');
76
- const args = [];
77
- if (label.name && label.name !== currentName) {
78
- args.push(`--name "${label.name.replace(/"/g, '\\"')}"`);
79
- }
80
- if (label.color) {
81
- args.push(`--color "${label.color.replace("#", "")}"`);
82
- }
83
- if (label.description !== void 0) {
84
- args.push(`--description "${label.description.replace(/"/g, '\\"')}"`);
85
- }
86
- if (args.length === 0) {
87
- return {
88
- name: currentName,
89
- color: label.color || "",
90
- description: label.description || ""
91
- };
92
- }
93
- this.exec(
94
- `gh label edit "${escapedCurrentName}" ${args.join(" ")} --repo ${this.repoPath}`
95
- );
96
- return {
97
- name: label.name || currentName,
98
- color: label.color || "",
99
- description: label.description || ""
100
- };
101
- } catch (error) {
102
- throw new Error(`Failed to update label "${currentName}": ${error}`);
103
- }
104
- }
105
- /**
106
- * Delete a label
107
- */
108
- async deleteLabel(name) {
109
- try {
110
- const escapedName = name.replace(/"/g, '\\"');
111
- this.exec(`gh label delete "${escapedName}" --repo ${this.repoPath} --yes`);
112
- } catch (error) {
113
- throw new Error(`Failed to delete label "${name}": ${error}`);
114
- }
115
- }
116
- /**
117
- * Check if label exists
118
- */
119
- async hasLabel(name) {
120
- try {
121
- const labels = await this.fetchLabels();
122
- return labels.some((label) => label.name.toLowerCase() === name.toLowerCase());
123
- } catch (error) {
124
- return false;
125
- }
126
- }
127
- };
128
-
129
- // src/github/sync.ts
130
- var _GitHubLabelSync = class _GitHubLabelSync {
131
- constructor(options) {
132
- __publicField(this, "client");
133
- __publicField(this, "options");
134
- this.client = new GitHubClient(options);
135
- this.options = options;
136
- }
137
- /**
138
- * Log message if verbose mode is enabled
139
- */
140
- log(message) {
141
- if (this.options.verbose) {
142
- console.log(`[labels-config] ${message}`);
143
- }
144
- }
145
- /**
146
- * Sync labels to GitHub repository with batch operations for better performance
147
- */
148
- async syncLabels(localLabels) {
149
- this.log("Fetching remote labels...");
150
- const remoteLabels = await this.client.fetchLabels();
151
- const result = {
152
- created: [],
153
- updated: [],
154
- deleted: [],
155
- unchanged: [],
156
- errors: []
157
- };
158
- const remoteLabelMap = new Map(remoteLabels.map((label) => [label.name.toLowerCase(), label]));
159
- const localLabelMap = new Map(localLabels.map((label) => [label.name.toLowerCase(), label]));
160
- const toCreate = [];
161
- const toUpdate = [];
162
- const toDelete = [];
163
- for (const label of localLabels) {
164
- const remoteLabel = remoteLabelMap.get(label.name.toLowerCase());
165
- if (!remoteLabel) {
166
- toCreate.push(label);
167
- } else if (this.hasChanges(label, remoteLabel)) {
168
- toUpdate.push({ current: remoteLabel.name, updated: label });
169
- } else {
170
- result.unchanged.push(label);
171
- this.log(`Unchanged label: ${label.name}`);
172
- }
173
- }
174
- if (this.options.deleteExtra) {
175
- for (const remoteLabel of remoteLabels) {
176
- if (!localLabelMap.has(remoteLabel.name.toLowerCase())) {
177
- toDelete.push(remoteLabel.name);
178
- }
179
- }
180
- }
181
- if (!this.options.dryRun) {
182
- for (let i = 0; i < toCreate.length; i += _GitHubLabelSync.BATCH_SIZE) {
183
- const batch = toCreate.slice(i, i + _GitHubLabelSync.BATCH_SIZE);
184
- const promises = batch.map(async (label) => {
185
- try {
186
- await this.client.createLabel(label);
187
- result.created.push(label);
188
- this.log(`Created label: ${label.name}`);
189
- } catch (error) {
190
- result.errors.push({
191
- name: label.name,
192
- error: error instanceof Error ? error.message : String(error)
193
- });
194
- this.log(`Error creating label "${label.name}": ${error}`);
195
- }
196
- });
197
- await Promise.all(promises);
198
- }
199
- for (let i = 0; i < toUpdate.length; i += _GitHubLabelSync.BATCH_SIZE) {
200
- const batch = toUpdate.slice(i, i + _GitHubLabelSync.BATCH_SIZE);
201
- const promises = batch.map(async ({ current, updated }) => {
202
- try {
203
- await this.client.updateLabel(current, updated);
204
- result.updated.push(updated);
205
- this.log(`Updated label: ${updated.name}`);
206
- } catch (error) {
207
- result.errors.push({
208
- name: updated.name,
209
- error: error instanceof Error ? error.message : String(error)
210
- });
211
- this.log(`Error updating label "${updated.name}": ${error}`);
212
- }
213
- });
214
- await Promise.all(promises);
215
- }
216
- for (let i = 0; i < toDelete.length; i += _GitHubLabelSync.BATCH_SIZE) {
217
- const batch = toDelete.slice(i, i + _GitHubLabelSync.BATCH_SIZE);
218
- const promises = batch.map(async (name) => {
219
- try {
220
- await this.client.deleteLabel(name);
221
- result.deleted.push(name);
222
- this.log(`Deleted label: ${name}`);
223
- } catch (error) {
224
- result.errors.push({
225
- name,
226
- error: error instanceof Error ? error.message : String(error)
227
- });
228
- this.log(`Error deleting label "${name}": ${error}`);
229
- }
230
- });
231
- await Promise.all(promises);
232
- }
233
- } else {
234
- result.created.push(...toCreate);
235
- result.updated.push(...toUpdate.map((op) => op.updated));
236
- result.deleted.push(...toDelete);
237
- toCreate.forEach((label) => this.log(`[DRY RUN] Would create label: ${label.name}`));
238
- toUpdate.forEach(({ updated }) => this.log(`[DRY RUN] Would update label: ${updated.name}`));
239
- toDelete.forEach((name) => this.log(`[DRY RUN] Would delete label: ${name}`));
240
- }
241
- return result;
242
- }
243
- /**
244
- * Check if label has changes
245
- */
246
- hasChanges(local, remote) {
247
- return local.color.toLowerCase() !== remote.color.toLowerCase() || local.description !== (remote.description || "");
248
- }
249
- /**
250
- * Fetch labels from GitHub
251
- */
252
- async fetchLabels() {
253
- const labels = await this.client.fetchLabels();
254
- return labels.map((label) => ({
255
- name: label.name,
256
- color: label.color,
257
- description: label.description || ""
258
- }));
259
- }
260
- /**
261
- * Delete a single label
262
- */
263
- async deleteLabel(name) {
264
- if (!this.options.dryRun) {
265
- await this.client.deleteLabel(name);
266
- }
267
- this.log(`Deleted label: ${name}`);
268
- }
269
- /**
270
- * Update a single label
271
- */
272
- async updateLabel(name, updates) {
273
- if (!this.options.dryRun) {
274
- await this.client.updateLabel(name, updates);
275
- }
276
- this.log(`Updated label: ${name}`);
277
- }
278
- };
279
- __publicField(_GitHubLabelSync, "BATCH_SIZE", 5);
280
- var GitHubLabelSync = _GitHubLabelSync;
281
-
282
- export {
283
- GitHubClient,
284
- GitHubLabelSync
285
- };