@wtf/platform-node 2.0.0 → 2.1.0

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/README.md CHANGED
@@ -19,6 +19,7 @@ package in the repo that imports `node:` modules.
19
19
  | `./terminal` | `nodeTerminal({ stdin, stdout, env })`: reads lines from stdin with `node:readline` |
20
20
  | `./open` | `nodeOpen({ env })`, `spawnOpen({ spawn, platform, browser })`, `openerOf`: `BROWSER`, else the platform's app |
21
21
  | `./process` | `nodeSpawn({ env })`: `spawn` from `node:child_process`, its pipes Web Streams by `Readable.toWeb` |
22
+ | `./module` | `nodeLoadModule(root, beside?)`: a module by its path from `root`, or a package `root` installs, else `beside` |
22
23
  | `./runtime` | `runMain(program, { fetch, storage })`, `processInterrupts({ signals })`; `processExitCode` |
23
24
 
24
25
  The standard members are Node's own: `setTimeout` and `clearTimeout` from `node:timers`,
@@ -69,5 +70,11 @@ give it. It aborts the program's signal at the first `SIGINT` or `SIGTERM`; a se
69
70
  process, as Node does by default. It sets `process.exitCode` from how the program ended: 0, or the
70
71
  `exitCode` of what it threw, or 1.
71
72
 
73
+ `nodeLoadModule(root, beside)` loads a module that a configuration names, as a plugin, by
74
+ `import()`. It reads a path from `root`, the root of your project. It looks for a package in
75
+ `root`, and else beside `beside`, as your tool gives its own `import.meta.url`, so a plugin
76
+ installed globally beside the tool is found too. A package found in neither is a `SystemError` of
77
+ `NotFound`. The function it gives is a `LoadModule` of `@wtf/platform/module`.
78
+
72
79
  It uses Node's own modules and runs only in Node. The file system is for your entry point to pass
73
80
  in, from `@wtf/platform-fs-node`.
