@rpcbase/cli 0.37.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
@@ -51,16 +51,24 @@ yargs(hideBin(process.argv))
51
51
  await update()
52
52
  })
53
53
  // sync-dotenv
54
- .command(["sync-dotenv"], "Compare and sync dotenv files", (yargs) => {
54
+ .command("sync-dotenv [source_env] [dest_env]", "Compare and sync dotenv files", (yargs) => {
55
55
  yargs
56
56
  .option("write", {
57
57
  type: "boolean",
58
58
  default: false,
59
59
  description: "Write the ouput to dest env file"
60
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
+ })
61
69
  }, async(argv) => {
62
70
  if (argv.version) return print_versions()
63
- await sync_dotenv()
71
+ await sync_dotenv(argv.source_env, argv.dest_env)
64
72
  })
65
73
  // Ping
66
74
  .command("ping", "Ping", () => {}, (argv) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/cli",
3
- "version": "0.37.0",
3
+ "version": "0.38.0",
4
4
  "license": "SSPL-1.0",
5
5
  "bin": {
6
6
  "rb": "./bin.js"
@@ -1,137 +1,125 @@
1
+ /* @flow */
1
2
  const path = require("path")
2
3
  const fs = require("fs")
3
4
  const os = require('os')
4
- const parseEnv = require("parse-dotenv")
5
- const dotenv = require("dotenv")
5
+ const parse_env = require("parse-dotenv")
6
+
6
7
 
7
8
  const DEFAULT_ENV_PATH = path.resolve(process.cwd(), ".env")
8
- const DEFAULT_SAMPLE_ENV = path.resolve(process.cwd(), ".env.sample")
9
+ const SAMPLE_ENV_SUFFIX = ".sample"
10
+ const COMMENT_PREFIX = "__COMMENT_"
9
11
 
10
- const envToString = (parsed) =>
12
+ const env_to_string = (parsed) =>
11
13
  Object.keys(parsed)
12
14
  .map(key => `${key}=${parsed[key] || ""}`)
13
15
  .join(os.EOL)
14
16
  .replace(/(__\w+_\d+__=)/g, "")
15
17
 
16
- const writeToSampleEnv = (path, parsedEnv) => {
18
+
19
+ const write_to_dest_env = (path, parsed_env) => {
17
20
  try {
18
- fs.writeFileSync(path, envToString(parsedEnv))
21
+ fs.writeFileSync(path, env_to_string(parsed_env))
19
22
  } catch (e) {
20
23
  throw new Error(`Sync failed. ${e.message}`)
21
24
  }
22
25
  }
23
26
 
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()
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()
31
37
  } else {
32
- const objArr = objCopy[key].split("#")
33
- objCopy[key] = `#${objArr.slice(-1)[0]}`
38
+ const objArr = obj_copy[key].split("#")
39
+ obj_copy[key] = `#${objArr.slice(-1)[0]}`
34
40
  }
35
41
 
36
42
  return
37
43
  }
38
44
 
39
- if (!key.startsWith("__COMMENT_")) {
40
- objCopy[key] = ""
45
+ if (!key.startsWith(COMMENT_PREFIX)) {
46
+ obj_copy[key] = ""
41
47
  }
42
48
  })
43
49
 
44
- return objCopy
50
+ return obj_copy
45
51
  }
46
52
 
47
- const getUniqueVarsFromEnvs = (
53
+ const get_unique_vars_from_envs = (
48
54
  env,
49
- envExample,
55
+ env_example,
50
56
  config = {}
51
57
  ) => {
52
- const ignoreKeys = config.preserve || []
53
-
54
- const uniqueKeys = new Set(Object.keys(env))
55
- const uniqueKeysArray = Array.from(uniqueKeys)
58
+ const unique_keys = new Set(Object.keys(env))
59
+ const unique_keys_list = Array.from(unique_keys)
56
60
 
57
- const uniqueFromSource = uniqueKeysArray.map((key) => {
58
- if (key.startsWith("__COMMENT_")) return { [key]: env[key] }
59
- return { [key]: envExample[key] || "" }
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] || "" }
60
64
  })
61
65
 
62
- const presevedVars = Object.keys(envExample)
63
- .map(key => ({ [key]: envExample[key] }))
66
+ const preseved_vars = Object.keys(env_example)
67
+ .map(key => ({ [key]: env_example[key] }))
64
68
  // .filter(env => {
65
69
  // console.log("ICICIC", env)
66
70
  // return ignoreKeys.length && ignoreKeys.includes(Object.keys(env)[0])
67
71
  // })
68
72
 
69
- return [...uniqueFromSource, ...presevedVars]
73
+ return [...unique_from_source, ...preseved_vars]
70
74
  }
71
75
 
72
76
 
73
- const syncWithSampleEnv = (
74
- envPath,
75
- envExamplePath
77
+ const sync_with_dest_env = (
78
+ env_path,
79
+ env_example_path
76
80
  ) => {
77
81
  const config = { emptyLines: true, comments: true }
78
82
 
79
- const sourceEnv = emptyObjProps(
80
- parseEnv(envPath, config)
83
+ const source_env = get_empty_obj_props(
84
+ parse_env(env_path, config)
81
85
  )
82
- const targetEnv = parseEnv(envExamplePath)
83
86
 
84
- const uniqueVars = getUniqueVarsFromEnvs(sourceEnv, targetEnv, config)
85
- const envCopy = {}
86
- uniqueVars.forEach(env => {
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 => {
87
93
  const [key] = Object.keys(env)
88
- envCopy[key] = env[key]
94
+ env_copy[key] = env[key]
89
95
  })
90
96
 
91
- writeToSampleEnv(envExamplePath, envCopy)
97
+ write_to_dest_env(env_example_path, env_copy)
92
98
  }
93
99
 
94
100
 
95
101
  const sync_dotenv = (
96
- source,
97
- sample_env,
98
- samples
102
+ env_path,
103
+ _dest_env,
99
104
  ) => {
100
- if (sample_env && (sample_env === ".env" || path.basename(sample_env) === ".env")) {
101
- throw new Error("Cannot sync .env with .env")
105
+ if (env_path && !fs.existsSync(env_path)) {
106
+ throw new Error(`env_path: ${env_path} doesn't exist`)
102
107
  }
103
108
 
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`)
109
+ const source_path = env_path || DEFAULT_ENV_PATH
122
110
 
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`)
111
+ if (!fs.existsSync(source_path)) {
112
+ throw new Error(`${env_path || ".env"} file not found`)
126
113
  }
127
114
 
128
- const sourcePath = envPath
115
+ const dest_env_path = _dest_env || `${source_path}${SAMPLE_ENV_SUFFIX}`
129
116
 
130
- for (const samplePath of sample_env_paths) {
131
- syncWithSampleEnv(sourcePath, samplePath)
117
+ // creates the sample env file if it doesn't exist
118
+ if (!fs.existsSync(dest_env_path)) {
119
+ fs.writeFileSync(dest_env_path, "")
132
120
  }
133
121
 
134
- return sample_env_paths.join(" ")
122
+ sync_with_dest_env(source_path, dest_env_path)
135
123
  }
136
124
 
137
125
 
@@ -1,22 +0,0 @@
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.