From c8848b8cb79b6dfd2e75949c9e7827e9c15f6a4e Mon Sep 17 00:00:00 2001 From: AI Engineer Date: Wed, 3 Jun 2026 18:39:52 +0800 Subject: [PATCH] chore: add publish script and update package files configuration --- package-lock.json | 8 ++++---- package.json | 11 ++++++++--- scripts/publish.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 scripts/publish.js diff --git a/package-lock.json b/package-lock.json index f7ed5f3..060ad33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "bootstrap", - "version": "1.0.0", + "name": "@apigo.cc/bootstrap", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "bootstrap", - "version": "1.0.0", + "name": "@apigo.cc/bootstrap", + "version": "1.0.1", "license": "ISC", "dependencies": { "bootstrap": "^5.3.8", diff --git a/package.json b/package.json index 4cb1ed3..2e8d84a 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,17 @@ { - "name": "bootstrap", - "version": "1.0.0", + "name": "@apigo.cc/bootstrap", + "version": "1.0.1", "type": "module", "main": "dist/bootstrap.js", + "module": "dist/bootstrap.js", + "files": [ + "dist" + ], "scripts": { "dev": "vite", "build": "vite build", - "test": "playwright test" + "test": "playwright test", + "pub": "node scripts/publish.js" }, "keywords": [], "author": "", diff --git a/scripts/publish.js b/scripts/publish.js new file mode 100644 index 0000000..b562aa9 --- /dev/null +++ b/scripts/publish.js @@ -0,0 +1,48 @@ +import { execSync } from 'child_process'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +try { + // 1. 获取最新 tag + let tag; + try { + tag = execSync('git describe --tags --abbrev=0', { encoding: 'utf8' }).trim(); + } catch (err) { + throw new Error('Failed to find git tags. Please make sure the repository has tags (e.g., v1.0.0) before publishing.'); + } + // 去掉 v 前缀 + const version = tag.startsWith('v') ? tag.slice(1) : tag; + + console.log(`Latest git tag: ${tag}, Version to publish: ${version}`); + + // 2. 读取并更新 package.json + const pkgPath = path.join(__dirname, '../package.json'); + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); + + // 保持原有名称(如果已经带有 @apigo.cc/ 前缀)或替换前缀 + if (!pkg.name.startsWith('@apigo.cc/')) { + const baseName = pkg.name.includes('/') ? pkg.name.split('/')[1] : pkg.name; + pkg.name = `@apigo.cc/${baseName}`; + } + pkg.version = version; + + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); + console.log(`Updated package.json: name=${pkg.name}, version=${pkg.version}`); + + // 3. 构建 + console.log('Running build...'); + execSync('npm run build', { stdio: 'inherit', cwd: path.join(__dirname, '..') }); + + // 4. 发布 + console.log('Publishing to npm...'); + const args = process.argv.slice(2).join(' '); + execSync(`npm publish --access public ${args}`, { stdio: 'inherit', cwd: path.join(__dirname, '..') }); + + console.log('Publish successful!'); +} catch (error) { + console.error('Publish failed:', error.message); + process.exit(1); +}