@coderich/sandman 0.0.6 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coderich/sandman",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -9,27 +9,36 @@ module.exports = class ConfigClient extends Config {
9
9
  #configDir; #mergeData = {};
10
10
 
11
11
  constructor(configDir) {
12
- super();
12
+ super({}, {
13
+ file: name => `file-${name}`,
14
+ });
13
15
  this.#configDir = configDir;
14
16
  this.mergeDir();
15
17
  }
16
18
 
17
19
  get(key, ...args) {
20
+ if (key?.startsWith?.('.')) {
21
+ const { dictionary } = this.toObject();
22
+ const k = key.substring(1);
23
+ return k.length ? Util.get(dictionary['.'], k) : dictionary['.'];
24
+ }
25
+
18
26
  const data = super.get(key, ...args);
19
- const flatData = key === undefined ? Util.flatten(data) : Util.flatten({ [key]: data });
20
- const apiKeys = Array.from(new Set(Object.keys(flatData).map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean)));
27
+ const mergedData = this.#mergeMergeData(key, data);
28
+ this.set(dataSymbol, mergedData);
29
+ const resolvedData = super.get(dataSymbol);
30
+ this.del(dataSymbol);
31
+ return resolvedData;
32
+ }
21
33
 
22
- apiKeys.forEach((apiKey) => {
23
- Reflect.ownKeys(this.#mergeData).forEach((mergeKey) => {
24
- if (mergeKey === dataSymbol) flatData[apiKey] = { ...this.#mergeData[dataSymbol], ...flatData[apiKey] };
25
- else if (apiKey.startsWith(mergeKey)) flatData[apiKey] = { ...this.#mergeData[mergeKey][dataSymbol], ...flatData[apiKey] };
26
- });
27
- });
34
+ raw(key) {
35
+ const data = Util.get(this.toObject().config, key);
36
+ return this.#mergeMergeData(key, data);
37
+ }
28
38
 
29
- const unflatData = Util.unflatten(flatData);
30
- const rawData = key === undefined ? unflatData : Util.get(unflatData, key);
31
- super.set(dataSymbol, rawData);
32
- return super.get(dataSymbol);
39
+ set(key = '', value) {
40
+ if (key.startsWith?.('.')) return this.resolve({ '.': { [key.substring(1)]: value } });
41
+ return super.set(key, value);
33
42
  }
34
43
 
35
44
  mergeDir(dir = this.#configDir) {
@@ -55,6 +64,23 @@ module.exports = class ConfigClient extends Config {
55
64
  this.del(key);
56
65
  }
57
66
  });
67
+
68
+ return watcher;
69
+ }
70
+
71
+ #mergeMergeData(key, data) {
72
+ const flatData = key === undefined ? Util.flatten(data) : Util.flatten({ [key]: data });
73
+ const apiKeys = Array.from(new Set(Object.keys(flatData).map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean)));
74
+
75
+ apiKeys.forEach((apiKey) => {
76
+ Reflect.ownKeys(this.#mergeData).forEach((mergeKey) => {
77
+ if (mergeKey === dataSymbol) flatData[apiKey] = { ...this.#mergeData[dataSymbol], ...flatData[apiKey] };
78
+ else if (apiKey.startsWith(mergeKey)) flatData[apiKey] = { ...this.#mergeData[mergeKey][dataSymbol], ...flatData[apiKey] };
79
+ });
80
+ });
81
+
82
+ const unflatData = Util.unflatten(flatData);
83
+ return key === undefined ? unflatData : Util.get(unflatData, key);
58
84
  }
59
85
 
60
86
  #ignore({ name, filepath, paths }) {
@@ -79,29 +105,6 @@ module.exports = class ConfigClient extends Config {
79
105
  return { ...parsed, filepath, paths, key };
80
106
  };
81
107
 
82
- // exports.decorateRequest = (mergeData, key, request) => {
83
- // const toMerge = key.split('.').reduce((prev, k, i, arr) => {
84
- // const $key = arr.slice(0, i).join('.');
85
- // return Merge({}, prev, mergeData[$key]?.request);
86
- // }, Merge({}, mergeData.request));
87
-
88
- // return Merge({}, toMerge, request);
89
- // };
90
-
91
- // #get(key, ...rest) {
92
- // const { config } = this.#configClient.toObject();
93
- // const value = get(config, key);
94
-
95
- // if (value?.request) {
96
- // const $request = FetchService.decorateRequest(this.#mergeData, key, value.request);
97
- // const request = this.#configClient.resolve({ vars: value.request.vars }).set(resolveSymbol, $request).get(resolveSymbol);
98
- // this.#configClient.del(resolveSymbol);
99
- // return Merge({}, value, { request });
100
- // }
101
-
102
- // return this.#configClient.get(key, ...rest);
103
- // }
104
-
105
108
  // mergeConfigDir(dir) {
106
109
  // const ignored = (parsed) => {
107
110
  // if (parsed.name.startsWith('.')) return true;
@@ -65,7 +65,7 @@ exports.toCURL = (request, { pretty = true, redactAuth = false } = {}) => {
65
65
  if (redactAuth && /^authorization$/i.test(k)) {
66
66
  hdrs[k] = hdrs[k].replace(/(?<=^.{6}).+/, '***REDACTED***');
67
67
  }
68
- parts.push('-H', shq(`${k}: ${hdrs[k]}`));
68
+ parts.push('-H', exports.shq(`${k}: ${hdrs[k]}`));
69
69
  }
