Skip to content

Commit 8bd19bc

Browse files
committed
add commit info
1 parent d974be6 commit 8bd19bc

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

src/package.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { checkPlatform, getSelectedApp } from './app';
66
import { getApkInfo, getIpaInfo, getAppInfo } from './utils';
77
import Table from 'tty-table';
88
import { depVersions } from './utils/dep-versions';
9+
import { getCommitInfo } from './utils/git';
10+
import type { Platform } from 'types';
911

1012
export async function listPackage(appId: string) {
1113
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
@@ -81,13 +83,14 @@ export const commands = {
8183
hash,
8284
buildTime,
8385
deps: depVersions,
86+
commit: await getCommitInfo(),
8487
});
8588
saveToLocal(fn, `${appId}/package/${id}.ipa`);
8689
console.log(
8790
`已成功上传ipa原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`,
8891
);
8992
},
90-
uploadApk: async ({ args }) => {
93+
uploadApk: async ({ args }: { args: string[] }) => {
9194
const fn = args[0];
9295
if (!fn || !fn.endsWith('.apk')) {
9396
throw new Error('使用方法: pushy uploadApk apk后缀文件');
@@ -119,13 +122,14 @@ export const commands = {
119122
hash,
120123
buildTime,
121124
deps: depVersions,
125+
commit: await getCommitInfo(),
122126
});
123127
saveToLocal(fn, `${appId}/package/${id}.apk`);
124128
console.log(
125129
`已成功上传apk原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`,
126130
);
127131
},
128-
uploadApp: async ({ args }) => {
132+
uploadApp: async ({ args }: { args: string[] }) => {
129133
const fn = args[0];
130134
if (!fn || !fn.endsWith('.app')) {
131135
throw new Error('使用方法: pushy uploadApp app后缀文件');
@@ -157,34 +161,35 @@ export const commands = {
157161
hash,
158162
buildTime,
159163
deps: depVersions,
164+
commit: await getCommitInfo(),
160165
});
161166
saveToLocal(fn, `${appId}/package/${id}.app`);
162167
console.log(
163168
`已成功上传app原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`,
164169
);
165170
},
166-
parseApp: async ({ args }) => {
171+
parseApp: async ({ args }: { args: string[] }) => {
167172
const fn = args[0];
168173
if (!fn || !fn.endsWith('.app')) {
169174
throw new Error('使用方法: pushy parseApp app后缀文件');
170175
}
171176
console.log(await getAppInfo(fn));
172177
},
173-
parseIpa: async ({ args }) => {
178+
parseIpa: async ({ args }: { args: string[] }) => {
174179
const fn = args[0];
175180
if (!fn || !fn.endsWith('.ipa')) {
176181
throw new Error('使用方法: pushy parseIpa ipa后缀文件');
177182
}
178183
console.log(await getIpaInfo(fn));
179184
},
180-
parseApk: async ({ args }) => {
185+
parseApk: async ({ args }: { args: string[] }) => {
181186
const fn = args[0];
182187
if (!fn || !fn.endsWith('.apk')) {
183188
throw new Error('使用方法: pushy parseApk apk后缀文件');
184189
}
185190
console.log(await getApkInfo(fn));
186191
},
187-
packages: async ({ options }) => {
192+
packages: async ({ options }: { options: { platform: Platform } }) => {
188193
const platform = checkPlatform(
189194
options.platform || (await question('平台(ios/android/harmony):')),
190195
);

src/utils/git.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import git from 'isomorphic-git';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
5+
export interface CommitInfo {
6+
hash: string;
7+
message: string;
8+
author: string;
9+
date: number;
10+
origin: string;
11+
}
12+
13+
function findGitRoot(dir = process.cwd()) {
14+
const gitRoot = fs.readdirSync(dir).find((dir) => dir === '.git');
15+
if (gitRoot) {
16+
// console.log({ gitRoot });
17+
return path.join(dir, gitRoot);
18+
}
19+
const parentDir = path.dirname(dir);
20+
if (parentDir === dir) {
21+
return null;
22+
}
23+
return findGitRoot(parentDir);
24+
}
25+
26+
const gitRoot = findGitRoot();
27+
28+
export async function getCommitInfo(): Promise<CommitInfo | undefined> {
29+
if (!gitRoot) {
30+
return;
31+
}
32+
try {
33+
const remotes = await git.listRemotes({ fs, gitdir: gitRoot });
34+
const origin =
35+
remotes.find((remote) => remote.remote === 'origin') || remotes[0];
36+
const { commit, oid } = (
37+
await git.log({ fs, gitdir: gitRoot, depth: 1 })
38+
)[0];
39+
return {
40+
hash: oid,
41+
message: commit.message,
42+
author: commit.author.name || commit.committer.name,
43+
date: commit.committer.timestamp,
44+
origin: origin.url,
45+
};
46+
} catch (error) {
47+
console.error(error);
48+
return;
49+
}
50+
}

src/utils/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { checkPlugins } from './check-plugin';
1111
import { read } from 'read';
1212
import { tempDir } from './constants';
1313
import { depVersions } from './dep-versions';
14-
import { getCommitInfo } from './git';
1514

1615
export async function question(query: string, password?: boolean) {
1716
if (NO_INTERACTIVE) {
@@ -169,8 +168,6 @@ async function getLatestVersion(pkgNames: string[]) {
169168
}
170169

171170
export async function printVersionCommand() {
172-
const result = await getCommitInfo();
173-
console.log(JSON.stringify(result, null, 2));
174171
let [latestPushyCliVersion, latestPushyVersion] = await getLatestVersion([
175172
'react-native-update-cli',
176173
'react-native-update',

src/versions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { checkPlatform, getSelectedApp } from './app';
55
import { choosePackage } from './package';
66
import { compare } from 'compare-versions';
77
import { depVersions } from './utils/dep-versions';
8+
import { getCommitInfo } from './utils/git';
89

910
async function showVersion(appId: string, offset: number) {
1011
const { data, count } = await get(`/app/${appId}/version/list`);
@@ -98,13 +99,15 @@ export const commands = {
9899

99100
const { hash } = await uploadFile(fn);
100101

101-
const versionName = name || (await question('输入版本名称: ')) || '(未命名)';
102+
const versionName =
103+
name || (await question('输入版本名称: ')) || '(未命名)';
102104
const { id } = await post(`/app/${appId}/version/create`, {
103105
name: versionName,
104106
hash,
105107
description: description || (await question('输入版本描述:')),
106108
metaInfo: metaInfo || (await question('输入自定义的 meta info:')),
107109
deps: depVersions,
110+
commit: await getCommitInfo(),
108111
});
109112
// TODO local diff
110113
saveToLocal(fn, `${appId}/ppk/${id}.ppk`);

0 commit comments

Comments
 (0)