@rpcbase/cli 0.36.0 → 0.37.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
@@ -11,6 +11,8 @@ const {hideBin} = require("yargs/helpers")
11
11
  const start_command = require("./src/start_command")
12
12
  const print_versions = require("./src/print_versions")
13
13
  const update = require("./src/update")
14
+ const sync_dotenv = require("./src/sync-dotenv")
15
+
14
16
 
15
17
  yargs(hideBin(process.argv))
16
18
  .command(["start", "*"], "Starts client, server, and infrastructure", (yargs) => {
@@ -32,6 +34,7 @@ yargs(hideBin(process.argv))
32
34
  description: "Run with debug logging"
33
35
  })
34
36
  }, (argv) => {
37
+ console.log("IS start")
35
38
  if (argv.version) return print_versions()
36
39
  start_command(argv)
37
40
  })
@@ -47,6 +50,18 @@ yargs(hideBin(process.argv))
47
50
  if (argv.version) return print_versions()
48
51
  await update()
49
52
  })
53
+ // sync-dotenv
54
+ .command(["sync-dotenv"], "Compare and sync dotenv files", (yargs) => {
55
+ yargs
56
+ .option("write", {
57
+ type: "boolean",
58
+ default: false,
59
+ description: "Write the ouput to dest env file"
60
+ })
61
+ }, async(argv) => {
62
+ if (argv.version) return print_versions()
63
+ await sync_dotenv()
64
+ })
50
65
  // Ping
