From eeed9d8cd78b2f66668f08eaa5f2edc5ea298194 Mon Sep 17 00:00:00 2001 From: chu fan Date: Thu, 7 Sep 2023 11:16:20 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- scripts/bundle | 162 +++++++++++++++++++++++++----------------------- scripts/deploy | 48 +++++++------- scripts/dev | 4 +- scripts/docker | 72 ++++++++++----------- scripts/lint | 15 ++--- scripts/release | 25 ++++---- scripts/test | 50 +++++++-------- 8 files changed, 188 insertions(+), 192 deletions(-) diff --git a/package.json b/package.json index 602e26f..c5babb1 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,18 @@ "name": "408CSFamily", "description": "专业代号408,计算机基础知识点合集", "version": "0.0.1-alpha.0", + "packageManager": "pnpm@7.2.1", "engines": { "node": "14.x || 16.x || 18.x", "pnpm": ">=7.2.1" }, - "packageManager": "pnpm@7.2.1", "author": { "name": "Chu Fan", "email": "fairy_408@2925.com", "url": "https://github.com/142vip" }, "scripts": { - "prepare": "husky install && npx husky add .husky/pre-commit \"./scripts/lint --fix\" && chmod +x .husky/pre-commit", + "prepare": "rm -f .husky/pre-commit && husky install && npx husky add .husky/pre-commit \"./scripts/lint --fix\" && chmod +x .husky/pre-commit", "dev": "vuepress dev docs", "build": "./scripts/bundle build", "build:proxy": "./scripts/bundle build_proxy", diff --git a/scripts/bundle b/scripts/bundle index 5c4e8fb..551e367 100755 --- a/scripts/bundle +++ b/scripts/bundle @@ -11,27 +11,32 @@ * - ./scripts/bundle 交互式选择执行的命令 */ -const { execShell } = require("./.exec"); -const { Select } = require('enquirer'); +const { execShell } = require('./.exec') +const { Select } = require('enquirer') -const packageVersion=require('../package.json').version -const projectName="408CSFamily" +const packageVersion = require('../package.json').version +const projectName = '408CSFamily' // 仓库地址 -const repoAddress="registry.cn-hangzhou.aliyuncs.com/142vip/doc_book" +const repoAddress = 'registry.cn-hangzhou.aliyuncs.com/142vip/doc_book' // 镜像地址 -const imageName=`${repoAddress}:${projectName}-${packageVersion}` +const imageName = `${repoAddress}:${projectName}-${packageVersion}` /** * 获取构建镜像的脚本 * @param containerBuild + * @param preBuild * @param needProxy + * @returns {string[]} */ -function getBuildImageScript({containerBuild,needProxy=false}){ - // 基础构建脚本 - const baseBuildScript=containerBuild?"":( - needProxy?"./scripts/bundle build_proxy":"./scripts/bundle build" - ) - return [ +function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) { + // 基础构建脚本 + let baseBuildScript = '' + + if (preBuild) { + baseBuildScript = needProxy ? './scripts/bundle build_proxy' : './scripts/bundle build' + } + + return [ // 构建镜像 ` ${baseBuildScript} @@ -53,82 +58,81 @@ function getBuildImageScript({containerBuild,needProxy=false}){ exit 1; fi ` - ] + ] } /** * 支持的脚本命令 */ -const SupportScripts={ - build:'vuepress build docs', - build_proxy:'PROXY_DOMAIN=true vuepress build docs', - image:getBuildImageScript({ - containerBuild:true, - needProxy:false - }), - image_proxy:getBuildImageScript({ - containerBuild:true, - needProxy:true - }), - // 直接从本地拿dist文件,生成镜像 - image_faster:getBuildImageScript({ - containerBuild:false, - needProxy:false +const SupportScripts = { + build: 'vuepress build docs', + build_proxy: 'PROXY_DOMAIN=true vuepress build docs', + image: getBuildImageScript({ + containerBuild: true, + needProxy: false + }), + image_proxy: getBuildImageScript({ + containerBuild: true, + needProxy: true + }), + // 直接从本地拿dist文件,生成镜像 + image_faster: getBuildImageScript({ + containerBuild: false, + needProxy: false + }) +} + + +async function getScriptCommand() { + const scriptName = process.argv[2] + let scriptCommand = SupportScripts.build + + if (scriptName == null) { + const prompt = new Select({ + header: '======================== 408CSFamily Cli For Building ========================', + footer: '======================== 408CSFamily Cli For Building ========================', + name: 'color', + message: 'What script will you want to run ', + choices: [ + { + message: 'build', + name: SupportScripts.build, + value: '#00ffff' + }, + { + message: 'build for fixing proxy', + name: SupportScripts.build_proxy, + value: '#000000' + }, + { + message: 'build to docker image', + name: SupportScripts.image, + value: '#0000ff' + }, + { + message: 'build to docker image with proxy', + name: SupportScripts.image_proxy, + value: '#0000ff' + }, + { + message: 'build to docker image faster', + name: SupportScripts.image_faster, + value: '#0000ff' + } + ] }) + scriptCommand = await prompt.run() + } else { + // 命中支持的脚本 + if (Object.keys(SupportScripts).includes(scriptName)) { scriptCommand = SupportScripts[scriptName] } + } + return scriptCommand } -async function getScriptCommand(){ - let scriptName=process.argv[2]; - let scriptCommand=SupportScripts.build - - if(scriptName==null){ - const prompt = new Select({ - header: '======================== 408CSFamily Cli For Building ========================', - footer: '======================== 408CSFamily Cli For Building ========================', - name: 'color', - message: 'What script will you want to run ', - choices: [ - { - message: 'build', - name: SupportScripts.build, - value: '#00ffff' - }, - { - message: 'build for fixing proxy', - name: SupportScripts.build_proxy, - value: '#000000' - }, - { - message: 'build to docker image', - name: SupportScripts.image, - value: '#0000ff' - }, - { - message: 'build to docker image with proxy', - name: SupportScripts.image_proxy, - value: '#0000ff' - }, - { - message: 'build to docker image faster', - name: SupportScripts.image_faster, - value: '#0000ff' - }, - ] - }); - scriptCommand = await prompt.run() - }else{ - // 命中支持的脚本 - if(Object.keys(SupportScripts).includes(scriptName)) - scriptCommand=SupportScripts[scriptName] - } - return scriptCommand -} - - -;(async ()=>{ - const scriptCommand= await getScriptCommand() - await execShell(scriptCommand) +;(async() => { + const scriptCommand = await getScriptCommand() + await execShell(scriptCommand) })() diff --git a/scripts/deploy b/scripts/deploy index 1db9758..7e8d002 100755 --- a/scripts/deploy +++ b/scripts/deploy @@ -5,19 +5,19 @@ * - ./scripts/deploy ali * - ./scripts/deploy github */ -const { execShell } = require("./.exec"); -const packageVersion=require('../package.json').version +const { execShell } = require('./.exec') +const packageVersion = require('../package.json').version -const dockerDeployInfo={ - repoAddress:"registry.cn-hangzhou.aliyuncs.com/142vip/doc_book", - containerName:'408CSFamily', - networkName:'service_env_net', +const dockerDeployInfo = { + repoAddress: 'registry.cn-hangzhou.aliyuncs.com/142vip/doc_book', + containerName: '408CSFamily', + networkName: 'service_env_net' } -const imageName=`${dockerDeployInfo.repoAddress}:${dockerDeployInfo.containerName}-${packageVersion}` +const imageName = `${dockerDeployInfo.repoAddress}:${dockerDeployInfo.containerName}-${packageVersion}` // 支持的命令 -const SupportScripts={ - Github:` +const SupportScripts = { + Github: ` set -e ./scripts/build proxy && cd docs/.vuepress/dist git init && git add -A @@ -30,7 +30,7 @@ const SupportScripts={ git push -f https://github.com/mmdapl/408CSFamily.git master:pages/github cd - `, - Ali:[ + Ali: [ // 容器存在即删除 ` if docker inspect --format='{{.State.Running}}' ${dockerDeployInfo.containerName} >/dev/null 2>&1;then @@ -59,31 +59,31 @@ const SupportScripts={ --restart=unless-stopped \ --ip=172.30.0.100 \ ${imageName} - `, - ] + ` + ] } -const deployName=process.argv[2]; +const deployName = process.argv[2] -function getDeployCommand(){ - let deployCommand=SupportScripts.Ali +function getDeployCommand() { + let deployCommand = SupportScripts.Ali // 部署到阿里云服务器 - if(deployName==='ali'){ - deployCommand=SupportScripts.Ali + if (deployName === 'ali') { + deployCommand = SupportScripts.Ali } // 部署到Github - if(deployName==='github'){ - deployCommand=SupportScripts.Github + if (deployName === 'github') { + deployCommand = SupportScripts.Github } return deployCommand } // 执行 -;(async ()=>{ - const deployCommand=getDeployCommand() - // console.log(deployCommand) - await execShell(deployCommand) -})() \ No newline at end of file +;(async() => { + const deployCommand = getDeployCommand() + // console.log(deployCommand) + await execShell(deployCommand) +})() diff --git a/scripts/dev b/scripts/dev index 0fd4a19..ea46530 100755 --- a/scripts/dev +++ b/scripts/dev @@ -6,5 +6,5 @@ * - ./scripts/dev */ -const { execShell } = require("./.exec"); -(async ()=>await execShell('vuepress dev docs'))() \ No newline at end of file +const { execShell } = require('./.exec'); +(async() => await execShell('vuepress dev docs'))() diff --git a/scripts/docker b/scripts/docker index 5308124..db1b2c5 100755 --- a/scripts/docker +++ b/scripts/docker @@ -7,8 +7,8 @@ * - ./scripts/network image xxx 镜像相关 * - ./scripts/docker network xxx 网络相关 */ -const { execShell, BaseSetting} = require("./.exec"); -const scriptName=process.argv[2]; +const { execShell, BaseSetting } = require('./.exec') +const scriptName = process.argv[2] /** @@ -17,19 +17,19 @@ const scriptName=process.argv[2]; * - 子网掩码 * - 网关地址 */ -const dockerNetworkInfo={ - defaultName:'service_env_net', - subnet:'172.30.0.0/24', - gateway:'172.30.0.1', +const dockerNetworkInfo = { + defaultName: 'service_env_net', + subnet: '172.30.0.0/24', + gateway: '172.30.0.1' } /** * docker containers 容器相关指令 */ -const SupportScriptsInContainer={ - run:'', - rm:'', - ps:'docker ps' +const SupportScriptsInContainer = { + run: '', + rm: '', + ps: 'docker ps' } /** @@ -43,9 +43,9 @@ const SupportScriptsInImage = { /** * docker network 相关脚本指令 */ -const SupportScriptsInNetWork={ - ls:'docker network ls', - create:[ +const SupportScriptsInNetWork = { + ls: 'docker network ls', + create: [ // 创建网关 ` docker network create \ @@ -58,7 +58,7 @@ const SupportScriptsInNetWork={ docker network inspect ${dockerNetworkInfo.defaultName} ` ], - rm:[ + rm: [ // 参数校验 ` if test -z "${dockerNetworkInfo.defaultName}";then @@ -86,44 +86,39 @@ const SupportScriptsInNetWork={ } +function getContainerCommand() { + const name = process.argv[3] -function getContainerCommand(){ - const name=process.argv[3]; - - if(SupportScriptsInContainer.hasOwnProperty(name)){ + if (name in SupportScriptsInContainer) { return SupportScriptsInContainer[name] } // 默认查看所有容器 return SupportScriptsInContainer.ps } -function getImageCommand(){ - const name=process.argv[3]; +function getImageCommand() { + const name = process.argv[3] - if(SupportScriptsInImage.hasOwnProperty(name)){ + if (name in SupportScriptsInImage) { return SupportScriptsInImage[name] } return SupportScriptsInImage.ps } - -function getNetworkCommand(){ - const name=process.argv[3]; - if(SupportScriptsInNetWork.hasOwnProperty(name)){ +function getNetworkCommand() { + const name = process.argv[3] + if (name in SupportScriptsInNetWork) { return SupportScriptsInNetWork[scriptName] } return SupportScriptsInNetWork.ls } - - - // 支持的命令 -const SupportScripts={ - ls:'docker network ls', - create:[ +const SupportScripts = { + ls: 'docker network ls', + create: [ // 创建网关 ` docker network create \ @@ -136,7 +131,7 @@ const SupportScripts={ docker network inspect ${dockerNetworkInfo.defaultName} ` ], - rm:[ + rm: [ // 参数校验 ` if test -z "${dockerNetworkInfo.defaultName}";then @@ -163,9 +158,9 @@ const SupportScripts={ ` } -function getCommand(){ - const scriptName=process.argv[3]; - switch (scriptName){ +function getCommand() { + const scriptName = process.argv[3] + switch (scriptName) { case 'network': return getNetworkCommand() case 'container': @@ -173,12 +168,11 @@ function getCommand(){ case 'image': return getImageCommand() } - } // 执行 -;(async ()=>{ - const command=getCommand(scriptName) +;(async() => { + const command = getCommand(scriptName) await execShell(command) -})() \ No newline at end of file +})() diff --git a/scripts/lint b/scripts/lint index 0d933c3..961c95d 100755 --- a/scripts/lint +++ b/scripts/lint @@ -1,4 +1,5 @@ #!/usr/bin/env node + /** * * 格式化代码 @@ -7,13 +8,9 @@ * - ./scripts/lint --fix */ -const { execShell } = require("./.exec"); -const scriptName=process.argv[2]; +const { execShell } = require('./.exec') +const scriptName = process.argv[2] +const fixed = scriptName != null ? '--fix' : ''; -let fixed='' - -if(scriptName!=null){ - fixed='--fix' -} - -;(async ()=>await execShell(`eslint ${fixed} --ext .js,.ts,.vue --ignore-path .gitignore .`))() \ No newline at end of file +// 可以在--fix后指定目录 +(async() => await execShell(`eslint ${fixed} --ext .js,.ts,.vue --ignore-path .gitignore .`))() diff --git a/scripts/release b/scripts/release index 20a7aac..ce439a9 100755 --- a/scripts/release +++ b/scripts/release @@ -1,16 +1,17 @@ -#!/usr/bin/env node -/** - * 版本发布脚本 - * 链接:https://jstools.dev/version-bump-prompt/ - * 使用: ./scripts/release - */ +#!/bin/bash -const {execShell} = require("./.exec"); +## +## 版本发布脚本 +## 链接:https://jstools.dev/version-bump-prompt/ +## 使用: ./scripts/release +## -// 利用commit-and-tag-version生成changelog文档,并跳过commit、tag操作 -const generateChangeLog='pnpm commit-and-tag-version && git add CHANGELOG.md' -// git提交信息 -const commitInfo='chore(release): publish v%s' -;(async ()=>await execShell(`bumpp --preid alpha --execute="${generateChangeLog}" --commit "${commitInfo}" --all --tag --push`))() \ No newline at end of file +## 利用commit-and-tag-version生成changelog文档,并跳过commit、tag操作 +readonly generateChangeLog='pnpm commit-and-tag-version && git add CHANGELOG.md' +## git提交信息 +readonly commitInfo='chore(release): publish v%s' + + +bumpp --preid alpha --execute="$generateChangeLog" --commit "$commitInfo" --all --tag --push \ No newline at end of file diff --git a/scripts/test b/scripts/test index ff7acf6..526c405 100755 --- a/scripts/test +++ b/scripts/test @@ -6,30 +6,30 @@ */ // const {execShell} = require("./.exec"); -(async ()=>{ - const { AutoComplete } = require('enquirer'); +(async() => { + const { AutoComplete } = require('enquirer') - const prompt = new AutoComplete({ - name: 'flavor', - message: 'Pick your favorite flavor', - limit: 10, - initial: 2, - choices: [ - 'Almond', - 'Apple', - 'Banana', - 'Blackberry', - 'Blueberry', - 'Cherry', - 'Chocolate', - 'Cinnamon', - 'Coconut', - 'Cranberry', - 'Grape', - ] - }); + const prompt = new AutoComplete({ + name: 'flavor', + message: 'Pick your favorite flavor', + limit: 10, + initial: 2, + choices: [ + 'Almond', + 'Apple', + 'Banana', + 'Blackberry', + 'Blueberry', + 'Cherry', + 'Chocolate', + 'Cinnamon', + 'Coconut', + 'Cranberry', + 'Grape' + ] + }) - prompt.run() - .then(answer => console.log('Answer:', answer)) - .catch(console.error); -})() \ No newline at end of file + prompt.run() + .then(answer => console.log('Answer:', answer)) + .catch(console.error) +})()