From 8bd19bc0f7897180e213d72b6d17a097882ed99d Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Sat, 15 Mar 2025 18:50:39 +0800 Subject: [PATCH] add commit info --- src/package.ts | 17 ++++++++++------ src/utils/git.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/index.ts | 3 --- src/versions.ts | 5 ++++- 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/utils/git.ts diff --git a/src/package.ts b/src/package.ts index 7fe7408..66e6af6 100644 --- a/src/package.ts +++ b/src/package.ts @@ -6,6 +6,8 @@ import { checkPlatform, getSelectedApp } from './app'; import { getApkInfo, getIpaInfo, getAppInfo } from './utils'; import Table from 'tty-table'; import { depVersions } from './utils/dep-versions'; +import { getCommitInfo } from './utils/git'; +import type { Platform } from 'types'; export async function listPackage(appId: string) { const { data } = await get(`/app/${appId}/package/list?limit=1000`); @@ -81,13 +83,14 @@ export const commands = { hash, buildTime, deps: depVersions, + commit: await getCommitInfo(), }); saveToLocal(fn, `${appId}/package/${id}.ipa`); console.log( `已成功上传ipa原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`, ); }, - uploadApk: async ({ args }) => { + uploadApk: async ({ args }: { args: string[] }) => { const fn = args[0]; if (!fn || !fn.endsWith('.apk')) { throw new Error('使用方法: pushy uploadApk apk后缀文件'); @@ -119,13 +122,14 @@ export const commands = { hash, buildTime, deps: depVersions, + commit: await getCommitInfo(), }); saveToLocal(fn, `${appId}/package/${id}.apk`); console.log( `已成功上传apk原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`, ); }, - uploadApp: async ({ args }) => { + uploadApp: async ({ args }: { args: string[] }) => { const fn = args[0]; if (!fn || !fn.endsWith('.app')) { throw new Error('使用方法: pushy uploadApp app后缀文件'); @@ -157,34 +161,35 @@ export const commands = { hash, buildTime, deps: depVersions, + commit: await getCommitInfo(), }); saveToLocal(fn, `${appId}/package/${id}.app`); console.log( `已成功上传app原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`, ); }, - parseApp: async ({ args }) => { + parseApp: async ({ args }: { args: string[] }) => { const fn = args[0]; if (!fn || !fn.endsWith('.app')) { throw new Error('使用方法: pushy parseApp app后缀文件'); } console.log(await getAppInfo(fn)); }, - parseIpa: async ({ args }) => { + parseIpa: async ({ args }: { args: string[] }) => { const fn = args[0]; if (!fn || !fn.endsWith('.ipa')) { throw new Error('使用方法: pushy parseIpa ipa后缀文件'); } console.log(await getIpaInfo(fn)); }, - parseApk: async ({ args }) => { + parseApk: async ({ args }: { args: string[] }) => { const fn = args[0]; if (!fn || !fn.endsWith('.apk')) { throw new Error('使用方法: pushy parseApk apk后缀文件'); } console.log(await getApkInfo(fn)); }, - packages: async ({ options }) => { + packages: async ({ options }: { options: { platform: Platform } }) => { const platform = checkPlatform( options.platform || (await question('平台(ios/android/harmony):')), ); diff --git a/src/utils/git.ts b/src/utils/git.ts new file mode 100644 index 0000000..2efc694 --- /dev/null +++ b/src/utils/git.ts @@ -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 { + 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; + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index beca48a..82f7cc4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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', diff --git a/src/versions.ts b/src/versions.ts index f36b672..e7c9f1a 100644 --- a/src/versions.ts +++ b/src/versions.ts @@ -5,6 +5,7 @@ import { checkPlatform, getSelectedApp } from './app'; import { choosePackage } from './package'; import { compare } from 'compare-versions'; import { depVersions } from './utils/dep-versions'; +import { getCommitInfo } from './utils/git'; async function showVersion(appId: string, offset: number) { const { data, count } = await get(`/app/${appId}/version/list`); @@ -98,13 +99,15 @@ export const commands = { const { hash } = await uploadFile(fn); - const versionName = name || (await question('输入版本名称: ')) || '(未命名)'; + const versionName = + name || (await question('输入版本名称: ')) || '(未命名)'; const { id } = await post(`/app/${appId}/version/create`, { name: versionName, hash, description: description || (await question('输入版本描述:')), metaInfo: metaInfo || (await question('输入自定义的 meta info:')), deps: depVersions, + commit: await getCommitInfo(), }); // TODO local diff saveToLocal(fn, `${appId}/ppk/${id}.ppk`);