@makoojs/cli 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.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.CN.md +373 -0
  3. package/README.md +373 -0
  4. package/dist/bin.js +1506 -0
  5. package/dist/command/_util.d.ts +16 -0
  6. package/dist/command/add.d.ts +1 -0
  7. package/dist/command/bin.d.ts +2 -0
  8. package/dist/command/build.d.ts +1 -0
  9. package/dist/command/dev.d.ts +1 -0
  10. package/dist/command/inspect.d.ts +1 -0
  11. package/dist/config/config.d.ts +3 -0
  12. package/dist/config/defaults.d.ts +26 -0
  13. package/dist/config/resolve.d.ts +17 -0
  14. package/dist/config/types.d.ts +140 -0
  15. package/dist/config/validation.d.ts +15 -0
  16. package/dist/error/error.d.ts +46 -0
  17. package/dist/generator/generator.d.ts +3 -0
  18. package/dist/generator/render/import/importAdapter.d.ts +3 -0
  19. package/dist/generator/render/import/importComp.d.ts +3 -0
  20. package/dist/generator/render/import/importInjector.d.ts +1 -0
  21. package/dist/generator/render/import/importRuntimeSetup.d.ts +1 -0
  22. package/dist/generator/render/init/initInjector.d.ts +3 -0
  23. package/dist/generator/render/init/registerComp.d.ts +2 -0
  24. package/dist/generator/render/run/renderInjectorRun.d.ts +1 -0
  25. package/dist/generator/render/util/value.d.ts +1 -0
  26. package/dist/generator/types.d.ts +21 -0
  27. package/dist/index.cjs +86 -0
  28. package/dist/index.d.ts +5 -0
  29. package/dist/index.js +5090 -0
  30. package/dist/monkey/clipboard.d.ts +3 -0
  31. package/dist/monkey/download.d.ts +3 -0
  32. package/dist/monkey/index.cjs +1 -0
  33. package/dist/monkey/index.d.ts +60 -0
  34. package/dist/monkey/index.js +60 -0
  35. package/dist/monkey/info.d.ts +5 -0
  36. package/dist/monkey/menu.d.ts +4 -0
  37. package/dist/monkey/notification.d.ts +3 -0
  38. package/dist/monkey/request.d.ts +7 -0
  39. package/dist/monkey/resource.d.ts +4 -0
  40. package/dist/monkey/storage.d.ts +11 -0
  41. package/dist/monkey/style.d.ts +4 -0
  42. package/dist/monkey/tab.d.ts +6 -0
  43. package/dist/scanner/collectDependenics.d.ts +2 -0
  44. package/dist/scanner/load/loadManifes.d.ts +3 -0
  45. package/dist/scanner/load/loadMeta.d.ts +2 -0
  46. package/dist/scanner/scanner.d.ts +3 -0
  47. package/dist/scanner/types.d.ts +23 -0
  48. package/dist/scanner/util.d.ts +2 -0
  49. package/dist/scanner/validation.d.ts +121 -0
  50. package/dist/vitePlugin/hmrController.d.ts +4 -0
  51. package/dist/vitePlugin/makoo.d.ts +3 -0
  52. package/dist/vitePlugin/makooMonkey.d.ts +3 -0
  53. package/dist/vitePlugin/types.d.ts +12 -0
  54. package/dist/vitePlugin/virtualModule.d.ts +2 -0
  55. package/dist/vitePlugin/watchList.d.ts +4 -0
  56. package/package.json +54 -0
