Skip to content

Commit 91dd5f7

Browse files
author
magic
committed
v2.7.1:完善 npm 发布脚本与文档
- 新增 release 自动化(打包/版本/commit/可选 publish) - 优化 README 使用与发布说明 Made-with: Cursor
1 parent d17a21c commit 91dd5f7

6 files changed

Lines changed: 272 additions & 23 deletions

File tree

README.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
# bmob-js-sdk-es6
1+
# hydrogen-js-sdk
22

3-
### SDK介绍
3+
### SDK 介绍
44

5-
本SDK基于es6开发,致力打造基于前端混合开发需求,支持微信小程序、抖音小程序、H5、快应用、游戏Cocos、混合App等平台, 整个SDK,就dist目录下Bmob.*.js 这个文件即可使用全部功能,请使用最新版本。
5+
本 SDK 基于 ES6 开发,面向前端混合开发场景,支持微信小程序、抖音小程序、H5、快应用、Cocos 游戏、混合 App 等平台。
6+
使用方式以 `dist/` 目录下的 `Bmob-<version>.min.js` 为准(下载最新版本或安装 npm 包)。
67

78

89

910
## 安装使用
1011

11-
### **在小程序中使用** 【微信小程序】【QQ小程序】【支付宝小程序】【头条小程序】【抖音小程序】
12+
### **在小程序中使用** 【微信小程序】【QQ小程序】【支付宝小程序】【头条小程序】【抖音小程序】【快应用】
1213

1314
下载dist目录中的最新版本Bmob.*.js 然后在项目中引入
1415
```
15-
const Bmob = require('../dist/Bmob-1.0.1.min.js');
16+
const Bmob = require('../dist/Bmob-<version>.min.js');
1617
```
1718

1819
### **在WEB端和Node中使用**
@@ -77,7 +78,7 @@ serviceWorker.unregister();
7778

7879
下载dist目录中的最新版本Bmob.*.js 然后在html中引入
7980
```
80-
<script src="Bmob-2.2.0.min.js"></script>
81+
<script src="Bmob-<version>.min.js"></script>
8182
<script>
8283
// 初始化
8384
Bmob.initialize("你的Secret Key", "你的API 安全码");
@@ -102,11 +103,15 @@ SDK版本 **2.0.0** 以下保留之前的初始化方法
102103
Bmob.initialize("你的Application ID", "你的REST API Key");
103104
```
104105

105-
> 具体详细开发可参照下面的开发文档
106+
> 具体详细开发可参照下面的开发文档
106107
107108
### 开发文档
108109

