@plugjs/plug 0.1.4 → 0.2.1

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 (115) hide show
  1. package/README.md +2 -2
  2. package/cli/plug.mjs +1397 -0
  3. package/cli/ts-loader.mjs +273 -0
  4. package/cli/tsrun.mjs +136 -0
  5. package/dist/asserts.cjs +1 -0
  6. package/dist/asserts.cjs.map +1 -1
  7. package/dist/asserts.mjs +1 -0
  8. package/dist/asserts.mjs.map +1 -1
  9. package/dist/build.cjs +51 -24
  10. package/dist/build.cjs.map +2 -2
  11. package/dist/build.d.ts +7 -3
  12. package/dist/build.mjs +50 -24
  13. package/dist/build.mjs.map +2 -2
  14. package/dist/files.cjs +9 -0
  15. package/dist/files.cjs.map +1 -1
  16. package/dist/files.mjs +9 -0
  17. package/dist/files.mjs.map +1 -1
  18. package/dist/fork.cjs +2 -0
  19. package/dist/fork.cjs.map +1 -1
  20. package/dist/fork.mjs +2 -0
  21. package/dist/fork.mjs.map +1 -1
  22. package/dist/fs.cjs +11 -4
  23. package/dist/fs.cjs.map +1 -1
  24. package/dist/fs.mjs +7 -4
  25. package/dist/fs.mjs.map +1 -1
  26. package/dist/helpers.cjs +1 -1
  27. package/dist/helpers.cjs.map +1 -1
  28. package/dist/helpers.mjs +1 -1
  29. package/dist/helpers.mjs.map +1 -1
  30. package/dist/index.cjs +7 -0
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.d.ts +6 -0
  33. package/dist/index.mjs +2 -0
  34. package/dist/index.mjs.map +1 -1
  35. package/dist/logging/levels.cjs.map +1 -1
  36. package/dist/logging/levels.mjs.map +1 -1
  37. package/dist/logging/logger.cjs.map +1 -1
  38. package/dist/logging/logger.mjs.map +1 -1
  39. package/dist/logging/options.cjs +5 -0
  40. package/dist/logging/options.cjs.map +1 -1
  41. package/dist/logging/options.mjs +5 -0
  42. package/dist/logging/options.mjs.map +1 -1
  43. package/dist/logging/report.cjs.map +1 -1
  44. package/dist/logging/report.mjs.map +1 -1
  45. package/dist/logging/spinner.cjs +12 -0
  46. package/dist/logging/spinner.cjs.map +1 -1
  47. package/dist/logging/spinner.mjs +12 -0
  48. package/dist/logging/spinner.mjs.map +1 -1
  49. package/dist/paths.cjs.map +1 -1
  50. package/dist/paths.mjs.map +1 -1
  51. package/dist/pipe.cjs +34 -0
  52. package/dist/pipe.cjs.map +1 -1
  53. package/dist/pipe.mjs +34 -0
  54. package/dist/pipe.mjs.map +1 -1
  55. package/dist/plugs/debug.cjs.map +1 -1
  56. package/dist/plugs/debug.mjs.map +1 -1
  57. package/dist/plugs/edit.cjs +1 -1
  58. package/dist/plugs/edit.cjs.map +1 -1
  59. package/dist/plugs/edit.d.ts +2 -2
  60. package/dist/plugs/edit.mjs +1 -1
  61. package/dist/plugs/edit.mjs.map +1 -1
  62. package/dist/plugs/esbuild/fix-extensions.cjs +4 -0
  63. package/dist/plugs/esbuild/fix-extensions.cjs.map +1 -1
  64. package/dist/plugs/esbuild/fix-extensions.mjs.map +1 -1
  65. package/dist/plugs/esbuild.cjs +6 -2
  66. package/dist/plugs/esbuild.cjs.map +1 -1
  67. package/dist/plugs/esbuild.mjs +6 -2
  68. package/dist/plugs/esbuild.mjs.map +1 -1
  69. package/dist/plugs/exec.cjs.map +1 -1
  70. package/dist/plugs/exec.mjs.map +1 -1
  71. package/dist/plugs/exports.cjs +122 -0
  72. package/dist/plugs/exports.cjs.map +6 -0
  73. package/dist/plugs/exports.d.ts +17 -0
  74. package/dist/plugs/exports.mjs +105 -0
  75. package/dist/plugs/exports.mjs.map +6 -0
  76. package/dist/plugs/filter.cjs.map +1 -1
  77. package/dist/plugs/filter.mjs.map +1 -1
  78. package/dist/plugs.cjs +1 -0
  79. package/dist/plugs.cjs.map +1 -1
  80. package/dist/plugs.d.ts +1 -0
  81. package/dist/plugs.mjs +1 -0
  82. package/dist/plugs.mjs.map +1 -1
  83. package/dist/types.d.ts +5 -1
  84. package/dist/utils/exec.cjs +9 -0
  85. package/dist/utils/exec.cjs.map +1 -1
  86. package/dist/utils/exec.mjs +5 -0
  87. package/dist/utils/exec.mjs.map +1 -1
  88. package/dist/utils/match.cjs +5 -0
  89. package/dist/utils/match.cjs.map +1 -1
  90. package/dist/utils/match.mjs +1 -0
  91. package/dist/utils/match.mjs.map +1 -1
  92. package/dist/utils/options.cjs.map +1 -1
  93. package/dist/utils/options.mjs.map +1 -1
  94. package/dist/utils/walk.cjs.map +1 -1
  95. package/dist/utils/walk.mjs.map +1 -1
  96. package/extra/plug.mts +373 -0
  97. package/extra/ts-loader.mts +545 -0
  98. package/extra/tsrun.mts +64 -0
  99. package/extra/utils.ts +168 -0
  100. package/package.json +9 -19
  101. package/src/build.ts +66 -33
  102. package/src/fork.ts +1 -0
  103. package/src/helpers.ts +1 -1
  104. package/src/index.ts +7 -0
  105. package/src/logging/spinner.ts +2 -0
  106. package/src/plugs/edit.ts +4 -3
  107. package/src/plugs/esbuild.ts +0 -1
  108. package/src/plugs/exports.ts +175 -0
  109. package/src/plugs.ts +1 -0
  110. package/src/types.ts +5 -1
  111. package/types/plugjs.d.ts +0 -2
  112. package/LICENSE.md +0 -211
  113. package/NOTICE.md +0 -13
  114. package/extra/cli.mjs +0 -1356
  115. package/extra/ts-loader.mjs +0 -223
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/utils/walk.ts"],
4
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAA+B;AAE/B,gBAA8B;AAC9B,qBAAwB;AACxB,mBAAoC;AACpC,mBAAsB;AAkCf,SAAS,KACZ,WACA,OACA,UAAuB,CAAC,GACU;AACpC,QAAM;AAAA,IACJ,WAAW;AAAA,IACX,iBAAiB;AAAA,IACjB,mBAAmB;AAAA,OAChB;AAAA,EACL,IAAI;AAGJ,QAAM,cAAc,CAAC,QAA+B;AAGlD,QAAI,QAAQ;AAAW,aAAO;AAC9B,UAAM,WAA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AM,oBAAoB,WAAW,QAAQ;AACnD,MAAI,CAAE,YAAY,GAAG;AAAG;AACxB,MAAI,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAEtC,MAAI;AACJ,MAAI;AACF,cAAU,MAAM,QAAQ,GAAG;AAAA,EAC7B,SAAS,OAAP;AACA,QAAI,MAAM,SAAS;AAAU,YAAM;AACnC,QAAI,KAAK,aAAa,GAAG,GAAG,GAAG,WAAW;AAC1C;AAAA,EACF;AAGA,mBAAiB,UAAU,SAAS;AAClC,UAAM,OAAO,KAAK,UAAU,OAAO,IAAI;AAGvC,QAAI,OAAO,OAAO,KAAK,gBAAgB,IAAI;AAAG,YAAM;AAAA,aAG3C,OAAO,YAAY,KAAM,QAAQ,UAAW;AACnD,YAAM,WAAW,OAAO,EAAE,GAAG,MAAM,UAAU,MAAM,OAAO,QAAQ,EAAE,CAAC;AACrE,uBAAiB,SAAS;AAAU,cAAM;AAAA,IAG5C,WAAW,OAAO,eAAe,KAAK,gBAAgB;AACpD,YAAM,OAAO,MAAM,KAAK,KAAK,WAAW,IAAI,CAAC;AAG7C,UAAI,KAAK,OAAO,KAAK,gBAAgB,IAAI;AAAG,cAAM;AAAA,eAGzC,KAAK,YAAY,KAAM,QAAQ,UAAW;AACjD,cAAM,WAAW,OAAO,EAAE,GAAG,MAAM,UAAU,MAAM,OAAO,QAAQ,EAAE,CAAC;AACrE,yBAAiB,SAAS;AAAU,gBAAM;AAAA,MAC5C;AAAA,IACF;AAAA,EACF;AACF;",
5
5
  "names": []
