agnostics 0.0.3 → 0.0.5

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.
Files changed (48) hide show
  1. package/lib/bases/_BaseOnCall.js +41 -0
  2. package/lib/bases/_BasePrompt.js +33 -0
  3. package/lib/bases/_BaseRemote.js +118 -0
  4. package/lib/bases/index.js +12 -0
  5. package/lib/helpers/_Yargs.js +28 -57
  6. package/lib/index.js +3 -0
  7. package/lib/loaders/_Loaders.js +62 -0
  8. package/lib/loaders/index.js +9 -0
  9. package/lib/logger/_Logger.js +1 -1
  10. package/lib/logger/index.js +1 -3
  11. package/lib/specification/_Properties.js +23 -8
  12. package/lib/targets/_InquirerTarget.js +15 -0
  13. package/lib/targets/_WebviewTarget.js +182 -0
  14. package/lib/targets/index.js +9 -0
  15. package/lib/workers/_AutoRunner.js +67 -0
  16. package/lib/workers/_AutoWorker.js +136 -0
  17. package/lib/workers/index.js +9 -0
  18. package/package.json +57 -31
  19. package/types/bases/_BaseOnCall.d.ts +7 -0
  20. package/types/bases/_BaseOnCall.d.ts.map +1 -0
  21. package/types/bases/_BasePrompt.d.ts +8 -0
  22. package/types/bases/_BasePrompt.d.ts.map +1 -0
  23. package/types/bases/_BaseRemote.d.ts +13 -0
  24. package/types/bases/_BaseRemote.d.ts.map +1 -0
  25. package/types/bases/index.d.ts +4 -0
  26. package/types/bases/index.d.ts.map +1 -0
  27. package/types/helpers/_Yargs.d.ts +2 -1
  28. package/types/helpers/_Yargs.d.ts.map +1 -1
  29. package/types/loaders/_Loaders.d.ts +8 -0
  30. package/types/loaders/_Loaders.d.ts.map +1 -0
  31. package/types/loaders/index.d.ts +2 -0
  32. package/types/loaders/index.d.ts.map +1 -0
  33. package/types/specification/_Properties.d.ts +41 -35
  34. package/types/targets/_InquirerTarget.d.ts +2 -0
  35. package/types/targets/_InquirerTarget.d.ts.map +1 -0
  36. package/types/targets/_WebviewTarget.d.ts +48 -0
  37. package/types/targets/_WebviewTarget.d.ts.map +1 -0
  38. package/types/targets/index.d.ts +2 -0
  39. package/types/targets/index.d.ts.map +1 -0
  40. package/types/workers/_AutoRunner.d.ts +2 -0
  41. package/types/workers/_AutoRunner.d.ts.map +1 -0
  42. package/types/workers/_AutoWorker.d.ts +17 -0
  43. package/types/workers/_AutoWorker.d.ts.map +1 -0
  44. package/types/workers/index.d.ts +2 -0
  45. package/types/workers/index.d.ts.map +1 -0
  46. package/lib/logger/_ASCII.js +0 -39
  47. package/lib/logger/_System.js +0 -191
  48. package/lib/logger/chars.js +0 -730
