@plugjs/plug 0.4.0 → 0.4.2

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 (72) hide show
  1. package/dist/asserts.cjs +1 -1
  2. package/dist/asserts.cjs.map +1 -1
  3. package/dist/asserts.mjs +1 -1
  4. package/dist/asserts.mjs.map +1 -1
  5. package/dist/async.cjs +5 -20
  6. package/dist/async.cjs.map +2 -2
  7. package/dist/async.mjs +5 -20
  8. package/dist/async.mjs.map +2 -2
  9. package/dist/build.cjs +95 -70
  10. package/dist/build.cjs.map +2 -2
  11. package/dist/build.d.ts +6 -8
  12. package/dist/build.mjs +92 -67
  13. package/dist/build.mjs.map +2 -2
  14. package/dist/cli.mjs +1 -1
  15. package/dist/files.cjs +5 -3
  16. package/dist/files.cjs.map +1 -1
  17. package/dist/files.d.ts +2 -1
  18. package/dist/files.mjs +11 -4
  19. package/dist/files.mjs.map +1 -1
  20. package/dist/helpers.cjs +3 -4
  21. package/dist/helpers.cjs.map +1 -1
  22. package/dist/helpers.mjs +3 -4
  23. package/dist/helpers.mjs.map +1 -1
  24. package/dist/logging/options.cjs +3 -10
  25. package/dist/logging/options.cjs.map +1 -1
  26. package/dist/logging/options.d.ts +44 -1
  27. package/dist/logging/options.mjs +3 -10
  28. package/dist/logging/options.mjs.map +1 -1
  29. package/dist/plugs/build.cjs +4 -7
  30. package/dist/plugs/build.cjs.map +1 -1
  31. package/dist/plugs/build.mjs +2 -5
  32. package/dist/plugs/build.mjs.map +1 -1
  33. package/dist/plugs/debug.cjs +7 -9
  34. package/dist/plugs/debug.cjs.map +1 -1
  35. package/dist/plugs/debug.mjs +8 -10
  36. package/dist/plugs/debug.mjs.map +1 -1
  37. package/dist/types.cjs +12 -0
  38. package/dist/types.cjs.map +1 -1
  39. package/dist/types.d.ts +33 -9
  40. package/dist/types.mjs +5 -0
  41. package/dist/types.mjs.map +2 -2
  42. package/dist/utils/diff.cjs +1 -4
  43. package/dist/utils/diff.cjs.map +1 -1
  44. package/dist/utils/diff.mjs +1 -4
  45. package/dist/utils/diff.mjs.map +1 -1
  46. package/dist/utils/{types.cjs → singleton.cjs} +14 -13
  47. package/dist/utils/singleton.cjs.map +6 -0
  48. package/dist/utils/singleton.d.ts +12 -0
  49. package/dist/utils/singleton.mjs +13 -0
  50. package/dist/utils/singleton.mjs.map +6 -0
  51. package/dist/utils.cjs +2 -2
  52. package/dist/utils.cjs.map +1 -1
  53. package/dist/utils.d.ts +1 -1
  54. package/dist/utils.mjs +1 -1
  55. package/package.json +1 -1
  56. package/src/asserts.ts +1 -1
  57. package/src/async.ts +6 -29
  58. package/src/build.ts +151 -117
  59. package/src/files.ts +14 -6
  60. package/src/helpers.ts +3 -4
  61. package/src/logging/options.ts +4 -13
  62. package/src/plugs/build.ts +2 -15
  63. package/src/plugs/debug.ts +10 -9
  64. package/src/types.ts +52 -23
  65. package/src/utils/diff.ts +1 -6
  66. package/src/utils/singleton.ts +19 -0
  67. package/src/utils.ts +1 -1
  68. package/dist/utils/types.cjs.map +0 -6
  69. package/dist/utils/types.d.ts +0 -4
  70. package/dist/utils/types.mjs +0 -12
  71. package/dist/utils/types.mjs.map +0 -6
  72. package/src/utils/types.ts +0 -11
