diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml new file mode 100644 index 0000000..f393954 --- /dev/null +++ b/.github/workflows/release-please.yml @@ -0,0 +1,39 @@ +name: Release and Publish +on: + push: + branches: + - main +permissions: + contents: write + pull-requests: write +jobs: + release-please: + runs-on: ubuntu-latest + steps: + - uses: google-github-actions/release-please-action@v4 + id: release + with: + release-type: node + default-branch: main + # Checkout + - uses: actions/checkout@v4 + if: ${{ steps.release.outputs.release_created }} + # Setup node + - uses: actions/setup-node@v4 + with: + node-version: 20 + registry-url: "https://registry.npmjs.org" + if: ${{ steps.release.outputs.release_created }} + # Install dependencies + - run: npm install + env: + NODE_AUTH_TOKEN: ${{ secrets.RELEASE }} + if: ${{ steps.release.outputs.release_created }} + # Build output + - run: npm run build:npm + if: ${{ steps.release.outputs.release_created }} + # Publish to npm + - run: npm run pub + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + if: ${{ steps.release.outputs.release_created }} diff --git a/init.js b/init.js new file mode 100644 index 0000000..ad19689 --- /dev/null +++ b/init.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node + +import fs from 'fs' +import path from 'path' +import { fileURLToPath } from 'url' + +const file = path.join(process.cwd(), 'package.json') +/** 获取当前文件的路径 */ +const filePath = fileURLToPath(import.meta.url) + +/** 如果不处于npm包环境 不修改 */ +if (filePath.includes('node_modules')) { + const main = './' + path.relative(process.cwd(), path.join(path.dirname(filePath), './lib/index.js')) + const pkg = JSON.parse(fs.readFileSync(file, 'utf-8')) + pkg.type = 'module' + pkg.main = main + pkg.types = main.replace(/js$/, 'd.ts') + if (!pkg.scripts) pkg.scripts = {} + pkg.scripts.start = 'node ' + main + fs.writeFileSync(file, JSON.stringify(pkg, null, 2)) +} diff --git a/package.json b/package.json index 9f25048..003f26f 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,12 @@ "LICENSE", "package.json" ], + "bin": { + "init": "init.js" + }, "scripts": { "build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json", + "postinstall": "node init.js", "pub": "npm publish --access public", "sort": "npx sort-package-json" },