6
6
  }
package/extra/plug.mts ADDED
@@ -0,0 +1,373 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+
4
+ import _fs from 'node:fs'
5
+ import _path from 'node:path'
6
+
7
+ import _yargs from 'yargs-parser'
8
+
9
+ import { $blu, $gry, $rst, $tsk, $und, $wht, isDirectory, isFile, main, version } from './utils.js'
10
+
11
+ import type { BuildFailure } from '../src/asserts.js'
12
+ import type { Build } from '../src/index.js'
13
+
14
+ /* ========================================================================== *
15
+ * ========================================================================== *
16
+ * BUILD INSPECTION *
17
+ * ========================================================================== *
18
+ * ========================================================================== */
19
+
20
+ /** Symbol indicating that an object is a Build */
21
+ const buildMarker = Symbol.for('plugjs:isBuild')
22
+
23
+ /** Symbol indicating that an object is a Build Failure */
24
+ const buildFailure = Symbol.for('plugjs:buildFailure')
25
+
26
+ /** Check if the specified build is actually a {@link Build} */
27
+ function isBuild(build: any): build is Build<Record<string, any>> & {
28
+ [buildMarker]: (tasks: string[], props?: Record<string, string | undefined>) => Promise<void>
29
+ } {
30
+ return build && typeof build[buildMarker] === 'function'
31
+ }
32
+
33
+ /** Check if the specified argument is a {@link BuildFailure} */
34
+ function isBuildFailure(arg: any): arg is BuildFailure {
35
+ return arg && arg[buildFailure] === buildFailure
36
+ }
37
+
38
+
39
+ /* ========================================================================== *
40
+ * ========================================================================== *
41
+ * PARSE COMMAND LINE ARGUMENTS *
42
+ * ========================================================================== *
43
+ * ========================================================================== */
44
+
45
+ /* Parsed and normalised command line options */
46
+ interface CommandLineOptions {
47
+ buildFile: string,
48
+ watchDirs: string[],
49
+ tasks: string[],
50
+ props: Record<string, string>
51
+ listOnly: boolean,
52
+ }
53
+
54
+ /** Parse `perocess.argv` and return our normalised command line options */
55
+ export function parseCommandLine(args: string[]): CommandLineOptions {
56
+ /* Yargs-parse our arguments */
57
+ const parsed = _yargs(args, {
58
+ configuration: {
59
+ 'camel-case-expansion': false,
60
+ 'strip-aliased': true,
61
+ 'strip-dashed': true,
62
+ },
63
+
64
+ alias: {
65
+ 'verbose': [ 'v' ],
66
+ 'quiet': [ 'q' ],
67
+ 'colors': [ 'c' ],
68
+ 'file': [ 'f' ],
69
+ 'list': [ 'l' ],
70
+ 'watch': [ 'w' ],
71
+ 'help': [ 'h' ],
72
+ },
73
+
74
+ string: [ 'file', 'watch' ],
75
+ boolean: [ 'help', 'colors', 'list', 'force-esm', 'force-cjs', 'version' ],
76
+ count: [ 'verbose', 'quiet' ],
77
+ })
78
+
79
+ /* ======================================================================== *
80
+ * NORMALIZE YARGS ARGUMENTS *
81
+ * ======================================================================== */
82
+
83
+ /* Our options */
84
+ const tasks: string[] = []
85
+ const props: Record<string, string> = {}
86
+ const watchDirs: string[] = []
87
+ let verbosity = 0 // yargs always returns 0 for count (quiet/verbose)
88
+ let colors: boolean | undefined = undefined
89
+ let file: string | undefined = undefined
90
+ let listOnly = false
91
+ let help = false
92
+
93
+ /* Switcharoo on arguments */
94
+ for (const [ key, value ] of Object.entries(parsed)) {
95
+ switch (key) {
96
+ case '_': // extra arguments
97
+ value.forEach((current: string) => {
98
+ const [ key, val ] = current.split(/=(.*)/, 2)
99
+ if (key && val) props[key] = val
100
+ else tasks.push(current)
101
+ })
102
+ break
103
+ case 'verbose': // increase verbosity
104
+ verbosity = verbosity + value
105
+ break
106
+ case 'quiet': // decrease verbosity
107
+ verbosity = verbosity - value
108
+ break
109
+ case 'file': // build file
110
+ file = value
111
+ break
112
+ case 'watch': // watch directory
113
+ if (Array.isArray(value)) watchDirs.push(...value)
114
+ else if (value) watchDirs.push(value)
115
+ break
116
+ case 'colors':
117
+ colors = !! value
118
+ break
119
+ case 'list':
120
+ listOnly = !! value
121
+ break
122
+ case 'help':
123
+ help = !! value
124
+ break
125
+ case 'version':
126
+ console.log(`v${version()}`)
127
+ process.exit(0)
128
+ break
129
+ default:
130
+ console.log(`Unsupported option "${key}" (try "--help")`)
131
+ process.exit(1)
132
+ }
133
+ }
134
+
135
+ /* ======================================================================== *
136
+ * HELP OR NOT *
137
+ * ======================================================================== */
138
+
139
+ /* If help, end here! */
140
+ if (help) {
141
+ console.log(`${$blu}${$und}Usage:${$rst}
142
+
143
+ ${$wht}plugjs${$rst} ${$gry}[${$rst}--options${$gry}] [...${$rst}prop=val${$gry}] [...${$rst}tasks${$gry}]${$rst}
144
+
145
+ ${$blu}${$und}Options:${$rst}
146
+
147
+ ${$wht}-f --file ${$gry}${$und}file${$rst} Specify the build file to use (default "./build.[ts/js/...]")
148
+ ${$wht}-w --watch ${$gry}${$und}dir${$rst} Watch for changes on the specified directory and run build
149
+ ${$wht}-v --verbose${$rst} Increase logging verbosity
150
+ ${$wht}-q --quiet${$rst} Decrease logging verbosity
151
+ ${$wht}-c --colors${$rst} Force colorful output (use "--no-colors" to force plain text)
152
+ ${$wht}-l --list${$rst} Only list the tasks defined by the build, nothing more!
153
+ ${$wht}-h --help${$rst} Help! You're reading it now!
154
+ ${$wht} --version${$rst} Version! This one: ${version()}!
155
+
156
+ ${$blu}${$und}Properties:${$rst}
157
+
158
+ Any argument in the format "key=value" will be interpeted as a property to
159
+ be injected in the build process (e.g. "mode=production").
160
+
161
+ ${$blu}${$und}Tasks:${$rst}
162
+
163
+ Any other argument will be treated as a task name. If no task names are
164
+ specified, the "default" task will be executed.
165
+
166
+ ${$blu}${$und}Watch Mode:${$rst}
167
+
168
+ The "-w" option can be specified multiple times, and each single directory
169
+ specified will be watched for changes. Please note that Plug's own watch
170
+ mode is incredibly basic, for more complex scenarios use something more
171
+ advanced like nodemon (https://www.npmjs.com/package/nodemon).
172
+
173
+ ${$blu}${$und}TypeScript module format:${$rst}
174
+
175
+ Normally our TypeScript loader will transpile ".ts" files to the "type"
176
+ specified in "package.json", either "commonjs" (the default) or "module".
177
+
178
+ To force a specific module format we can use one of the following flags:
179
+
180
+ ${$wht}--force-esm ${$rst} Force transpilation of ".ts" files to EcmaScript modules
181
+ ${$wht}--force-cjs ${$rst} Force transpilation of ".ts" files to CommonJS modules
182
+ `)
183
+ process.exit(0)
184
+ }
185
+
186
+ /* ======================================================================== *
187
+ * LOG OPTIONS AS ENVIRONMENT VARIABLES *
188
+ * ======================================================================== */
189
+
190
+ /* Log colors, overriding our LOG_COLORS environment variable */
191
+ if (colors !== undefined) process.env.LOG_COLORS = `${colors}`
192
+
193
+ /* Log level (from verbosity) overriding LOG_LEVEL */
194
+ if (verbosity) {
195
+ const levels = [ 'TRACE', 'DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERROR', 'OFF' ]
196
+ let level = levels.indexOf('NOTICE') - verbosity
197
+ if (level >= levels.length) level = levels.length - 1
198
+ else if (level < 0) level = 0
199
+
200
+ process.env.LOG_LEVEL = levels[level]
201
+ }
202
+
203
+ /* ======================================================================== *
204
+ * BUILD FILE RESOLUTION *
205
+ * ======================================================================== */
206
+
207
+ /* Find our build file */
208
+ const exts = [ 'ts', 'mts', 'mjs', 'js', 'mjs', 'cjs' ]
209
+
210
+ let buildFile: string | undefined = undefined
211
+
212
+ if (file) {
213
+ const absolute = _path.resolve(file)
214
+ if (! isFile(absolute)) {
215
+ console.log(`Specified build file "${file}" was not found`)
216
+ process.exit(1)
217
+ } else {
218
+ buildFile = absolute
219
+ }
220
+ } else {
221
+ for (const ext of exts) {
222
+ const absolute = _path.resolve(`build.${ext}`)
223
+ if (! isFile(absolute)) continue
224
+ buildFile = absolute
225
+ break
226
+ }
227
+ }
228
+
229
+ /* Final check */
230
+ if (! buildFile) {
231
+ console.log(`Unable to find build file "./build.[${exts.join('|')}]`)
232
+ process.exit(1)
233
+ }
234
+
235
+ /* ======================================================================== *
236
+ * WATCH MODE *
237
+ * ======================================================================== */
238
+
239
+ watchDirs.forEach((watchDir) => {
240
+ const absolute = _path.resolve(watchDir)
241
+ if (! isDirectory(absolute)) {
242
+ console.log(`Specified watch directory "${watchDir}" was not found`)
243
+ process.exit(1)
244
+ } else {
245
+ watchDir = absolute
246
+ }
247
+ })
248
+
249
+ /* ======================================================================== *
250
+ * ALL DONE *
251
+ * ======================================================================== */
252
+
253
+ return { buildFile, watchDirs, tasks, props, listOnly }
254
+ }
255
+
256
+ /* ========================================================================== *
257
+ * ========================================================================== *
258
+ * MAIN ENTRY POINT *
259
+ * ========================================================================== *
260
+ * ========================================================================== */
261
+
262
+ /* ========================================================================== *
263
+ * ========================================================================== *
264
+ * PROCESS SETUP *
265
+ * ========================================================================== *
266
+ * ========================================================================== */
267
+
268
+ /* eslint-disable no-console */
269
+
270
+ /* We have everyhing we need to start our asynchronous main! */
271
+ main(async (args: string[]): Promise<void> => {
272
+ // Parse and destructure command line
273
+ const {
274
+ buildFile,
275
+ watchDirs,
276
+ tasks,
277
+ props,
278
+ listOnly,
279
+ } = parseCommandLine(args)
280
+
281
+ // Default task if none specified
282
+ if (tasks.length === 0) tasks.push('default')
283
+
284
+ // Import and check build file
285
+ let maybeBuild = await import(buildFile)
286
+ while (maybeBuild) {
287
+ if (isBuild(maybeBuild)) break
288
+ maybeBuild = maybeBuild.default
289
+ }
290
+
291
+ // We _need_ a build
292
+ if (! isBuild(maybeBuild)) {
293
+ console.log('Build file did not export a proper build')
294
+ console.log()
295
+ console.log('- If using CommonJS export your build as "module.exports"')
296
+ console.log(' e.g.: module.exports = build({ ... })')
297
+ console.log()
298
+ console.log('- If using ESM modules export your build as "default"')
299
+ console.log(' e.g.: export default build({ ... })')
300
+ console.log()
301
+ process.exit(1)
302
+ }
303
+
304
+ const build = maybeBuild
305
+
306
+ // List tasks
307
+ if (listOnly) {
308
+ const taskNames: string[] = []
309
+ const propNames: string[] = []
310
+
311
+ for (const [ key, value ] of Object.entries(build)) {
312
+ (typeof value === 'string' ? propNames : taskNames).push(key)
313
+ }
314
+
315
+ const buildFileName = _path.relative(process.cwd(), buildFile)
316
+
317
+ console.log(`\n${$gry}Outline of ${$wht}${buildFileName}${$rst}`)
318
+
319
+ console.log('\nKnown tasks:\n')
320
+ for (const taskName of taskNames.sort()) {
321
+ console.log(` ${$gry}\u25a0${$tsk} ${taskName}${$rst}`)
322
+ }
323
+
324
+ console.log('\nKnown properties:\n')
325
+ for (const propName of propNames.sort()) {
326
+ const value = build[propName] ?
327
+ ` ${$gry}(default "${$rst}${$und}${build[propName]}${$gry})` : ''
328
+ console.log(` ${$gry}\u25a1${$blu} ${propName}${value}${$rst}`)
329
+ }
330
+
331
+ console.log()
332
+ return
333
+ }
334
+
335
+ // Watch directories
336
+ if (watchDirs.length) {
337
+ let timeout: NodeJS.Timeout | undefined = undefined
338
+
339
+ const runme = (): void => {
340
+ build[buildMarker](tasks, props)
341
+ .then(() => {
342
+ console.log(`\n${$gry}Watching for files change...${$rst}\n`)
343
+ }, (error) => {
344
+ if (isBuildFailure(error)) {
345
+ console.log(`\n${$gry}Watching for files change...${$rst}\n`)
346
+ } else {
347
+ console.log(error)
348
+ watchers.forEach((watcher) => watcher.close())
349
+ }
350
+ })
351
+ .finally(() => {
352
+ timeout = undefined
353
+ })
354
+ }
355
+
356
+ const watchers = watchDirs.map((watchDir) => {
357
+ return _fs.watch(watchDir, { recursive: true }, () => {
358
+ if (! timeout) timeout = setTimeout(runme, 250)
359
+ })
360
+ })
361
+
362
+ runme()
363
+ return
364
+ }
365
+
366
+ // Normal build (no list, no watchers)
367
+ try {
368
+ await build[buildMarker](tasks, props)
369
+ } catch (error) {
370
+ if (! isBuildFailure(error)) console.log(error)
371
+ process.exit(1)
372
+ }
373
+ })