1
0
mirror of https://gitcode.com/github-mirrors/react-native-update-cli.git synced 2025-10-30 22:33:11 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

add commit info

This commit is contained in:
sunnylqm
2025-03-15 18:50:39 +08:00
parent d974be6706
commit 8bd19bc0f7
4 changed files with 65 additions and 10 deletions

50
src/utils/git.ts Normal file
View File

@@ -0,0 +1,50 @@
import git from 'isomorphic-git';
import fs from 'node:fs';
import path from 'node:path';
export interface CommitInfo {
hash: string;
message: string;
author: string;
date: number;
origin: string;
}
function findGitRoot(dir = process.cwd()) {
const gitRoot = fs.readdirSync(dir).find((dir) => dir === '.git');
if (gitRoot) {
// console.log({ gitRoot });
return path.join(dir, gitRoot);
}
const parentDir = path.dirname(dir);
if (parentDir === dir) {
return null;
}
return findGitRoot(parentDir);
}
const gitRoot = findGitRoot();
export async function getCommitInfo(): Promise<CommitInfo | undefined> {
if (!gitRoot) {
return;
}
try {
const remotes = await git.listRemotes({ fs, gitdir: gitRoot });
const origin =
remotes.find((remote) => remote.remote === 'origin') || remotes[0];
const { commit, oid } = (
await git.log({ fs, gitdir: gitRoot, depth: 1 })
)[0];
return {
hash: oid,
message: commit.message,
author: commit.author.name || commit.committer.name,
date: commit.committer.timestamp,
origin: origin.url,
};
} catch (error) {
console.error(error);
return;
}
}

View File

@@ -11,7 +11,6 @@ import { checkPlugins } from './check-plugin';
import { read } from 'read';
import { tempDir } from './constants';
import { depVersions } from './dep-versions';
import { getCommitInfo } from './git';
export async function question(query: string, password?: boolean) {
if (NO_INTERACTIVE) {
@@ -169,8 +168,6 @@ async function getLatestVersion(pkgNames: string[]) {
}
export async function printVersionCommand() {
const result = await getCommitInfo();
console.log(JSON.stringify(result, null, 2));
let [latestPushyCliVersion, latestPushyVersion] = await getLatestVersion([
'react-native-update-cli',
'react-native-update',