-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpublish.ts
More file actions
756 lines (644 loc) · 21.2 KB
/
Copy pathpublish.ts
File metadata and controls
756 lines (644 loc) · 21.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* eslint-disable */
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import { exec } from "child_process";
import util from "util";
import crypto from "crypto";
// https://linear.app/discourse-graphs/issue/ENG-766/upgrade-all-commonjs-to-esm
// TODO if possible: change apps/obsidian to ESM. Use require until then.
// import { Octokit } from "@octokit/core";
const { Octokit } = require("@octokit/core");
import os from "os";
dotenv.config();
const execPromise = util.promisify(exec);
type PublishConfig = {
version: string;
targetRepo: string;
releaseName?: string;
};
type ExecOptions = {
env?: Record<string, string>;
cwd?: string;
};
const EXCLUDE_PATTERNS = [
"node_modules",
"dist",
".env*",
".turbo",
".DS_Store",
"*.log",
"coverage",
".next",
"out",
"build",
"scripts",
".git",
".vscode",
".cursor",
"*.pem",
"temp-obsidian-publish",
];
const REQUIRED_BUILD_FILES = [
"main.js",
"manifest.json",
"styles.css",
] as const;
const BLOB_UPLOAD_BATCH_SIZE = 10;
const MAX_GITHUB_RETRIES = 5;
const BASE_RETRY_DELAY_MS = 2_000;
const TARGET_REPO = "DiscourseGraphs/discourse-graph-obsidian";
const OWNER = "DiscourseGraphs";
const REPO = "discourse-graph-obsidian";
const log = (message: string): void => {
console.log(`[Obsidian Publisher] ${message}`);
};
const sleep = async (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
const isSecondaryRateLimitError = (error: unknown): boolean => {
const maybeError = error as {
status?: number;
response?: { data?: { message?: string } };
message?: string;
};
const message =
maybeError?.response?.data?.message?.toLowerCase() ??
maybeError?.message?.toLowerCase() ??
"";
return maybeError?.status === 403 && message.includes("secondary rate limit");
};
const getRetryDelayMs = (error: unknown, attempt: number): number => {
const maybeError = error as {
response?: { headers?: Record<string, string | undefined> };
};
const retryAfterHeader = maybeError?.response?.headers?.["retry-after"];
const retryAfterSeconds = Number(retryAfterHeader);
if (Number.isFinite(retryAfterSeconds) && retryAfterSeconds > 0) {
return retryAfterSeconds * 1000;
}
return BASE_RETRY_DELAY_MS * 2 ** attempt;
};
const requestWithRetry = async <T = unknown>(
request: () => Promise<T>,
context: string,
): Promise<T> => {
let attempt = 0;
while (true) {
try {
return await request();
} catch (error) {
if (!isSecondaryRateLimitError(error) || attempt >= MAX_GITHUB_RETRIES) {
throw error;
}
const delayMs = getRetryDelayMs(error, attempt);
log(
`Secondary rate limit hit during ${context}. Retrying in ${Math.ceil(delayMs / 1000)}s (attempt ${attempt + 1}/${MAX_GITHUB_RETRIES})...`,
);
await sleep(delayMs);
attempt += 1;
}
}
};
const getAllFiles = (dir: string, baseDir: string = dir): string[] => {
const files: string[] = [];
fs.readdirSync(dir, { withFileTypes: true }).forEach((entry) => {
const fullPath = path.join(dir, entry.name);
const relativePath = path.relative(baseDir, fullPath);
if (shouldExclude(fullPath, baseDir)) {
log(`Excluding: ${relativePath}`);
return;
}
if (entry.isDirectory()) {
files.push(...getAllFiles(fullPath, baseDir));
} else {
files.push(relativePath);
}
});
return files;
};
const getGitBlobSha = (content: Buffer): string => {
const header = Buffer.from(`blob ${content.length}\0`, "utf8");
return crypto
.createHash("sha1")
.update(Buffer.concat([header, content]))
.digest("hex");
};
const chunk = <T>(items: T[], size: number): T[][] => {
const chunks: T[][] = [];
for (let i = 0; i < items.length; i += size) {
chunks.push(items.slice(i, i + size));
}
return chunks;
};
const getEnvVar = (name: string): string => {
const value = process.env[name];
if (!value) {
throw new Error(`${name} environment variable is required`);
}
return value;
};
const parseArgs = (): PublishConfig => {
const args = process.argv.slice(2);
const config: Partial<PublishConfig> = {
targetRepo: TARGET_REPO,
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
const nextArg = args[i + 1];
switch (arg) {
case "--version":
case "-v":
if (!nextArg || nextArg.startsWith("-")) {
throw new Error("Version argument is required after --version");
}
config.version = nextArg;
i++;
break;
case "--release-name":
if (!nextArg || nextArg.startsWith("-")) {
throw new Error(
"Release name argument is required after --release-name",
);
}
config.releaseName = nextArg;
i++;
break;
case "--help":
case "-h":
showHelp();
process.exit(0);
}
}
if (!config.version) {
throw new Error("Version is required. Use --version <version> or --help");
}
validateVersion(config.version);
return config as PublishConfig;
};
const validateVersion = (version: string): void => {
const basicVersionPattern = /^\d+\.\d+\.\d+/;
if (!basicVersionPattern.test(version)) {
throw new Error(
`Invalid version format: ${version}. Expected format: x.y.z, x.y.z-suffix, or x.y.z-custom-name`,
);
}
};
const isExternalRelease = (version: string): boolean => {
// External releases are:
// 1. Stable releases (x.y.z)
// 2. Beta releases (x.y.z-beta.n)
// Stable release pattern (x.y.z)
const stablePattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
if (stablePattern.test(version)) {
return true;
}
// Beta release pattern (x.y.z-beta.n)
const betaPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)-beta(\.\d+)?$/;
if (betaPattern.test(version)) {
return true;
}
// Everything else (including alpha releases) is internal
return false;
};
const showHelp = (): void => {
console.log(`
Usage: tsx scripts/publish-obsidian.ts --version <version> [options]
Required:
--version, -v <version> Version to publish (see formats below)
Options:
--release-name <name> Custom release name (defaults to "Discourse Graph v{version}")
--help, -h Show this help message
Version Formats:
x.y.z Stable release - auto-picked up by BRAT (e.g., 1.0.0)
x.y.z-beta.n Beta release - auto-picked up by BRAT (e.g., 1.0.0-beta.1)
x.y.z-alpha-name Internal release - manual install only (e.g., 0.1.0-alpha-canvas-feature)
Release Type Auto-Detection:
- External releases (stable, beta): Auto-updated by BRAT users
- Internal releases (alpha prefix): Marked as pre-release, manual install only
BRAT Version Priority:
BRAT uses alphabetical ordering, so alpha < beta < stable
- 0.1.0-alpha-feature (lowest priority)
- 0.1.0-beta.1 (higher priority)
- 0.1.0 (highest priority)
Examples:
# Internal release with custom name
tsx scripts/publish-obsidian.ts --version 0.1.0-alpha-canvas --release-name "Canvas Integration Feature"
# Beta release with feature description
tsx scripts/publish-obsidian.ts --version 1.0.0-beta.1 --release-name "Beta: New Graph View"
# Stable release (uses default name)
tsx scripts/publish-obsidian.ts --version 1.0.0
`);
};
const execCommand = async (
command: string,
options: ExecOptions = {},
): Promise<string> => {
try {
const { stdout, stderr } = await execPromise(command, {
...options,
env: {
...process.env,
...options.env,
GIT_ASKPASS: "echo",
GIT_TERMINAL_PROMPT: "0",
},
});
log(`Command: ${command}`);
log(`stdout: ${stdout.trim()}`);
if (stderr) log(`stderr: ${stderr.trim()}`);
return stdout.trim();
} catch (error) {
const token = getEnvVar("OBSIDIAN_PLUGIN_REPO_TOKEN");
if (token) {
throw new Error((error as Error).message.replace(token, "***"));
}
throw error;
}
};
const shouldExclude = (filePath: string, baseDir: string): boolean => {
const relativePath = path.relative(baseDir, filePath);
return EXCLUDE_PATTERNS.some((pattern) => {
if (pattern.includes("*")) {
const regex = new RegExp(pattern.replace(/\*/g, ".*"));
return regex.test(relativePath) || regex.test(path.basename(filePath));
}
return (
relativePath.includes(pattern) || path.basename(filePath) === pattern
);
});
};
const copyDirectory = ({
src,
dest,
baseDir,
}: {
src: string;
dest: string;
baseDir: string;
}): void => {
if (!fs.existsSync(src)) {
throw new Error(`Source directory does not exist: ${src}`);
}
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src, { withFileTypes: true }).forEach((entry) => {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (shouldExclude(srcPath, baseDir)) {
log(`Excluding: ${path.relative(baseDir, srcPath)}`);
return;
}
if (entry.isDirectory()) {
copyDirectory({ src: srcPath, dest: destPath, baseDir });
} else {
try {
fs.copyFileSync(srcPath, destPath);
} catch (error) {
throw new Error(`Failed to copy ${srcPath}: ${error}`);
}
}
});
};
const buildPlugin = async (dir: string): Promise<void> => {
log("Building plugin...");
await execCommand("pnpm run build", { cwd: dir });
const buildDir = path.join(dir, "dist");
const missingFiles = REQUIRED_BUILD_FILES.filter(
(file) => !fs.existsSync(path.join(buildDir, file)),
);
if (missingFiles.length > 0) {
throw new Error(`Required build files missing: ${missingFiles.join(", ")}`);
}
};
const updateManifest = (tempDir: string, version: string): void => {
const manifestPath = path.join(tempDir, "manifest.json");
if (!fs.existsSync(manifestPath)) {
throw new Error("manifest.json not found in temp directory");
}
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.version = version;
manifest.id = "discourse-graphs";
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
log(`Updated manifest version to ${version}`);
};
const copyBuildFiles = (buildDir: string, tempDir: string): void => {
REQUIRED_BUILD_FILES.forEach((file) => {
const srcPath = path.join(buildDir, file);
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, path.join(tempDir, file));
log(`Copied ${file}`);
}
});
};
const sanitizePackageJsonForMirror = (tempDir: string): void => {
const packageJsonPath = path.join(tempDir, "package.json");
if (!fs.existsSync(packageJsonPath)) return;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
if (packageJson?.scripts) {
delete packageJson.scripts;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
log("Removed package.json scripts for mirrored publish repo");
}
};
const updateLocalVersion = (obsidianDir: string, version: string): void => {
const packageJsonPath = path.join(obsidianDir, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
packageJson.version = version;
fs.writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, null, 2) + "\n",
);
const manifestPath = path.join(obsidianDir, "manifest.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.version = version;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + "\n");
log(`Updated local package.json and manifest.json to version ${version}`);
};
const updateMainBranch = async (
tempDir: string,
version: string,
): Promise<void> => {
log(`Updating main branch of repository: ${TARGET_REPO}...`);
const token = getEnvVar("OBSIDIAN_PLUGIN_REPO_TOKEN");
const octokit = new Octokit({ auth: token });
const owner = OWNER;
const repo = REPO;
try {
const { data: ref } = await requestWithRetry<any>(
() =>
octokit.request("GET /repos/{owner}/{repo}/git/refs/{ref}", {
owner,
repo,
ref: "heads/main",
}),
"fetching main branch ref",
);
if (!ref?.object?.sha) {
throw new Error("Failed to get main branch reference");
}
const currentSha = ref.object.sha;
const { data: currentCommit } = await requestWithRetry<any>(
() =>
octokit.request("GET /repos/{owner}/{repo}/git/commits/{commit_sha}", {
owner,
repo,
commit_sha: currentSha,
}),
"fetching current main commit",
);
if (!currentCommit?.tree?.sha) {
throw new Error("Failed to get current commit tree");
}
const currentTreeSha = currentCommit.tree.sha;
const { data: existingTree } = await requestWithRetry<any>(
() =>
octokit.request("GET /repos/{owner}/{repo}/git/trees/{tree_sha}", {
owner,
repo,
tree_sha: currentTreeSha,
recursive: "1",
}),
"fetching recursive main tree",
);
const existingBlobShasByPath = new Map<string, string>(
(existingTree.tree ?? [])
.filter(
(entry: any): entry is { path: string; sha: string; type: string } =>
Boolean(entry.path && entry.sha && entry.type === "blob"),
)
.map((entry: { path: string; sha: string }) => [entry.path, entry.sha]),
);
const allFiles = getAllFiles(tempDir);
log(`Found ${allFiles.length} files to update`);
const normalizedAllFiles = allFiles.map((filePath) =>
filePath.replace(/\\/g, "/"),
);
const currentRepoFiles = new Set<string>(existingBlobShasByPath.keys());
const localFiles = new Set<string>(normalizedAllFiles);
const filesToDelete = [...currentRepoFiles].filter(
(repoFilePath) => !localFiles.has(repoFilePath),
);
const filesToUpdate = allFiles.filter((filePath) => {
const fullPath = path.join(tempDir, filePath);
const content = fs.readFileSync(fullPath);
const normalizedPath = filePath.replace(/\\/g, "/");
const existingSha = existingBlobShasByPath.get(normalizedPath);
return getGitBlobSha(content) !== existingSha;
});
log(
`Detected ${filesToUpdate.length} changed files (${allFiles.length - filesToUpdate.length} unchanged skipped)`,
);
log(`Detected ${filesToDelete.length} files to delete from target repo`);
if (filesToUpdate.length === 0 && filesToDelete.length === 0) {
log("No changes detected on main branch; skipping commit update");
return;
}
const blobBatchChunks = chunk(filesToUpdate, BLOB_UPLOAD_BATCH_SIZE);
const blobs: Array<{ path: string; sha: string }> = [];
for (const [batchIndex, blobBatch] of blobBatchChunks.entries()) {
log(
`Uploading blob batch ${batchIndex + 1}/${blobBatchChunks.length} (${blobBatch.length} files)...`,
);
const batchBlobs = await Promise.all(
blobBatch.map(async (filePath) => {
const fullPath = path.join(tempDir, filePath);
const content = fs.readFileSync(fullPath);
const { data: blob } = await requestWithRetry<any>(
() =>
octokit.request("POST /repos/{owner}/{repo}/git/blobs", {
owner,
repo,
content: content.toString("base64"),
encoding: "base64",
}),
`creating blob for ${filePath}`,
);
if (!blob?.sha) {
throw new Error(`Failed to create blob for ${filePath}`);
}
return {
path: filePath.replace(/\\/g, "/"), // Normalize path separators for GitHub
sha: blob.sha,
};
}),
);
blobs.push(...batchBlobs);
}
const treeUpdates = blobs.map((blob) => ({
path: blob.path,
mode: "100644" as const,
type: "blob" as const,
sha: blob.sha,
}));
const treeDeletions = filesToDelete.map((filePath) => ({
path: filePath,
mode: "100644" as const,
type: "blob" as const,
sha: null,
}));
const { data: newTree } = await requestWithRetry<any>(
() =>
octokit.request("POST /repos/{owner}/{repo}/git/trees", {
owner,
repo,
base_tree: currentTreeSha,
tree: [...treeUpdates, ...treeDeletions],
}),
"creating updated git tree",
);
if (!newTree?.sha) {
throw new Error("Failed to create new tree");
}
const { data: newCommit } = await requestWithRetry<any>(
() =>
octokit.request("POST /repos/{owner}/{repo}/git/commits", {
owner,
repo,
message: `Release v${version}`,
tree: newTree.sha,
parents: [currentSha],
}),
"creating release commit",
);
if (!newCommit?.sha) {
throw new Error("Failed to create new commit");
}
await requestWithRetry(
() =>
octokit.request("PATCH /repos/{owner}/{repo}/git/refs/{ref}", {
owner,
repo,
ref: "heads/main",
sha: newCommit.sha,
}),
"updating main branch reference",
);
log(`Successfully updated main branch with commit: ${newCommit.sha}`);
log(
`Updated ${blobs.length} files and deleted ${filesToDelete.length} files`,
);
} catch (error) {
log(`Failed to update main branch: ${error}`);
throw error;
}
};
const createGithubRelease = async ({
version,
releaseName,
}: {
version: string;
releaseName?: string;
}): Promise<void> => {
log("Creating GitHub release...");
const token = getEnvVar("OBSIDIAN_PLUGIN_REPO_TOKEN");
const octokit = new Octokit({ auth: token });
const owner = OWNER;
const repo = REPO;
const tagName = `${version}`;
const releaseTitle = releaseName || `Discourse Graph v${version}`;
const isPrerelease = !isExternalRelease(version);
const releaseTempDir = path.join(os.tmpdir(), "temp-obsidian-release-assets");
try {
if (fs.existsSync(releaseTempDir)) {
fs.rmSync(releaseTempDir, { recursive: true });
}
fs.mkdirSync(releaseTempDir, { recursive: true });
const buildDir = path.join(path.resolve("."), "dist");
copyBuildFiles(buildDir, releaseTempDir);
const obsidianDir = path.resolve(".");
const manifestSrc = path.join(obsidianDir, "manifest.json");
const manifestDest = path.join(releaseTempDir, "manifest.json");
fs.copyFileSync(manifestSrc, manifestDest);
updateManifest(releaseTempDir, version);
const release = await requestWithRetry<any>(
() =>
octokit.request("POST /repos/{owner}/{repo}/releases", {
owner,
repo,
tag_name: tagName,
name: releaseTitle,
prerelease: isPrerelease,
generate_release_notes: true,
}),
"creating GitHub release",
);
if (!release.data.upload_url) {
throw new Error("Failed to get upload URL from release response");
}
for (const file of REQUIRED_BUILD_FILES) {
const filePath = path.join(releaseTempDir, file);
if (!fs.existsSync(filePath)) continue;
const contentType =
{
".js": "application/javascript",
".json": "application/json",
".css": "text/css",
}[path.extname(file)] || "application/octet-stream";
const fileContent = fs.readFileSync(filePath);
const stats = fs.statSync(filePath);
const uploadUrl = release.data.upload_url.replace(
"{?name,label}",
`?name=${file}`,
);
await requestWithRetry(
() =>
octokit.request(`POST ${uploadUrl}`, {
headers: {
"content-type": contentType,
"content-length": String(stats.size),
},
data: fileContent,
name: file,
}),
`uploading release asset ${file}`,
);
log(`Uploaded ${file}`);
}
} finally {
if (fs.existsSync(releaseTempDir)) {
fs.rmSync(releaseTempDir, { recursive: true });
}
}
};
const publish = async (config: PublishConfig): Promise<void> => {
const { version, releaseName } = config;
const obsidianDir = path.resolve(".");
const buildDir = path.join(obsidianDir, "dist");
const tempDir = path.join(os.tmpdir(), "temp-obsidian-publish");
try {
const isExternal = isExternalRelease(version);
const releaseType = isExternal ? "external" : "internal";
log(`Publishing Obsidian plugin v${version} (${releaseType} release)`);
await buildPlugin(obsidianDir);
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true });
}
copyDirectory({ src: obsidianDir, dest: tempDir, baseDir: obsidianDir });
copyBuildFiles(buildDir, tempDir);
sanitizePackageJsonForMirror(tempDir);
if (isExternal) {
updateManifest(tempDir, version);
await updateMainBranch(tempDir, version);
updateLocalVersion(obsidianDir, version);
} else {
log("Skipping main branch update for internal or pre-release");
}
await createGithubRelease({
version,
releaseName,
});
log("Publication completed successfully!");
} catch (error) {
log(`Publication failed: ${error}`);
throw error;
} finally {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true });
}
}
};
if (require.main === module) {
publish(parseArgs()).catch((error) => {
console.error(error);
process.exit(1);
});
}