mirror of
https://gitcode.com/github-mirrors/react-native-update-cli.git
synced 2025-10-30 22:33:11 +08:00
add commit info
This commit is contained in:
50
src/utils/git.ts
Normal file
50
src/utils/git.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user