@rpcbase/cli 0.39.0 → 0.40.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 CHANGED
@@ -8,10 +8,11 @@ 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 start_command = require("./src/start_command")
11
+ const increment_pkg = require("./src/increment-pkg")
12
12
  const print_versions = require("./src/print_versions")
13
- const update = require("./src/update")
13
+ const start_command = require("./src/start_command")
14
14
  const sync_dotenv = require("./src/sync-dotenv")
15
+ const update = require("./src/update")
15
16
 
16
17
 
17
18
  yargs(hideBin(process.argv))
@@ -54,6 +55,12 @@ yargs(hideBin(process.argv))
54
55
  if (argv.version) return print_versions()
55
56
  await update()
56
57
  })
58
+ // Increment pkg
59
+ .command(["increment-pkg <files...>"], "Bump a package version if necessary by checking its source on github", (yargs) => {
60
+ }, async(argv) => {
61
+ if (argv.version) return print_versions()
62
+ await increment_pkg(argv)
63
+ })
57
64
  // sync-dotenv
58
65
  .command("sync-dotenv [source_env] [dest_env]", "Compare and sync dotenv files", (yargs) => {
59
66
  yargs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/cli",
3
- "version": "0.39.0",
3
+ "version": "0.40.0",
4
4
  "license": "SSPL-1.0",
5
5
  "bin": {
6
6
  "rb": "./bin.js"
@@ -13,6 +13,7 @@
13
13
  "concurrently": "8.2.1",
14
14
  "debug": "4.3.4",
15
15
  "dotenv": "16.3.1",
16
+ "octokit": "2.1.0",
16
17
  "parse-dotenv": "2.1.0",
17
18
  "picocolors": "1.0.0",
18
19
  "semver": "7.5.4",
@@ -0,0 +1,30 @@
1
+ /* @flow */
2
+ const fs = require("fs")
3
+ const path = require("path")
4
+
5
+ const get_gh_token = () => {
6
+ let token
7
+
8
+ const npmrc_path = path.join(process.cwd(), "./.npmrc")
9
+ const gh_token_path = path.join(process.cwd(), "./.gh_token")
10
+
11
+ if (fs.existsSync(npmrc_path)) {
12
+ const content = fs.readFileSync(npmrc_path, "utf8")
13
+ const match = content.match(/^\/\/npm\.pkg\.github\.com\/:_authToken=(\S+)$/m)
14
+
15
+ if (match) {
16
+ token = match[1]
17
+ }
18
+ } else if (fs.existsSync(gh_token_path)) {
19
+ try {
20
+ token = JSON.parse(fs.readFileSync(gh_token_path))
21
+ } catch (err) {
22
+ console.log("unable to parse token from .gh_token")
23
+ }
24
+ }
25
+
26
+
27
+ return token
28
+ }
29
+
30
+ module.exports = get_gh_token
@@ -0,0 +1,125 @@
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