@@ -0,0 +1,41 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // BASE //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+ import { CreateLogger } from 'agnostics/logger'
10
+ const { ERR } = CreateLogger( import.meta.url )
11
+
12
+ export class BaseOnCall {
13
+
14
+ className = 'Base'
15
+
16
+ $listenerCbs = {}
17
+
18
+ constructor() {}
19
+
20
+ $call( name, res ) {
21
+ const alls = ['all', '*']
22
+ const includesKey = Object.keys(this.$listenerCbs).includes(name)
23
+ if (!includesKey) return ERR(`NONEXISTENT:`, name)
24
+ for (const callback of this.$listenerCbs[name]) callback(res)
25
+ for (const all of alls) {
26
+ if (this.$listenerCbs[all]) {
27
+ for (const callback of this.$listenerCbs[all]) callback(name, res)
28
+ }
29
+ }
30
+
31
+ }
32
+
33
+ on( name, callback ) {
34
+ const isAll = name === 'all' || name === '*'
35
+ const includesKey = Object.keys(this.$listenerCbs).includes(name)
36
+ if (!includesKey && !isAll) return ERR(`NONEXISTENT:`, name)
37
+ if (!this.$listenerCbs[name]) this.$listenerCbs[name] = []
38
+ this.$listenerCbs[name].push(callback)
39
+ }
40
+
41
+ }
@@ -0,0 +1,33 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // BASE PROMPT //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+ import { CreateLogger } from 'agnostics/logger'
10
+ const { SAY, HMM, ERR, YAY } = CreateLogger( import.meta.url )
11
+
12
+ import { BaseOnCall } from './_BaseOnCall.js'
13
+
14
+ export class BasePrompt extends BaseOnCall {
15
+
16
+ className = 'BasePrompt:BaseOnCall'
17
+
18
+ constructor( ) {
19
+ super()
20
+ }
21
+
22
+ ask( config = { message: '', schema: {} } ) {
23
+
24
+ HMM('ASK NOT IMPLEMENTED:', this.className)
25
+ const { message, schema } = config
26
+ return new Promise( resolve => {
27
+ const res = {}
28
+ for (const [key,value] of Object.entries(schema)) res[key] = true
29
+ resolve(res)
30
+ })
31
+ }
32
+ }
33
+
@@ -0,0 +1,118 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // BASE REMOTE //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+ /*
10
+
11
+ CLASSES MUST HAVE:
12
+
13
+ 1) single config on constructor
14
+ 2) some way to post / receive
15
+ 3)
16
+
17
+
18
+ */
19
+
20
+ import { CreateLogger } from 'agnostics/logger'
21
+ const { SAY, ERR, YAY, HMM, HUH } = CreateLogger( import.meta.url )
22
+
23
+ import { BaseOnCall } from './_BaseOnCall.js'
24
+
25
+ export class BaseRemote extends BaseOnCall {
26
+
27
+ className = 'BaseRemote:BaseOnCall'
28
+
29
+ $methodReqs = {
30
+ }
31
+
32
+ constructor() {
33
+ super()
34
+ this.isIniting = true
35
+ }
36
+
37
+ construct() {}
38
+
39
+ getMethodListenerNames() {
40
+ let methodNames = new Set()
41
+ let current = this
42
+ const exclude = ['getMethodListenerNames', 'construct', 'on']
43
+
44
+ do {
45
+ Object.getOwnPropertyNames(current).forEach(prop => {
46
+ if (prop === 'constructor') return
47
+ const desc = Object.getOwnPropertyDescriptor(current, prop)
48
+ if (!desc || prop[0] === '$' || exclude.includes(prop)) return
49
+ if (
50
+ typeof desc.value === 'function' ||
51
+ typeof desc.get === 'function' ||
52
+ typeof desc.set === 'function'
53
+ ) {
54
+ methodNames.add(prop)
55
+ }
56
+ })
57
+
58
+ current = Object.getPrototypeOf(current)
59
+ } while (current !== null && current !== Object.prototype)
60
+
61
+ methodNames = [...methodNames].sort()
62
+ const listenerNames = Object.keys(this.$listenerCbs || {})
63
+ return { methodNames, listenerNames }
64
+ }
65
+
66
+ $initMethodsAndListeners( methodNames, listenerNames ) {
67
+
68
+ // ========= METHODS =========
69
+
70
+ for (const name of methodNames) {
71
+ this.$methodReqs[name] = []
72
+
73
+ this[name] = (...req) => {
74
+
75
+ const type = 'method'
76
+ const index = this.$methodReqs[name].length
77
+ this.$sendRequest( type, name, req, index )
78
+ return new Promise( resolve => {
79
+ this.$methodReqs[name].push( resolve )
80
+ })
81
+ }
82
+ }
83
+
84
+ // ========= CALLBACKS =========
85
+
86
+ for (const listenerCb of listenerNames) {
87
+ this.$listenerCbs[listenerCb] = []
88
+ }
89
+
90
+ // ========= READY =========
91
+
92
+ YAY('REMOTE:', this.className)
93
+
94
+ HUH('➔ METHODS:', methodNames.join(' '))
95
+ HUH('➔ LISTENERS:', listenerNames.join(' '))
96
+ this.isIniting = false
97
+ }
98
+
99
+ $handleResponse( type, index, name, res ) {
100
+
101
+ if (type === 'method') {
102
+ const resolver = this.$methodReqs[name]?.[index]
103
+ if (!resolver) {
104
+ ERR(`REMOTE NO RESOLVER:`, name, index)
105
+ } else {
106
+ this.$methodReqs[name][index](res)
107
+ this.$methodReqs[name][index] = null
108
+ const allDone = this.$methodReqs[name].every(item => item === null)
109
+ if (allDone) this.$methodReqs[name] = []
110
+ }
111
+ } else if (type === 'listener') {
112
+ this.$call( name, res )
113
+ } else {
114
+ HMM(`UNKNOWN RESPONSE:`, {type,name})
115
+ }
116
+ }
117
+
118
+ }
@@ -0,0 +1,12 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // BASES //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+
10
+ export * from './_BaseOnCall.js'
11
+ export * from './_BasePrompt.js'
12
+ export * from './_BaseRemote.js'
@@ -10,45 +10,15 @@ import { Obj, isArr, isNum, isBool } from '../inputs/index.js'
10
10
  import { NormaliseAccept } from './_Parsers.js'