51
66
  .command("ping", "Ping", () => {}, (argv) => {
52
67
  if (argv.version) return print_versions()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/cli",
3
- "version": "0.36.0",
3
+ "version": "0.37.0",
4
4
  "license": "SSPL-1.0",
5
5
  "bin": {
6
6
  "rb": "./bin.js"
@@ -13,9 +13,13 @@
13
13
  "concurrently": "8.2.1",
14
14
  "debug": "4.3.4",
15
15
  "dotenv": "16.3.1",
16
+ "parse-dotenv": "2.1.0",
16
17
  "picocolors": "1.0.0",
17
18
  "semver": "7.5.4",
18
19
  "validator": "13.11.0",
19
20
  "yargs": "17.7.2"
21
+ },
22
+ "devDependencies": {
23
+ "jest": "29.7.0"
20
24
  }
21
25
  }
@@ -7,11 +7,11 @@ const exit_with_message = require("./exit_with_message")
7
7
 
8
8
  const {CONTAINER_MODE} = process.env
9
9
 
10
- if (!["docker", "native"].includes(CONTAINER_MODE.trim()))
11
- throw new Error("expected CONTAINER_MODE to be native|docker")
12
-
13
10
  // run type is client | server | both
14
11
  const get_run_configs = () => {
12
+ if (!["docker", "native"].includes(CONTAINER_MODE.trim()))
13
+ throw new Error("expected CONTAINER_MODE to be native|docker")
14
+
15
15
  const cwd = process.cwd()
16
16
 
17
17
  // check if running parent of rb project
@@ -14,7 +14,6 @@ const has_tailscale = process.platform === "darwin" && fs.existsSync(TAILSCALE_P
14
14
  const get_port = (run_configs) => {
15
15
  const project_root_dir = get_root_dir(run_configs)
16
16
 
17
- // TODO: should join w/ .env.base
18
17
  const server_env_path = path.join(project_root_dir, "./server/server/.env")
19
18
 
20
19
  const server_env_buf = fs.readFileSync(server_env_path)
@@ -0,0 +1,3 @@
1
+ DOMAIN=localhost3
2
+ GHEllo=
3
+ # yo
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Luqman Olushi O.
4
+ Copyright (c) 2023 rpcbase
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,138 @@
1
+ const path = require("path")
2
+ const fs = require("fs")
3
+ const os = require('os')
4
+ const parseEnv = require("parse-dotenv")
5
+ const dotenv = require("dotenv")
6
+
7
+ const DEFAULT_ENV_PATH = path.resolve(process.cwd(), ".env")
8
+ const DEFAULT_SAMPLE_ENV = path.resolve(process.cwd(), ".env.sample")
9
+
10
+ const envToString = (parsed) =>
11
+ Object.keys(parsed)
12
+ .map(key => `${key}=${parsed[key] || ""}`)
13
+ .join(os.EOL)
14
+ .replace(/(__\w+_\d+__=)/g, "")
15
+
16
+ const writeToSampleEnv = (path, parsedEnv) => {
17
+ try {
18
+ fs.writeFileSync(path, envToString(parsedEnv))
19
+ } catch (e) {
20
+ throw new Error(`Sync failed. ${e.message}`)
21
+ }
22
+ }
23
+
24
+ const emptyObjProps = (obj) => {
25
+ const objCopy = { ...obj }
26
+ Object.keys(objCopy).forEach(key => {
27
+ if (objCopy[key].includes("#")) {
28
+ if (objCopy[key].match(/(".*"|'.*')/g)) {
29
+ const objArr = objCopy[key].split(/(".*"|'.*')/)
30
+ objCopy[key] = objArr.slice(-1)[0].trim()
31
+ } else {
32
+ const objArr = objCopy[key].split("#")
33
+ objCopy[key] = `#${objArr.slice(-1)[0]}`
34
+ }
35
+
36
+ return
37
+ }
38
+
39
+ if (!key.startsWith("__COMMENT_")) {
40
+ objCopy[key] = ""
41
+ }
42
+ })
43
+
44
+ return objCopy
45
+ }
46
+
47
+ const getUniqueVarsFromEnvs = (
48
+ env,
49
+ envExample,
50
+ config = {}
51
+ ) => {
52
+ const ignoreKeys = config.preserve || []
53
+
54
+ const uniqueKeys = new Set(Object.keys(env))
55
+ const uniqueKeysArray = Array.from(uniqueKeys)
56
+
57
+ const uniqueFromSource = uniqueKeysArray.map((key) => {
58
+ if (key.startsWith("__COMMENT_")) return { [key]: env[key] }
59
+ return { [key]: envExample[key] || "" }
60
+ })
61
+
62
+ const presevedVars = Object.keys(envExample)
63
+ .map(key => ({ [key]: envExample[key] }))
64
+ // .filter(env => {
65
+ // console.log("ICICIC", env)
66
+ // return ignoreKeys.length && ignoreKeys.includes(Object.keys(env)[0])
67
+ // })
68
+
69
+ return [...uniqueFromSource, ...presevedVars]
70
+ }
71
+
72
+
73
+ const syncWithSampleEnv = (
74
+ envPath,
75
+ envExamplePath
76
+ ) => {
77
+ const config = { emptyLines: true, comments: true }
78
+
79
+ const sourceEnv = emptyObjProps(
80
+ parseEnv(envPath, config)
81
+ )
82
+ const targetEnv = parseEnv(envExamplePath)
83
+
84
+ const uniqueVars = getUniqueVarsFromEnvs(sourceEnv, targetEnv, config)
85
+ const envCopy = {}
86
+ uniqueVars.forEach(env => {
87
+ const [key] = Object.keys(env)
88
+ envCopy[key] = env[key]
89
+ })
90
+
91
+ writeToSampleEnv(envExamplePath, envCopy)
92
+ }
93
+
94
+
95
+ const sync_dotenv = (
96
+ source,
97
+ sample_env,
98
+ samples
99
+ ) => {
100
+ if (sample_env && (sample_env === ".env" || path.basename(sample_env) === ".env")) {
101
+ throw new Error("Cannot sync .env with .env")
102
+ }
103
+
104
+ const sample_env_paths = !samples
105
+ ? [path.resolve(process.cwd(), sample_env || DEFAULT_SAMPLE_ENV)]
106
+ : globby
107
+ .sync(samples)
108
+ .map((sample) => path.resolve(process.cwd(), sample))
109
+
110
+ const envPath = source
111
+ ? fs.existsSync(source)
112
+ ? source
113
+ : null
114
+ : DEFAULT_ENV_PATH
115
+
116
+ if (envPath === null) throw new Error(`${source} not found`)
117
+
118
+ if (!source && !fs.existsSync(envPath)) throw new Error(".env doesn't exists")
119
+
120
+ if (!sample_env_paths.length)
121
+ throw new Error(`${samples} did not match any file`)
122
+
123
+ if (!fs.existsSync(sample_env_paths[0])) {
124
+ fs.writeFileSync(sample_env_paths[0], "")
125
+ // throw new Error(`${sample_env || path.basename(DEFAULT_SAMPLE_ENV)} not found`)
126
+ }
127
+
128
+ const sourcePath = envPath
129
+
130
+ for (const samplePath of sample_env_paths) {
131
+ syncWithSampleEnv(sourcePath, samplePath)
132
+ }
133
+
134
+ return sample_env_paths.join(" ")
135
+ }
136
+
137
+
138
+ module.exports = sync_dotenv
@@ -0,0 +1,8 @@
1
+ /* @flow */
2
+
3
+ const {syncEnv} = require("./index")
4
+
5
+
6
+ const res = syncEnv(".env", ".env.sample")
7
+
8
+ // console.log("FNDE", res)