heymark 1.1.1 → 1.1.3

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,80 +1,97 @@
1
- "use strict";
2
-
3
- const fs = require("fs");
4
- const path = require("path");
5
- const { execSync } = require("child_process");
6
-
7
- const CACHE_DIR_NAME = path.join(".heymark", "cache");
8
-
9
- /**
10
- * 저장소 URL에서 캐시 폴더명으로 쓸 수 있는 문자열 추출
11
- * https://github.com/org/repo -> org-repo
12
- * git@github.com:org/repo.git -> org-repo
13
- */
14
- function sanitizeRepoName(url) {
15
- let s = url.trim();
16
- if (s.endsWith(".git")) s = s.slice(0, -4);
17
- const match =
18
- s.match(/github\.com[:/]([^/]+\/[^/]+?)(?:\/|$)/) || s.match(/([^/]+\/[^/]+?)(?:\/|$)/);
19
- if (match) {
20
- return match[1].replace(/\//g, "-");
21
- }
22
- return s.replace(/[^a-zA-Z0-9._-]/g, "-") || "repo";
23
- }
24
-
25
- /**
26
- * 원격 저장소를 clone 또는 pull하여, 규칙 .md가 있는 로컬 디렉터리 절대 경로를 반환합니다.
27
- * Private repo는 사용자의 git 인증(SSH 키, credential)으로 접근해야 합니다.
28
- * @param {string} projectRoot - 현재 프로젝트 루트
29
- * @param {{ rulesSource: string, branch?: string, rulesSourceDir?: string }} config
30
- * @returns {string} - .md 파일이 있는 디렉터리의 절대 경로
31
- */
32
- function getRulesDirFromRepo(projectRoot, config) {
33
- const url = config.rulesSource;
34
- const branch = config.branch || "main";
35
- const subDir = config.rulesSourceDir || "";
36
-
37
- const cacheBase = path.join(projectRoot, CACHE_DIR_NAME);
38
- const repoName = sanitizeRepoName(url);
39
- const clonePath = path.join(cacheBase, repoName);
40
-
41
- if (!fs.existsSync(clonePath)) {
42
- fs.mkdirSync(cacheBase, { recursive: true });
43
- try {
44
- execSync(`git clone --depth 1 --branch "${branch}" "${url}" "${clonePath}"`, {
45
- stdio: "inherit",
46
- cwd: projectRoot,
47
- });
48
- } catch (err) {
49
- console.error("[Error] Failed to clone rules repository.");
50
- console.error(" For private repos, ensure you have access (SSH key or HTTPS token).");
51
- console.error(" Example: heymark init https://github.com/org/repo.git");
52
- process.exit(1);
53
- }
54
- } else {
55
- try {
56
- execSync(
57
- "git fetch origin && git checkout --quiet . && git pull --quiet origin " + branch,
58
- {
59
- stdio: "pipe",
60
- cwd: clonePath,
61
- }
62
- );
63
- } catch (err) {
64
- // pull 실패 시(네트워크 등) 기존 클론 내용으로 진행
65
- }
66
- }
67
-
68
- const rulesDir = subDir ? path.join(clonePath, subDir) : clonePath;
69
- if (!fs.existsSync(rulesDir) || !fs.statSync(rulesDir).isDirectory()) {
70
- console.error(`[Error] Rules directory not found in repo: ${subDir || "(root)"}`);
71
- process.exit(1);
72
- }
73
- return rulesDir;
74
- }
75
-
76
- module.exports = {
77
- CACHE_DIR_NAME,
78
- getRulesDirFromRepo,
79
- sanitizeRepoName,
80
- };
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { execSync } = require("child_process");
6
+
7
+ const CACHE_DIR_NAME = path.join(".heymark", "cache");
8
+ const DEFAULT_BRANCH = "main";
9
+ const GITHUB_REPO_PATTERN = /github\.com[:/]([^/]+\/[^/]+?)(?:\/|$)/;
10
+ const GENERIC_REPO_PATTERN = /([^/]+\/[^/]+?)(?:\/|$)/;
11
+
12
+ /**
13
+ * Convert repository URL to a stable cache directory name.
14
+ * https://github.com/org/repo -> org-repo
15
+ * git@github.com:org/repo.git -> org-repo
16
+ * @param {string} repoUrl
17
+ * @returns {string}
18
+ */
19
+ function sanitizeRepoName(repoUrl) {
20
+ let normalized = repoUrl.trim();
21
+ if (normalized.endsWith(".git")) {
22
+ normalized = normalized.slice(0, -4);
23
+ }
24
+
25
+ const match = normalized.match(GITHUB_REPO_PATTERN) || normalized.match(GENERIC_REPO_PATTERN);
26
+ if (match) {
27
+ return match[1].replace(/\//g, "-");
28
+ }
29
+
30
+ return normalized.replace(/[^a-zA-Z0-9._-]/g, "-") || "repo";
31
+ }
32
+
33
+ function cloneRulesRepo(projectRoot, clonePath, branch, repoUrl) {
34
+ execSync(`git clone --depth 1 --branch "${branch}" "${repoUrl}" "${clonePath}"`, {
35
+ stdio: "inherit",
36
+ cwd: projectRoot,
37
+ });
38
+ }
39
+
40
+ function updateRulesRepo(clonePath, branch) {
41
+ execSync(`git fetch origin && git checkout --quiet . && git pull --quiet origin "${branch}"`, {
42
+ stdio: "pipe",
43
+ cwd: clonePath,
44
+ });
45
+ }
46
+
47
+ function resolveRulesDirectory(clonePath, rulesSourceDir) {
48
+ const rulesDir = rulesSourceDir ? path.join(clonePath, rulesSourceDir) : clonePath;
49
+ if (!fs.existsSync(rulesDir) || !fs.statSync(rulesDir).isDirectory()) {
50
+ console.error(`[Error] Rules directory not found in repo: ${rulesSourceDir || "(root)"}`);
51
+ process.exit(1);
52
+ }
53
+ return rulesDir;
54
+ }
55
+
56
+ /**
57
+ * Clone or update remote repository and return local rules directory.
58
+ * Private repositories require user git credentials (SSH key or token).
59
+ * @param {string} projectRoot
60
+ * @param {{ rulesSource: string, branch?: string, rulesSourceDir?: string }} config
61
+ * @returns {string}
62
+ */
63
+ function getRulesDirFromRepo(projectRoot, config) {
64
+ const repoUrl = config.rulesSource;
65
+ const branch = config.branch || DEFAULT_BRANCH;
66
+ const rulesSourceDir = config.rulesSourceDir || "";
67
+
68
+ const cacheBase = path.join(projectRoot, CACHE_DIR_NAME);
69
+ const repoName = sanitizeRepoName(repoUrl);
70
+ const clonePath = path.join(cacheBase, repoName);
71
+
72
+ if (!fs.existsSync(clonePath)) {
73
+ fs.mkdirSync(cacheBase, { recursive: true });
74
+ try {
75
+ cloneRulesRepo(projectRoot, clonePath, branch, repoUrl);
76
+ } catch {
77
+ console.error("[Error] Failed to clone rules repository.");
78
+ console.error(" For private repos, ensure you have access (SSH key or HTTPS token).");
79
+ console.error(" Example: heymark init https://github.com/org/repo.git");
80
+ process.exit(1);
81
+ }
82
+ } else {
83
+ try {
84
+ updateRulesRepo(clonePath, branch);
85
+ } catch {
86
+ // Continue with cached clone when fetch or pull fails.
87
+ }
88
+ }
89
+
90
+ return resolveRulesDirectory(clonePath, rulesSourceDir);
91
+ }
92
+
93
+ module.exports = {
94
+ CACHE_DIR_NAME,
95
+ getRulesDirFromRepo,
96
+ sanitizeRepoName,
97
+ };