11
11
 
12
12
  import { CreateLogger } from '../logger/index.js'
13
+ import path from 'path'
13
14
  const { SAY, ERR, HMM, HUH } = CreateLogger( import.meta.url )
14
15
 
15
-
16
- async function LoadYargs() {
17
-
18
- if (!globalThis.yargsLib || !globalThis.yargsHideBin) {
19
- try {
20
- globalThis.yargsLib = (await import ('yargs')).default
21
- globalThis.yargsHideBin = (await import ('yargs/helpers')).hideBin
22
- return true
23
- } catch(err) {
24
- return ERR(`YARGS UNAVAILABLE`, err)
25
- }
26
- }
27
- }
28
-
29
- async function LoadGlobby() {
30
-
31
- if (!globalThis.globbyLib || !globalThis.pathExtname) {
32
- try {
33
- globalThis.globbyLib = (await import ('globby')).globby
34
- globalThis.pathExtname = (await import ('path')).extname
35
- return true
36
- } catch(err) {
37
- return ERR(`GLOBS UNAVAILABLE`, err)
38
- }
39
- }
40
- }
41
-
42
16
  async function GetYargsFiles( value, extensions ) {
43
17
 
44
18
  try {
45
-
46
- if (!(await LoadGlobby())) return []
47
-
48
-
49
19
  const anything = (extensions.length === 0 || extensions.includes('*'))
50
20
 
51
- const found = await globalThis.globbyLib(value, {
21
+ const found = await globalThis.Yargs(value, {
52
22
  onlyFiles: true,
53
23
  expandDirectories: false,
54
24
  caseSensitiveMatch: false
@@ -56,7 +26,7 @@ async function GetYargsFiles( value, extensions ) {
56
26
 
57
27
  const exts = extensions.map( ext => ext.toLowerCase().replaceAll('.',''))
58
28
  const files = anything ? found : found.filter(file => {
59
- const ext = globalThis.pathExtname(file).replaceAll('.','')
29
+ const ext = path.extname(file).replaceAll('.','')
60
30
  return exts.includes(ext)
61
31
  })
62
32
 
@@ -111,14 +81,17 @@ export function ConvertYargsProperty( def, override = {} ) {
111
81
 
112
82
  const array = def.gui == 'files' || isArr(def.type)
113
83
 
114
- return Object.assign({
84
+ const yargsDef = Object.assign({
115
85
  array,
116
86
  describe: def.description,
117
87
  type: GetYargsType(def.type),
118
- coerce: def.oncheck,
88
+ coerce: def.onsend,
119
89
  default: def.default,
120
- accept: NormaliseAccept( def.accept )
90
+ accept: NormaliseAccept( def.accept ),
91
+ choices: def.enum
121
92
  }, override)
93
+
94
+ return yargsDef
122
95
  }
123
96
 
124
97
 
@@ -136,7 +109,7 @@ export function YargsProcessWorkaround() {
136
109
 
137
110
  export class YargsCommand {
138
111
 
139
- command = '$0'
112
+ command = null
140
113
  description = 'N/A'
141
114
  version = 'N/A'
142
115
  positionals = {}
@@ -146,11 +119,13 @@ export class YargsCommand {
146
119
  $options = {}
147
120
  $aliasStore = []
148
121
 
149
- constructor( { positionals, options = {}, description = 'N/A', version = 'N/A' } ) {
150
-
122
+ constructor(
123
+ { positionals, options = {}, description = 'N/A', version = 'N/A' },
124
+ runCallback = async options => {} ) {
151
125
 
152
126
  this.description = description
153
127
  this.version = version
128
+ this.runCallback = runCallback
154
129
 
155
130
  // --------- OPTIONS ---------
156
131
 
@@ -187,17 +162,14 @@ export class YargsCommand {
187
162
 
188
163
  }
189
164
 
190
- async run( runCallback = async yargsData => {} ) {
191
-
192
- if (!(await LoadYargs())) return
165
+ async run() {
193
166
 
194
- YargsProcessWorkaround()
195
167
 
196
- const args = globalThis.yargsHideBin( process.argv )
197
- const instance = globalThis.yargsLib( args )
168
+ const args = globalThis.YargsBin( process.argv )
169
+ const instance = globalThis.Yargs( args )
198
170
 
199
171
  instance.command(
200
- this.command,
172
+ '$0 '+ this.command + ' [run]', // hack
201
173
  this.description,
202
174
  yargsData => {
203
175
  const positionals = Object.entries(this.positionals)
@@ -218,7 +190,7 @@ export class YargsCommand {
218
190
  }
219
191
  }
220
192
 
221
- await runCallback( yargsData )
193
+ if (this.runCallback) await this.runCallback( yargsData )
222
194
 
223
195
  }
224
196
  )
@@ -248,7 +220,9 @@ export class YargsCLI {
248
220
  $options = {}
249
221
  $commands = {}
250
222
 
251
- constructor( { commands, options = {}, description = 'N/A', version = 'N/A' } ) {
223
+ constructor(
224
+ { commands, options = {}, description = 'N/A', version = 'N/A' },
225
+ runCallback = async options => {} ) {
252
226
 
253
227
  if (typeof commands != 'object') throw Error('no commands defined')
254
228
  if (!description) HMM('no description defined (recommended)')
@@ -256,6 +230,7 @@ export class YargsCLI {
256
230
 
257
231
  this.description = description
258
232
  this.version = version
233
+ this.runCallback = runCallback
259
234
 
260
235
  // --------- OPTIONS ---------
261
236
 
@@ -268,15 +243,10 @@ export class YargsCLI {
268
243
 
269
244
  }
270
245
 
271
- async run( runCallback = async (cmdKey, yargsData) => {} ) {
272
-
273
- if (!(await LoadYargs())) return
274
-
275
- // YargsProcessWorkaround()
276
-
246
+ async run() {
277
247
 
278
- const args = globalThis.yargsHideBin( process.argv )
279
- const instance = globalThis.yargsLib( args )
248
+ const args = globalThis.YargsBin( process.argv )
249
+ const instance = globalThis.Yargs( args )
280
250
 
281
251
  for (const [cmdKey, cmdObj] of Object.entries(this.$commands)) {
282
252
 
@@ -303,7 +273,8 @@ export class YargsCLI {
303
273
  }
304
274
  }
305
275
 
306
- await runCallback( cmdKey, yargsData )
276
+ if (cmdObj.runCallback) await cmdObj.runCallback( yargsData )
277
+ if (this.runCallback) await this.runCallback( yargsData )
307
278
 
308
279
  })
309
280
  }
package/lib/index.js CHANGED
@@ -6,6 +6,9 @@
6
6
  // //
7
7
  //////////////////////////////////////////
8
8
 
9
+ import { fileURLToPath } from 'node:url'
10
+ import { dirname } from 'node:path'
11
+
9
12
  export * from './helpers/index.js'
10
13
  export * from './inputs/index.js'
11
14
  export * from './specification/index.js'
@@ -0,0 +1,62 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // LOADERS //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+ import { CreateLogger } from 'agnostics/logger'
10
+ const { YAY, ERR, SAY } = CreateLogger( import.meta.url )
11
+
12
+ import path from 'path'
13
+ import fs from 'fs/promises'
14
+ import { pathToFileURL } from 'node:url'
15
+
16
+ export async function LoadDep( name, url ) {
17
+
18
+ const [ base, stem ] = url.split('.')
19
+ let mod
20
+
21
+ if (!globalThis[base]) {
22
+ try {
23
+ mod = (await import (base))
24
+ } catch(err) {
25
+ try {
26
+ const pkgDir = path.join(process.cwd(), 'node_modules', base)
27
+ const pkgJsonPath = path.join(pkgDir, 'package.json')
28
+ const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath))
29
+ let entry = pkgJson.main || pkgJson.module || 'index.js'
30
+ if (pkgJson.exports && pkgJson.exports['.']) {
31
+ const exportIndex = pkgJson.exports['.']?.index || pkgJson.exports['.']
32
+ entry = exportIndex.replace(/^\.\//, '')
33
+ }
34
+ const fullEntryPath = path.join(pkgDir, entry)
35
+ const entryUrl = pathToFileURL(fullEntryPath).href
36
+ mod = (await import (entryUrl))
37
+ } catch(err) {
38
+ return ERR(`FAIL:`, name, err.message)
39
+ }
40
+ }
41
+ if (stem) mod = mod[stem]
42
+ globalThis[name] = mod
43
+ YAY(`LOADED:`, name)
44
+ return globalThis[name]
45
+ }
46
+ }
47
+
48
+ export async function LoadWebview() {
49
+ return await LoadDep('Webview', '@webviewjs/webview.Application')
50
+ }
51
+
52
+ export async function LoadYargs() {
53
+
54
+ const YargsLib = await LoadDep('Yargs', 'yargs.default')
55
+ const YargsBin = await LoadDep('YargsBin', 'yargs/helpers.hideBin')
56
+
57
+ return { YargsLib, YargsBin }
58
+ }
59
+
60
+ export async function LoadGlobby() {
61
+ return await LoadDep('Globby', 'globby.globby')
62
+ }
@@ -0,0 +1,9 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // LOADERS //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+ export * from './_Loaders.js'
@@ -51,7 +51,7 @@ function CreateMethod( method, name, filename, timestamp, prepend, color, log, t
51
51
 
52
52
  } else {
53
53
 
54
- const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
54
+ const isDarkMode = typeof window !== 'undefined' && window.matchMedia?.('(prefers-color-scheme: dark)')?.matches
55
55
  const cssColor = isDarkMode ? ANSIDark[color] : ANSILight[color]
56
56
 
57
57
  const parts = [`%c${ts}${label}${prepend || ''}`, `color:${cssColor}`]
@@ -1,5 +1,3 @@
1
1
  export * from './_Defs.js'
2
2
  export * from './_Settings.js'
3
- export * from './_Logger.js'
4
- export * from './_System.js'
5
- export * from './_ASCII.js'
3
+ export * from './_Logger.js'
@@ -8,6 +8,29 @@
8
8
 
9
9
  export const Properties = {
10
10
 
11
+ // --------- EVENTS ---------
12
+
13
+ onsend: {
14
+ description: 'callback for checking value before sending',
15
+ matches: [ 'oncheck', 'validate', 'coerce', 'check']
16
+ },
17
+ onreceive: {
18
+ description: 'callback for checking value when received',
19
+ matches: [ 'onreceive', 'validate', 'coerce', 'check']
20
+ },
21
+ onupdate: {
22
+ description: 'callback for updated value',
23
+ matches: [ 'onupdate', 'update', 'change', 'onchange']
24
+ },
25
+ onencode: {
26
+ description: 'callback for encoding (sending origin)',
27
+ matches: [ 'onencode', 'encode']
28
+ },
29
+ ondecode: {
30
+ description: 'callback for decoding (receiving end)',
31
+ matches: [ 'ondecode', 'decode']
32
+ },
33
+
11
34
  // --------- DEFINITION ---------
12
35
 
13
36
  type: {
@@ -68,14 +91,6 @@ export const Properties = {
68
91
  description: 'property can only be read',
69
92
  matches: ['readonly', 'ro']
70
93
  },
71
- oncheck: {
72
- description: 'callback for checking value',
73
- matches: [ 'oncheck', 'validate', 'coerce', 'check']
74
- },
75
- onupdate: {
76
- description: 'callback for updated value',
77
- matches: [ 'onupdate', 'update', 'change', 'onchange']
78
- },
79
94
 
80
95
  // --------- STRUCTURAL ---------
81
96
 
@@ -0,0 +1,15 @@
1
+
2
+
3
+ // @inquirer/prompts
4
+
5
+ async function LoadInquirer() {
6
+
7
+ if (!globalThis.inquirerLib) {
8
+ try {
9
+ globalThis.inquirerLib = (await import ('inquirer')).inquirer
10
+ return true
11
+ } catch(err) {
12
+ return ERR(`GLOBS UNAVAILABLE`, err)
13
+ }
14
+ }
15
+ }