@plugjs/plug 0.3.5 → 0.4.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 (119) hide show
  1. package/dist/asserts.cjs +10 -12
  2. package/dist/asserts.cjs.map +1 -1
  3. package/dist/asserts.d.ts +1 -2
  4. package/dist/asserts.mjs +9 -10
  5. package/dist/asserts.mjs.map +1 -1
  6. package/dist/async.cjs +5 -20
  7. package/dist/async.cjs.map +2 -2
  8. package/dist/async.mjs +5 -20
  9. package/dist/async.mjs.map +2 -2
  10. package/dist/build.cjs +113 -64
  11. package/dist/build.cjs.map +2 -2
  12. package/dist/build.d.ts +9 -7
  13. package/dist/build.mjs +110 -63
  14. package/dist/build.mjs.map +2 -2
  15. package/dist/cli.d.mts +12 -0
  16. package/dist/cli.mjs +266 -0
  17. package/dist/cli.mjs.map +6 -0
  18. package/dist/files.cjs +5 -3
  19. package/dist/files.cjs.map +1 -1
  20. package/dist/files.d.ts +2 -1
  21. package/dist/files.mjs +11 -4
  22. package/dist/files.mjs.map +1 -1
  23. package/dist/fork.cjs +30 -12
  24. package/dist/fork.cjs.map +1 -1
  25. package/dist/fork.d.ts +10 -0
  26. package/dist/fork.mjs +31 -13
  27. package/dist/fork.mjs.map +1 -1
  28. package/dist/helpers.cjs +32 -13
  29. package/dist/helpers.cjs.map +2 -2
  30. package/dist/helpers.d.ts +12 -0
  31. package/dist/helpers.mjs +37 -14
  32. package/dist/helpers.mjs.map +2 -2
  33. package/dist/index.cjs +5 -0
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.ts +2 -1
  36. package/dist/index.mjs +4 -1
  37. package/dist/index.mjs.map +1 -1
  38. package/dist/logging/emit.cjs +4 -4
  39. package/dist/logging/emit.cjs.map +1 -1
  40. package/dist/logging/emit.mjs +4 -4
  41. package/dist/logging/emit.mjs.map +1 -1
  42. package/dist/logging/logger.cjs +43 -2
  43. package/dist/logging/logger.cjs.map +1 -1
  44. package/dist/logging/logger.d.ts +36 -3
  45. package/dist/logging/logger.mjs +43 -3
  46. package/dist/logging/logger.mjs.map +1 -1
  47. package/dist/logging/options.cjs +8 -12
  48. package/dist/logging/options.cjs.map +1 -1
  49. package/dist/logging/options.d.ts +44 -1
  50. package/dist/logging/options.mjs +8 -12
  51. package/dist/logging/options.mjs.map +1 -1
  52. package/dist/logging.cjs +14 -3
  53. package/dist/logging.cjs.map +1 -1
  54. package/dist/logging.d.ts +2 -0
  55. package/dist/logging.mjs +13 -3
  56. package/dist/logging.mjs.map +1 -1
  57. package/dist/plugs/build.cjs +63 -0
  58. package/dist/plugs/build.cjs.map +6 -0
  59. package/dist/plugs/build.d.ts +13 -0
  60. package/dist/plugs/build.mjs +37 -0
  61. package/dist/plugs/build.mjs.map +6 -0
  62. package/dist/plugs/debug.cjs +7 -9
  63. package/dist/plugs/debug.cjs.map +1 -1
  64. package/dist/plugs/debug.mjs +8 -10
  65. package/dist/plugs/debug.mjs.map +1 -1
  66. package/dist/types.cjs +12 -0
  67. package/dist/types.cjs.map +1 -1
  68. package/dist/types.d.ts +35 -9
  69. package/dist/types.mjs +5 -0
  70. package/dist/types.mjs.map +2 -2
  71. package/dist/utils/diff.cjs +1 -4
  72. package/dist/utils/diff.cjs.map +1 -1
  73. package/dist/utils/diff.mjs +1 -4
  74. package/dist/utils/diff.mjs.map +1 -1
  75. package/dist/utils/exec.cjs +5 -12
  76. package/dist/utils/exec.cjs.map +2 -2
  77. package/dist/utils/exec.d.ts +0 -2
  78. package/dist/utils/exec.mjs +6 -13
  79. package/dist/utils/exec.mjs.map +1 -1
  80. package/dist/utils/{types.cjs → singleton.cjs} +14 -13
  81. package/dist/utils/singleton.cjs.map +6 -0
  82. package/dist/utils/singleton.d.ts +12 -0
  83. package/dist/utils/singleton.mjs +13 -0
  84. package/dist/utils/singleton.mjs.map +6 -0
  85. package/dist/utils.cjs +2 -2
  86. package/dist/utils.cjs.map +1 -1
  87. package/dist/utils.d.ts +1 -1
  88. package/dist/utils.mjs +1 -1
  89. package/package.json +7 -9
  90. package/src/asserts.ts +10 -12
  91. package/src/async.ts +6 -29
  92. package/src/build.ts +169 -106
  93. package/{extra/plug.mts → src/cli.mts} +115 -141
  94. package/src/files.ts +14 -6
  95. package/src/fork.ts +42 -16
  96. package/src/helpers.ts +56 -5
  97. package/src/index.ts +2 -1
  98. package/src/logging/emit.ts +4 -4
  99. package/src/logging/logger.ts +60 -7
  100. package/src/logging/options.ts +9 -14
  101. package/src/logging.ts +20 -5
  102. package/src/plugs/build.ts +45 -0
  103. package/src/plugs/debug.ts +10 -9
  104. package/src/types.ts +54 -23
  105. package/src/utils/diff.ts +1 -6
  106. package/src/utils/exec.ts +6 -20
  107. package/src/utils/singleton.ts +19 -0
  108. package/src/utils.ts +1 -1
  109. package/cli/plug.mjs +0 -1385
  110. package/cli/ts-loader.mjs +0 -275
  111. package/cli/tsrun.mjs +0 -1204
  112. package/dist/utils/types.cjs.map +0 -6
  113. package/dist/utils/types.d.ts +0 -4
  114. package/dist/utils/types.mjs +0 -12
  115. package/dist/utils/types.mjs.map +0 -6
  116. package/extra/ts-loader.mts +0 -546
  117. package/extra/tsrun.mts +0 -127
  118. package/extra/utils.ts +0 -150
  119. package/src/utils/types.ts +0 -11
