Skip to content

Commit

Permalink
feat(@142vip/utils): 增加docker相关api方法
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Sep 19, 2024
1 parent f79237b commit 461bb18
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
123 changes: 123 additions & 0 deletions packages/utils/src/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { execCommand } from './exec'
import { VipLogger } from './logger'
import { vipSymbols } from './color'

interface DockerOptions {
logger?: boolean
}

interface BuildImageDockerOptions {
imageName: string
buildArgs?: [string, string][]
}

/**
* 判断是否存在镜像
* @param imageName
*/
export async function isExistImage(imageName: string) {
const command = `docker images -q ${imageName}`
const { code, stdout } = await execCommand(command)
return code === 0 && stdout.trim() !== ''
}

/**
* 删除Docker镜像
* @param imageName
*/
export async function deleteImage(imageName: string) {
const command = `docker rmi -f ${imageName}`
return await execCommand(command)
}

/**
* 删除虚悬镜像
*/
export async function deletePruneImages() {
const command = 'docker image prune -f'
return await execCommand(command)
}

export async function isExistContainer(containerName: string) {
const command = `docker ps -aq -f name=${containerName}`
const { code, stdout } = await execCommand(command)

return code === 0 && stdout.trim() !== ''
}

/**
* 删除容器
* @param containerName
*/
export async function deleteContainer(containerName: string) {
const command = `docker rm -f ${containerName}`
return await execCommand(command)
}

/**
* 是否安装docker
*/
export async function isInstallDocker(args?: DockerOptions) {
const command = 'docker -v'
const { code, stdout, stderr } = await execCommand(command)

// 打印日志
if (args?.logger) {
const vipLog = new VipLogger()
if (code === 0) {
vipLog.log(`检测到docker,版本信息:\n`, { startLabel: vipSymbols.success })
vipLog.log(stdout)
}
else {
vipLog.log(`未检测到docker,请先安装!!\n`, { startLabel: vipSymbols.error })
vipLog.error(stderr)
}
}
return code === 0 && stdout.includes('Docker')
}

/**
* 是否安装docker-compose
*/
export async function isInstallDockerCompose(args?: DockerOptions) {
const command = 'docker-compose -v'
const { code, stdout, stderr } = await execCommand(command)

// 打印日志
if (args?.logger) {
const vipLog = new VipLogger()
if (code === 0) {
vipLog.log(`检测到docker-compose,版本信息:\n`, { startLabel: vipSymbols.success })
vipLog.log(stdout)
}
else {
vipLog.log(`未检测到docker-compose,请先安装!!\n`, { startLabel: vipSymbols.error })
vipLog.error(stderr)
}
}
return code === 0 && stdout.includes('Docker Compose')
}

/**
* 推送Docker镜像到指定仓库
* @param imageName
*/
export async function pushImage(imageName: string) {
const command = `docker push ${imageName}`
return await execCommand(command)
}

/**
* 构建Docker镜像
* @param args
*/
export async function buildImage(args: BuildImageDockerOptions) {
// 构建参数
let buildArg = ''
if (args.buildArgs != null) {
buildArg = args.buildArgs.map(arg => `--build-arg ${arg[0]}=${arg[1]}`).join(' ')
}
const command = `docker build ${buildArg} -t ${args.imageName} .`
console.log(111, command)
return await execCommand(command)
}
2 changes: 1 addition & 1 deletion packages/utils/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as process from 'node:process'

type Command = string | string[]

interface CmdResult {
export interface CmdResult {
code?: number | null
stdout: string
stderr: string
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './exec'
export * from './shell'
export * from './inquirer'
export * from './git'
export * from './docker'

0 comments on commit 461bb18

Please sign in to comment.