Skip to content

Commit

Permalink
refactor(jsShell): 修改传参方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Feb 25, 2024
1 parent 833c182 commit 084b859
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lazy-js-utils",
"version": "0.0.84",
"version": "0.0.85",
"packageManager": "pnpm@7.6.0",
"description": "js-tool",
"author": "Simon He",
Expand Down
50 changes: 11 additions & 39 deletions src/node/jsShell.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
import child_process from 'node:child_process'
import { isArray } from '../is/isArray'
import { isBool } from '../is/isBool'
import { isStr } from '../is/isStr'
import type { IShellMessage } from '../types'

export function jsShell<T extends string | string[]>(
commander: T,
errorExit?: boolean,
isLog?: boolean,
): T extends string ? IShellMessage : IShellMessage[]
export function jsShell<T extends string | string[]>(
commander: T,
stdio: 'inherit' | 'pipe',
errorExit?: boolean,
isLog?: boolean,
): T extends string ? IShellMessage : IShellMessage[]
export function jsShell<T extends string | string[]>(
commander: T,
stdio: 'inherit' | 'pipe',
errorExit?: boolean,
isLog?: boolean,
): T extends string ? IShellMessage : IShellMessage[]
export function jsShell<T extends string | string[]>(
commander: T,
args: string[],
stdio?: 'inherit' | 'pipe',
errorExit?: boolean,
isLog?: boolean,
): T extends string ? IShellMessage : IShellMessage[]
/**
*
* @param { string | string[] } commander 指令
Expand All @@ -36,21 +10,19 @@ export function jsShell<T extends string | string[]>(
* @returns
*/

interface Options {
args?: string[]
stdio?: 'inherit' | 'pipe'
errorExit?: boolean
isLog?: boolean
cwd?: string
}
export function jsShell<T extends string | string[]>(
commander: T,
args: string[] | boolean | 'inherit' | 'pipe' = [],
stdio: 'inherit' | 'pipe' | boolean = 'inherit',
errorExit?: boolean,
isLog?: boolean,
options: Options = {},
) {
if (isBool(args)) {
errorExit = args as boolean
args = []
stdio = 'inherit'
} else if (isStr(args)) {
stdio = args as 'inherit' | 'pipe'
args = []
}
const { args = [], stdio = 'inherit', errorExit, isLog, cwd } = options

return (
isArray(commander)
? commander.map((command) => executor(command))
Expand All @@ -64,10 +36,10 @@ export function jsShell<T extends string | string[]>(
shell: true,
encoding: 'utf8',
stdio: stdio === 'inherit' ? 'inherit' : ['inherit', 'pipe', 'inherit'],
cwd,
},
)
const result = output[1]?.trim()

if (status === 130) {
if (isLog) console.log('已取消...')
return { status, result } as IShellMessage
Expand Down

0 comments on commit 084b859

Please sign in to comment.