package/extra/tsrun.mts DELETED
@@ -1,127 +0,0 @@
1
- #!/usr/bin/env node
2
- /* eslint-disable no-console */
3
-
4
- import _path from 'node:path'
5
- import _repl from 'node:repl'
6
- import _module from 'node:module'
7
-
8
- import _yargs from 'yargs-parser'
9
-
10
- import { $blu, $gry, $rst, $und, $wht, main } from './utils.js'
11
-
12
- /** Version injected by esbuild */
13
- declare const __version: string
14
-
15
- /** Our minimalistic help */
16
- function help(): never {
17
- console.log(`${$blu}${$und}Usage:${$rst}
18
-
19
- ${$wht}tsrun${$rst} ${$gry}[${$rst}--options${$gry}] script.ts [...${$rst}script args${$gry}]${$rst}
20
-
21
- ${$blu}${$und}Options:${$rst}
22
-
23
- ${$wht}-h --help ${$rst} Help! You're reading it now!
24
- ${$wht}-v --version ${$rst} Version! This one: ${__version}!
25
- ${$wht}-e --eval ${$rst} Evaluate the script
26
- ${$wht}-p --print ${$rst} Evaluate the script and print the result
27
- ${$wht} --force-esm${$rst} Force transpilation of ".ts" files to EcmaScript modules
28
- ${$wht} --force-cjs${$rst} Force transpilation of ".ts" files to CommonJS modules
29
-
30
- ${$blu}${$und}Description:${$rst}
31
-
32
- ${$wht}tsrun${$rst} is a minimalistic TypeScript loader, using "esbuild" to transpile TS
33
- code to JavaScript, and running it. Being extremely un-sofisticated, it's
34
- not meant to to be in any way a replacement for more complete alternatives
35
- like "ts-node".
36
- `)
37
-
38
- process.exit(1)
39
- }
40
-
41
- /** Process the command line */
42
- main((args: string[]): void => {
43
- let _script: string | undefined
44
- let _scriptArgs: string[] = []
45
- let _print: boolean = false
46
- let _eval: boolean = false
47
-
48
- /* Yargs-parse our arguments */
49
- const parsed = _yargs(args, {
50
- configuration: {
51
- 'camel-case-expansion': false,
52
- 'strip-aliased': true,
53
- 'strip-dashed': true,
54
- },
55
-
56
- alias: {
57
- 'version': [ 'v' ],
58
- 'help': [ 'h' ],
59
- 'eval': [ 'e' ],
60
- 'print': [ 'p' ],
61
- },
62
-
63
- boolean: [ 'help', 'eval', 'print', 'force-esm', 'version', 'force-cjs' ],
64
- })
65
-
66
- // Parse options, leaving script and scriptArgs with our code to run
67
- for (const [ key, value ] of Object.entries(parsed)) {
68
- switch (key) {
69
- case '_': // extra arguments
70
- [ _script, ..._scriptArgs ] = value
71
- break
72
- case 'help': // help screen
73
- help()
74
- break
75
- case 'version': // version dump
76
- console.log(`v${__version}`)
77
- process.exit(0)
78
- break
79
- case 'eval': // eval script
80
- _eval = value
81
- break
82
- case 'print': // eval script and print return value
83
- _print = true
84
- _eval = true
85
- break
86
- }
87
- }
88
-
89
- // Start the repl or run the script?
90
- if (! _script) {
91
- // No script? Then repl
92
- console.log(`Welcome to Node.js ${process.version} (tsrun v${__version}).`)
93
- console.log('Type ".help" for more information.')
94
- _repl.start()
95
- } else if (_eval) {
96
- // If we are evaluating a script, we need to use some node internals to do
97
- // all the tricks to run this... We a fake script running the code to
98
- // evaluate, instrumenting "globalThis" with all required vars and modules
99
- const script = `
100
- globalThis.module = module;
101
- globalThis.require = require;
102
- globalThis.exports = exports;
103
- globalThis.__dirname = __dirname;
104
- globalThis.__filename = __filename;
105
-
106
- for (const module of require('repl').builtinModules) {
107
- if (module.indexOf('/') >= 0) continue;
108
- if (Object.hasOwn(globalThis, module)) continue;
109
- Object.defineProperty(globalThis, module, { get: () => require(module) });
110
- }
111
-
112
- return require('node:vm').runInThisContext(${JSON.stringify(_script)}, '[eval]')
113
- `
114
-
115
- // Use the Node internal "Module._compile" to compile and run our script
116
- const result = (new _module('[eval]') as any)._compile(script, '[eval]')
117
-
118
- // If we need to print, then let's do it!
119
- if (_print) console.log(result)
120
- } else {
121
- // Resolve the _full_ path of the script, and tweak our process.argv
122
- // arguments, them simply import the script and let Node do its thing...
123
- _script = _path.resolve(process.cwd(), _script)
124
- process.argv = [ process.argv0, _script, ..._scriptArgs ]
125
- import(_script)
126
- }
127
- })
package/extra/utils.ts DELETED
@@ -1,150 +0,0 @@
1
- /* eslint-disable no-console */
2
- import _childProcess from 'node:child_process'
3
- import _fs from 'node:fs'
4
- import _path from 'node:path'
5
- import _url from 'node:url'
6
- import _util from 'node:util'
7
-
8
- /* ========================================================================== *
9
- * PRETTY COLORS *
10
- * ========================================================================== */
11
-
12
- export const $rst = process.stdout.isTTY ? '\u001b[0m' : '' // reset all colors to default
13
- export const $und = process.stdout.isTTY ? '\u001b[4m' : '' // underline on
14
- export const $gry = process.stdout.isTTY ? '\u001b[38;5;240m' : '' // somewhat gray
15
- export const $blu = process.stdout.isTTY ? '\u001b[38;5;69m' : '' // brighter blue
16
- export const $wht = process.stdout.isTTY ? '\u001b[1;38;5;255m' : '' // full-bright white
17
- export const $tsk = process.stdout.isTTY ? '\u001b[38;5;141m' : '' // the color for tasks (purple)
18
-
19
-
20
- /* ========================================================================== *
21
- * TS LOADER FORCE TYPE *
22
- * ========================================================================== */
23
-
24
- function forceType(type: 'commonjs' | 'module'): void {
25
- const debug = _util.debuglog('plug:cli')
26
-
27
- const tsLoaderMarker = Symbol.for('plugjs:tsLoader')
28
-
29
- if (!(tsLoaderMarker in globalThis)) {
30
- throw new Error('TypeScript Loader not available')
31
- }
32
- debug(`Forcing type to "${type}"`)
33
- ;(globalThis as any)[tsLoaderMarker] = type
34
- }
35
-
36
-
37
- /* ========================================================================== *
38
- * FILES UTILITIES *
39
- * ========================================================================== */
40
-
41
- /* Returns a boolean indicating whether the specified file exists or not */
42
- export function isFile(path: string): boolean {
43
- try {
44
- return _fs.statSync(path).isFile()
45
- } catch (error) {
46
- return false
47
- }
48
- }
49
-
50
- /* Returns a boolean indicating whether the specified directory exists or not */
51
- export function isDirectory(path: string): boolean {
52
- try {
53
- return _fs.statSync(path).isDirectory()
54
- } catch (error) {
55
- return false
56
- }
57
- }
58
-
59
-
60
- /* ========================================================================== *
61
- * MAIN ENTRY POINT *
62
- * ========================================================================== */
63
- export function main(callback: (args: string[]) => void | Promise<void>): void {
64
- const debug = _util.debuglog('plug:cli')
65
-
66
- /* Check for source maps and typescript support */
67
- const sourceMapsEnabled = process.execArgv.indexOf('--enable-source-maps') >= 0
68
-
69
- /* Check if our `ts-loader` loader is enabled */
70
- const tsLoaderMarker = Symbol.for('plugjs:tsLoader')
71
- const typeScriptEnabled = (globalThis as any)[tsLoaderMarker]
72
-
73
- /* Some debugging if needed */
74
- debug('SourceMaps enabled =', sourceMapsEnabled)
75
- debug('TypeScript enabled =', typeScriptEnabled || false)
76
-
77
- /* If both source maps and typescript are on, run! */
78
- if (sourceMapsEnabled && typeScriptEnabled) {
79
- const args = process.argv.slice(2).filter((arg: string): string | void => {
80
- if (arg === '--force-esm') {
81
- return forceType('module')
82
- } else if (arg === '--force-cjs') {
83
- return forceType('commonjs')
84
- } else {
85
- return arg
86
- }
87
- })
88
-
89
-
90
- Promise.resolve().then(() => callback(args)).catch(console.error)
91
- } else {
92
- const script = _url.fileURLToPath(import.meta.url)
93
-
94
- /* Fork out ourselves with new options */
95
- const execArgv = [ ...process.execArgv ]
96
-
97
- /* Enable source maps if not done already */
98
- if (! sourceMapsEnabled) execArgv.push('--enable-source-maps')
99
-
100
- /* Enable our ESM TypeScript loader if not done already */
101
- if (! typeScriptEnabled) {
102
- const directory = _path.dirname(script)
103
- const extension = _path.extname(script) // .mts or .mjs
104
- const loader = _path.resolve(directory, `ts-loader${extension}`)
105
- execArgv.push(`--experimental-loader=${loader}`, '--no-warnings')
106
- }
107
-
108
- /*
109
- * It seems that setting "type" as "module" in "package.json" creates some
110
- * problems when the module is being imported from a "commonjs" one.
111
- *
112
- * TypeScript _incorrectly_ says (regardless of how we set up our conditional
113
- * exports) that we must use dynamic imports:
114
- *
115
- * Module '@plugjs/plug' cannot be imported using this construct. The
116
- * specifier only resolves to an ES module, which cannot be imported
117
- * synchronously. Use dynamic import instead.
118
- * TS(1471)
119
- *
120
- * So for now our only option is to leave "type" as "commonjs", and for those
121
- * brave souls willing to force ESM irregardless of what's in "package.json",
122
- * we allow the "--force-esm" option, and instruct `ts-loader` that the
123
- * current directory (and subdirs) will transpile as ESM always.
124
- */
125
-
126
- /* Fork ourselves! */
127
- const child = _childProcess.fork(script, [ ...process.argv.slice(2) ], {
128
- stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ],
129
- execArgv,
130
- })
131
-
132
- /* Monitor child process... */
133
- child.on('error', (error) => {
134
- console.log('Error respawning CLI', error)
135
- process.exit(1)
136
- })
137
-
138
- child.on('exit', (code, signal) => {
139
- if (signal) {
140
- console.log(`CLI process exited with signal ${signal}`)
141
- process.exit(1)
142
- } else if (typeof code !== 'number') {
143
- console.log('CLI process failed for an unknown reason')
144
- process.exit(1)
145
- } else {
146
- process.exit(code)
147
- }
148
- })
149
- }
150
- }
@@ -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
- }