Skip to content

Commit

Permalink
chore: 重写打包发布脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
renzp94 committed Apr 28, 2024
1 parent f083bf4 commit 9353654
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 85 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@renzp/utils",
"version": "0.0.5",
"version": "0.0.6",
"exports": {
"./array": "./src/array.ts",
"./has": "./src/has.ts",
Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"description": "一款零依赖、实用的Javascript/Typescript工具库",
"type": "module",
"main": "dist/index.js",
"files": [
"dist"
],
"files": ["dist"],
"types": "dist/index.d.ts",
"author": "renzp <1074720760.qq.com>",
"repository": {
Expand All @@ -17,9 +15,7 @@
"bugs": {
"url": "https://github.com/renzp94/utils/issues"
},
"keywords": [
"utils"
],
"keywords": ["utils"],
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org",
Expand All @@ -29,6 +25,7 @@
"build": "bun run scripts/build.ts",
"pub:npm": "bun run scripts/pub-npm.ts",
"pub:jsr": "bun run scripts/pub-jsr.ts",
"pub:all": "bun run scripts/pub-all.ts",
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
Expand Down
56 changes: 9 additions & 47 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,13 @@
import { exists, readdir, unlink } from 'node:fs/promises'
import dts from 'bun-plugin-dts'
import { build } from './common'

const rmDist = async () => {
const hasDist = await exists('./dist')
if (hasDist) {
const distFiles = await readdir('./dist')
const rmFiles = distFiles.map((file) => {
return unlink(`./dist/${file}`)
})
const result = await build()

await Promise.all(rmFiles)
}
}

export const getEntrypoints = async () => {
const files = await readdir('./src')
const entrypoints = files
.filter((file) => !file.includes('_'))
.map((file) => `./src/${file}`)

return entrypoints
}

const runBuild = async () => {
if (result.success) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包中...')
await rmDist()
const entrypoints = await getEntrypoints()
const result = await Bun.build({
entrypoints,
outdir: './dist',
naming: '[name].[ext]',
splitting: true,
minify: true,
format: 'esm',
plugins: [dts()],
})

if (result.success) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包成功 🎉🎉🎉')
} else {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包失败 🚨\n')
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(result.logs)
}
console.log('📦 打包成功 🎉🎉🎉')
} else {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包失败 🚨\n')
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(result.logs)
}

runBuild()
101 changes: 101 additions & 0 deletions scripts/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { exists, readdir, unlink } from 'node:fs/promises'
import dts from 'bun-plugin-dts'

/**
* 删除dist 目录
*/
export const rmDist = async () => {
const hasDist = await exists('./dist')
if (hasDist) {
const distFiles = await readdir('./dist')
const rmFiles = distFiles.map((file) => {
return unlink(`./dist/${file}`)
})

await Promise.all(rmFiles)
}
}

/**
* 获取入口文件
*/
const getEntrypoints = async () => {
const files = await readdir('./src')
const entrypoints = files
.filter((file) => !file.includes('_'))
.map((file) => `./src/${file}`)

return entrypoints
}

/**
* 打包
*/
export const build = async () => {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包中...')
await rmDist()
const entrypoints = await getEntrypoints()
return await Bun.build({
entrypoints,
outdir: './dist',
naming: '[name].[ext]',
splitting: true,
minify: true,
format: 'esm',
plugins: [dts()],
})
}

/**
* npm发布
*/
export const npmPublish = async () => {
await Bun.$`bunx standard-version`
await Bun.$`npm publish`
}

/**
* 获取jsr导出路径
*/
const getExports = async () => {
const files = await readdir('./src')
const exports = files
.filter((file) => !file.includes('_'))
.reduce((prev, file) => {
if (file === 'index.ts') {
return { ...prev, '.': './src/index.ts' }
}

return {
...prev,
[`./${file.replace('.ts', '')}`]: `./src/${file}`,
}
}, {})

return exports
}

/**
* jsr发布
*/
export const jsrPublish = async () => {
const pkg = await Bun.file('./package.json').json()
const jsr = await Bun.file('./jsr.json').json()

if (jsr.version === pkg.version) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('无新版本发布')
return
}

const exports = await getExports()
const jsrConfig = {
name: pkg.name,
version: pkg.version,
exports,
}

await Bun.write('./jsr.json', JSON.stringify(jsrConfig, null, 2))
await Bun.$`bunx jsr publish --allow-dirty`
}
16 changes: 16 additions & 0 deletions scripts/pub-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { build, jsrPublish, npmPublish } from './common'

const result = await build()

if (result.success) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包成功 🎉🎉🎉')
await npmPublish()
await jsrPublish()
await Bun.$`git push origin --follow-tags`
} else {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包失败 🚨\n')
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(result.logs)
}
33 changes: 2 additions & 31 deletions scripts/pub-jsr.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
import { readdir } from 'node:fs/promises'
import { jsrPublish } from './common'

const pkg = await Bun.file('./package.json').json()

const getExports = async () => {
const files = await readdir('./src')
const exports = files
.filter((file) => !file.includes('_'))
.reduce((prev, file) => {
if (file === 'index.ts') {
return { ...prev, '.': './src/index.ts' }
}

return {
...prev,
[`./${file.replace('.ts', '')}`]: `./src/${file}`,
}
}, {})

return exports
}

const exports = await getExports()
const jsrConfig = {
name: pkg.name,
version: pkg.version,
exports,
}

await Bun.write('./jsr.json', JSON.stringify(jsrConfig, null, 2))

await Bun.$`bunx jsr publish --allow-dirty`
jsrPublish()
15 changes: 15 additions & 0 deletions scripts/pub-npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { build, npmPublish } from './common'

const result = await build()

if (result.success) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包成功 🎉🎉🎉')
await npmPublish()
await Bun.$`git push origin --follow-tags`
} else {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('📦 打包失败 🚨\n')
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(result.logs)
}

0 comments on commit 9353654

Please sign in to comment.