-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (47 loc) · 2.09 KB
/
index.ts
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
import { exec } from "child_process";
import { readFile, writeFile } from "fs/promises";
import { globby } from "globby";
import path, { relative, resolve } from "path";
import { promisify } from "util";
export default async function monopkgs({ } = {}) {
const rootPkg = resolve("./package.json");
const root = JSON.parse(await readFile(rootPkg, "utf8"));
const rootDir = path.parse(rootPkg).dir;
if (root.repository) {
const [, ghUrl] =
root.repository?.url?.match?.(/(https\:\/\/github\.com\/.*?\/.*?)\.git/) || [];
root.repository = ghUrl || root.repository;
if (typeof root.repository !== "string") throw new Error("root.repository need to be string");
if (root.repository.endsWith("/")) throw new Error("root.repository need to be without /");
}
const root_repository = root.repository?.replace(/\.git$/, "");
const pkgs = await globby("packages/**/package.json", { gitignore: true });
// master will redirect to main rather than 404 if main is not existed in github.
const currentBranch = (await promisify(exec)("git branch --show-current"))?.stdout.trim();
const mainBranchName = currentBranch || "master";
const pkgParse = async (pkgPath) => {
const pkgRelDir = relative(rootDir, path.parse(pkgPath).dir).replace(/\\/g, "/");
const pkg = JSON.parse(await readFile(pkgPath, "utf8"));
const pkgName = pkg.name;
const repository = root_repository && `${root_repository}/tree/${mainBranchName}/${pkgRelDir}`;
const homepage = repository && `${repository}#readme`;
const bugs = root_repository && {
url: `${root_repository}/issues?q=is%3Aissue+is%3Aopen+${pkgName}`,
};
const { license, author } = root;
const info = {
...(repository && { repository }),
...(homepage && { homepage }),
...(bugs && { bugs }),
...(license && { license }),
...(author && { author }),
};
Object.assign(pkg, info);
const outJson = JSON.stringify(pkg, null, 2);
const out = `${outJson}\n`;
// console.log(out);
console.log(pkgPath);
await writeFile(pkgPath, out);
};
await Promise.all(pkgs.map(pkgParse));
}