@wolfstar/kit 0.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/LICENSE +201 -0
- package/README.md +81 -0
- package/dist/esm/index.d.ts +136 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +215 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2022 Wolfstar Project
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @wolfstar/kit
|
|
2
|
+
|
|
3
|
+
`defineModule` and the module runtime for installable Stars modules — the `@nuxt/kit` counterpart. A module is a
|
|
4
|
+
package that says it is one; the project lists it in `modules` in `stars.config`:
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
// stars.config.ts
|
|
8
|
+
import { defineConfig } from '@wolfstar/http-framework/config';
|
|
9
|
+
|
|
10
|
+
export default defineConfig({
|
|
11
|
+
modules: ['@wolfstar/plugin-cache', ['@wolfstar/plugin-gateway', { shards: 2 }]]
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// the module's package: its default export
|
|
17
|
+
import { defineModule } from '@wolfstar/kit';
|
|
18
|
+
|
|
19
|
+
export default defineModule<{ ttl: number }>({
|
|
20
|
+
meta: {
|
|
21
|
+
name: '@wolfstar/plugin-cache',
|
|
22
|
+
compatibility: { framework: '>=6.1.0' }
|
|
23
|
+
},
|
|
24
|
+
defaults: { ttl: 60_000 },
|
|
25
|
+
hooks: {
|
|
26
|
+
'build:done': (outcome) => {}
|
|
27
|
+
},
|
|
28
|
+
setup(options, ctx) {
|
|
29
|
+
ctx.addPlugin({ from: new URL('./plugin.js', import.meta.url), options });
|
|
30
|
+
ctx.addImports('@wolfstar/plugin-cache');
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
// plugin.js — runs in the bot
|
|
37
|
+
import { definePlugin } from '@wolfstar/http-framework';
|
|
38
|
+
|
|
39
|
+
export default definePlugin((options) => ({
|
|
40
|
+
name: '@wolfstar/plugin-cache',
|
|
41
|
+
preLoad(client) {}
|
|
42
|
+
}));
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`@wolfstar/kit` depends on `@wolfstar/schema` only, so a module depends on it (as a peer) without pulling in the
|
|
46
|
+
framework or the CLI.
|
|
47
|
+
|
|
48
|
+
## What `setup` can do
|
|
49
|
+
|
|
50
|
+
`setup(options, ctx)` runs once, in the CLI process, while the project loads. `options` is the module's `defaults`
|
|
51
|
+
merged under the ones written in `stars.config` (plain objects merge deeply, arrays are replaced).
|
|
52
|
+
|
|
53
|
+
- `ctx.addPlugin(source)` registers a runtime plugin. The plugin lives in the bot's process and `setup` in the CLI's,
|
|
54
|
+
so a plugin is given by **source** — `{ from, export?, options? }` — rather than by value. `from` is a package
|
|
55
|
+
specifier, an absolute path or a `file:` URL (a relative path is rejected: it would resolve differently in each
|
|
56
|
+
build tool); the `export` (default `'default'`) is the plugin or, when it is a function, a factory called with
|
|
57
|
+
`options`, which must be JSON-serialisable. A legacy `Plugin` class is registered as it is, never called.
|
|
58
|
+
- `ctx.addImports(preset)` adds a package to the auto imports presets.
|
|
59
|
+
- `ctx.hook(name, callback)` / `ctx.callHook(name, …)` use the CLI's hooks (`StarsHooks`), the same registry as `hooks`
|
|
60
|
+
in `stars.config`. A module's `hooks` are registered before `setup` runs; an unknown hook name or a value that is not
|
|
61
|
+
a function fails the install.
|
|
62
|
+
- `ctx.installModule(module, options?)` installs another module, and `dependencies` installs modules first. A module
|
|
63
|
+
is installed once, by `meta.name`: the first installation wins.
|
|
64
|
+
|
|
65
|
+
`meta.compatibility` (`framework`, `stars`) is a semver range checked against the project's installed
|
|
66
|
+
`@wolfstar/http-framework` and the running `@wolfstar/cli`; a mismatch fails early instead of leaving a plugin that
|
|
67
|
+
silently never runs. `meta.configKey` is reserved.
|
|
68
|
+
|
|
69
|
+
## Production with `tsc` or `none`
|
|
70
|
+
|
|
71
|
+
`tsdown`, `vite` and Nitro builds register the runtime plugins through the entry. `tsc` and `none` builds cannot be
|
|
72
|
+
transformed: `stars dev` preloads the plugins with `node --import`, and for production `stars prepare` (and
|
|
73
|
+
`stars build`) writes `.stars/modules.mjs`, to be preloaded the same way: `node --import ./.stars/modules.mjs dist/main.js`.
|
|
74
|
+
`stars build`, `stars prepare` and `stars info` report a warning (`MODULES_PRELOAD_REQUIRED`) when such a project has modules with runtime plugins; `stars dev` does not print it, since it preloads them itself. With `env` enabled, `modules.mjs` imports `.stars/env.mjs` first, so the environment is loaded before any plugin is evaluated.
|
|
75
|
+
|
|
76
|
+
## Programmatic usage
|
|
77
|
+
|
|
78
|
+
`setupModules({ config, hooks, load, versions })` is what the CLI calls: `load` imports a specifier from the project,
|
|
79
|
+
`hooks` is a `hookable`-shaped registry, and the result (`ModulesRuntime`) lists the installed modules, the runtime
|
|
80
|
+
plugins and the import presets they contributed. Failures are `ModuleError`s with a `code` (`MODULE_LOAD_FAILED`,
|
|
81
|
+
`MODULE_INVALID`, `MODULE_INCOMPATIBLE`, `MODULE_SETUP_FAILED`, `MODULE_PLUGIN_INVALID`) and the `moduleName`.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { InstalledModule, ModulesRuntime, ModulesRuntime as ModulesRuntime$1, ResolvedStarsConfig, RuntimePluginRegistration, StarsHookName, StarsHooks } from "@wolfstar/schema";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
4
|
+
/** The options a module declares, merged from its `defaults` and the ones written in `stars.config`. */
|
|
5
|
+
type ModuleOptions = Record<string, any>;
|
|
6
|
+
interface ModuleMeta {
|
|
7
|
+
/** The module's identity, used to install it once, to attribute errors and to report it. Usually the package name. */
|
|
8
|
+
name: string;
|
|
9
|
+
version?: string;
|
|
10
|
+
/** Reserved for reading the module's options from a `stars.config` key of this name. */
|
|
11
|
+
configKey?: string;
|
|
12
|
+
/** Version ranges the module works with. A range is only checked when the host knows the version. */
|
|
13
|
+
compatibility?: {
|
|
14
|
+
/** A range for the project's installed `@wolfstar/http-framework`. */
|
|
15
|
+
framework?: string;
|
|
16
|
+
/** A range for the running `@wolfstar/cli`. */
|
|
17
|
+
stars?: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** What the host knows about the project: `null` when a version could not be found. */
|
|
21
|
+
interface ModuleVersions {
|
|
22
|
+
framework?: string | null;
|
|
23
|
+
stars?: string | null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The hook registry of the host. The CLI hands over its `hookable` instance, so a module's hooks, `stars.config`'s
|
|
27
|
+
* `hooks` and the CLI's own `callHook` calls all share one registry.
|
|
28
|
+
*/
|
|
29
|
+
interface ModuleHookHost {
|
|
30
|
+
hook<Name extends StarsHookName>(name: Name, callback: StarsHooks[Name]): unknown;
|
|
31
|
+
callHook<Name extends StarsHookName>(name: Name, ...args: Parameters<StarsHooks[Name]>): Awaitable<unknown>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A runtime plugin to register with `Client.use`. It is given by source: the plugin lives in the bot's process, the
|
|
35
|
+
* module's `setup` in the CLI's. The `export` of `from` is the plugin (`definePlugin({ … })`) or, when it is a
|
|
36
|
+
* function, a factory called with `options` (`definePlugin((options) => ({ … }))`). A legacy `Plugin` class is
|
|
37
|
+
* passed to `Client.use` as it is, never called.
|
|
38
|
+
*/
|
|
39
|
+
interface ModulePluginSource {
|
|
40
|
+
/**
|
|
41
|
+
* A package specifier (resolved from the project), an absolute path or a `file:` URL, e.g. `new URL('./plugin.js',
|
|
42
|
+
* import.meta.url)`. A relative path is rejected: it would resolve differently in each build tool.
|
|
43
|
+
*/
|
|
44
|
+
from: string | URL;
|
|
45
|
+
/** @default 'default' */
|
|
46
|
+
export?: string;
|
|
47
|
+
/** Must be JSON-serialisable: it is written into the built entry. */
|
|
48
|
+
options?: unknown;
|
|
49
|
+
}
|
|
50
|
+
/** A module to install: its specifier, or its definition, optionally with options. */
|
|
51
|
+
type ModuleInput<Options extends ModuleOptions = ModuleOptions> = string | StarsModule<Options>;
|
|
52
|
+
interface ModuleContext {
|
|
53
|
+
/** Absolute project root. */
|
|
54
|
+
readonly root: string;
|
|
55
|
+
/** The resolved `stars.config`, before any module contributed. */
|
|
56
|
+
readonly config: ResolvedStarsConfig;
|
|
57
|
+
readonly versions: Readonly<ModuleVersions>;
|
|
58
|
+
/** Registers a runtime plugin. See {@link ModulePluginSource}. */
|
|
59
|
+
addPlugin(plugin: string | URL | ModulePluginSource): void;
|
|
60
|
+
/** Adds a package to the auto imports presets (`imports.presets`): all its exports become auto imports. */
|
|
61
|
+
addImports(preset: string | {
|
|
62
|
+
from: string;
|
|
63
|
+
}): void;
|
|
64
|
+
/** Registers a hook of the `stars` CLI. */
|
|
65
|
+
hook: ModuleHookHost['hook'];
|
|
66
|
+
/** Calls a hook of the `stars` CLI. */
|
|
67
|
+
callHook: ModuleHookHost['callHook'];
|
|
68
|
+
/** Installs another module, once: a module that is already installed is left as it is, whatever options it got. */
|
|
69
|
+
installModule<Options extends ModuleOptions>(module: ModuleInput<Options>, options?: Partial<Options>): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
interface StarsModule<Options extends ModuleOptions = ModuleOptions> {
|
|
72
|
+
meta: ModuleMeta;
|
|
73
|
+
/** Merged under the options written in `stars.config`. */
|
|
74
|
+
defaults?: Options;
|
|
75
|
+
/** Modules installed before this one. */
|
|
76
|
+
dependencies?: readonly ModuleInput[];
|
|
77
|
+
/** Hooks of the `stars` CLI, registered before `setup` runs. An unknown hook name or a value that is not a function is rejected. */
|
|
78
|
+
hooks?: { [Name in StarsHookName]?: StarsHooks[Name]; };
|
|
79
|
+
/** Runs in the CLI process, once, while the project loads. */
|
|
80
|
+
setup?(options: Options, ctx: ModuleContext): Awaitable<void>;
|
|
81
|
+
}
|
|
82
|
+
type ModuleErrorCode = 'MODULE_LOAD_FAILED' | 'MODULE_INVALID' | 'MODULE_INCOMPATIBLE' | 'MODULE_SETUP_FAILED' | 'MODULE_PLUGIN_INVALID';
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/define.d.ts
|
|
85
|
+
/**
|
|
86
|
+
* Defines an installable module. This is an identity helper that types the definition; the module is installed by
|
|
87
|
+
* listing its package in `modules` in `stars.config`.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { defineModule } from '@wolfstar/kit';
|
|
92
|
+
*
|
|
93
|
+
* export default defineModule<{ ttl: number }>({
|
|
94
|
+
* meta: { name: '@wolfstar/plugin-cache', compatibility: { framework: '>=6.1.0' } },
|
|
95
|
+
* defaults: { ttl: 60_000 },
|
|
96
|
+
* setup(options, ctx) {
|
|
97
|
+
* ctx.addPlugin({ from: new URL('./plugin.js', import.meta.url), options });
|
|
98
|
+
* ctx.addImports('@wolfstar/plugin-cache');
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare function defineModule<Options extends ModuleOptions = ModuleOptions>(module: StarsModule<Options>): StarsModule<Options>;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/errors.d.ts
|
|
106
|
+
/** Why a module could not be installed. `moduleName` is the module's `meta.name`, or the specifier it was listed by. */
|
|
107
|
+
export declare class ModuleError extends Error {
|
|
108
|
+
readonly code: ModuleErrorCode;
|
|
109
|
+
readonly moduleName: string;
|
|
110
|
+
constructor(code: ModuleErrorCode, moduleName: string, message: string, cause?: unknown);
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/options.d.ts
|
|
114
|
+
/**
|
|
115
|
+
* Merges `options` over `defaults`: plain objects merge deeply, everything else (arrays included) is replaced, and an
|
|
116
|
+
* `undefined` value in `options` keeps the default.
|
|
117
|
+
*/
|
|
118
|
+
export declare function mergeOptions<T extends Record<string, unknown>>(defaults: T | undefined, options: Record<string, unknown> | undefined): T;
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/setup.d.ts
|
|
121
|
+
interface SetupModulesOptions {
|
|
122
|
+
/** The resolved configuration: `root` and `modules` are read from it. */
|
|
123
|
+
config: ResolvedStarsConfig;
|
|
124
|
+
hooks: ModuleHookHost;
|
|
125
|
+
/** Imports a module specifier from the project. Its default export (or the namespace itself) is the module. */
|
|
126
|
+
load(specifier: string): Promise<unknown>;
|
|
127
|
+
versions?: ModuleVersions;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Installs the modules listed in `config.modules`, in order and each dependency first, and returns what they
|
|
131
|
+
* contributed. A module that is already installed (by `meta.name`) is skipped.
|
|
132
|
+
*/
|
|
133
|
+
export declare function setupModules(options: SetupModulesOptions): Promise<ModulesRuntime$1>;
|
|
134
|
+
//#endregion
|
|
135
|
+
export type { InstalledModule, ModuleContext, ModuleErrorCode, ModuleHookHost, ModuleInput, ModuleMeta, ModuleOptions, ModulePluginSource, ModuleVersions, ModulesRuntime, RuntimePluginRegistration, SetupModulesOptions, StarsModule };
|
|
136
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["ModulesRuntime"],"sources":["../../src/types.ts","../../src/define.ts","../../src/errors.ts","../../src/options.ts","../../src/setup.ts"],"mappings":";;KAEK,UAAU,KAAK,IAAI,YAAY;;KAGxB,gBAAgB;UAEX;;EAEhB;EACA;;EAEA;;EAEA;;IAEC;;IAEA;;;;UAKe;EAChB;EACA;;;;;;UAOgB;EAChB,KAAK,aAAa,eAAe,MAAM,MAAM,UAAU,WAAW;EAClE,SAAS,aAAa,eAAe,MAAM,SAAS,MAAM,WAAW,WAAW,SAAS;;;;;;;;UASzE;;;;;EAKhB,eAAe;;EAEf;;EAEA;;;KAIW,YAAY,gBAAgB,gBAAgB,0BAA0B,YAAY;UAE7E;;WAEP;;WAEA,QAAQ;WACR,UAAU,SAAS;;EAE5B,UAAU,iBAAiB,MAAM;;EAEjC,WAAW;IAAmB;;;EAE9B,MAAM;;EAEN,UAAU;;EAEV,cAAc,gBAAgB,eAAe,QAAQ,YAAY,UAAU,UAAU,QAAQ,WAAW;;UAGxF,YAAY,gBAAgB,gBAAgB;EAC5D,MAAM;;EAEN,WAAW;;EAEX,wBAAwB;;EAExB,WAAW,QAAQ,iBAAiB,WAAW;;EAE/C,OAAO,SAAS,SAAS,KAAK,gBAAgB;;KAGnC;;;;;;;;;;;;;;;;;;;;;wBCpEI,aAAa,gBAAgB,gBAAgB,eAAe,QAAQ,YAAY,WAAW,YAAY;;;;qBCjB1G,oBAAoB;WAChB,MAAM;WACN;EAEhB,YAAmB,MAAM,iBAAiB,oBAAoB,iBAAiB;;;;;;;;wBCChE,aAAa,UAAU,yBAAyB,UAAU,eAAe,SAAS,sCAAsC;;;UCKvH;;EAEhB,QAAQ;EACR,OAAO;;EAEP,KAAK,oBAAoB;EACzB,WAAW;;;;;;wBAOU,aAAa,SAAS,sBAAsB,QAAQA"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { STARS_HOOK_NAMES } from "@wolfstar/schema";
|
|
2
|
+
import { satisfies } from "semver";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
//#region src/define.ts
|
|
6
|
+
/**
|
|
7
|
+
* Defines an installable module. This is an identity helper that types the definition; the module is installed by
|
|
8
|
+
* listing its package in `modules` in `stars.config`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { defineModule } from '@wolfstar/kit';
|
|
13
|
+
*
|
|
14
|
+
* export default defineModule<{ ttl: number }>({
|
|
15
|
+
* meta: { name: '@wolfstar/plugin-cache', compatibility: { framework: '>=6.1.0' } },
|
|
16
|
+
* defaults: { ttl: 60_000 },
|
|
17
|
+
* setup(options, ctx) {
|
|
18
|
+
* ctx.addPlugin({ from: new URL('./plugin.js', import.meta.url), options });
|
|
19
|
+
* ctx.addImports('@wolfstar/plugin-cache');
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function defineModule(module) {
|
|
25
|
+
return module;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region \0@oxc-project+runtime@0.151.0/helpers/esm/typeof.js
|
|
30
|
+
function _typeof(o) {
|
|
31
|
+
"@babel/helpers - typeof";
|
|
32
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
33
|
+
return typeof o;
|
|
34
|
+
} : function(o) {
|
|
35
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
36
|
+
}, _typeof(o);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region \0@oxc-project+runtime@0.151.0/helpers/esm/toPrimitive.js
|
|
41
|
+
function toPrimitive(t, r) {
|
|
42
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
43
|
+
var e = t[Symbol.toPrimitive];
|
|
44
|
+
if (void 0 !== e) {
|
|
45
|
+
var i = e.call(t, r || "default");
|
|
46
|
+
if ("object" != _typeof(i)) return i;
|
|
47
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
48
|
+
}
|
|
49
|
+
return ("string" === r ? String : Number)(t);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region \0@oxc-project+runtime@0.151.0/helpers/esm/toPropertyKey.js
|
|
54
|
+
function toPropertyKey(t) {
|
|
55
|
+
var i = toPrimitive(t, "string");
|
|
56
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region \0@oxc-project+runtime@0.151.0/helpers/esm/defineProperty.js
|
|
61
|
+
function _defineProperty(e, r, t) {
|
|
62
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
63
|
+
value: t,
|
|
64
|
+
enumerable: !0,
|
|
65
|
+
configurable: !0,
|
|
66
|
+
writable: !0
|
|
67
|
+
}) : e[r] = t, e;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/errors.ts
|
|
72
|
+
/** Why a module could not be installed. `moduleName` is the module's `meta.name`, or the specifier it was listed by. */
|
|
73
|
+
var ModuleError = class extends Error {
|
|
74
|
+
constructor(code, moduleName, message, cause) {
|
|
75
|
+
super(message, cause === void 0 ? void 0 : { cause });
|
|
76
|
+
_defineProperty(this, "code", void 0);
|
|
77
|
+
_defineProperty(this, "moduleName", void 0);
|
|
78
|
+
this.name = "ModuleError";
|
|
79
|
+
this.code = code;
|
|
80
|
+
this.moduleName = moduleName;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/options.ts
|
|
86
|
+
function isPlainObject(value) {
|
|
87
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Merges `options` over `defaults`: plain objects merge deeply, everything else (arrays included) is replaced, and an
|
|
91
|
+
* `undefined` value in `options` keeps the default.
|
|
92
|
+
*/
|
|
93
|
+
function mergeOptions(defaults, options) {
|
|
94
|
+
const merged = { ...defaults };
|
|
95
|
+
for (const [key, value] of Object.entries(options ?? {})) {
|
|
96
|
+
if (value === void 0) continue;
|
|
97
|
+
const base = merged[key];
|
|
98
|
+
merged[key] = isPlainObject(base) && isPlainObject(value) ? mergeOptions(base, value) : value;
|
|
99
|
+
}
|
|
100
|
+
return merged;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/setup.ts
|
|
105
|
+
/**
|
|
106
|
+
* Installs the modules listed in `config.modules`, in order and each dependency first, and returns what they
|
|
107
|
+
* contributed. A module that is already installed (by `meta.name`) is skipped.
|
|
108
|
+
*/
|
|
109
|
+
async function setupModules(options) {
|
|
110
|
+
const { config, hooks, load, versions = {} } = options;
|
|
111
|
+
const installed = /* @__PURE__ */ new Set();
|
|
112
|
+
const modules = [];
|
|
113
|
+
const plugins = [];
|
|
114
|
+
const imports = /* @__PURE__ */ new Set();
|
|
115
|
+
async function resolveModule(input) {
|
|
116
|
+
if (typeof input !== "string") return validate(input, input.meta?.name ?? "(inline)");
|
|
117
|
+
let loaded;
|
|
118
|
+
try {
|
|
119
|
+
loaded = await load(input);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
throw new ModuleError("MODULE_LOAD_FAILED", input, `The module "${input}" could not be loaded: ${message(error)}`, error);
|
|
122
|
+
}
|
|
123
|
+
const exported = loaded?.default ?? loaded;
|
|
124
|
+
return validate(exported?.default ?? exported, input);
|
|
125
|
+
}
|
|
126
|
+
async function install(input, userOptions) {
|
|
127
|
+
const module = await resolveModule(input);
|
|
128
|
+
const { name } = module.meta;
|
|
129
|
+
if (installed.has(name)) return;
|
|
130
|
+
installed.add(name);
|
|
131
|
+
checkCompatibility(module, versions);
|
|
132
|
+
for (const dependency of module.dependencies ?? []) await install(dependency, void 0);
|
|
133
|
+
for (const [hook, callback] of Object.entries(module.hooks ?? {})) {
|
|
134
|
+
if (!KNOWN_HOOKS.has(hook) || typeof callback !== "function") throw new ModuleError("MODULE_INVALID", name, KNOWN_HOOKS.has(hook) ? `The hook "${hook}" of the module "${name}" must be a function.` : `The module "${name}" declares an unknown hook "${hook}". Known hooks: ${STARS_HOOK_NAMES.join(", ")}.`);
|
|
135
|
+
hooks.hook(hook, callback);
|
|
136
|
+
}
|
|
137
|
+
const installedModule = module.meta.version === void 0 ? { name } : {
|
|
138
|
+
name,
|
|
139
|
+
version: module.meta.version
|
|
140
|
+
};
|
|
141
|
+
const context = {
|
|
142
|
+
root: config.root,
|
|
143
|
+
config,
|
|
144
|
+
versions,
|
|
145
|
+
addPlugin: (plugin) => plugins.push(toRegistration(name, plugin)),
|
|
146
|
+
addImports: (preset) => void imports.add(typeof preset === "string" ? preset : preset.from),
|
|
147
|
+
hook: (hookName, callback) => hooks.hook(hookName, callback),
|
|
148
|
+
callHook: (hookName, ...args) => hooks.callHook(hookName, ...args),
|
|
149
|
+
installModule: (nested, nestedOptions) => install(nested, nestedOptions)
|
|
150
|
+
};
|
|
151
|
+
try {
|
|
152
|
+
await module.setup?.(mergeOptions(module.defaults, userOptions), context);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (error instanceof ModuleError) throw error;
|
|
155
|
+
throw new ModuleError("MODULE_SETUP_FAILED", name, `The module "${name}" failed during setup: ${message(error)}`, error);
|
|
156
|
+
}
|
|
157
|
+
modules.push(installedModule);
|
|
158
|
+
}
|
|
159
|
+
for (const entry of config.modules) await install(entry.specifier, entry.options);
|
|
160
|
+
return {
|
|
161
|
+
modules,
|
|
162
|
+
plugins,
|
|
163
|
+
imports: [...imports]
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const KNOWN_HOOKS = new Set(STARS_HOOK_NAMES);
|
|
167
|
+
function validate(value, label) {
|
|
168
|
+
const module = value;
|
|
169
|
+
if (module === null || typeof module !== "object" || typeof module.meta?.name !== "string" || module.meta.name === "") throw new ModuleError("MODULE_INVALID", label, `"${label}" is not a Stars module: its default export must be created with \`defineModule\` and have a \`meta.name\`.`);
|
|
170
|
+
if (module.setup !== void 0 && typeof module.setup !== "function") throw new ModuleError("MODULE_INVALID", module.meta.name, `The \`setup\` of the module "${module.meta.name}" must be a function.`);
|
|
171
|
+
return module;
|
|
172
|
+
}
|
|
173
|
+
function checkCompatibility(module, versions) {
|
|
174
|
+
const { name, compatibility } = module.meta;
|
|
175
|
+
for (const [key, label] of [["framework", "@wolfstar/http-framework"], ["stars", "@wolfstar/cli"]]) {
|
|
176
|
+
const range = compatibility?.[key];
|
|
177
|
+
const version = versions[key];
|
|
178
|
+
if (range === void 0 || version === void 0 || version === null) continue;
|
|
179
|
+
if (!satisfies(version, range, { includePrerelease: true })) throw new ModuleError("MODULE_INCOMPATIBLE", name, `The module "${name}" needs ${label} ${range}, but ${version} is installed.`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const RELATIVE_PATH = /^\.\.?(?:[/\\]|$)/;
|
|
183
|
+
function toRegistration(moduleName, plugin) {
|
|
184
|
+
const source = typeof plugin === "string" || plugin instanceof URL ? { from: plugin } : plugin;
|
|
185
|
+
const from = typeof source.from === "string" ? source.from : source.from?.href;
|
|
186
|
+
if (typeof from !== "string" || from === "") throw new ModuleError("MODULE_PLUGIN_INVALID", moduleName, `A plugin added by "${moduleName}" needs a \`from\` specifier, path or URL.`);
|
|
187
|
+
if (RELATIVE_PATH.test(from)) throw new ModuleError("MODULE_PLUGIN_INVALID", moduleName, `The plugin "${from}" added by "${moduleName}" is a relative path, which would resolve differently per build tool. Use a package specifier, an absolute path or a \`file:\` URL, e.g. \`new URL('./plugin.js', import.meta.url)\`.`);
|
|
188
|
+
if (source.options !== void 0 && !isJsonSerialisable(source.options)) throw new ModuleError("MODULE_PLUGIN_INVALID", moduleName, `The options of a plugin added by "${moduleName}" must be JSON-serialisable: they are written into the built entry.`);
|
|
189
|
+
const registration = {
|
|
190
|
+
module: moduleName,
|
|
191
|
+
from: from.startsWith("file:") ? fileURLToPath(from) : from,
|
|
192
|
+
export: source.export ?? "default"
|
|
193
|
+
};
|
|
194
|
+
return source.options === void 0 ? registration : {
|
|
195
|
+
...registration,
|
|
196
|
+
options: source.options
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function isJsonSerialisable(value) {
|
|
200
|
+
try {
|
|
201
|
+
return JSON.stringify(value, (_key, item) => {
|
|
202
|
+
if (typeof item === "function" || typeof item === "symbol" || typeof item === "bigint") throw new TypeError("not serialisable");
|
|
203
|
+
return item;
|
|
204
|
+
}) !== void 0;
|
|
205
|
+
} catch {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function message(error) {
|
|
210
|
+
return error instanceof Error ? error.message : String(error);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
//#endregion
|
|
214
|
+
export { ModuleError, defineModule, mergeOptions, setupModules };
|
|
215
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/define.ts","../../src/errors.ts","../../src/options.ts","../../src/setup.ts"],"sourcesContent":["import type { ModuleOptions, StarsModule } from './types.js';\r\n\r\n/**\r\n * Defines an installable module. This is an identity helper that types the definition; the module is installed by\r\n * listing its package in `modules` in `stars.config`.\r\n *\r\n * @example\r\n * ```ts\r\n * import { defineModule } from '@wolfstar/kit';\r\n *\r\n * export default defineModule<{ ttl: number }>({\r\n * \tmeta: { name: '@wolfstar/plugin-cache', compatibility: { framework: '>=6.1.0' } },\r\n * \tdefaults: { ttl: 60_000 },\r\n * \tsetup(options, ctx) {\r\n * \t\tctx.addPlugin({ from: new URL('./plugin.js', import.meta.url), options });\r\n * \t\tctx.addImports('@wolfstar/plugin-cache');\r\n * \t}\r\n * });\r\n * ```\r\n */\r\nexport function defineModule<Options extends ModuleOptions = ModuleOptions>(module: StarsModule<Options>): StarsModule<Options> {\r\n\treturn module;\r\n}\r\n","import type { ModuleErrorCode } from './types.js';\r\n\r\n/** Why a module could not be installed. `moduleName` is the module's `meta.name`, or the specifier it was listed by. */\r\nexport class ModuleError extends Error {\r\n\tpublic readonly code: ModuleErrorCode;\r\n\tpublic readonly moduleName: string;\r\n\r\n\tpublic constructor(code: ModuleErrorCode, moduleName: string, message: string, cause?: unknown) {\r\n\t\tsuper(message, cause === undefined ? undefined : { cause });\r\n\t\tthis.name = 'ModuleError';\r\n\t\tthis.code = code;\r\n\t\tthis.moduleName = moduleName;\r\n\t}\r\n}\r\n","function isPlainObject(value: unknown): value is Record<string, unknown> {\r\n\treturn value !== null && typeof value === 'object' && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;\r\n}\r\n\r\n/**\r\n * Merges `options` over `defaults`: plain objects merge deeply, everything else (arrays included) is replaced, and an\r\n * `undefined` value in `options` keeps the default.\r\n */\r\nexport function mergeOptions<T extends Record<string, unknown>>(defaults: T | undefined, options: Record<string, unknown> | undefined): T {\r\n\tconst merged: Record<string, unknown> = { ...defaults };\r\n\tfor (const [key, value] of Object.entries(options ?? {})) {\r\n\t\tif (value === undefined) continue;\r\n\t\tconst base = merged[key];\r\n\t\tmerged[key] = isPlainObject(base) && isPlainObject(value) ? mergeOptions(base, value) : value;\r\n\t}\r\n\r\n\treturn merged as T;\r\n}\r\n","import {\r\n\tSTARS_HOOK_NAMES,\r\n\ttype InstalledModule,\r\n\ttype ModulesRuntime,\r\n\ttype ResolvedStarsConfig,\r\n\ttype RuntimePluginRegistration\r\n} from '@wolfstar/schema';\r\nimport { satisfies } from 'semver';\r\nimport { fileURLToPath } from 'node:url';\r\nimport { ModuleError } from './errors.js';\r\nimport { mergeOptions } from './options.js';\r\nimport type { ModuleContext, ModuleHookHost, ModuleInput, ModuleOptions, ModulePluginSource, ModuleVersions, StarsModule } from './types.js';\r\n\r\nexport interface SetupModulesOptions {\r\n\t/** The resolved configuration: `root` and `modules` are read from it. */\r\n\tconfig: ResolvedStarsConfig;\r\n\thooks: ModuleHookHost;\r\n\t/** Imports a module specifier from the project. Its default export (or the namespace itself) is the module. */\r\n\tload(specifier: string): Promise<unknown>;\r\n\tversions?: ModuleVersions;\r\n}\r\n\r\n/**\r\n * Installs the modules listed in `config.modules`, in order and each dependency first, and returns what they\r\n * contributed. A module that is already installed (by `meta.name`) is skipped.\r\n */\r\nexport async function setupModules(options: SetupModulesOptions): Promise<ModulesRuntime> {\r\n\tconst { config, hooks, load, versions = {} } = options;\r\n\tconst installed = new Set<string>();\r\n\tconst modules: InstalledModule[] = [];\r\n\tconst plugins: RuntimePluginRegistration[] = [];\r\n\tconst imports = new Set<string>();\r\n\r\n\tasync function resolveModule(input: ModuleInput): Promise<StarsModule> {\r\n\t\tif (typeof input !== 'string') return validate(input, input.meta?.name ?? '(inline)');\r\n\r\n\t\tlet loaded: unknown;\r\n\t\ttry {\r\n\t\t\tloaded = await load(input);\r\n\t\t} catch (error) {\r\n\t\t\tthrow new ModuleError('MODULE_LOAD_FAILED', input, `The module \"${input}\" could not be loaded: ${message(error)}`, error);\r\n\t\t}\r\n\r\n\t\tconst exported = (loaded as { default?: unknown } | null)?.default ?? loaded;\r\n\t\t// A CommonJS package imported from ESM nests its `exports.default` under `default`.\r\n\t\tconst unwrapped = (exported as { default?: unknown } | null)?.default ?? exported;\r\n\t\treturn validate(unwrapped, input);\r\n\t}\r\n\r\n\tasync function install(input: ModuleInput, userOptions: Record<string, unknown> | undefined): Promise<void> {\r\n\t\tconst module = await resolveModule(input);\r\n\t\tconst { name } = module.meta;\r\n\t\tif (installed.has(name)) return;\r\n\t\tinstalled.add(name);\r\n\r\n\t\tcheckCompatibility(module, versions);\r\n\t\tfor (const dependency of module.dependencies ?? []) await install(dependency, undefined);\r\n\r\n\t\tfor (const [hook, callback] of Object.entries(module.hooks ?? {})) {\r\n\t\t\tif (!KNOWN_HOOKS.has(hook) || typeof callback !== 'function') {\r\n\t\t\t\tthrow new ModuleError(\r\n\t\t\t\t\t'MODULE_INVALID',\r\n\t\t\t\t\tname,\r\n\t\t\t\t\tKNOWN_HOOKS.has(hook)\r\n\t\t\t\t\t\t? `The hook \"${hook}\" of the module \"${name}\" must be a function.`\r\n\t\t\t\t\t\t: `The module \"${name}\" declares an unknown hook \"${hook}\". Known hooks: ${STARS_HOOK_NAMES.join(', ')}.`\r\n\t\t\t\t);\r\n\t\t\t}\r\n\r\n\t\t\thooks.hook(hook as never, callback as never);\r\n\t\t}\r\n\r\n\t\tconst installedModule: InstalledModule = module.meta.version === undefined ? { name } : { name, version: module.meta.version };\r\n\t\tconst context: ModuleContext = {\r\n\t\t\troot: config.root,\r\n\t\t\tconfig,\r\n\t\t\tversions,\r\n\t\t\taddPlugin: (plugin) => plugins.push(toRegistration(name, plugin)),\r\n\t\t\taddImports: (preset) => void imports.add(typeof preset === 'string' ? preset : preset.from),\r\n\t\t\thook: (hookName, callback) => hooks.hook(hookName, callback),\r\n\t\t\tcallHook: (hookName, ...args) => hooks.callHook(hookName, ...args),\r\n\t\t\tinstallModule: (nested, nestedOptions) => install(nested as ModuleInput, nestedOptions)\r\n\t\t};\r\n\r\n\t\ttry {\r\n\t\t\tawait module.setup?.(mergeOptions<ModuleOptions>(module.defaults, userOptions), context);\r\n\t\t} catch (error) {\r\n\t\t\tif (error instanceof ModuleError) throw error;\r\n\t\t\tthrow new ModuleError('MODULE_SETUP_FAILED', name, `The module \"${name}\" failed during setup: ${message(error)}`, error);\r\n\t\t}\r\n\r\n\t\tmodules.push(installedModule);\r\n\t}\r\n\r\n\tfor (const entry of config.modules) await install(entry.specifier, entry.options as Record<string, unknown>);\r\n\r\n\treturn { modules, plugins, imports: [...imports] };\r\n}\r\n\r\nconst KNOWN_HOOKS: ReadonlySet<string> = new Set(STARS_HOOK_NAMES);\r\n\r\nfunction validate(value: unknown, label: string): StarsModule {\r\n\tconst module = value as Partial<StarsModule> | null;\r\n\tif (module === null || typeof module !== 'object' || typeof module.meta?.name !== 'string' || module.meta.name === '') {\r\n\t\tthrow new ModuleError(\r\n\t\t\t'MODULE_INVALID',\r\n\t\t\tlabel,\r\n\t\t\t`\"${label}\" is not a Stars module: its default export must be created with \\`defineModule\\` and have a \\`meta.name\\`.`\r\n\t\t);\r\n\t}\r\n\r\n\tif (module.setup !== undefined && typeof module.setup !== 'function') {\r\n\t\tthrow new ModuleError('MODULE_INVALID', module.meta.name, `The \\`setup\\` of the module \"${module.meta.name}\" must be a function.`);\r\n\t}\r\n\r\n\treturn module as StarsModule;\r\n}\r\n\r\nfunction checkCompatibility(module: StarsModule, versions: ModuleVersions): void {\r\n\tconst { name, compatibility } = module.meta;\r\n\tfor (const [key, label] of [\r\n\t\t['framework', '@wolfstar/http-framework'],\r\n\t\t['stars', '@wolfstar/cli']\r\n\t] as const) {\r\n\t\tconst range = compatibility?.[key];\r\n\t\tconst version = versions[key];\r\n\t\tif (range === undefined || version === undefined || version === null) continue;\r\n\t\tif (!satisfies(version, range, { includePrerelease: true })) {\r\n\t\t\tthrow new ModuleError('MODULE_INCOMPATIBLE', name, `The module \"${name}\" needs ${label} ${range}, but ${version} is installed.`);\r\n\t\t}\r\n\t}\r\n}\r\n\r\nconst RELATIVE_PATH = /^\\.\\.?(?:[/\\\\]|$)/;\r\n\r\nfunction toRegistration(moduleName: string, plugin: string | URL | ModulePluginSource): RuntimePluginRegistration {\r\n\tconst source: ModulePluginSource = typeof plugin === 'string' || plugin instanceof URL ? { from: plugin } : plugin;\r\n\tconst from = typeof source.from === 'string' ? source.from : source.from?.href;\r\n\tif (typeof from !== 'string' || from === '') {\r\n\t\tthrow new ModuleError('MODULE_PLUGIN_INVALID', moduleName, `A plugin added by \"${moduleName}\" needs a \\`from\\` specifier, path or URL.`);\r\n\t}\r\n\r\n\tif (RELATIVE_PATH.test(from)) {\r\n\t\tthrow new ModuleError(\r\n\t\t\t'MODULE_PLUGIN_INVALID',\r\n\t\t\tmoduleName,\r\n\t\t\t`The plugin \"${from}\" added by \"${moduleName}\" is a relative path, which would resolve differently per build tool. Use a package specifier, an absolute path or a \\`file:\\` URL, e.g. \\`new URL('./plugin.js', import.meta.url)\\`.`\r\n\t\t);\r\n\t}\r\n\r\n\tif (source.options !== undefined && !isJsonSerialisable(source.options)) {\r\n\t\tthrow new ModuleError(\r\n\t\t\t'MODULE_PLUGIN_INVALID',\r\n\t\t\tmoduleName,\r\n\t\t\t`The options of a plugin added by \"${moduleName}\" must be JSON-serialisable: they are written into the built entry.`\r\n\t\t);\r\n\t}\r\n\r\n\tconst registration = { module: moduleName, from: from.startsWith('file:') ? fileURLToPath(from) : from, export: source.export ?? 'default' };\r\n\treturn source.options === undefined ? registration : { ...registration, options: source.options };\r\n}\r\n\r\nfunction isJsonSerialisable(value: unknown): boolean {\r\n\ttry {\r\n\t\treturn (\r\n\t\t\tJSON.stringify(value, (_key, item: unknown) => {\r\n\t\t\t\tif (typeof item === 'function' || typeof item === 'symbol' || typeof item === 'bigint') throw new TypeError('not serialisable');\r\n\t\t\t\treturn item;\r\n\t\t\t}) !== undefined\r\n\t\t);\r\n\t} catch {\r\n\t\treturn false;\r\n\t}\r\n}\r\n\r\nfunction message(error: unknown): string {\r\n\treturn error instanceof Error ? error.message : String(error);\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,aAA4D,QAAoD;CAC/H,OAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACnBA,IAAa,cAAb,cAAiC,MAAM;CAItC,AAAO,YAAY,MAAuB,YAAoB,SAAiB,OAAiB;EAC/F,MAAM,SAAS,UAAU,SAAY,SAAY,EAAE,MAAM,CAAC;wBAJ3C;wBACA;EAIf,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,aAAa;CACnB;AACD;;;;ACbA,SAAS,cAAc,OAAkD;CACxE,OAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,KAAK,OAAO,eAAe,KAAK,MAAM,OAAO;AACxH;;;;;AAMA,SAAgB,aAAgD,UAAyB,SAAiD;CACzI,MAAM,SAAkC,EAAE,GAAG,SAAS;CACtD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,WAAW,CAAC,CAAC,GAAG;EACz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AAI;EACH,OACC,KAAK,UAAU,QAAQ,MAAM,SAAkB;GAC9C,IAAI,OAAO,SAAS,cAAc,OAAO,SAAS,YAAY,OAAO,SAAS,UAAU,MAAM,IAAI,UAAU,kBAAkB;GAC9H,OAAO;EACR,CAAC,MAAM;CAET,QAAQ;EACP,OAAO;CACR;AACD;AAEA,SAAS,QAAQ,OAAwB;CACxC,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAC7D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wolfstar/kit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "defineModule and the module setup runtime for installable Stars modules, the @nuxt/kit counterpart",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"api",
|
|
7
|
+
"discord",
|
|
8
|
+
"http",
|
|
9
|
+
"kit",
|
|
10
|
+
"modules",
|
|
11
|
+
"pnpm",
|
|
12
|
+
"stars",
|
|
13
|
+
"ts",
|
|
14
|
+
"typescript",
|
|
15
|
+
"wolfstar"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/wolfstar-project/stars-components/tree/main/packages/kit",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/wolfstar-project/stars-components/issues"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": "@wolfstar",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/wolfstar-project/stars-components.git",
|
|
26
|
+
"directory": "packages/kit"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/"
|
|
30
|
+
],
|
|
31
|
+
"type": "module",
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"main": "dist/esm/index.js",
|
|
34
|
+
"module": "dist/esm/index.js",
|
|
35
|
+
"types": "dist/esm/index.d.ts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"import": {
|
|
39
|
+
"types": "./dist/esm/index.d.ts",
|
|
40
|
+
"default": "./dist/esm/index.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"provenance": true
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@wolfstar/schema": "^0.5.0",
|
|
51
|
+
"semver": "^7.8.5"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/semver": "^7.8.0",
|
|
55
|
+
"@vitest/coverage-v8": "^5.0.1",
|
|
56
|
+
"golar": "^0.1.10",
|
|
57
|
+
"tsdown": "^0.23.0",
|
|
58
|
+
"typescript": "~7.0.2",
|
|
59
|
+
"vitest": "^5.0.1"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=20"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"build": "tsdown --config-loader unrun",
|
|
67
|
+
"watch": "tsdown --config-loader unrun --watch",
|
|
68
|
+
"typecheck": "golar tsc -p ../../tsconfig.json",
|
|
69
|
+
"lint": "oxlint src --fix"
|
|
70
|
+
}
|
|
71
|
+
}
|