@coderich/sandman 0.0.10 → 0.0.11

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.10",
3
+ "version": "0.0.11",
4
4
  "main": "index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -42,7 +42,7 @@ module.exports = class ConfigClient extends Config {
42
42
  return resolvedData;
43
43
  }
44
44
 
45
- raw(key) {
45
+ raw(key = '') {
46
46
  const data = Util.get(this.toObject().config, key);
47
47
  return this.#mergeMergeData(key, data);
48
48
  }
package/src/Sandman.js CHANGED
@@ -1,26 +1,31 @@
1
+ const Path = require('path');
1
2
  const EventEmitter = require('events');
3
+ const { spawn } = require('child_process');
2
4
  const ReadlineService = require('./ReadlineService');
3
5
  const UtilService = require('./UtilService');
4
6
  const FetchService = require('./FetchService');
5
7
  const ConfigClient = require('./ConfigClient');
6
8
 
9
+ const cmdNotFound = Symbol('cmdNotFound');
10
+
7
11
  module.exports = class Sandman extends EventEmitter {
8
- #configClient; #options; #readline; #cli; #cliCounter = 0;
12
+ #configClient; #options; #readline; #cli; #cliCounter = 0; #configDir;
9
13
 
10
14
  constructor(configDir, options) {
11
15
  super();
12
16
  this.#options = options;
13
- this.#configClient = new ConfigClient(configDir);
17
+ this.#configDir = configDir;
18
+ this.#configClient = new ConfigClient(this.#configDir);
14
19
  this.#createCLI();
15
20
  this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
16
- this.#configClient.watch(configDir, event => this.emit('save', event));
21
+ this.#configClient.watch(this.#configDir, event => this.emit('save', event));
17
22
 
18
23
  this.#readline.on('line', async (line) => {
19
24
  if (!line) return this.#prompt();
20
- const [cmd, ...args] = UtilService.parseArgs(line.trim());
21
- const info = this.#cli[cmd] ? { cmd, args } : { cmd: 'run', args: [cmd, ...args] };
22
- const value = await Promise.resolve(this.#cli[info.cmd](...info.args)).catch(e => e);
23
- return this.emit(cmd, value);
25
+ const [cmd, key, ...args] = UtilService.parseArgs(line.trim());
26
+ const $cmd = Object.keys(this.#cli).includes(cmd) ? cmd : cmdNotFound;
27
+ const value = await Promise.resolve(this.#cli[$cmd](key, ...args)).catch(e => e);
28
+ return this.emit($cmd, value);
24
29
  });
25
30
 
26
31
  this.#prompt();
@@ -63,10 +68,17 @@ module.exports = class Sandman extends EventEmitter {
63
68
  const self = this;
64
69
 
65
70
  this.#cli = Object.defineProperties(new Proxy({
66
- raw: key => this.#configClient.raw(key),
67
- get: (...args) => this.#configClient.get(...args),
68
- set: (...args) => this.#configClient.set(...args),
69
- del: (...args) => this.#configClient.del(...args),
71
+ '/': (...args) => this.#run(...args),
72
+ edit: (key, ext = '.yaml') => {
73
+ const path = this.#configClient.get('ide', 'open');
74
+ const filePath = Path.join(this.#configDir, ...key.split('.')).concat('.yaml');
75
+ const child = spawn(path, [filePath], { detached: true, stdio: 'ignore' });
76
+ child.unref();
77
+ },
78
+ show: key => ({
79
+ $: this.#configClient.raw(key),
80
+ [key]: this.#configClient.get(key),
81
+ }),
70
82
  curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
71
83
  quit: () => process.exit(),
72
84
  }, {
@@ -88,14 +100,26 @@ module.exports = class Sandman extends EventEmitter {
88
100
  return value;
89
101
  },
90
102
  }), {
91
- '/': {
103
+ get: {
92
104
  configurable: true,
93
- value: (...args) => this.#run(...args),
105
+ value: (...args) => this.#configClient.get(...args),
106
+ },
107
+ set: {
108
+ configurable: true,
109
+ value: (...args) => this.#configClient.set(...args),
110
+ },
111
+ del: {
112
+ configurable: true,
113
+ value: (...args) => this.#configClient.del(...args),
94
114
  },
95
115
  run: {
96
116
  configurable: true,
97
117
  value: (...args) => this.#run(...args),
98
118
  },
119
+ [cmdNotFound]: {
120
+ configurable: true,
121
+ value: key => this.emit('error', { key, error: 'Command Not Found' }),
122
+ },
99
123
  resolve: {
100
124
  configurable: true,
101
125
  value: (...args) => {