@rpcbase/cli 0.36.0 → 0.38.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,26 @@ 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 [source_env] [dest_env]", "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
|
+
.positional("source_env", {
|
|
62
|
+
describe: "source .env file",
|
|
63
|
+
type: "string"
|
|
64
|
+
})
|
|
65
|
+
.positional("dest_env", {
|
|
66
|
+
describe: "destination env file",
|
|
67
|
+
type: "string"
|
|
68
|
+
})
|
|
69
|
+
}, async(argv) => {
|
|
70
|
+
if (argv.version) return print_versions()
|
|
71
|
+
await sync_dotenv(argv.source_env, argv.dest_env)
|
|
72
|
+
})
|
|
50
73
|
// Ping
|
|
51
74
|
.command("ping", "Ping", () => {}, (argv) => {
|
|
52
75
|
if (argv.version) return print_versions()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.38.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,126 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const path = require("path")
|
|
3
|
+
const fs = require("fs")
|
|
4
|
+
const os = require('os')
|
|
5
|
+
const parse_env = require("parse-dotenv")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const DEFAULT_ENV_PATH = path.resolve(process.cwd(), ".env")
|
|
9
|
+
const SAMPLE_ENV_SUFFIX = ".sample"
|
|
10
|
+
const COMMENT_PREFIX = "__COMMENT_"
|
|
11
|
+
|
|
12
|
+
const env_to_string = (parsed) =>
|
|
13
|
+
Object.keys(parsed)
|
|
14
|
+
.map(key => `${key}=${parsed[key] || ""}`)
|
|
15
|
+
.join(os.EOL)
|
|
16
|
+
.replace(/(__\w+_\d+__=)/g, "")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const write_to_dest_env = (path, parsed_env) => {
|
|
20
|
+
try {
|
|
21
|
+
fs.writeFileSync(path, env_to_string(parsed_env))
|
|
22
|
+
} catch (e) {
|
|
23
|
+
throw new Error(`Sync failed. ${e.message}`)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const get_empty_obj_props = (obj) => {
|
|
29
|
+
|
|
30
|
+
const obj_copy = { ...obj }
|
|
31
|
+
|
|
32
|
+
Object.keys(obj_copy).forEach(key => {
|
|
33
|
+
if (obj_copy[key].includes("#")) {
|
|
34
|
+
if (obj_copy[key].match(/(".*"|'.*')/g)) {
|
|
35
|
+
const objArr = obj_copy[key].split(/(".*"|'.*')/)
|
|
36
|
+
obj_copy[key] = objArr.slice(-1)[0].trim()
|
|
37
|
+
} else {
|
|
38
|
+
const objArr = obj_copy[key].split("#")
|
|
39
|
+
obj_copy[key] = `#${objArr.slice(-1)[0]}`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!key.startsWith(COMMENT_PREFIX)) {
|
|
46
|
+
obj_copy[key] = ""
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return obj_copy
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const get_unique_vars_from_envs = (
|
|
54
|
+
env,
|
|
55
|
+
env_example,
|
|
56
|
+
config = {}
|
|
57
|
+
) => {
|
|
58
|
+
const unique_keys = new Set(Object.keys(env))
|
|
59
|
+
const unique_keys_list = Array.from(unique_keys)
|
|
60
|
+
|
|
61
|
+
const unique_from_source = unique_keys_list.map((key) => {
|
|
62
|
+
if (key.startsWith(COMMENT_PREFIX)) return { [key]: env[key] }
|
|
63
|
+
return { [key]: env_example[key] || "" }
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const preseved_vars = Object.keys(env_example)
|
|
67
|
+
.map(key => ({ [key]: env_example[key] }))
|
|
68
|
+
// .filter(env => {
|
|
69
|
+
// console.log("ICICIC", env)
|
|
70
|
+
// return ignoreKeys.length && ignoreKeys.includes(Object.keys(env)[0])
|
|
71
|
+
// })
|
|
72
|
+
|
|
73
|
+
return [...unique_from_source, ...preseved_vars]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const sync_with_dest_env = (
|
|
78
|
+
env_path,
|
|
79
|
+
env_example_path
|
|
80
|
+
) => {
|
|
81
|
+
const config = { emptyLines: true, comments: true }
|
|
82
|
+
|
|
83
|
+
const source_env = get_empty_obj_props(
|
|
84
|
+
parse_env(env_path, config)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const target_env = parse_env(env_example_path)
|
|
88
|
+
|
|
89
|
+
const unique_vars = get_unique_vars_from_envs(source_env, target_env, config)
|
|
90
|
+
const env_copy = {}
|
|
91
|
+
|
|
92
|
+
unique_vars.forEach(env => {
|
|
93
|
+
const [key] = Object.keys(env)
|
|
94
|
+
env_copy[key] = env[key]
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
write_to_dest_env(env_example_path, env_copy)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
const sync_dotenv = (
|
|
102
|
+
env_path,
|
|
103
|
+
_dest_env,
|
|
104
|
+
) => {
|
|
105
|
+
if (env_path && !fs.existsSync(env_path)) {
|
|
106
|
+
throw new Error(`env_path: ${env_path} doesn't exist`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const source_path = env_path || DEFAULT_ENV_PATH
|
|
110
|
+
|
|
111
|
+
if (!fs.existsSync(source_path)) {
|
|
112
|
+
throw new Error(`${env_path || ".env"} file not found`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const dest_env_path = _dest_env || `${source_path}${SAMPLE_ENV_SUFFIX}`
|
|
116
|
+
|
|
117
|
+
// creates the sample env file if it doesn't exist
|
|
118
|
+
if (!fs.existsSync(dest_env_path)) {
|
|
119
|
+
fs.writeFileSync(dest_env_path, "")
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
sync_with_dest_env(source_path, dest_env_path)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
module.exports = sync_dotenv
|