@@ -0,0 +1,10 @@
1
+ import type { LoadModule } from '@wtf/platform/module';
2
+ /**
3
+ * Node's {@link LoadModule}, which loads a module by `import()` of its file: an absolute path as
4
+ * it is; a path from the root, `./` or `../`, of `root`, the root of the project, as the entry
5
+ * point gives it; a package, the one `root` installs, else the one installed beside `beside`, as
6
+ * a tool gives its own `import.meta.url`, so a package a global install puts beside the tool is
7
+ * found too. A package found in neither is a `SystemError` of `NotFound`; what fails as it is
8
+ * imported fails of the error of the import.
9
+ */
10
+ export declare const nodeLoadModule: (root: string, beside?: string | URL) => LoadModule;
package/dist/module.js ADDED
@@ -0,0 +1,73 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ /**
10
+ * Node's loader of modules: {@link nodeLoadModule}, which loads the module a name or a path names
11
+ * by Node's resolution and `import()`, looked for from the root of a project, then beside a tool,
12
+ * as a configuration names a plugin of the tool. It reads nothing of the process: the entry point
13
+ * gives the root.
14
+ *
15
+ * ```ts
16
+ * import { nodeLoadModule } from '@wtf/platform-node/module'
17
+ *
18
+ * declare const root: string
19
+ *
20
+ * const load = nodeLoadModule(root, import.meta.url)
21
+ * const formatters = await load('./formatters.mjs')
22
+ * ```
23
+ *
24
+ * @packageDocumentation
25
+ */
26
+ import { createRequire } from 'node:module';
27
+ import { isAbsolute, join } from 'node:path';
28
+ import { pathToFileURL } from 'node:url';
29
+ import { SystemError } from '@wtf/platform/errors';
30
+ /**
31
+ * The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or
32
+ * `../`, of `root`; of a package, the one `root` installs, as a devDependency of the project, else
33
+ * the one installed beside `beside`, as of a global install of a tool and its plugins. A package
34
+ * found in neither is a `SystemError` of `NotFound`, of the error of `root`'s resolve.
35
+ */
36
+ function fileOf(module, root, beside) {
37
+ if (isAbsolute(module))
38
+ return module;
39
+ if (/^\.{1,2}\//.test(module))
40
+ return join(root, module);
41
+ try {
42
+ return createRequire(join(root, 'package.json')).resolve(module);
43
+ }
44
+ catch (error) {
45
+ if (beside !== undefined)
46
+ try {
47
+ return createRequire(beside).resolve(module);
48
+ }
49
+ catch {
50
+ // Not beside the tool either: the cause is the root's, where a project installs it.
51
+ }
52
+ throw new SystemError({
53
+ reason: 'NotFound',
54
+ module: 'LoadModule',
55
+ method: 'load',
56
+ pathOrDescriptor: module,
57
+ description: beside === undefined
58
+ ? `No package ${module} is installed in ${root}.`
59
+ : `No package ${module} is installed in ${root}, nor beside ${String(beside)}.`,
60
+ cause: error
61
+ });
62
+ }
63
+ }
64
+ /**
65
+ * Node's {@link LoadModule}, which loads a module by `import()` of its file: an absolute path as
66
+ * it is; a path from the root, `./` or `../`, of `root`, the root of the project, as the entry
67
+ * point gives it; a package, the one `root` installs, else the one installed beside `beside`, as
68
+ * a tool gives its own `import.meta.url`, so a package a global install puts beside the tool is
69
+ * found too. A package found in neither is a `SystemError` of `NotFound`; what fails as it is
70
+ * imported fails of the error of the import.
71
+ */
72
+ export const nodeLoadModule = (root, beside) => async (module) => import(__rewriteRelativeImportExtension(pathToFileURL(fileOf(module, root, beside)).href));
73
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAGlD;;;;;GAKG;AACH,SAAS,MAAM,CAAC,MAAc,EAAE,IAAY,EAAE,MAAgC;IAC5E,IAAI,UAAU,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAA;IACrC,IAAI,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACxD,IAAI,CAAC;QACH,OAAO,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAClE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,MAAM,KAAK,SAAS;YACtB,IAAI,CAAC;gBACH,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;YAC9C,CAAC;YAAC,MAAM,CAAC;gBACP,oFAAoF;YACtF,CAAC;QACH,MAAM,IAAI,WAAW,CAAC;YACpB,MAAM,EAAE,UAAU;YAClB,MAAM,EAAE,YAAY;YACpB,MAAM,EAAE,MAAM;YACd,gBAAgB,EAAE,MAAM;YACxB,WAAW,EACT,MAAM,KAAK,SAAS;gBAClB,CAAC,CAAC,cAAc,MAAM,oBAAoB,IAAI,GAAG;gBACjD,CAAC,CAAC,cAAc,MAAM,oBAAoB,IAAI,gBAAgB,MAAM,CAAC,MAAM,CAAC,GAAG;YACnF,KAAK,EAAE,KAAK;SACb,CAAC,CAAA;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GACzB,CAAC,IAAY,EAAE,MAAqB,EAAc,EAAE,CACpD,KAAK,EAAE,MAAM,EAAE,EAAE,CACf,MAAM,kCAAC,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,EAAC,CAAA","sourcesContent":["/**\n * Node's loader of modules: {@link nodeLoadModule}, which loads the module a name or a path names\n * by Node's resolution and `import()`, looked for from the root of a project, then beside a tool,\n * as a configuration names a plugin of the tool. It reads nothing of the process: the entry point\n * gives the root.\n *\n * ```ts\n * import { nodeLoadModule } from '@wtf/platform-node/module'\n *\n * declare const root: string\n *\n * const load = nodeLoadModule(root, import.meta.url)\n * const formatters = await load('./formatters.mjs')\n * ```\n *\n * @packageDocumentation\n */\nimport { createRequire } from 'node:module'\nimport { isAbsolute, join } from 'node:path'\nimport { pathToFileURL } from 'node:url'\n\nimport { SystemError } from '@wtf/platform/errors'\nimport type { LoadModule } from '@wtf/platform/module'\n\n/**\n * The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or\n * `../`, of `root`; of a package, the one `root` installs, as a devDependency of the project, else\n * the one installed beside `beside`, as of a global install of a tool and its plugins. A package\n * found in neither is a `SystemError` of `NotFound`, of the error of `root`'s resolve.\n */\nfunction fileOf(module: string, root: string, beside: string | URL | undefined): string {\n if (isAbsolute(module)) return module\n if (/^\\.{1,2}\\//.test(module)) return join(root, module)\n try {\n return createRequire(join(root, 'package.json')).resolve(module)\n } catch (error) {\n if (beside !== undefined)\n try {\n return createRequire(beside).resolve(module)\n } catch {\n // Not beside the tool either: the cause is the root's, where a project installs it.\n }\n throw new SystemError({\n reason: 'NotFound',\n module: 'LoadModule',\n method: 'load',\n pathOrDescriptor: module,\n description:\n beside === undefined\n ? `No package ${module} is installed in ${root}.`\n : `No package ${module} is installed in ${root}, nor beside ${String(beside)}.`,\n cause: error\n })\n }\n}\n\n/**\n * Node's {@link LoadModule}, which loads a module by `import()` of its file: an absolute path as\n * it is; a path from the root, `./` or `../`, of `root`, the root of the project, as the entry\n * point gives it; a package, the one `root` installs, else the one installed beside `beside`, as\n * a tool gives its own `import.meta.url`, so a package a global install puts beside the tool is\n * found too. A package found in neither is a `SystemError` of `NotFound`; what fails as it is\n * imported fails of the error of the import.\n */\nexport const nodeLoadModule =\n (root: string, beside?: string | URL): LoadModule =>\n async (module) =>\n import(pathToFileURL(fileOf(module, root, beside)).href)\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wtf/platform-node",
3
- "version": "2.0.0",
4
- "description": "Node's platform: nodePlatform({ fetch, storage }) builds it from Node's own modules, with spawn over node:child_process, and runMain runs your program with it, stopping on SIGINT or SIGTERM.",
3
+ "version": "2.1.0",
4
+ "description": "Node's platform: nodePlatform({ fetch, storage }) builds it from Node's own modules, with spawn over node:child_process, and runMain runs your program with it, stopping on SIGINT or SIGTERM; nodeLoadModule loads a module by its path or package name, from a project's root.",
5
5
  "homepage": "https://gitlab.com/wetryfun/platform/platform/-/tree/main/packages/platform-node",
6
6
  "bugs": {
7
7
  "url": "https://gitlab.com/wetryfun/platform/platform/-/issues"
@@ -50,6 +50,10 @@
50
50
  "types": "./dist/open.d.ts",
51
51
  "default": "./dist/open.js"
52
52
  },
53
+ "./module": {
54
+ "types": "./dist/module.d.ts",
55
+ "default": "./dist/module.js"
56
+ },
53
57
  "./process": {
54
58
  "types": "./dist/process.d.ts",
55
59
  "default": "./dist/process.js"
@@ -74,10 +78,10 @@
74
78
  "build": "tsc -b"
75
79
  },
76
80
  "dependencies": {
77
- "@wtf/platform": "2.0.0"
81
+ "@wtf/platform": "2.1.0"
78
82
  },
79
83
  "devDependencies": {
80
- "@wtf/platform-suite": "2.0.0",
84
+ "@wtf/platform-suite": "2.1.0",
81
85
  "@wtf/project-wpt": "^1.0.0"
82
86
  },
83
87
  "engines": {