@mdocs/server 0.1.0 → 0.1.2
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/dist/index.d.ts +1 -0
- package/dist/index.js +24 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -33,9 +33,11 @@ function healthRouter() {
|
|
|
33
33
|
import { Router as Router3 } from "express";
|
|
34
34
|
import { randomUUID } from "crypto";
|
|
35
35
|
import { join as join4 } from "path";
|
|
36
|
+
import { mkdirSync } from "fs";
|
|
36
37
|
|
|
37
38
|
// src/config.ts
|
|
38
39
|
import { resolve } from "path";
|
|
40
|
+
import { homedir } from "os";
|
|
39
41
|
var DEFAULT_PORT = 4873;
|
|
40
42
|
var DEFAULT_HOST = "127.0.0.1";
|
|
41
43
|
var MDOCS_DIR = ".mdocs";
|
|
@@ -43,14 +45,15 @@ var REPOS_SUBDIR = "repos";
|
|
|
43
45
|
var DEFAULT_ORIGINS = [
|
|
44
46
|
"http://localhost:3000",
|
|
45
47
|
"http://127.0.0.1:3000",
|
|
46
|
-
"https://
|
|
48
|
+
"https://idocs-md-viewer.vercel.app/"
|
|
47
49
|
];
|
|
48
50
|
function parseConfig(overrides = {}) {
|
|
49
51
|
return {
|
|
50
52
|
port: overrides.port ?? parseInt(process.env["PORT"] ?? String(DEFAULT_PORT), 10),
|
|
51
53
|
host: overrides.host ?? process.env["HOST"] ?? DEFAULT_HOST,
|
|
52
|
-
dataDir: overrides.dataDir ?? process.env["DATA_DIR"] ??
|
|
53
|
-
origins: overrides.origins ?? DEFAULT_ORIGINS
|
|
54
|
+
dataDir: overrides.dataDir ?? process.env["DATA_DIR"] ?? homedir(),
|
|
55
|
+
origins: overrides.origins ?? DEFAULT_ORIGINS,
|
|
56
|
+
githubToken: overrides.githubToken ?? process.env["GITHUB_TOKEN"]
|
|
54
57
|
};
|
|
55
58
|
}
|
|
56
59
|
function reposDir(dataDir) {
|
|
@@ -87,14 +90,27 @@ function run(args, cwd) {
|
|
|
87
90
|
if (code === 0) resolve4(stdout.trim());
|
|
88
91
|
else reject(new Error(stderr.trim() || `git exited with code ${code}`));
|
|
89
92
|
});
|
|
90
|
-
proc.on("error",
|
|
93
|
+
proc.on("error", (err) => {
|
|
94
|
+
if (err.code === "ENOENT") {
|
|
95
|
+
reject(new Error("git not found \u2014 make sure Git is installed and available in PATH"));
|
|
96
|
+
} else {
|
|
97
|
+
reject(err);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
91
100
|
});
|
|
92
101
|
}
|
|
93
|
-
async function cloneRepo(url, dest, branch) {
|
|
94
|
-
const
|
|
102
|
+
async function cloneRepo(url, dest, branch, token) {
|
|
103
|
+
const cloneUrl = token ? injectToken(url, token) : url;
|
|
104
|
+
const args = ["clone", cloneUrl, dest, "--single-branch"];
|
|
95
105
|
if (branch) args.push("--branch", branch);
|
|
96
106
|
await run(args);
|
|
97
107
|
}
|
|
108
|
+
function injectToken(url, token) {
|
|
109
|
+
const parsed = new URL(url);
|
|
110
|
+
parsed.username = "oauth2";
|
|
111
|
+
parsed.password = token;
|
|
112
|
+
return parsed.toString();
|
|
113
|
+
}
|
|
98
114
|
async function pullRepo(repoPath) {
|
|
99
115
|
await run(["pull", "--ff-only"], repoPath);
|
|
100
116
|
}
|
|
@@ -277,7 +293,8 @@ function reposRouter(config) {
|
|
|
277
293
|
const id = randomUUID();
|
|
278
294
|
const cloneDir = join4(reposDir2, id);
|
|
279
295
|
try {
|
|
280
|
-
|
|
296
|
+
mkdirSync(reposDir2, { recursive: true });
|
|
297
|
+
await cloneRepo(url, cloneDir, branch, config.githubToken);
|
|
281
298
|
const detectedBranch = branch ?? await getDefaultBranch(cloneDir);
|
|
282
299
|
const currentCommit = await getHeadCommit(cloneDir);
|
|
283
300
|
const files = scanMarkdownFiles(cloneDir, id);
|