109-
https://bmob.github.io/hydrogen-js-sdk/#/?id=登陆
110+
- [登陆文档](https://bmob.github.io/hydrogen-js-sdk/#/?id=登陆)
111+
112+
#### 版本 v2.7.1
113+
> - 支撑微信虚拟支付
114+
> - 优化登录流程
110115
111116
#### 版本 v2.6.3
112117
> - 修复v2.6.1严重 bug,配置污染:多个模块实例可能共享同一个全局 Bmob 对象
@@ -406,6 +411,30 @@ npm install
406411
npm run dev
407412
```
408413

414+
### 构建与发布(dist / npm)
415+
416+
#### 构建 dist
417+
```bash
418+
npm run build
419+
```
420+
构建产物为 `dist/Bmob-<version>.min.js`(文件名由 `package.json.version` 决定)。
421+
422+
#### 发布到 npm(版本 + git 提交 + 可选发布)
423+
`utils/hydrogen-js-sdk` 目录下执行:
424+
```bash
425+
npm run release -- --bump patch
426+
```
427+
你也可以在发布时一并打 tag/发布:
428+
```bash
429+
npm run release -- --bump patch --tag --publish
430+
```
431+
432+
说明:
433+
- `--bump``patch | minor | major | x.y.z`,默认 `patch`
434+
- `release` 会更新 `package.json` / `package-lock.json`,然后执行 `npm run build`
435+
- 默认只提交 `package.json``package-lock.json``dist`(避免误提交其它未完成修改);需要全量提交可加 `--all`
436+
- `npm publish` 需要你本机已登录 npm(或配置了正确的 token)
437+
409438
### 目录结构
410439

411440
```
@@ -535,8 +564,8 @@ npm run dev
535564

536565
---
537566

538-
1. 【链接】多对多关系BmobRelation学习笔记(js,微信小程https://www.zybuluo.com/z77/note/1114404
539-
2. API 接口文档 http://doc.bmobapp.com/data/restful/index.html
567+
1. 【链接】多对多关系 BmobRelation 学习笔记(JS/小程序):https://www.zybuluo.com/z77/note/1114404
568+
2. API 接口文档https://doc.bmobapp.com/data/restful/index.html
540569

541570

542571

dist/Bmob-2.7.1.min.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 6 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
22
"name": "hydrogen-js-sdk",
3-
"version": "2.6.3",
3+
"version": "2.7.1",
44
"description": "本SDK基于es6开发,致力打造基于前端混合开发需求,支持微信小程序、H5、快应用、游戏Cocos、混合App等平台, 整个SDK,就dist目录下Bmob.*.js 这个文件即可使用全部功能,请使用最新版本。",
55
"main": "./index.js",
66
"typings": "./index.d.ts",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"build": "webpack --config config/prod.env.js",
10+
"pack": "npm pack",
11+
"publish:dry-run": "npm publish --dry-run",
12+
"publish": "npm run build && npm publish",
13+
"release": "node scripts/release.js",
1014
"watch": "webpack --watch --config config/prod.env.js",
1115
"dev": "webpack-dev-server --config config/dev.env.js"
1216
},

scripts/release.js

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
const fs = require('fs')
4+
const path = require('path')
5+
const { execSync } = require('child_process')
6+
7+
const ROOT = path.resolve(__dirname, '..')
8+
const PKG_PATH = path.join(ROOT, 'package.json')
9+
const LOCK_PATH = path.join(ROOT, 'package-lock.json')
10+
11+
function escapeRegExp(str) {
12+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
13+
}
14+
15+
function parseArgs(argv) {
16+
const args = argv.slice(2)
17+
const out = {
18+
bump: 'patch', // patch|minor|major|x.y.z
19+
publish: false,
20+
tag: false,
21+
stageAll: false
22+
}
23+
24+
for (let i = 0; i < args.length; i++) {
25+
const a = args[i]
26+
if (a === '--bump' || a === '-b') {
27+
out.bump = args[i + 1]
28+
i++
29+
continue
30+
}
31+
if (a === '--publish') {
32+
out.publish = true
33+
continue
34+
}
35+
if (a === '--tag') {
36+
out.tag = true
37+
continue
38+
}
39+
if (a === '--all') {
40+
out.stageAll = true
41+
continue
42+
}
43+
if (a === '--help' || a === '-h') {
44+
out.help = true
45+
continue
46+
}
47+
// allow: node scripts/release.js patch
48+
if (!a.startsWith('-') && !out._freeBumpSet) {
49+
out.bump = a
50+
out._freeBumpSet = true
51+
}
52+
}
53+
54+
return out
55+
}
56+
57+
function bumpSemver(oldVersion, bump) {
58+
const m = oldVersion.match(/^(\d+)\.(\d+)\.(\d+)$/)
59+
if (!m) {
60+
throw new Error(`Unsupported version format: ${oldVersion} (expected x.y.z)`)
61+
}
62+
let major = Number(m[1])
63+
let minor = Number(m[2])
64+
let patch = Number(m[3])
65+
66+
if (/^\d+\.\d+\.\d+$/.test(bump)) {
67+
return bump
68+
}
69+
70+
if (bump === 'patch') patch += 1
71+
else if (bump === 'minor') {
72+
minor += 1
73+
patch = 0
74+
} else if (bump === 'major') {
75+
major += 1
76+
minor = 0
77+
patch = 0
78+
} else {
79+
throw new Error(`Unsupported bump: ${bump} (use patch|minor|major|x.y.z)`)
80+
}
81+
82+
return `${major}.${minor}.${patch}`
83+
}
84+
85+
function updatePackageJsonVersion(oldVersion, newVersion) {
86+
const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8'))
87+
if (pkg.version !== oldVersion) {
88+
throw new Error(`package.json version mismatch: expected ${oldVersion}, got ${pkg.version}`)
89+
}
90+
pkg.version = newVersion
91+
fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2) + '\n', 'utf8')
92+
}
93+
94+
function updatePackageLockVersion(oldVersion, newVersion) {
95+
if (!fs.existsSync(LOCK_PATH)) return
96+
let lock = fs.readFileSync(LOCK_PATH, 'utf8')
97+
98+
const topNeedle = `"name": "hydrogen-js-sdk",\n "version": "${oldVersion}",`
99+
const topReplace = `"name": "hydrogen-js-sdk",\n "version": "${newVersion}",`
100+
101+
const pkgNeedle = `"name": "hydrogen-js-sdk",\n "version": "${oldVersion}",`
102+
const pkgReplace = `"name": "hydrogen-js-sdk",\n "version": "${newVersion}",`
103+
104+
let replaced = 0
105+
if (lock.includes(topNeedle)) {
106+
lock = lock.replace(topNeedle, topReplace)
107+
replaced++
108+
}
109+
if (lock.includes(pkgNeedle)) {
110+
lock = lock.replace(pkgNeedle, pkgReplace)
111+
replaced++
112+
}
113+
114+
if (replaced !== 2) {
115+
// fallback: try a generic targeted replace (still safer than rewriting whole lock file)
116+
const escapedOld = escapeRegExp(oldVersion)
117+
const r1 = new RegExp(`("name"\\s*:\\s*"hydrogen-js-sdk"\\s*,\\s*\\n\\s*"version"\\s*:\\s*")${escapedOld}(")`)
118+
const r2 = new RegExp(`("name"\\s*:\\s*"hydrogen-js-sdk"[\\s\\S]*?\\n\\s*"version"\\s*:\\s*")${escapedOld}(")`)
119+
120+
const before = lock
121+
lock = lock.replace(r1, `$1${newVersion}$2`)
122+
lock = lock.replace(r2, `$1${newVersion}$2`)
123+
if (lock === before) {
124+
throw new Error(`Failed to update package-lock.json version (${oldVersion} -> ${newVersion}).`)
125+
}
126+
}
127+
128+
fs.writeFileSync(LOCK_PATH, lock, 'utf8')
129+
}
130+
131+
function run(cmd, opts = {}) {
132+
execSync(cmd, { stdio: 'inherit', cwd: ROOT, ...opts })
133+
}
134+
135+
function git(cmd) {
136+
return execSync(cmd, { stdio: 'pipe', cwd: ROOT }).toString('utf8').trim()
137+
}
138+
139+
function gitIsInsideRepo() {
140+
try {
141+
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore', cwd: ROOT })
142+
return true
143+
} catch {
144+
return false
145+
}
146+
}
147+
148+
function main() {
149+
const args = parseArgs(process.argv)
150+
if (args.help) {
151+
console.log(`
152+
Usage:
153+
node scripts/release.js --bump patch|minor|major|x.y.z [--tag] [--publish] [--all]
154+
155+
Examples:
156+
npm run release -- --bump patch
157+
npm run release -- --bump minor --tag
158+
npm run release -- --bump 2.7.3 --publish
159+
npm run release -- --bump patch --all
160+
`.trim())
161+
process.exit(0)
162+
}
163+
164+
if (!gitIsInsideRepo()) {
165+
throw new Error('Not a git repository. release script needs git.')
166+
}
167+
168+
const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8'))
169+
const oldVersion = pkg.version
170+
const newVersion = bumpSemver(oldVersion, args.bump)
171+
172+
console.log(`Releasing: ${oldVersion} -> ${newVersion}`)
173+
174+
updatePackageJsonVersion(oldVersion, newVersion)
175+
updatePackageLockVersion(oldVersion, newVersion)
176+
177+
// Build dist with the bumped version
178+
run('npm run build')
179+
180+
// Stage + commit (commit message includes the new version)
181+
if (args.stageAll) run('git add -A')
182+
else run('git add package.json package-lock.json dist')
183+
184+
const stagedNames = git('git diff --cached --name-only')
185+
if (!stagedNames) {
186+
console.log('No staged changes to commit.')
187+
process.exit(0)
188+
}
189+
190+
run(`git commit -m "chore(release): v${newVersion}"`)
191+
192+
if (args.tag) {
193+
// Avoid failing when tag already exists.
194+
try {
195+
run(`git tag v${newVersion}`)
196+
} catch (e) {
197+
console.log(`Skip git tag v${newVersion} (tag may already exist).`)
198+
}
199+
}
200+
201+
if (args.publish) {
202+
run('npm publish --access public')
203+
}
204+
}
205+
206+
main()
207+

src/lib/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ try {
1717
VERSION = `v1.0.0`;
1818
}
1919

20-
// const HOST = "https://api.bmobcloud.com";
21-
const HOST = "http://apitest.codenow.cn";
20+
const HOST = "https://api.bmobcloud.com";
21+
// const HOST = "http://apitest.codenow.cn";
2222
// const HOST = 'http://website-restful.bmobapp.com'
2323

2424
const SECRET_KEY = "";

0 commit comments

Comments
 (0)