70
70
 
71
71
  // body (skip for GET)
@@ -1,5 +1,5 @@
1
1
  const Readline = require('readline');
2
- const { get, flatten } = require('@coderich/util');
2
+ const Util = require('@coderich/util');
3
3
 
4
4
  exports.createInterface = (cli, configClient) => {
5
5
  const captureInfo = {
@@ -28,13 +28,16 @@ exports.createInterface = (cli, configClient) => {
28
28
  if (lastToken.startsWith('.')) {
29
29
  const api = configClient.get(tokens.at(-2));
30
30
  if (!api?.request) return [[], path];
31
- const dataPath = ['request'].concat(paths.slice(1, -1)).join('.');
32
- const data = get(api, dataPath, {});
33
- return [Object.keys(data).filter(k => k.toLowerCase().startsWith(path.toLowerCase())), path];
31
+ const { config } = configClient.toObject();
32
+ const conf = Util.get(config, tokens.at(-2), {});
33
+ return [Object.keys(conf), path];
34
+ // const dataPath = ['request'].concat(paths.slice(1, -1)).join('.');
35
+ // const data = get(api, dataPath, {});
36
+ // return [Object.keys(data).filter(k => k.toLowerCase().startsWith(path.toLowerCase())), path];
34
37
  }
35
38
 
36
39
  //
37
- const flatKeys = Object.keys(flatten(configClient.get()));
40
+ const flatKeys = Object.keys(Util.flatten(configClient.get()));
38
41
 
39
42
  // These keys follow the typing of the user
40
43
  const startsWithCandidates = Array.from(new Set(flatKeys.map((flatKey) => {
package/src/Sandman.js CHANGED
@@ -14,26 +14,19 @@ module.exports = class Sandman extends EventEmitter {
14
14
  this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
15
15
 
16
16
  this.#readline.on('line', async (line) => {
17
- if (!line) return this.#readline.prompt();
18
- const [cmd, ...args] = line.trim().split(' ');
17
+ if (!line) return this.#prompt();
18
+ const [cmd, ...args] = Sandman.parseArgs(line.trim());
19
19
  const info = this.#cli[cmd] ? { cmd, args } : { cmd: 'run', args: [cmd, ...args] };
20
20
  const value = await Promise.resolve(this.#cli[info.cmd](...info.args)).catch(e => e);
21
21
  return this.emit(cmd, value);
22
22
  });
23
23
 
24
+ this.#configClient.watch();
24
25
  this.#prompt();
25
26
  }
26
27
 
27
28
  cli() {
28
- return Object.defineProperties(this.#cli, {
29
- resolve: {
30
- value: (...args) => {
31
- this.#configClient.resolve(...args);
32
- return this.#cli;
33
- },
34
- configurable: true,
35
- },
36
- });
29
+ return this.#cli;
37
30
  }
38
31
 
39
32
  #run(key) {
@@ -60,14 +53,18 @@ module.exports = class Sandman extends EventEmitter {
60
53
  return this;
61
54
  }
62
55
 
56
+ set(key, value) {
57
+ if (key.startsWith?.('.')) this.resolve({ '.': { [key.substring(1)]: value } });
58
+ return super.set(key, value);
59
+ }
60
+
63
61
  #createCLI() {
64
62
  const self = this;
65
63
 
66
- this.#cli = new Proxy({
67
- run: (...args) => this.#run(...args),
64
+ this.#cli = Object.defineProperties(new Proxy({
65
+ raw: key => this.#configClient.raw(key),
68
66
  get: (...args) => this.#configClient.get(...args),
69
- set: (key = '', value = null) => this.#configClient.set(key, value),
70
- del: (key = '') => this.#configClient.del(key),
67
+ set: (...args) => this.#configClient.set(...args),
71
68
  curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
72
69
  quit: () => process.exit(),
73
70
  }, {
@@ -88,6 +85,36 @@ module.exports = class Sandman extends EventEmitter {
88
85
 
89
86
  return value;
90
87
  },
88
+ }), {
89
+ run: {
90
+ configurable: true,
91
+ value: (...args) => this.#run(...args),
92
+ },
93
+ resolve: {
94
+ configurable: true,
95
+ value: (...args) => {
96
+ this.#configClient.resolve(...args);
97
+ return this.#cli;
98
+ },
99
+ },
91
100
  });
92
101
  }
102
+
103
+ static parseArgs(line) {
104
+ const regex = /"([^"]*)"|'([^']*)'|(\S+)/g;
105
+ const args = [];
106
+ let match;
107
+
108
+ while ((match = regex.exec(line)) !== null) {
109
+ if (match[1] !== undefined) {
110
+ args.push(match[1]); // double-quoted
111
+ } else if (match[2] !== undefined) {
112
+ args.push(match[2]); // single-quoted
113
+ } else if (match[3] !== undefined) {
114
+ args.push(match[3]); // bare word
115
+ }
116
+ }
117
+
118
+ return args;
119
+ }
93
120
  };