@rpcbase/cli 0.45.0 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +2 -2
- package/package.json +1 -1
- package/src/versions/helpers/sanitize_str.js +5 -0
- package/src/versions/increment-pkg/get_gh_client.js +26 -0
- package/src/versions/increment-pkg/index.js +194 -0
- package/src/increment-pkg/index.js +0 -125
- /package/src/{increment-pkg → versions/increment-pkg}/get_gh_token.js +0 -0
package/bin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/* @flow */
|
|
3
|
-
process.title = "rb"
|
|
3
|
+
process.title = "rb-cli"
|
|
4
4
|
// load .env
|
|
5
5
|
const path = require("path")
|
|
6
6
|
require("dotenv").config({path: path.join(process.cwd(), "./.env")})
|
|
@@ -8,7 +8,7 @@ require("dotenv").config({path: path.join(process.cwd(), "./.env")})
|
|
|
8
8
|
const yargs = require("yargs/yargs")
|
|
9
9
|
const {hideBin} = require("yargs/helpers")
|
|
10
10
|
|
|
11
|
-
const increment_pkg = require("./src/increment-pkg")
|
|
11
|
+
const increment_pkg = require("./src/versions/increment-pkg")
|
|
12
12
|
const print_versions = require("./src/print_versions")
|
|
13
13
|
const start_command = require("./src/start_command")
|
|
14
14
|
const sync_dotenv = require("./src/sync-dotenv")
|
package/package.json
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const {Octokit} = require("octokit")
|
|
3
|
+
const colors = require("picocolors")
|
|
4
|
+
|
|
5
|
+
const get_gh_token = require("./get_gh_token")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const get_gh_client = () => {
|
|
9
|
+
const gh_token = get_gh_token()
|
|
10
|
+
|
|
11
|
+
if (!gh_token) return
|
|
12
|
+
|
|
13
|
+
const octokit = new Octokit({
|
|
14
|
+
auth: gh_token
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
if (!octokit) {
|
|
18
|
+
console.log(colors.bold(colors.yellow("rb increment-pkg:")), "unable to get a github client, do you have a token in .npmrc or .github_token ?")
|
|
19
|
+
process.exit(0)
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return octokit
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = get_gh_client
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const fs = require("fs")
|
|
3
|
+
const path = require("path")
|
|
4
|
+
const {execSync} = require("child_process")
|
|
5
|
+
|
|
6
|
+
const Promise = require("bluebird")
|
|
7
|
+
const semver = require("semver")
|
|
8
|
+
|
|
9
|
+
const sanitize_str = require("../helpers/sanitize_str")
|
|
10
|
+
|
|
11
|
+
const get_gh_client = require("./get_gh_client")
|
|
12
|
+
|
|
13
|
+
const octokit = get_gh_client()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const resolve_repo = () => {
|
|
17
|
+
const origin_url = execSync("git remote get-url origin").toString().trim()
|
|
18
|
+
const owner = path.dirname(origin_url).split(":").pop()
|
|
19
|
+
const repo = path.basename(origin_url).replace(/\.git$/, "")
|
|
20
|
+
|
|
21
|
+
const branch_name = execSync("git branch --show-current").toString().trim()
|
|
22
|
+
|
|
23
|
+
return {repo, owner, branch_name}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const get_latest_branch_version = async({owner, repo, remote_path, branch_name}) => {
|
|
28
|
+
let res
|
|
29
|
+
try {
|
|
30
|
+
res = await octokit.request("GET /repos/{owner}/{repo}/contents/{path}", {
|
|
31
|
+
owner,
|
|
32
|
+
repo,
|
|
33
|
+
path: remote_path,
|
|
34
|
+
ref: branch_name,
|
|
35
|
+
})
|
|
36
|
+
} catch (err) {
|
|
37
|
+
if (err.response.status === 404) {
|
|
38
|
+
return
|
|
39
|
+
} else {
|
|
40
|
+
console.log(err)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const file_contents = Buffer.from(res.data.content, "base64").toString()
|
|
45
|
+
const pack_obj = JSON.parse(file_contents)
|
|
46
|
+
const branch_version = pack_obj.version
|
|
47
|
+
|
|
48
|
+
return branch_version
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
const get_remote_next_version = async({owner, repo, branch_name, remote_path, local_version}) => {
|
|
53
|
+
let remote_version
|
|
54
|
+
let next_version
|
|
55
|
+
|
|
56
|
+
const sanitized_name = sanitize_str(branch_name)
|
|
57
|
+
|
|
58
|
+
if (branch_name === "master") {
|
|
59
|
+
remote_version = await get_latest_branch_version({
|
|
60
|
+
owner,
|
|
61
|
+
repo,
|
|
62
|
+
remote_path,
|
|
63
|
+
branch_name,
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
// simple bump from version on master
|
|
67
|
+
if (remote_version && semver.lte(local_version, remote_version)) {
|
|
68
|
+
next_version = semver.inc(remote_version, "minor")
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
} else {
|
|
72
|
+
// try to get the remote version on the current branch name (but if the branch hasnt been pushed yet, this will fail and fallback to master)
|
|
73
|
+
remote_version = await get_latest_branch_version({
|
|
74
|
+
owner,
|
|
75
|
+
repo,
|
|
76
|
+
remote_path,
|
|
77
|
+
branch_name,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// there is already a remote version on a pre-release branch, so we
|
|
81
|
+
if (remote_version) {
|
|
82
|
+
// TODO: handle the case where master has been updated and we need to re-bump the minor version + create a pre-release one
|
|
83
|
+
next_version = semver.inc(remote_version, "prerelease", sanitized_name)
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// fallback to master
|
|
88
|
+
if (!remote_version) {
|
|
89
|
+
remote_version = await get_latest_branch_version({
|
|
90
|
+
owner,
|
|
91
|
+
repo,
|
|
92
|
+
remote_path,
|
|
93
|
+
branch_name: "master",
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// there was no remote version from this branch yet, so we first increment the master version, when we create a "prerelease" for this branch
|
|
97
|
+
if (remote_version) {
|
|
98
|
+
const bumped = semver.inc(remote_version, "minor")
|
|
99
|
+
next_version = `${bumped}-${sanitized_name}.0`
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!remote_version) {
|
|
105
|
+
console.log(`${owner}/${repo}/${remote_path} remote package version not found, will skip version bump`)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {remote_version, next_version}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
// lint-staged script to increment package version when any package in the pkg/ folder is modified
|
|
113
|
+
const increment_pkg = async(args) => {
|
|
114
|
+
const {files} = args
|
|
115
|
+
|
|
116
|
+
const touched_packages = {}
|
|
117
|
+
|
|
118
|
+
const packages_dir = path.join(process.cwd(), "./pkg")
|
|
119
|
+
|
|
120
|
+
files.forEach((f) => {
|
|
121
|
+
const rel_path = path.relative(packages_dir, f)
|
|
122
|
+
// get package dirname
|
|
123
|
+
const [pack_dir] = path.dirname(rel_path).split("/")
|
|
124
|
+
// check if it is actually a package
|
|
125
|
+
const pack_json_path = path.join(packages_dir, `./${pack_dir}/package.json`)
|
|
126
|
+
if (fs.existsSync(pack_json_path)) {
|
|
127
|
+
touched_packages[pack_dir] = true
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
const {repo, owner, branch_name} = resolve_repo()
|
|
132
|
+
|
|
133
|
+
const process_package = async(pack_dir) => {
|
|
134
|
+
const local_pack_path = path.join(packages_dir, `./${pack_dir}/package.json`)
|
|
135
|
+
const local_pack = JSON.parse(fs.readFileSync(local_pack_path))
|
|
136
|
+
|
|
137
|
+
const local_version = local_pack.version
|
|
138
|
+
if (!local_version) {
|
|
139
|
+
// skipping because package has no local version
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!semver.valid(local_version)) {
|
|
144
|
+
console.log("error:", "local version not valid, got:", JSON.stringify(local_version), "skipping")
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// get remote + next version
|
|
149
|
+
const remote_path = `pkg/${pack_dir}/package.json`
|
|
150
|
+
|
|
151
|
+
const master_version = await get_latest_branch_version({
|
|
152
|
+
owner,
|
|
153
|
+
repo,
|
|
154
|
+
remote_path,
|
|
155
|
+
branch_name: "master"
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
if (!master_version || !semver.valid(master_version)) {
|
|
159
|
+
console.log("error:", "remote master version not valid, got:", JSON.stringify(master_version), "skipping")
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const bumped_version = semver.inc(master_version, "minor")
|
|
164
|
+
|
|
165
|
+
if (semver.lt(local_version, bumped_version)) {
|
|
166
|
+
local_pack.version = bumped_version,
|
|
167
|
+
fs.writeFileSync(local_pack_path, JSON.stringify(local_pack, null, 2) + "\n")
|
|
168
|
+
execSync(`git add ${local_pack_path}`)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// TODO: this will run on the PR workflow but not before commit here, as we only want a minor bump when incrementing from master, the release channels are handled in CI
|
|
172
|
+
// const {remote_version, next_version} = await get_remote_next_version({owner, repo, branch_name, remote_path, local_version})
|
|
173
|
+
//
|
|
174
|
+
// if (!semver.valid(remote_version)) {
|
|
175
|
+
// console.log("warning:", "live version not valid, got:", JSON.stringify(remote_version), "skipping")
|
|
176
|
+
// return
|
|
177
|
+
// }
|
|
178
|
+
//
|
|
179
|
+
// // if (next_version && semver.neq(remote_version, next_version)) {
|
|
180
|
+
// if (next_version && semver.neq(local_version, next_version)) {
|
|
181
|
+
// console.log("bumped", local_pack.name, "to", next_version)
|
|
182
|
+
//
|
|
183
|
+
// local_pack.version = next_version
|
|
184
|
+
//
|
|
185
|
+
// fs.writeFileSync(local_pack_path, JSON.stringify(local_pack, null, 2) + "\n")
|
|
186
|
+
// // add to git index, in case change was not triggered by package.json
|
|
187
|
+
// execSync(`git add ${local_pack_path}`)
|
|
188
|
+
// }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
await Promise.map(Object.keys(touched_packages), process_package)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
module.exports = increment_pkg
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
const fs = require("fs")
|
|
3
|
-
const path = require("path")
|
|
4
|
-
const {execSync} = require("child_process")
|
|
5
|
-
|
|
6
|
-
const colors = require("picocolors")
|
|
7
|
-
const Promise = require("bluebird")
|
|
8
|
-
const semver = require("semver")
|
|
9
|
-
const {Octokit} = require("octokit")
|
|
10
|
-
|
|
11
|
-
const get_gh_token = require("./get_gh_token")
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const get_gh_client = () => {
|
|
15
|
-
const gh_token = get_gh_token()
|
|
16
|
-
|
|
17
|
-
if (!gh_token) return
|
|
18
|
-
|
|
19
|
-
const octokit = new Octokit({
|
|
20
|
-
auth: gh_token
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
return octokit
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const get_latest_live_version = async(pack_dir) => {
|
|
28
|
-
const octokit = get_gh_client()
|
|
29
|
-
|
|
30
|
-
if (!octokit) {
|
|
31
|
-
console.log(colors.bold(colors.yellow("rb increment-pkg:")), "unable to get a github client, do you have a token in .npmrc or .github_token ?")
|
|
32
|
-
process.exit(0)
|
|
33
|
-
return
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const origin_url = execSync("git remote get-url origin").toString().trim()
|
|
37
|
-
const owner = path.dirname(origin_url).split(":").pop()
|
|
38
|
-
const repo = path.basename(origin_url).replace(/\.git$/, "")
|
|
39
|
-
|
|
40
|
-
const remote_path = `pkg/${pack_dir}/package.json`
|
|
41
|
-
|
|
42
|
-
let res
|
|
43
|
-
try {
|
|
44
|
-
res = await octokit.request("GET /repos/{owner}/{repo}/contents/{path}", {
|
|
45
|
-
owner,
|
|
46
|
-
repo,
|
|
47
|
-
path: remote_path,
|
|
48
|
-
})
|
|
49
|
-
} catch (err) {
|
|
50
|
-
if (err.response.status === 404) {
|
|
51
|
-
console.log(`${owner}/${repo}/${remote_path} remote package not found, skipping version bump`)
|
|
52
|
-
return
|
|
53
|
-
} else {
|
|
54
|
-
console.log(err)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const file_contents = Buffer.from(res.data.content, "base64").toString()
|
|
59
|
-
const pack_obj = JSON.parse(file_contents)
|
|
60
|
-
const live_version = pack_obj.version
|
|
61
|
-
|
|
62
|
-
return live_version
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
// lint-staged script to increment package version when any package in the pkg/ folder is modified
|
|
67
|
-
const increment_pkg = async(args) => {
|
|
68
|
-
const {files} = args
|
|
69
|
-
|
|
70
|
-
const touched_packages = {}
|
|
71
|
-
|
|
72
|
-
const packages_dir = path.join(process.cwd(), "./pkg")
|
|
73
|
-
|
|
74
|
-
files.forEach((f) => {
|
|
75
|
-
const rel_path = path.relative(packages_dir, f)
|
|
76
|
-
// get package dirname
|
|
77
|
-
const [pack_dir] = path.dirname(rel_path).split("/")
|
|
78
|
-
// check if it is actually a package
|
|
79
|
-
const pack_json_path = path.join(packages_dir, `./${pack_dir}/package.json`)
|
|
80
|
-
if (fs.existsSync(pack_json_path)) {
|
|
81
|
-
touched_packages[pack_dir] = true
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
const process_package = async(pack_dir) => {
|
|
86
|
-
const local_pack_path = path.join(packages_dir, `./${pack_dir}/package.json`)
|
|
87
|
-
const local_pack = JSON.parse(fs.readFileSync(local_pack_path))
|
|
88
|
-
|
|
89
|
-
const local_version = local_pack.version
|
|
90
|
-
if (!local_version) {
|
|
91
|
-
// skipping because package has no local version
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const live_version = await get_latest_live_version(pack_dir)
|
|
96
|
-
|
|
97
|
-
if (!semver.valid(live_version)) {
|
|
98
|
-
console.log("warning:", "live version not valid, got:", JSON.stringify(live_version), "skipping")
|
|
99
|
-
return
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (!semver.valid(local_version)) {
|
|
103
|
-
console.log("error:", "local version not valid, got:", JSON.stringify(local_version), "skipping")
|
|
104
|
-
process.exit(1)
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// if local_version == live_version -> bump
|
|
109
|
-
if (live_version && semver.eq(local_version, live_version)) {
|
|
110
|
-
const new_version = semver.inc(local_version, "minor")
|
|
111
|
-
|
|
112
|
-
console.log("bumped", local_pack.name, "to", new_version)
|
|
113
|
-
|
|
114
|
-
local_pack.version = new_version
|
|
115
|
-
fs.writeFileSync(local_pack_path, JSON.stringify(local_pack, null, 2) + "\n")
|
|
116
|
-
|
|
117
|
-
// add to git index, in case change was not triggered by package.json
|
|
118
|
-
execSync(`git add ${local_pack_path}`)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
await Promise.map(Object.keys(touched_packages), process_package)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
module.exports = increment_pkg
|
|
File without changes
|