package/README.md ADDED
@@ -0,0 +1,373 @@
1
+ # @makoojs/cli
2
+
3
+ `@makoojs/cli` is the main entry point for Makoo projects. It provides the Vite plugin, CLI commands, manifest type helpers, injection scanning, virtual entry generation, and connects userscript development, build, and install flows to [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey).
4
+
5
+ If you are building a regular Makoo userscript project, you should usually start from this package. `@makoojs/core` provides the low-level injection runtime, `@makoojs/vue` and `@makoojs/react` provide component mounting adapters, and `@makoojs/cli` organizes them into the Vite and userscript build flow.
6
+
7
+ ## Use Cases
8
+
9
+ - Develop Makoo userscript projects with Vite.
10
+ - Scan injection modules from the `injections` directory and generate runtime code.
11
+ - Write `injections/manifest.ts` and module-level `manifest.ts` files.
12
+ - Generate userscript metadata, dev entries, and build output through [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey).
13
+ - Use commands such as `makoo dev`, `makoo build`, `makoo add`, and `makoo inspect`.
14
+ - Use Makoo's stable GM API entry through `@makoojs/cli/monkey`.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ // npm install @makoojs/cli
20
+ // yarn add @makoojs/cli
21
+ pnpm add @makoojs/cli
22
+ ```
23
+
24
+ If you create a project with `@makoojs/create-makoo`, `@makoojs/cli` is usually configured for you.
25
+
26
+ ## Minimal Vite Config
27
+
28
+ `makoo()` returns an array of Vite plugins, so it is usually spread into `plugins` with `...makoo(...)`.
29
+
30
+ ```ts
31
+ import { defineConfig } from 'vite';
32
+ import { makoo } from '@makoojs/cli';
33
+ import vue from '@vitejs/plugin-vue';
34
+
35
+ export default defineConfig({
36
+ plugins: [
37
+ vue(),
38
+ ...makoo({
39
+ app: {
40
+ name: 'my-userscript',
41
+ version: '0.0.1',
42
+ description: 'My first Makoo script'
43
+ },
44
+ monkey: {
45
+ userscript: {
46
+ namespace: 'https://example.com',
47
+ match: ['https://example.com/*']
48
+ }
49
+ }
50
+ })
51
+ ]
52
+ });
53
+ ```
54
+
55
+ `makoo()` first injects Makoo's own scanning and virtual entry plugin, then connects to [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) for userscript development and build behavior.
56
+
57
+ ## Recommended Project Structure
58
+
59
+ Makoo scans the `injections` directory under the project root by default.
60
+
61
+ ```txt
62
+ .
63
+ ├─ vite.config.ts
64
+ └─ injections
65
+ ├─ manifest.ts
66
+ ├─ profile-card
67
+ │ ├─ app.vue
68
+ │ └─ manifest.ts
69
+ └─ react-badge
70
+ ├─ app.tsx
71
+ └─ manifest.ts
72
+ ```
73
+
74
+ The top-level `injections/manifest.ts` declares global injection config and the module list. A module-level `injections/<module>/manifest.ts` can override or add config for a single module, which is useful when a module should own fields such as `injectAt`, `component`, `framework`, `match`, or lifecycle hooks.
75
+
76
+ ## Manifest Basics
77
+
78
+ `@makoojs/cli` exports `defineInjections()` and `defineInjection()` to provide type constraints for manifests.
79
+
80
+ Object form is suitable for most projects:
81
+
82
+ ```ts
83
+ import { defineInjections } from '@makoojs/cli';
84
+
85
+ export default defineInjections({
86
+ globalInjector: {
87
+ alive: false,
88
+ scope: 'local',
89
+ timeout: 5000
90
+ },
91
+ injections: {
92
+ profile: {
93
+ injectAt: '#app',
94
+ component: './profile-card/app.vue',
95
+ framework: 'Vue',
96
+ match: {
97
+ include: ['https://example.com/users/*'],
98
+ exclude: ['https://example.com/users/settings']
99
+ }
100
+ },
101
+ badge: {
102
+ injectAt: 'body',
103
+ component: './react-badge/app.tsx',
104
+ framework: 'React'
105
+ }
106
+ }
107
+ });
108
+ ```
109
+
110
+ Array form is useful for generated entries or cases where you need to declare `name` explicitly:
111
+
112
+ ```ts
113
+ import { defineInjections } from '@makoojs/cli';
114
+
115
+ export default defineInjections({
116
+ injections: [
117
+ {
118
+ name: 'profile',
119
+ injectAt: '#app',
120
+ component: './profile-card/app.vue',
121
+ framework: 'Vue'
122
+ }
123
+ ]
124
+ });
125
+ ```
126
+
127
+ Module-level manifests can use `defineInjection()`:
128
+
129
+ ```ts
130
+ import { defineInjection } from '@makoojs/cli';
131
+
132
+ export default defineInjection({
133
+ injectAt: '#app',
134
+ component: './app.vue',
135
+ framework: 'Vue',
136
+ alive: true
137
+ });
138
+ ```
139
+
140
+ Common fields:
141
+
142
+ | Field | Description |
143
+ | --- | --- |
144
+ | `injectAt` | Target DOM selector |
145
+ | `component` | Component path, relative to the top-level manifest or the module directory |
146
+ | `framework` | `Vue`, `React`, or `auto`; when omitted, Makoo infers it from the file extension |
147
+ | `enabled` | Whether the module is enabled, defaults to `true` |
148
+ | `match` | Module-level URL matching rule |
149
+ | `alive` | Whether to retry injection after the target DOM is removed |
150
+ | `scope` | Alive observation scope, supports `local` and `global` |
151
+ | `timeout` | Timeout for waiting for the target DOM |
152
+ | `hooks` | Lifecycle observation hooks passed to `@makoojs/core` |
153
+
154
+ `match` supports array shorthand:
155
+
156
+ ```ts
157
+ match: ['https://example.com/*']
158
+ ```
159
+
160
+ It also supports an include/exclude object:
161
+
162
+ ```ts
163
+ match: {
164
+ include: ['https://example.com/*'],
165
+ exclude: ['https://example.com/admin/*']
166
+ }
167
+ ```
168
+
169
+ `match` is a module-level filter. Which pages the userscript itself runs on is still controlled by userscript metadata such as `monkey.userscript.match`.
170
+
171
+ ## Configuration Overview
172
+
173
+ `makoo()` has four main config areas:
174
+
175
+ ```ts
176
+ makoo({
177
+ app: {
178
+ name: 'my-script',
179
+ version: '0.0.1',
180
+ description: 'demo script'
181
+ },
182
+ source: {
183
+ include: ['*'],
184
+ exclude: []
185
+ },
186
+ injector: {
187
+ alive: false,
188
+ scope: 'local',
189
+ timeout: 5000
190
+ },
191
+ runtime: {
192
+ setup: ['./injections/vue-setup.ts']
193
+ },
194
+ monkey: {
195
+ userscript: {
196
+ match: ['https://example.com/*']
197
+ }
198
+ }
199
+ });
200
+ ```
201
+
202
+ | Config | Description |
203
+ | --- | --- |
204
+ | `app` | Generates userscript `name`, `version`, and `description` |
205
+ | `source` | Controls which injection module directories are scanned |
206
+ | `injector` | Global injection defaults passed to `@makoojs/core` |
207
+ | `runtime` | Controls runtime setup imports in the Makoo-generated entry |
208
+ | `monkey` | Most options are passed through to [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) |
209
+
210
+ `source.include` and `source.exclude` filter module directory names under `injections`, not page URLs. The default include is `['*']`, and the default exclude is `[]`.
211
+
212
+ Most `monkey` options are passed through to [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) for userscript metadata, dev server behavior, and build behavior. However, Makoo manages these fields internally:
213
+
214
+ - `entry`: fixed to the virtual entry generated by Makoo.
215
+ - `clientAlias`: fixed to the GM API alias used internally by Makoo.
216
+ - `server.mountGmApi`: managed by Makoo and not user-configurable.
217
+
218
+ By default, `monkey.build.autoGrant` is `true`, so [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) generates `@grant` from the final code.
219
+
220
+ ## CLI Commands
221
+
222
+ After installation, you can use the `makoo` command:
223
+
224
+ | Command | Description |
225
+ | --- | --- |
226
+ | `makoo dev` | Starts the Vite dev server and prints the local URL |
227
+ | `makoo build` | Runs Vite build and generates userscript output |
228
+ | `makoo add <name>` | Creates a new injection module and updates the manifest |
229
+ | `makoo add <name> --framework Vue` | Creates a Vue injection module |
230
+ | `makoo add <name> --framework React` | Creates a React injection module |
231
+ | `makoo inspect` | Prints the resolved Makoo config and scanner result |
232
+
233
+ `makoo add` uses React by default. It creates a component file under `injections/<name>` and writes the module record to `injections/manifest.ts`.
234
+
235
+ ## Scanning And Generation Flow
236
+
237
+ Makoo's Vite plugin runs this flow during dev startup and build:
238
+
239
+ 1. Load `injections/manifest.ts`.
240
+ 2. Scan module directories under `injections` that match `source.include` / `source.exclude`.
241
+ 3. Read module-level `manifest.ts` files and merge them with the top-level manifest.
242
+ 4. Resolve component paths, module IDs, framework, match, alive, scope, timeout, and related config.
243
+ 5. Generate adapter imports based on the frameworks actually used.
244
+ 6. Generate a virtual entry, create an `Injector`, register components, and call `run()`.
245
+ 7. Hand the virtual entry to [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) so it can be bundled as a userscript.
246
+
247
+ If the top-level manifest is missing, no enabled injection modules are found, or the framework cannot be inferred, the CLI throws Makoo's own error types to make the issue easier to locate.
248
+
249
+ ## HMR Behavior
250
+
251
+ In dev mode, Makoo separates structural changes from normal component updates.
252
+
253
+ | Change | Behavior |
254
+ | --- | --- |
255
+ | Top-level `injections/manifest.ts` changes | Rescan and update the virtual entry |
256
+ | Module-level `injections/<module>/manifest.ts` changes | Rescan and update the virtual entry |
257
+ | Local helpers statically imported by a manifest change | Track dependencies, rescan, and update the virtual entry |
258
+ | Module directory is added or removed | Rescan and update the virtual entry |
259
+ | Regular component files change | Let Vite handle native HMR |
260
+ | Third-party dependencies change | Not tracked as Makoo structural scan dependencies |
261
+
262
+ Structural changes update the virtual entry. Changes inside regular components keep Vite's own hot update experience.
263
+
264
+ ## Runtime Setup
265
+
266
+ `runtime.setup` imports side-effect-only runtime files before Makoo initializes adapters, registers injections, and calls `injector.run()`.
267
+
268
+ Use it to register Vue plugins, initialize GM helpers, import global styles, or run analytics setup.
269
+
270
+ ```ts
271
+ // vite.config.ts
272
+ import { defineConfig } from 'vite';
273
+ import { makoo } from '@makoojs/cli';
274
+
275
+ export default defineConfig({
276
+ plugins: [
277
+ ...makoo({
278
+ app: {
279
+ name: 'my-script',
280
+ version: '0.0.1'
281
+ },
282
+ runtime: {
283
+ setup: ['./injections/vue-setup.ts']
284
+ },
285
+ monkey: {
286
+ userscript: {
287
+ match: ['https://example.com/*']
288
+ }
289
+ }
290
+ })
291
+ ]
292
+ });
293
+ ```
294
+
295
+ ```ts
296
+ // injections/vue-setup.ts
297
+ import { VuePlugin } from '@makoojs/vue';
298
+ import router from './router';
299
+ import i18n from './i18n';
300
+
301
+ VuePlugin.usePlugins(router, i18n);
302
+ ```
303
+
304
+ `runtime.setup` supports a string or an array of strings. Relative paths are resolved from the project root:
305
+
306
+ ```ts
307
+ runtime: {
308
+ setup: [
309
+ './injections/vue-setup.ts',
310
+ './injections/gm-setup.ts'
311
+ ]
312
+ }
313
+ ```
314
+
315
+ In dev mode, setup files and their statically imported local dependencies participate in structural updates. When these files change, Makoo rescans and updates the virtual entry.
316
+
317
+ ## Use GM APIs
318
+
319
+ `@makoojs/cli` provides the `@makoojs/cli/monkey` subpath as Makoo's stable wrapper around [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) GM APIs.
320
+
321
+ ```ts
322
+ import { gmStorage, gmStyle } from '@makoojs/cli/monkey';
323
+
324
+ gmStyle.add('.makoo-panel { z-index: 999999; }');
325
+ gmStorage.set('enabled', true);
326
+ ```
327
+
328
+ You can also use the grouped entry:
329
+
330
+ ```ts
331
+ import { GMapi } from '@makoojs/cli/monkey';
332
+
333
+ GMapi.storage.set('enabled', true);
334
+ ```
335
+
336
+ Prefer capability-level imports when you want the generated `@grant` surface to stay as small as possible. `GMapi` is more convenient for shared code or exploratory work. The complete GM API reference will be moved to a dedicated documentation site later.
337
+
338
+ ## Reduce Build Size
339
+
340
+ `@makoojs/cli` re-exports the `cdn` helper from [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey). You can use it with `monkey.build.externalGlobals` to load external dependencies from a CDN and reduce userscript bundle size.
341
+
342
+ ```ts
343
+ import { defineConfig } from 'vite';
344
+ import { cdn, makoo } from '@makoojs/cli';
345
+
346
+ export default defineConfig({
347
+ plugins: makoo({
348
+ app: {
349
+ name: 'my-script',
350
+ version: '0.0.1'
351
+ },
352
+ monkey: {
353
+ build: {
354
+ externalGlobals: {
355
+ vue: cdn.jsdelivr('Vue', 'dist/vue.global.prod.js')
356
+ }
357
+ }
358
+ }
359
+ })
360
+ });
361
+ ```
362
+
363
+ ## Relationship To Other Packages
364
+
365
+ | Package | Responsibility |
366
+ | --- | --- |
367
+ | `@makoojs/cli` | Vite plugin, CLI commands, scanning, code generation, and userscript build integration |
368
+ | `@makoojs/core` | Framework-agnostic injection runtime core |
369
+ | `@makoojs/vue` | Vue adapter and Vue plugin registration helpers |
370
+ | `@makoojs/react` | React adapter |
371
+ | `@makoojs/create-makoo` | Project scaffold |
372
+
373
+ `@makoojs/cli` is not a complete runtime implementation by itself. Many features cannot exist independently and rely on the injection scheduling capabilities provided by `@makoojs/core`.