diff --git a/bun.lockb b/bun.lockb index f4b4255..9dec3e9 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/jsr.json b/jsr.json index b00920d..988ee08 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@renzp/utils", - "version": "0.0.5", + "version": "0.0.6", "exports": { "./array": "./src/array.ts", "./has": "./src/has.ts", diff --git a/package.json b/package.json index fe72967..6584640 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -17,9 +15,7 @@ "bugs": { "url": "https://github.com/renzp94/utils/issues" }, - "keywords": [ - "utils" - ], + "keywords": ["utils"], "license": "MIT", "publishConfig": { "registry": "https://registry.npmjs.org", @@ -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" diff --git a/scripts/build.ts b/scripts/build.ts index c337932..0a60a34 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -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: - 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: - console.log('📦 打包成功 🎉🎉🎉') - } else { - // biome-ignore lint/suspicious/noConsoleLog: - console.log('📦 打包失败 🚨\n') - // biome-ignore lint/suspicious/noConsoleLog: - console.log(result.logs) - } + console.log('📦 打包成功 🎉🎉🎉') +} else { + // biome-ignore lint/suspicious/noConsoleLog: + console.log('📦 打包失败 🚨\n') + // biome-ignore lint/suspicious/noConsoleLog: + console.log(result.logs) } - -runBuild() diff --git a/scripts/common.ts b/scripts/common.ts new file mode 100644 index 0000000..bdf0054 --- /dev/null +++ b/scripts/common.ts @@ -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: + 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: + 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` +} diff --git a/scripts/pub-all.ts b/scripts/pub-all.ts new file mode 100644 index 0000000..384ed31 --- /dev/null +++ b/scripts/pub-all.ts @@ -0,0 +1,16 @@ +import { build, jsrPublish, npmPublish } from './common' + +const result = await build() + +if (result.success) { + // biome-ignore lint/suspicious/noConsoleLog: + console.log('📦 打包成功 🎉🎉🎉') + await npmPublish() + await jsrPublish() + await Bun.$`git push origin --follow-tags` +} else { + // biome-ignore lint/suspicious/noConsoleLog: + console.log('📦 打包失败 🚨\n') + // biome-ignore lint/suspicious/noConsoleLog: + console.log(result.logs) +} diff --git a/scripts/pub-jsr.ts b/scripts/pub-jsr.ts index 919ced4..c30b0f7 100644 --- a/scripts/pub-jsr.ts +++ b/scripts/pub-jsr.ts @@ -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() diff --git a/scripts/pub-npm.ts b/scripts/pub-npm.ts new file mode 100644 index 0000000..4bc9902 --- /dev/null +++ b/scripts/pub-npm.ts @@ -0,0 +1,15 @@ +import { build, npmPublish } from './common' + +const result = await build() + +if (result.success) { + // biome-ignore lint/suspicious/noConsoleLog: + console.log('📦 打包成功 🎉🎉🎉') + await npmPublish() + await Bun.$`git push origin --follow-tags` +} else { + // biome-ignore lint/suspicious/noConsoleLog: + console.log('📦 打包失败 🚨\n') + // biome-ignore lint/suspicious/noConsoleLog: + console.log(result.logs) +}