1- import { exec as execCmd } from 'node:child_process'
21import fs from 'node:fs'
32
3+ import { exec as execCmd , type ExecOptions , type ExecReturn , execSync as execSyncCmd } from '@candriajs/exec'
44import convert , { type RGB } from 'color-convert'
55import dayjs from 'dayjs'
66import relativeTime from 'dayjs/plugin/relativeTime.js'
@@ -10,15 +10,13 @@ import LanguageColors from 'language-colors'
1010import { basePath } from '@/root'
1111import type {
1212 ContributionResult ,
13- ExecOptions ,
14- ExecReturn ,
15- RepoBaseParamType
13+ GitRepoType
1614} from '@/types'
1715
1816const localeCache = new Set < string > ( [ 'en' ] )
1917
2018/**
21- * 执行 shell 命令
19+ * 异步执行 shell 命令
2220 * @param cmd 命令
2321 * @param options 选项
2422 * @param options.log 是否打印日志 默认不打印
@@ -35,54 +33,36 @@ const localeCache = new Set<string>(['en'])
3533 * // -> 打印执行命令和结果
3634 * ```
3735 */
38- export function exec < T extends boolean = false > (
36+ export async function exec < T extends boolean = false > (
3937 cmd : string ,
4038 options ?: ExecOptions < T >
4139) : Promise < ExecReturn < T > > {
42- const logger = console
43- return new Promise ( ( resolve ) => {
44- if ( options ?. log ) {
45- logger . info ( [
46- '[exec] 执行命令:' ,
47- `pwd: ${ options ?. cwd ?? process . cwd ( ) } ` ,
48- `cmd: ${ cmd } ` ,
49- `options: ${ JSON . stringify ( options ) } `
50- ] . join ( '\n' ) )
51- }
52-
53- execCmd ( cmd , options , ( error , stdout , stderr ) => {
54- if ( options ?. log ) {
55- const info = error as Error
56- if ( info . message ) info . message = `\x1b[91m${ info . message } \x1b[0m`
57- logger . info ( [
58- '[exec] 执行结果:' ,
59- `stderr: ${ stderr . toString ( ) } ` ,
60- `stdout: ${ stdout . toString ( ) } ` ,
61- `error: ${ JSON . stringify ( info , null , 2 ) } `
62- ] . join ( '\n' ) )
63- }
64-
65- if ( options ?. booleanResult ) {
66- return resolve ( ( ! error ) as ExecReturn < T > )
67- }
68-
69- stdout = stdout . toString ( )
70- stderr = stderr . toString ( )
71-
72- if ( options ?. trim ) {
73- stdout = stdout . trim ( )
74- stderr = stderr . trim ( )
75- }
40+ return await execCmd ( cmd , options )
41+ }
7642
77- const value = {
78- status : ! error ,
79- error,
80- stdout,
81- stderr
82- } as ExecReturn < T >
83- resolve ( value )
84- } )
85- } )
43+ /**
44+ * 同步执行 shell 命令
45+ * @param cmd 命令
46+ * @param options 选项
47+ * @param options.log 是否打印日志 默认不打印
48+ * @param options.booleanResult 是否只返回布尔值 表示命令是否成功执行 默认返回完整的结果
49+ * @example
50+ * ```ts
51+ * const { status, error, stdout, stderr } = execSync('ls -al')
52+ * // -> { status: true, error: null, stdout: '...', stderr: '...' }
53+ *
54+ * const status = execSync('ls -al', { booleanResult: true })
55+ * // -> true
56+ *
57+ * const { status, error, stdout, stderr } = execSync('ls -al', { log: true })
58+ * // -> 打印执行命令和结果
59+ * ```
60+ */
61+ export function execSync < T extends boolean = false > (
62+ cmd : string ,
63+ options ?: ExecOptions < T >
64+ ) : ExecReturn < T > {
65+ return execSyncCmd ( cmd , options )
8666}
8767
8868/**
@@ -202,14 +182,14 @@ export async function get_relative_time (
202182 * @example
203183 * ```ts
204184 * console.log(parse_git_url('https://github.com/user/repo.git'))
205- * -> { owner: 'user', repo: 'repo' }
185+ * -> { owner: 'user', repo: 'repo', html_url: 'https://github.com/user/repo.git' }
206186 * console.log(parse_git_url('https://ghproxy.com/github.com/user/repo.git'))
207- * -> { owner: 'user', repo: 'repo' }
187+ * -> { owner: 'user', repo: 'repo', html_url: 'https://github.com/user/repo.git' }
208188 * console.log(parse_git_url('https://ghproxy.com/https://github.com/user/repo.git'))
209- * -> { owner: 'user', repo: 'repo' }
189+ * -> { owner: 'user', repo: 'repo', html_url: 'https://github.com/user/repo.git' }
210190 * ```
211191 */
212- export function parse_git_url ( url : string ) : RepoBaseParamType {
192+ export function parse_git_url ( url : string ) : GitRepoType {
213193 const proxyRegex = / ^ h t t p s ? : \/ \/ [ ^ / ] + \/ (?: h t t p s ? : \/ \/ ) ? ( [ ^ / ] + \/ [ ^ / ] + \/ [ ^ / ] + ) /
214194 const proxyMatch = url . match ( proxyRegex )
215195
@@ -220,6 +200,7 @@ export function parse_git_url (url: string): RepoBaseParamType {
220200
221201 const info = GitUrlParse ( url )
222202 return {
203+ html_url : info . href ,
223204 owner : info . owner ,
224205 repo : info . name
225206 }
0 commit comments