From 461bb18089f6e72bd959c63b72616a1795f50bfe Mon Sep 17 00:00:00 2001 From: chufan Date: Fri, 20 Sep 2024 00:14:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(@142vip/utils):=20=E5=A2=9E=E5=8A=A0`docke?= =?UTF-8?q?r`=E7=9B=B8=E5=85=B3`api`=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/utils/src/docker.ts | 123 +++++++++++++++++++++++++++++++++++ packages/utils/src/exec.ts | 2 +- packages/utils/src/index.ts | 1 + 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 packages/utils/src/docker.ts diff --git a/packages/utils/src/docker.ts b/packages/utils/src/docker.ts new file mode 100644 index 0000000..5c097be --- /dev/null +++ b/packages/utils/src/docker.ts @@ -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) +} diff --git a/packages/utils/src/exec.ts b/packages/utils/src/exec.ts index e893487..fb14aea 100644 --- a/packages/utils/src/exec.ts +++ b/packages/utils/src/exec.ts @@ -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 diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 144eeea..a3bba0c 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -4,3 +4,4 @@ export * from './exec' export * from './shell' export * from './inquirer' export * from './git' +export * from './docker'