package/src/types.ts CHANGED
@@ -38,21 +38,23 @@ export interface State {
38
38
  * The {@link Task} interface normalizes a task definition, associating it with
39
39
  * its build file, its sibling {@link Task}s and available _properties_.
40
40
  */
41
- export interface Task<T extends Result = Result, P extends Props = Record<string, string>> {
42
- /** Simply invoke this task stand alone */
43
- (props?: Partial<P>): Promise<T>
41
+ export interface Task<T extends Result = Result> {
42
+ /** The unique ID of this {@link Task} */
43
+ readonly id: number,
44
+ /** The _original_ name of this task */
45
+ readonly name: string
44
46
  /** All _properties_ siblings to this {@link Task} */
45
47
  readonly props: Props
46
48
  /** All {@link Tasks} sibling to this {@link Task} */
47
49
  readonly tasks: Tasks
48
50
  /** The absolute file name where this {@link Task} was defined */
49
51
  readonly buildFile: AbsolutePath,
50
- /** Invoke a task from in the context of a {@link Build} */
51
- invoke(state: State, taskName: string): Promise<T>
52
52
  /** Other {@link Task}s hooked _before_ this one */
53
53
  readonly before: Task[]
54
54
  /** Other {@link Task}s hooked _after_ this one */
55
55
  readonly after: Task[]
56
+ /** Invoke a task from in the context of a {@link Build} */
57
+ invoke(state: State, taskName: string): Promise<T>
56
58
  }
57
59
 
58
60
  /**
@@ -64,6 +66,12 @@ export type TaskResult = Pipe | Files | void | undefined
64
66
  /** The {@link TaskDef} type identifies the _definition_ of a task. */
65
67
  export type TaskDef<R extends TaskResult = TaskResult> = () => R | Promise<R>
66
68
 
69
+ /** A callable, compiled {@link Task} from a {@link TaskDef} */
70
+ export type TaskCall<D extends BuildDef = BuildDef, R extends Result = Result> = {
71
+ (props?: Partial<Props<D>>): Promise<R>
72
+ task: Task<R>
73
+ }
74
+
67
75
  /* ========================================================================== *
68
76
  * TASKS AND PROPERTIES *
69
77
  * ========================================================================== */
@@ -75,12 +83,12 @@ export type Props<D extends BuildDef = BuildDef> = {
75
83
 
76
84
  /** A type identifying all _tasks_ in a {@link Build} */
77
85
  export type Tasks<D extends BuildDef = BuildDef> = {
78
- readonly [ k in string & keyof D as D[k] extends TaskDef | Task ? k : never ] :
86
+ readonly [ k in string & keyof D as D[k] extends TaskDef | TaskCall ? k : never ] :
79
87
  D[k] extends TaskDef<infer R> ?
80
- R extends void | undefined ? Task<undefined, Props<D>> :
81
- R extends Pipe | Files ? Task<Files, Props<D>> :
88
+ R extends void | undefined ? TaskCall<D, undefined> :
89
+ R extends Pipe | Files ? TaskCall<D, Files> :
82
90
  never :
83
- D[k] extends Task ? D[k] :
91
+ D[k] extends TaskCall ? D[k] :
84
92
  never
85
93
  }
86
94
 
@@ -93,7 +101,7 @@ export type Tasks<D extends BuildDef = BuildDef> = {
93
101
  * all its properties and tasks.
94
102
  */
95
103
  export interface BuildDef {
96
- [ k : string ] : string | TaskDef | Task
104
+ [ k : string ] : string | TaskDef | TaskCall
97
105
  }
98
106
 
99
107
  /**
@@ -101,24 +109,45 @@ export interface BuildDef {
101
109
  * {@link TaskDef | task definitions }.
102
110
  */
103
111
  export type ThisBuild<D extends BuildDef> = {
104
- readonly [ k in keyof D ] :
105
- k extends string ?
106
- D[k] extends TaskDef<infer R> ?
107
- R extends Promise<undefined> | void | undefined ? () => Promise<undefined> :
108
- R extends Pipe | Files ? () => Pipe :
109
- never :
110
- D[k] extends Task<infer R> ?
111
- R extends undefined ? () => Promise<undefined> :
112
- R extends Files ? () => Pipe :
113
- never :
114
- D[k] extends string ?
115
- string :
112
+ readonly [ k in keyof D as k extends string ? k : never ] :
113
+ D[k] extends TaskDef<infer R> ?
114
+ R extends Promise<undefined> | void | undefined ? () => Promise<undefined> :
115
+ R extends Pipe | Files ? () => Pipe :
116
+ never :
117
+ D[k] extends TaskCall<any, infer R> ?
118
+ R extends undefined ? () => Promise<undefined> :
119
+ R extends Files ? () => Pipe :
116
120
  never :
121
+ D[k] extends string ?
122
+ string :
117
123
  never
118
124
  }
119
125
 
126
+ /**
127
+ * Symbol indicating that an object is a {@link Build}.
128
+ *
129
+ * In a compiled {@link Build} this symbol will be associated with a function
130
+ * taking an array of strings (task names) and record of props to override
131
+ */
132
+ export const buildMarker = Symbol.for('plugjs:plug:types:Build')
133
+
120
134
  /**
121
135
  * The {@link Build} type represents the collection of {@link Task}s
122
136
  * and _properties_ compiled from a {@link BuildDef | build definition}.
123
137
  */
124
- export type Build<D extends BuildDef = BuildDef> = Tasks<D> & Props<D>
138
+ export type Build<D extends BuildDef = BuildDef> = Props<D> & Tasks<D> & {
139
+ readonly [buildMarker]: (
140
+ tasks: readonly string[],
141
+ props?: Record<string, string | undefined>,
142
+ ) => Promise<void>
143
+ }
144
+
145
+ /** A type identifying all _task names_ in a {@link Build}. */
146
+ export type BuildTasks<B extends Build> = string & keyof {
147
+ [ name in keyof B as B[name] extends Function ? name : never ] : any
148
+ }
149
+
150
+ /** A type identifying a subset of _properties_ for a {@link Build}. */
151
+ export type BuildProps<B extends Build> = {
152
+ [ name in keyof B as B[name] extends string ? name : never ]? : string
153
+ }
package/src/utils/diff.ts CHANGED
@@ -3,7 +3,6 @@ import { inspect, isDeepStrictEqual } from 'node:util'
3
3
 
4
4
  import { assert } from '../asserts'
5
5
  import { $grn, $red, logOptions } from '../logging'
6
- import { getTypeOf } from './types'
7
6
 
8
7
  import type { InspectOptions } from 'node:util'
9
8
 
@@ -326,13 +325,9 @@ export function textDiff(
326
325
  let lhsLines: string[]
327
326
  let rhsLines: string[]
328
327
 
329
- // Get the _real_ types of both arguments
330
- const lhsType = getTypeOf(lhs)
331
- const rhsType = getTypeOf(rhs)
332
-
333
328
  // If _both_ arguments are strings, then just split and compare, otherwise
334
329
  // we nuse NodeJS' inspect to prep their string version
335
- if ((lhsType === 'string') && (rhsType === 'string')) {
330
+ if ((typeof lhs === 'string') && (typeof rhs === 'string')) {
336
331
  lhsLines = lhs.split('\n')
337
332
  rhsLines = rhs.split('\n')
338
333
  } else {
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Get the instance of a _singleton_ variable.
3
+ *
4
+ * Sometimes we need unique instances _per process_ (for example our async
5
+ * context). The problem is that the code might get called from two (or three)
6
+ * different versions of this file: the .cjs transpiled code, the .mjs
7
+ * transpiled one, or the .ts dynamically transpiled by our dynamic loader.
8
+ *
9
+ * A _singleton_ associates an instance with a symbol in `globalThis` and ensure
10
+ * there is only _one_ instance per process (per `globalThis`).
11
+ */
12
+ export function getSingleton<T>(symbol: symbol, factory: () => T): T {
13
+ const anyGlobalThis = globalThis as any
14
+ if (anyGlobalThis[symbol]) return anyGlobalThis[symbol] as T
15
+
16
+ const value = factory()
17
+ Object.defineProperty(anyGlobalThis, symbol, { value })
18
+ return value
19
+ }
package/src/utils.ts CHANGED
@@ -2,5 +2,5 @@ export * from './utils/diff'
2
2
  export * from './utils/exec'
3
3
  export * from './utils/match'
4
4
  export * from './utils/options'
5
- export * from './utils/types'
5
+ export * from './utils/singleton'
6
6
  export * from './utils/walk'
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/utils/types.ts"],
4
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,SAAS,UAAU,MAA0B;AAClD,MAAI,MAAM,QAAQ,IAAI;AAAG,WAAO;AAChC,MAAI,SAAS;AAAM,WAAO;AAC1B,SAAO,OAAO;AAChB;",
5
- "names": []
6
- }
@@ -1,4 +0,0 @@
1
- /** A type adding the values `null` or `array` to the result of `typeof` */
2
- export type BasicType = 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'null' | 'array';
3
- /** Get the _real_ type of a value, including `null` or `array` */
4
- export declare function getTypeOf(what: unknown): BasicType;
@@ -1,12 +0,0 @@
1
- // utils/types.ts
2
- function getTypeOf(what) {
3
- if (Array.isArray(what))
4
- return "array";
5
- if (what === null)
6
- return "null";
7
- return typeof what;
8
- }
9
- export {
10
- getTypeOf
11
- };
12
- //# sourceMappingURL=types.mjs.map
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/utils/types.ts"],
4
- "mappings": ";AAMO,SAAS,UAAU,MAA0B;AAClD,MAAI,MAAM,QAAQ,IAAI;AAAG,WAAO;AAChC,MAAI,SAAS;AAAM,WAAO;AAC1B,SAAO,OAAO;AAChB;",
5
- "names": []
6
- }
@@ -1,11 +0,0 @@
1
- /** A type adding the values `null` or `array` to the result of `typeof` */
2
- export type BasicType =
3
- | 'string' | 'number' | 'bigint' | 'boolean' | 'symbol'
4
- | 'undefined' | 'object' | 'function' | 'null' | 'array'
5
-
6
- /** Get the _real_ type of a value, including `null` or `array` */
7
- export function getTypeOf(what: unknown): BasicType {
8
- if (Array.isArray(what)) return 'array'
9
- if (what === null) return 'null'
10
- return typeof what
11
- }