Skip to content

Commit 6c2dc7b

Browse files
✨ Add GitHubh elper
1 parent 6aa055b commit 6c2dc7b

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@staart/site",
3-
"version": "0.9.12",
3+
"version": "0.9.13",
44
"main": "dist/index.js",
55
"repository": "[email protected]:staart/site",
66
"author": "Anand Chowdhary <[email protected]>",

src/config.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
import cosmiconfig from "cosmiconfig";
22
import { StaartSiteConfig } from "./interfaces";
3+
import { join } from "path";
4+
import { readJSON } from "fs-extra";
5+
import gitUrlParse from "git-url-parse";
6+
import { cached } from "./cache";
37
const explorer = cosmiconfig("staart");
48

5-
export const getConfig = async () => {
9+
export const getConfig = async (): Promise<StaartSiteConfig> => {
10+
return cached("config", _getConfig);
11+
};
12+
13+
const _getConfig = async () => {
614
const searchResult = await explorer.search();
715
const config: StaartSiteConfig = searchResult ? searchResult.config : {};
16+
let gitRepoUrl = config.repo;
17+
if (searchResult && searchResult.filepath) {
18+
const packagePath = join(searchResult.filepath, "..", "package.json");
19+
try {
20+
const pkg = await readJSON(packagePath);
21+
gitRepoUrl = gitRepoUrl || pkg.repository;
22+
} catch (error) {}
23+
}
24+
if (gitRepoUrl) {
25+
config._gitRepo = gitUrlParse(gitRepoUrl);
26+
}
827
return config;
928
};

src/github.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getConfig } from "./config";
2+
3+
export const getGitHubRepoUrl = async () => {
4+
const config = await getConfig();
5+
if (config._gitRepo && config._gitRepo.source === "github.com")
6+
return `https://${config._gitRepo.source}${config._gitRepo.pathname}`;
7+
};

src/index.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { success, error } from "signale";
2-
import { getConfig } from "./config";
3-
import { cached } from "./cache";
2+
import { getGitHubRepoUrl } from "./github";
43

54
const staartSite = async () => {
6-
const config = await getConfig();
7-
const anand = await cached("anand", async () => {
8-
return "anand";
9-
});
10-
console.log("Anand is", anand);
5+
console.log("GitHub URL", await getGitHubRepoUrl());
116
};
127

138
const startTime = new Date().getTime();

src/interfaces.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { GitUrl } from "git-url-parse";
2+
13
export interface StaartSiteConfig {
24
title?: string;
5+
repo?: string;
6+
_gitRepo?: GitUrl;
37
}

0 commit comments

Comments
 (0)