Skip to content

Commit

Permalink
feat: 优化 @mango-scripts/dev-scripts 发包脚本逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertLin0923 committed Nov 16, 2024
1 parent 90d7c14 commit 70d2dd2
Show file tree
Hide file tree
Showing 19 changed files with 771 additions and 413 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "AlbertLin0923",
"scripts": {
"release": "dev-scripts releasePackage",
"dev": "pnpm -r --filter './packages/**' run dev",
"dev": "tsx scripts/dev.mts",
"build": "pnpm -r --filter './packages/**' run build",
"prepare": "husky install",
"preinstall": "npx only-allow pnpm",
Expand All @@ -34,6 +34,7 @@
"@commitlint/config-conventional": "^18.4.3",
"@mango-scripts/dev-scripts": "workspace:*",
"@mango-scripts/esp-config": "^2.0.8",
"@mango-scripts/utils": "^2.0.4",
"@types/node": "^20.10.5",
"@types/react": "^18.2.42",
"@types/react-dom": "^18.2.17",
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"prepublishOnly": "pnpm run build"
},
"dependencies": {
"@mango-scripts/utils": "^2.0.4",
"@mango-scripts/utils": "workspace:*",
"npm-keyword": "^7.0.0",
"package-json": "^8.1.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-scripts/src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import changeExtname from './scripts/changeExtname.mjs'
import addPackage from './scripts/addPackage.mjs'
import gitGkd from './scripts/gitGkd.mjs'
import releasePackage from './scripts/releasePackage.mjs'
import releasePackage from './scripts/releasePackage/index.mjs'

const packageJson = fs.readJSONSync(
path.resolve(
Expand Down
11 changes: 8 additions & 3 deletions packages/dev-scripts/src/scripts/addPackage.mts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import npmKeyword from 'npm-keyword'
import packageJson from 'package-json'
import { pico, consola, inquirer } from '@mango-scripts/utils'
import {
pico,
consola,
inquirer,
getMonorepoPkgListInfo,
} from '@mango-scripts/utils'

import { run, getPkgInfoList } from '../utils/releaseUtils.mjs'
import { run } from '../utils/index.mjs'

const typeMap = [
{ name: 'dependencies', value: '--save-prod' },
{ name: 'devDependencies', value: '--save-dev' },
]

const addPackage = async (): Promise<void> => {
const appList = await getPkgInfoList()
const appList = await getMonorepoPkgListInfo()

const { appName, installPkgName, type } = await inquirer.prompt([
{
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-scripts/src/scripts/gitGkd.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pico, consola, getGitRepoInfo } from '@mango-scripts/utils'

import { run } from '../utils/releaseUtils.mjs'
import { run } from '../utils/index.mjs'

type GitGkdOptionsType = {
targetBranch: string[]
Expand Down
186 changes: 0 additions & 186 deletions packages/dev-scripts/src/scripts/releasePackage.mts

This file was deleted.

75 changes: 75 additions & 0 deletions packages/dev-scripts/src/scripts/releasePackage/confirmEnv.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
pico,
consola,
inquirer,
getGitRepoInfo,
ora,
} from '@mango-scripts/utils'

import { run } from '../../utils/index.mjs'

export const confirmRegistry = async () => {
const registry = (
await run('npm', ['config', 'get', 'registry'], { stdio: 'pipe' })
).stdout

const { yes }: { yes: boolean } = await inquirer.prompt([
{
type: 'confirm',
name: 'yes',
message: `当前 npm registry 为: ${pico.yellow(registry)},确定? `,
},
])

return yes
}

export const confirmGitBranch = async () => {
const { branch } = getGitRepoInfo()
const { yes }: { yes: boolean } = await inquirer.prompt([
{
type: 'confirm',
name: 'yes',
message: `当前发布的分支为: ${pico.yellow(branch)},确定?`,
},
])

return yes
}

export const confirmWorktreeEmpty = async () => {
const isWorktreeEmpty = !(
await run('git', ['status', '--porcelain'], { stdio: 'pipe' })
)?.stdout

!isWorktreeEmpty &&
consola.error('检测到当前工作区有尚未提交的代码,请先提交代码')

return isWorktreeEmpty
}

export const confirmNpmLoggedIn = async () => {
try {
const spinner = ora().start(`获取 npm 登录状态...`)
const user = (await run('npm', ['whoami'], { stdio: 'pipe' })).stdout.trim()
spinner.stop()

if (!user) {
consola.error('检测到你尚未登录 npm,请使用 `npm login` 登录后再继续。')
return false
}

const { yes }: { yes: boolean } = await inquirer.prompt([
{
type: 'confirm',
name: 'yes',
message: `当前登录的 npm 用户为: ${pico.cyan(user)},确定?`,
},
])

return yes
} catch (error) {
consola.error('检测到您尚未登录 npm,请使用 `npm login` 登录后再继续。')
return false
}
}
Loading

0 comments on commit 70d2dd2

Please sign in to comment.