stonyx 0.2.3-beta.0 → 0.2.3-beta.2

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.
@@ -1,6 +1,8 @@
1
1
  name: Publish to NPM
2
2
 
3
3
  on:
4
+ repository_dispatch:
5
+ types: [cascade-publish]
4
6
  workflow_dispatch:
5
7
  inputs:
6
8
  version-type:
@@ -17,10 +19,14 @@ on:
17
19
  type: string
18
20
  pull_request:
19
21
  types: [opened, synchronize, reopened]
20
- branches: [main, dev]
22
+ branches: [main]
21
23
  push:
22
24
  branches: [main]
23
25
 
26
+ concurrency:
27
+ group: ${{ github.event_name == 'repository_dispatch' && 'cascade-update' || format('publish-{0}', github.ref) }}
28
+ cancel-in-progress: false
29
+
24
30
  permissions:
25
31
  contents: write
26
32
  id-token: write
@@ -28,8 +34,18 @@ permissions:
28
34
 
29
35
  jobs:
30
36
  publish:
37
+ if: "!contains(github.event.head_commit.message, '[skip ci]')"
31
38
  uses: abofs/stonyx-workflows/.github/workflows/npm-publish.yml@main
32
39
  with:
33
40
  version-type: ${{ github.event.inputs.version-type }}
34
41
  custom-version: ${{ github.event.inputs.custom-version }}
42
+ cascade-source: ${{ github.event.client_payload.source_package || '' }}
43
+ secrets: inherit
44
+
45
+ cascade:
46
+ needs: publish
47
+ uses: abofs/stonyx-workflows/.github/workflows/cascade.yml@main
48
+ with:
49
+ package-name: ${{ needs.publish.outputs.package-name }}
50
+ published-version: ${{ needs.publish.outputs.published-version }}
35
51
  secrets: inherit
package/README.md CHANGED
@@ -1,206 +1,48 @@
1
1
  # Stonyx
2
2
 
3
- **Stonyx** is a lightweight, modular framework for building modern Node.js applications. It provides a **plug-and-play architecture**, centralized color-coded logging, and seamless integration of asynchronous modules, making development faster, cleaner, and more maintainable.
3
+ **Stonyx** is a lightweight, modular framework for building modern Node.js applications. It provides a plug-and-play architecture, centralized color-coded logging, and seamless async module integration.
4
4
 
5
- ### Highlights
6
-
7
- * 100% JavaScript
8
- * ✅ Drop-in file system for most modules
9
- * ✅ Don’t even hit the ground — just fly
10
- * ✅ High performance
11
-
12
- Stonyx acts as a **base application host**, allowing you to add official modules (`@stonyx/*`) or your own custom modules without boilerplate initialization.
13
-
14
- ---
5
+ - 100% JavaScript (ES Modules)
6
+ - Drop-in module system — no boilerplate
7
+ - Automatic async module loading and initialization
8
+ - Built-in CLI for bootstrapping and testing
15
9
 
16
10
  ## Quick Start
17
11
 
18
- ### ESM Usage (Application Startup)
19
-
20
- For standard applications, the **bootstrap file** ensures that the Stonyx framework and all submodules are fully loaded before your application code runs:
21
-
22
- ```js
23
- // index.js - your project's entry point
24
- import Stonyx from './stonyx-bootstrap.cjs';
25
- await Stonyx.ready; // wait until all modules are initialized
26
-
27
- const { default: App } = await import('./app.js'); // your application
28
- new App();
29
- ```
30
-
31
- ### CommonJS Bootstrap Helper
32
- Auto-generated and added to your project post installation
33
-
34
- ```js
35
- // stonyx-bootstrap.cjs
36
- const Stonyx = require('stonyx').default;
37
- const config = require('./config/environment.js').default;
38
-
39
- new Stonyx(config, __dirname);
40
- ```
41
-
42
- ---
43
-
44
- ## Core Features
45
-
46
- ### 1. **Modular Architecture**
47
-
48
- * Automatically detects modules in `devDependencies` prefixed with `@stonyx/`.
49
- * Supports async initialization with `stonyx-async` modules.
50
- * Safe module sequencing for submodule development with `waitForModule()`.
51
-
52
- ### 2. **Color-Coded Logging**
53
-
54
- * Centralized logging via [Chronicle](https://github.com/abofs/chronicle).
55
- * Module-specific logs configurable in `environment.js`:
56
-
57
- ```js
58
- restServer: { logColor: 'yellow', logMethod: 'api', logTimestamp: true }
59
- ```
60
-
61
- * Create custom logs for any class with minimal configuration.
62
-
63
- ### 3. **Singleton Design**
64
-
65
- * Only one instance of Stonyx exists per project.
66
- * Ensures consistent access to modules, logs, and configuration.
67
-
68
- ### 4. **Plug-and-Play Module Loading**
69
-
70
- * Modules with `stonyx-module` keyword in `package.json` are auto-initialized.
71
- * Modules with `stonyx-async` keyword are awaited automatically before usage.
72
-
73
- ---
74
-
75
- ## Official Modules
76
-
77
- ### **[@stonyx/cron](https://github.com/abofs/stonyx-cron)**
78
-
79
- Lightweight asynchronous job scheduling utility.
80
-
81
- ```js
82
- import Cron from '@stonyx/cron';
83
-
84
- const cron = new Cron();
85
- cron.register('exampleJob', async () => console.log('Job executed!'), 5, true);
86
- ```
87
-
88
- * Efficient scheduling using a min-heap.
89
- * Optional logging via `config.cron`.
90
-
91
- ---
92
-
93
- ### **[@stonyx/events](https://github.com/abofs/stonyx-events)**
94
-
95
- Lightweight pub/sub event system for application-wide event handling.
96
-
97
- ```js
98
- import Events from '@stonyx/events';
99
-
100
- const events = new Events();
101
-
102
- // Register available events
103
- events.setup(['userLogin', 'userLogout', 'dataChange']);
104
-
105
- // Subscribe to events
106
- events.subscribe('userLogin', (user) => {
107
- console.log(`${user.name} logged in`);
108
- });
109
-
110
- // Emit events
111
- events.emit('userLogin', { name: 'Alice' });
112
- ```
113
-
114
- * Singleton pattern for shared event bus
115
- * Async support with error isolation
116
- * Type-safe event registration
117
-
118
- ---
119
-
120
- ### **[@stonyx/rest-server](https://github.com/abofs/stonyx-rest-server)**
121
-
122
- Dynamic REST server module with auto-route registration.
123
-
124
- ```js
125
- import Stonyx from 'stonyx';
126
- import config from './config/environment.js';
127
-
128
- new Stonyx(config);
129
- ```
130
-
131
- * Zero configuration for routes: drop request classes into the `requests` directory.
132
- * Automatic path generation, JSON parsing, and CORS handling.
133
- * Supports per-route authentication hooks.
134
-
135
- ---
136
-
137
- ### **[@stonyx/orm](https://github.com/abofs/stonyx-orm)**
138
-
139
- Lightweight ORM with model definitions, relationships, serializers, and optional REST integration.
140
-
141
- ```js
142
- import Stonyx from 'stonyx';
143
- import config from './config/environment.js';
144
-
145
- new Stonyx(config);
146
-
147
- // Define models
148
- import { Model, attr, hasMany, belongsTo } from '@stonyx/orm';
149
-
150
- class Owner extends Model {
151
- id = attr('string');
152
- pets = hasMany('animal');
153
- }
154
- ```
155
-
156
- * Auto-loads models, serializers, transforms, and access classes.
157
- * Optional JSON file persistence with auto-save intervals.
158
- * Integrates with `@stonyx/rest-server` for automatic route setup.
159
-
160
- ---
161
-
162
- ## Configuration
163
-
164
- All modules are configurable via `config/environment.js`:
165
-
166
- ```js
167
- export default {
168
- restServer: { logColor: 'yellow', port: 3000 },
169
- orm: { logColor: 'white', db: { file: './db.json', autosave: true } },
170
- cron: { log: true }
171
- };
12
+ ```bash
13
+ npm install stonyx
172
14
  ```
173
15
 
174
- ---
175
-
176
- ## Running the Application
16
+ The CLI handles everything — no manual `new Stonyx()` calls needed:
177
17
 
178
18
  ```bash
179
- node . # Start the main app
180
- npm start # Run using npm script
19
+ stonyx serve # Bootstrap + run app.js
20
+ stonyx test # Bootstrap + run tests
181
21
  ```
182
22
 
183
- ---
184
-
185
- ## Developing Submodules
23
+ Stonyx reads `config/environment.js`, initializes all `@stonyx/*` modules from your `devDependencies`, and runs your application.
186
24
 
187
- For developers building new Stonyx modules or experimenting with async modules:
25
+ ## Documentation
188
26
 
189
- ```js
190
- import Stonyx, { waitForModule } from 'stonyx';
191
- import config from './config/environment.js';
27
+ | Section | Description |
28
+ |---------|-------------|
29
+ | [CLI](docs/cli.md) | Commands, aliases, and module commands |
30
+ | [Configuration](docs/configuration.md) | Environment config, module config, test overrides |
31
+ | [Modules](docs/modules.md) | Module architecture, async loading, official modules |
32
+ | [Logging](docs/logging.md) | Chronicle integration and custom log types |
33
+ | [Lifecycle](docs/lifecycle.md) | Startup and shutdown hooks |
34
+ | [Testing](docs/testing.md) | Test runner, helpers, and conventions |
35
+ | [Developing Modules](docs/developing-modules.md) | Guide for building custom Stonyx modules |
36
+ | [API Reference](docs/api.md) | Public exports and class documentation |
192
37
 
193
- const app = new Stonyx(config, __dirname);
194
-
195
- // Wait for specific async module readiness
196
- await waitForModule('restServer');
197
- ```
198
-
199
- * Use `waitForModule()` **only for submodule development**, testing, or when you need to ensure a specific module is fully initialized before continuing.
200
- * Official modules automatically initialize during normal application startup, so end-users do **not** need to call `waitForModule()`.
38
+ ## Official Modules
201
39
 
202
- ---
40
+ | Module | Description |
41
+ |--------|-------------|
42
+ | [@stonyx/cron](https://github.com/abofs/stonyx-cron) | Lightweight async job scheduling |
43
+ | [@stonyx/rest-server](https://github.com/abofs/stonyx-rest-server) | Dynamic REST server with auto-route registration |
44
+ | [@stonyx/orm](https://github.com/abofs/stonyx-orm) | ORM with models, relationships, and serializers |
203
45
 
204
46
  ## License
205
47
 
206
- Apache 2.0 — do what you want, just keep attribution.
48
+ Apache 2.0
@@ -4,8 +4,7 @@ const {
4
4
  NODE_ENV,
5
5
  } = process.env;
6
6
 
7
- const isTest = typeof QUnit !== 'undefined';
8
- const environment = isTest ? 'test' : (NODE_ENV ?? 'development');
7
+ const environment = NODE_ENV ?? 'development';
9
8
 
10
9
  export default {
11
10
  environment,
@@ -4,8 +4,7 @@ const {
4
4
  NODE_ENV,
5
5
  } = process.env;
6
6
 
7
- const isTest = typeof QUnit !== 'undefined';
8
- const environment = isTest ? 'test' : (NODE_ENV ?? 'development');
7
+ const environment = NODE_ENV ?? 'development';
9
8
 
10
9
  export default {
11
10
  environment,
package/docs/api.md ADDED
@@ -0,0 +1,89 @@
1
+ # API Reference
2
+
3
+ Public exports available from the `stonyx` package.
4
+
5
+ ## Exports Map
6
+
7
+ | Import Path | Export | Description |
8
+ |-------------|--------|-------------|
9
+ | `stonyx` | `default` (Stonyx class) | Main framework class (singleton) |
10
+ | `stonyx` | `waitForModule(name)` | Wait for an async module to be ready |
11
+ | `stonyx/config` | `default` (config object) | Live reference to Stonyx configuration |
12
+ | `stonyx/log` | `default` (Chronicle instance) | Live reference to Chronicle logger |
13
+ | `stonyx/lifecycle` | `runStartupHooks(modules)` | Run startup hooks on a module array |
14
+ | `stonyx/lifecycle` | `runShutdownHooks(modules)` | Run shutdown hooks in reverse order |
15
+ | `stonyx/test-helpers` | `setupIntegrationTests(hooks)` | QUnit hook for integration test setup |
16
+
17
+ ## Stonyx Class
18
+
19
+ ```js
20
+ import Stonyx from 'stonyx';
21
+ ```
22
+
23
+ ### Constructor
24
+
25
+ ```js
26
+ new Stonyx(config, rootPath)
27
+ ```
28
+
29
+ - **config** — Full environment configuration object
30
+ - **rootPath** — Absolute path to the project root
31
+
32
+ Returns the existing instance if one already exists (singleton pattern).
33
+
34
+ ### Static Properties
35
+
36
+ | Property | Type | Description |
37
+ |----------|------|-------------|
38
+ | `Stonyx.instance` | `Stonyx` | The singleton instance |
39
+ | `Stonyx.ready` | `Promise` | Resolves when all modules are initialized |
40
+ | `Stonyx.initialized` | `boolean` | Whether Stonyx has started initialization |
41
+
42
+ ### Static Getters
43
+
44
+ | Getter | Returns | Throws |
45
+ |--------|---------|--------|
46
+ | `Stonyx.config` | Config object | If not initialized |
47
+ | `Stonyx.log` | Chronicle instance | If not initialized |
48
+
49
+ ### Instance Properties
50
+
51
+ | Property | Type | Description |
52
+ |----------|------|-------------|
53
+ | `instance.config` | `object` | Merged environment configuration |
54
+ | `instance.chronicle` | `Chronicle` | Logger instance |
55
+ | `instance.modules` | `Array` | Loaded module instances |
56
+
57
+ ## waitForModule
58
+
59
+ ```js
60
+ import { waitForModule } from 'stonyx';
61
+
62
+ await waitForModule('rest-server');
63
+ ```
64
+
65
+ Waits for a specific `@stonyx/*` module to complete initialization. Pass the module name **without** the `@stonyx/` prefix.
66
+
67
+ Throws if the module is not registered in project dependencies.
68
+
69
+ ## Lifecycle Functions
70
+
71
+ ```js
72
+ import { runStartupHooks, runShutdownHooks } from 'stonyx/lifecycle';
73
+ ```
74
+
75
+ ### runStartupHooks(modules)
76
+
77
+ Calls `startup()` on each module in array order. Skips modules without a `startup` method.
78
+
79
+ ### runShutdownHooks(modules)
80
+
81
+ Calls `shutdown()` on each module in **reverse** array order. Errors are caught and logged — one failing hook does not prevent others from running.
82
+
83
+ ## setupIntegrationTests
84
+
85
+ ```js
86
+ import { setupIntegrationTests } from 'stonyx/test-helpers';
87
+ ```
88
+
89
+ Registers a QUnit `before` hook that waits for `Stonyx.ready`. Use within a `module()` block to ensure Stonyx is fully initialized before tests run.
package/docs/cli.md ADDED
@@ -0,0 +1,78 @@
1
+ # CLI
2
+
3
+ Stonyx includes a CLI that handles bootstrapping, module initialization, and application execution.
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ stonyx <command> [...args]
9
+ ```
10
+
11
+ ## Built-in Commands
12
+
13
+ | Command | Alias | Description |
14
+ |---------|-------|-------------|
15
+ | `serve` | `s` | Bootstrap Stonyx and run the app |
16
+ | `test` | `t` | Bootstrap Stonyx in test mode and run tests |
17
+ | `help` | `h` | Show available commands |
18
+
19
+ ### serve
20
+
21
+ Bootstraps Stonyx (loads config, initializes modules, runs lifecycle hooks), then imports your application entry point.
22
+
23
+ ```bash
24
+ stonyx serve # Runs app.js by default
25
+ stonyx serve --entry custom.js # Runs a custom entry file
26
+ ```
27
+
28
+ The serve command also registers `SIGTERM` and `SIGINT` handlers that run [shutdown hooks](lifecycle.md) before exiting.
29
+
30
+ ### test
31
+
32
+ Runs your test suite using [QUnit](https://qunitjs.com/) with automatic Stonyx bootstrapping. Sets `NODE_ENV=test` and applies any [test config overrides](configuration.md#test-environment-overrides).
33
+
34
+ ```bash
35
+ stonyx test # Runs test/**/*-test.js by default
36
+ stonyx test "test/unit/**/*.js" # Custom test glob
37
+ ```
38
+
39
+ ### help
40
+
41
+ Displays all available commands, including any [module commands](#module-commands).
42
+
43
+ ```bash
44
+ stonyx help
45
+ ```
46
+
47
+ ## Module Commands
48
+
49
+ Stonyx modules can register custom CLI commands by exporting a `./commands` entry in their `package.json`. These are automatically discovered and available through the CLI.
50
+
51
+ ```json
52
+ {
53
+ "exports": {
54
+ "./commands": "./src/commands.js"
55
+ }
56
+ }
57
+ ```
58
+
59
+ The commands file should export an object mapping command names to definitions:
60
+
61
+ ```js
62
+ export default {
63
+ 'db:migrate': {
64
+ description: 'Run database migrations',
65
+ bootstrap: true,
66
+ run: async ({ args, cwd }) => { /* ... */ }
67
+ }
68
+ };
69
+ ```
70
+
71
+ - **`bootstrap: true`** — Stonyx will be fully initialized before the command runs
72
+ - **`bootstrap: false`** — The command runs without Stonyx initialization
73
+
74
+ Module commands appear under "Module commands" in `stonyx help` output. If two modules register the same command name, the first one loaded wins and a warning is printed.
75
+
76
+ ## Environment Variables
77
+
78
+ The CLI automatically loads `.env` files via `process.loadEnvFile()` before executing any command.
@@ -0,0 +1,72 @@
1
+ # Configuration
2
+
3
+ Stonyx uses a centralized configuration file that all modules read from at startup.
4
+
5
+ ## Environment Config
6
+
7
+ Your project's configuration lives at `config/environment.js`:
8
+
9
+ ```js
10
+ const { DEBUG, NODE_ENV } = process.env;
11
+
12
+ const environment = NODE_ENV ?? 'development';
13
+
14
+ export default {
15
+ environment,
16
+ debug: DEBUG ?? environment === 'development',
17
+
18
+ // Module-specific configuration
19
+ restServer: { logColor: 'yellow', port: 3000 },
20
+ cron: { log: true },
21
+ };
22
+ ```
23
+
24
+ This file is auto-generated on `npm install` via the postinstall script if it doesn't already exist.
25
+
26
+ > **Note:** `config/environment.js` is gitignored by default so each environment can have its own settings.
27
+
28
+ ## Module Configuration
29
+
30
+ Each Stonyx module reads its configuration from a top-level key matching its camelCase name. For example, `@stonyx/rest-server` reads from `config.restServer`.
31
+
32
+ Async modules ship with their own default config at `config/environment.js` inside the module package. Your project config is merged on top of these defaults — you only need to specify overrides.
33
+
34
+ ## Logging Configuration
35
+
36
+ Any config key with a `logColor` property automatically creates a [Chronicle](logging.md) log type:
37
+
38
+ ```js
39
+ export default {
40
+ myService: {
41
+ logColor: 'purple', // Required — enables log creation
42
+ logMethod: 'highlight', // Optional — custom method name (defaults to key name)
43
+ logTimestamp: true, // Optional — include timestamps
44
+ },
45
+ };
46
+ ```
47
+
48
+ This works for both module configs and custom service configs. See [Logging](logging.md) for details.
49
+
50
+ ## Test Environment Overrides
51
+
52
+ When `NODE_ENV=test`, Stonyx automatically looks for `test/config/environment.js` in your project root:
53
+
54
+ ```js
55
+ // test/config/environment.js
56
+ export default {
57
+ debug: false,
58
+ restServer: { port: 0 },
59
+ };
60
+ ```
61
+
62
+ These overrides are deep-merged into the main config using in-place mutation, so any existing references (like `stonyx/config` exports) stay valid.
63
+
64
+ ## Accessing Config at Runtime
65
+
66
+ ```js
67
+ import config from 'stonyx/config';
68
+
69
+ console.log(config.environment); // 'development', 'test', etc.
70
+ ```
71
+
72
+ The `stonyx/config` export is a live reference to the Stonyx instance config. It will throw if accessed before Stonyx is initialized.
@@ -0,0 +1,108 @@
1
+ # Developing Modules
2
+
3
+ Guide for building custom Stonyx modules.
4
+
5
+ ## Package Setup
6
+
7
+ Your module's `package.json` must include:
8
+
9
+ ```json
10
+ {
11
+ "name": "@stonyx/my-module",
12
+ "keywords": ["stonyx-module"],
13
+ "main": "src/index.js"
14
+ }
15
+ ```
16
+
17
+ Add `stonyx-async` to keywords if your module requires asynchronous initialization.
18
+
19
+ ## Module Class
20
+
21
+ Export a default class. Optionally define `init()`, `startup()`, and `shutdown()` methods:
22
+
23
+ ```js
24
+ export default class MyModule {
25
+ // Called during module loading (async modules only)
26
+ async init() {
27
+ // Connect to services, load resources, etc.
28
+ }
29
+
30
+ // Called after ALL modules are initialized, before app entry runs
31
+ async startup() {
32
+ // Register routes, start listeners, etc.
33
+ }
34
+
35
+ // Called on SIGTERM/SIGINT, in reverse load order
36
+ async shutdown() {
37
+ // Close connections, flush data, etc.
38
+ }
39
+ }
40
+ ```
41
+
42
+ ## Default Configuration
43
+
44
+ Async modules must include `config/environment.js` with sensible defaults:
45
+
46
+ ```js
47
+ export default {
48
+ logColor: 'cyan',
49
+ logMethod: 'myModule',
50
+ logTimestamp: true,
51
+ // Module-specific defaults...
52
+ };
53
+ ```
54
+
55
+ User project config is merged on top of these defaults.
56
+
57
+ ## Waiting for Other Modules
58
+
59
+ If your module depends on another async module being ready:
60
+
61
+ ```js
62
+ import { waitForModule } from 'stonyx';
63
+
64
+ export default class MyModule {
65
+ async init() {
66
+ await waitForModule('rest-server');
67
+ // @stonyx/rest-server is now fully initialized
68
+ }
69
+ }
70
+ ```
71
+
72
+ Pass the module name without the `@stonyx/` prefix.
73
+
74
+ ## Standalone Development
75
+
76
+ When running a module standalone (project path contains `stonyx-`), Stonyx auto-transforms the config structure. Your module's config is wrapped under its camelCase name:
77
+
78
+ ```js
79
+ // If your module is @stonyx/rest-server and config is { port: 3000 }
80
+ // Stonyx transforms it to: { restServer: { port: 3000 } }
81
+ ```
82
+
83
+ ## Custom CLI Commands
84
+
85
+ Modules can register CLI commands by adding a `./commands` export:
86
+
87
+ ```json
88
+ {
89
+ "exports": {
90
+ "./commands": "./src/commands.js"
91
+ }
92
+ }
93
+ ```
94
+
95
+ ```js
96
+ // src/commands.js
97
+ export default {
98
+ 'my-module:setup': {
99
+ description: 'Initialize module resources',
100
+ bootstrap: true,
101
+ run: async ({ args, cwd }) => {
102
+ // Command implementation
103
+ }
104
+ }
105
+ };
106
+ ```
107
+
108
+ Use namespaced command names (e.g., `my-module:setup`) to avoid conflicts with other modules. See [CLI](cli.md#module-commands) for details.
package/docs/index.md ADDED
@@ -0,0 +1,15 @@
1
+ # Stonyx Documentation
2
+
3
+ ## Guides
4
+
5
+ - [CLI](cli.md) — Commands, usage, and module-contributed commands
6
+ - [Configuration](configuration.md) — Environment config, module config, and test overrides
7
+ - [Modules](modules.md) — Module discovery, sync vs async, and official modules
8
+ - [Lifecycle](lifecycle.md) — Startup/shutdown hooks and signal handling
9
+ - [Logging](logging.md) — Color-coded logging via Chronicle
10
+ - [Testing](testing.md) — Running tests, config overrides, and integration helpers
11
+ - [Developing Modules](developing-modules.md) — Guide for building custom Stonyx modules
12
+
13
+ ## Reference
14
+
15
+ - [API Reference](api.md) — Public exports and class documentation
@@ -0,0 +1,47 @@
1
+ # Lifecycle Hooks
2
+
3
+ Stonyx modules can define `startup()` and `shutdown()` methods that run at specific points in the application lifecycle.
4
+
5
+ ## Startup Hooks
6
+
7
+ Called sequentially (in module load order) after all modules have been initialized, right before your application entry point runs.
8
+
9
+ ```js
10
+ export default class MyModule {
11
+ async startup() {
12
+ // Called after all modules are init'd, before app.js runs
13
+ }
14
+ }
15
+ ```
16
+
17
+ ## Shutdown Hooks
18
+
19
+ Called sequentially in **reverse** module load order when the process receives `SIGTERM` or `SIGINT`. Errors in one hook do not prevent other hooks from running.
20
+
21
+ ```js
22
+ export default class MyModule {
23
+ async shutdown() {
24
+ // Clean up connections, flush buffers, etc.
25
+ }
26
+ }
27
+ ```
28
+
29
+ ## Signal Handling
30
+
31
+ The `stonyx serve` command registers signal handlers automatically:
32
+
33
+ - **SIGTERM** — triggers shutdown hooks, then `process.exit(0)`
34
+ - **SIGINT** — triggers shutdown hooks, then `process.exit(0)`
35
+
36
+ Shutdown is idempotent — calling the handler multiple times only runs hooks once.
37
+
38
+ ## Using Lifecycle Hooks Directly
39
+
40
+ For advanced use cases, the lifecycle functions are available as a public export:
41
+
42
+ ```js
43
+ import { runStartupHooks, runShutdownHooks } from 'stonyx/lifecycle';
44
+
45
+ await runStartupHooks(modules); // Calls startup() on each module in order
46
+ await runShutdownHooks(modules); // Calls shutdown() on each module in reverse order
47
+ ```
@@ -0,0 +1,52 @@
1
+ # Logging
2
+
3
+ Stonyx provides centralized, color-coded logging via [Chronicle](https://github.com/abofs/chronicle).
4
+
5
+ ## Overview
6
+
7
+ Every Stonyx instance creates a Chronicle logger with a default `title` log type (green). Modules and user services can register additional log types with custom colors.
8
+
9
+ ## Module Logs
10
+
11
+ Modules that specify `logColor` in their configuration automatically get a Chronicle log type:
12
+
13
+ ```js
14
+ // config/environment.js
15
+ export default {
16
+ restServer: {
17
+ logColor: 'yellow',
18
+ logMethod: 'api', // Optional — defaults to the config key name
19
+ logTimestamp: true, // Optional — adds timestamps to log output
20
+ },
21
+ };
22
+ ```
23
+
24
+ This creates a `chronicle.api()` method that outputs in yellow with timestamps.
25
+
26
+ ## Custom Logs
27
+
28
+ You can define logs for any class or service in your project — not just Stonyx modules:
29
+
30
+ ```js
31
+ export default {
32
+ myWorker: {
33
+ logColor: 'cyan',
34
+ logMethod: 'worker',
35
+ logTimestamp: true,
36
+ },
37
+ };
38
+ ```
39
+
40
+ Any top-level config key with a `logColor` property will have a log type created automatically.
41
+
42
+ ## Accessing the Logger
43
+
44
+ ```js
45
+ import log from 'stonyx/log';
46
+
47
+ log.title('Application started');
48
+ log.api('Request received'); // If restServer config defines logMethod: 'api'
49
+ log.worker('Processing job'); // If myWorker config defines logMethod: 'worker'
50
+ ```
51
+
52
+ The `stonyx/log` export is a live reference to the Chronicle instance. It will throw if accessed before Stonyx is initialized.
@@ -0,0 +1,78 @@
1
+ # Module System
2
+
3
+ Stonyx uses a plug-and-play module architecture. Modules are automatically detected, loaded, and initialized at startup.
4
+
5
+ ## How Modules Are Discovered
6
+
7
+ Stonyx scans your project's `devDependencies` for packages prefixed with `@stonyx/`. Each matching package must include the `stonyx-module` keyword in its `package.json` to be loaded.
8
+
9
+ ```json
10
+ {
11
+ "name": "@stonyx/my-module",
12
+ "keywords": ["stonyx-module"],
13
+ "main": "src/index.js"
14
+ }
15
+ ```
16
+
17
+ ## Sync vs Async Modules
18
+
19
+ ### Sync Modules
20
+
21
+ Modules with only the `stonyx-module` keyword are treated as synchronous. They are instantiated but their promise resolves immediately — no init phase is awaited.
22
+
23
+ ### Async Modules
24
+
25
+ Modules that also include the `stonyx-async` keyword require initialization before they're ready:
26
+
27
+ ```json
28
+ {
29
+ "keywords": ["stonyx-module", "stonyx-async"]
30
+ }
31
+ ```
32
+
33
+ Async modules **must** include a `config/environment.js` with default configuration. This is merged with the user's project config before initialization.
34
+
35
+ Async modules can define an `init()` method that Stonyx awaits:
36
+
37
+ ```js
38
+ export default class MyModule {
39
+ async init() {
40
+ // Connect to database, start server, etc.
41
+ }
42
+ }
43
+ ```
44
+
45
+ All module `init()` calls run concurrently via `Promise.all`.
46
+
47
+ ## Module Lifecycle
48
+
49
+ 1. **Discovery** — scan `devDependencies` for `@stonyx/*` packages
50
+ 2. **Validation** — verify `stonyx-module` keyword exists
51
+ 3. **Config merge** — async module defaults merged with user config
52
+ 4. **Log setup** — module-specific Chronicle log created if `logColor` is set
53
+ 5. **Instantiation** — module class is `new`'d
54
+ 6. **Initialization** — `init()` called (async modules only)
55
+ 7. **Startup hooks** — `startup()` called after all modules init (see [Lifecycle](lifecycle.md))
56
+ 8. **Shutdown hooks** — `shutdown()` called on process exit (see [Lifecycle](lifecycle.md))
57
+
58
+ ## waitForModule
59
+
60
+ For submodule developers who need to ensure another async module is ready:
61
+
62
+ ```js
63
+ import { waitForModule } from 'stonyx';
64
+
65
+ await waitForModule('rest-server'); // Waits for @stonyx/rest-server
66
+ ```
67
+
68
+ > **Note:** `waitForModule` is only needed during submodule development or testing. End-user applications don't need it — the CLI ensures all modules are initialized before running your app.
69
+
70
+ ## Official Modules
71
+
72
+ | Module | Description |
73
+ |--------|-------------|
74
+ | [@stonyx/cron](https://github.com/abofs/stonyx-cron) | Lightweight async job scheduling with min-heap |
75
+ | [@stonyx/rest-server](https://github.com/abofs/stonyx-rest-server) | Dynamic REST server with auto-route registration |
76
+ | [@stonyx/orm](https://github.com/abofs/stonyx-orm) | ORM with models, relationships, serializers, and optional REST integration |
77
+
78
+ See each module's repository for its specific documentation.
@@ -0,0 +1,64 @@
1
+ # Testing
2
+
3
+ Stonyx includes built-in test infrastructure using [QUnit](https://qunitjs.com/).
4
+
5
+ ## Running Tests
6
+
7
+ ```bash
8
+ stonyx test # Runs test/**/*-test.js by default
9
+ stonyx test "test/unit/**/*.js" # Custom glob pattern
10
+ ```
11
+
12
+ The test command:
13
+ 1. Sets `NODE_ENV=test`
14
+ 2. Bootstraps Stonyx via a `--require` setup file
15
+ 3. Applies [test config overrides](configuration.md#test-environment-overrides)
16
+ 4. Runs QUnit with the specified glob
17
+
18
+ ## Test Config Overrides
19
+
20
+ Create `test/config/environment.js` to override configuration during tests:
21
+
22
+ ```js
23
+ export default {
24
+ debug: false,
25
+ restServer: { port: 0 },
26
+ };
27
+ ```
28
+
29
+ These are deep-merged into the main config. See [Configuration](configuration.md#test-environment-overrides).
30
+
31
+ ## Integration Test Helper
32
+
33
+ For tests that need Stonyx fully initialized (e.g., testing modules with database connections):
34
+
35
+ ```js
36
+ import { setupIntegrationTests } from 'stonyx/test-helpers';
37
+
38
+ const { module, test } = QUnit;
39
+
40
+ module('My Integration Test', function(hooks) {
41
+ setupIntegrationTests(hooks);
42
+
43
+ test('can access modules', function(assert) {
44
+ // Stonyx is fully initialized here
45
+ assert.ok(true);
46
+ });
47
+ });
48
+ ```
49
+
50
+ `setupIntegrationTests` adds a `before` hook that waits for `Stonyx.ready` to resolve.
51
+
52
+ ## Test File Convention
53
+
54
+ Place tests under `test/` with the `-test.js` suffix:
55
+
56
+ ```
57
+ test/
58
+ unit/
59
+ my-feature-test.js
60
+ cli/
61
+ serve-test.js
62
+ integration/
63
+ api-test.js
64
+ ```
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "stonyx",
3
- "version": "0.2.3-beta.0",
3
+ "version": "0.2.3-beta.2",
4
4
  "description": "Base stonyx framework module",
5
5
  "main": "main.js",
6
6
  "type": "module",
7
+ "bin": {
8
+ "stonyx": "./src/cli.js"
9
+ },
7
10
  "exports": {
8
11
  ".": "./src/main.js",
9
12
  "./config": "./src/exports/config.js",
10
13
  "./log": "./src/exports/log.js",
11
- "./test-helpers": "./src/exports/test-helpers.js"
14
+ "./test-helpers": "./src/exports/test-helpers.js",
15
+ "./lifecycle": "./src/lifecycle.js"
12
16
  },
13
17
  "repository": {
14
18
  "type": "git",
@@ -29,7 +33,7 @@
29
33
  "node-chronicle": "^0.2.0"
30
34
  },
31
35
  "devDependencies": {
32
- "@stonyx/utils": "^0.2.2",
36
+ "@stonyx/utils": "0.2.3-beta.3",
33
37
  "qunit": "^2.24.1",
34
38
  "sinon": "^21.0.0"
35
39
  },
@@ -2,7 +2,6 @@ import { copyFile, createDirectory } from '@stonyx/utils/file';
2
2
 
3
3
  const projectDir = process.env.INIT_CWD;
4
4
  const configDir = `${projectDir}/config`;
5
- const bootstrapFile = 'stonyx-bootstrap.cjs';
6
5
  const envFile = 'environment.js';
7
6
 
8
7
  createDirectory(configDir);
@@ -10,7 +9,3 @@ createDirectory(configDir);
10
9
  copyFile(`./config/environment copy.js`, `${configDir}/${envFile}`).then(result => {
11
10
  if (result) console.log(`Stonyx: ${envFile} has been successfully created. Please see README.md for more information.`);
12
11
  });
13
-
14
- copyFile(`./src/bootstrap.cjs`, `${projectDir}/${bootstrapFile}`).then(result => {
15
- if (result) console.log(`Stonyx: ${bootstrapFile} has been successfully created. Please see README.md for more information.`);
16
- });
@@ -0,0 +1,29 @@
1
+ export default async function help({ args, builtInCommands, loadModuleCommands } = {}) {
2
+ console.log('\nUsage: stonyx <command> [...args]\n');
3
+ console.log('Commands:\n');
4
+
5
+ if (builtInCommands) {
6
+ for (const [name, { description }] of Object.entries(builtInCommands)) {
7
+ console.log(` ${name.padEnd(20)} ${description}`);
8
+ }
9
+ }
10
+
11
+ if (loadModuleCommands) {
12
+ try {
13
+ const moduleCommands = await loadModuleCommands();
14
+ const entries = Object.entries(moduleCommands);
15
+
16
+ if (entries.length) {
17
+ console.log('\nModule commands:\n');
18
+
19
+ for (const [name, { description }] of entries) {
20
+ console.log(` ${name.padEnd(20)} ${description}`);
21
+ }
22
+ }
23
+ } catch {
24
+ // Module commands not available (e.g., no project context)
25
+ }
26
+ }
27
+
28
+ console.log('\nAliases: s=serve, t=test, h=help\n');
29
+ }
@@ -0,0 +1,56 @@
1
+ import { readFile } from 'fs/promises';
2
+ import path from 'path';
3
+
4
+ export default async function loadModuleCommands() {
5
+ const cwd = process.cwd();
6
+ const commands = {};
7
+
8
+ let projectPackage;
9
+
10
+ try {
11
+ const raw = await readFile(path.join(cwd, 'package.json'), 'utf8');
12
+ projectPackage = JSON.parse(raw);
13
+ } catch {
14
+ return commands;
15
+ }
16
+
17
+ const allDeps = {
18
+ ...projectPackage.dependencies,
19
+ ...projectPackage.devDependencies
20
+ };
21
+
22
+ const stonyxModules = Object.keys(allDeps).filter(name => name.startsWith('@stonyx/'));
23
+
24
+ for (const moduleName of stonyxModules) {
25
+ let modulePackage;
26
+
27
+ try {
28
+ const raw = await readFile(path.join(cwd, 'node_modules', moduleName, 'package.json'), 'utf8');
29
+ modulePackage = JSON.parse(raw);
30
+ } catch {
31
+ continue;
32
+ }
33
+
34
+ const { exports: moduleExports } = modulePackage;
35
+
36
+ if (!moduleExports || !moduleExports['./commands']) continue;
37
+
38
+ try {
39
+ const commandsModule = await import(path.join(cwd, 'node_modules', moduleName, moduleExports['./commands']));
40
+ const moduleCommands = commandsModule.default;
41
+
42
+ for (const [name, command] of Object.entries(moduleCommands)) {
43
+ if (commands[name]) {
44
+ console.warn(`Warning: Command "${name}" from ${moduleName} conflicts with existing command. Skipping.`);
45
+ continue;
46
+ }
47
+
48
+ commands[name] = { ...command, module: moduleName };
49
+ }
50
+ } catch {
51
+ continue;
52
+ }
53
+ }
54
+
55
+ return commands;
56
+ }
@@ -0,0 +1,38 @@
1
+ import { runStartupHooks, runShutdownHooks } from '../lifecycle.js';
2
+
3
+ export function createShutdownHandler(modules) {
4
+ let shuttingDown = false;
5
+ return async () => {
6
+ if (shuttingDown) return;
7
+ shuttingDown = true;
8
+
9
+ await runShutdownHooks(modules);
10
+ process.exit(0);
11
+ };
12
+ }
13
+
14
+ export default async function serve({ args }) {
15
+ const cwd = process.cwd();
16
+ const entryFlag = args.indexOf('--entry');
17
+ const entryPoint = entryFlag !== -1 ? args[entryFlag + 1] : 'app.js';
18
+
19
+ const { default: config } = await import(`${cwd}/config/environment.js`);
20
+ const { default: Stonyx } = await import('../main.js');
21
+
22
+ new Stonyx(config, cwd);
23
+ await Stonyx.ready;
24
+
25
+ const { modules } = Stonyx.instance;
26
+ await runStartupHooks(modules);
27
+
28
+ const shutdown = createShutdownHandler(modules);
29
+
30
+ process.on('SIGTERM', shutdown);
31
+ process.on('SIGINT', shutdown);
32
+
33
+ const entryModule = await import(`${cwd}/${entryPoint}`);
34
+
35
+ if (entryModule.default) {
36
+ new entryModule.default();
37
+ }
38
+ }
@@ -0,0 +1,8 @@
1
+ import { pathToFileURL } from 'url';
2
+
3
+ const cwd = process.cwd();
4
+
5
+ const { default: Stonyx } = await import('stonyx');
6
+ const { default: config } = await import(pathToFileURL(`${cwd}/config/environment.js`));
7
+
8
+ new Stonyx(config, cwd);
@@ -0,0 +1,27 @@
1
+ import { spawn } from 'child_process';
2
+ import { fileURLToPath, pathToFileURL } from 'url';
3
+ import path from 'path';
4
+
5
+ export default async function test({ args }) {
6
+ const cwd = process.cwd();
7
+ const setupFile = path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'test-setup.js');
8
+ const setupFileUrl = pathToFileURL(setupFile).href;
9
+ const qunitBin = path.resolve(cwd, 'node_modules/qunit/bin/qunit.js');
10
+
11
+ // Default to conventional test glob if no args provided
12
+ const testArgs = args.length > 0 ? args : ['test/**/*-test.js'];
13
+
14
+ const child = spawn(process.execPath, [
15
+ '--import', setupFileUrl,
16
+ qunitBin,
17
+ ...testArgs
18
+ ], {
19
+ cwd,
20
+ stdio: 'inherit',
21
+ env: { ...process.env, NODE_ENV: 'test' }
22
+ });
23
+
24
+ child.on('close', (code) => {
25
+ process.exit(code);
26
+ });
27
+ }
package/src/cli.js ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env node
2
+
3
+ import serve from './cli/serve.js';
4
+ import test from './cli/test.js';
5
+ import help from './cli/help.js';
6
+ import loadModuleCommands from './cli/load-commands.js';
7
+
8
+ try { process.loadEnvFile(); } catch { /* no .env file */ }
9
+
10
+ const aliases = { s: 'serve', t: 'test', h: 'help' };
11
+
12
+ const builtInCommands = {
13
+ serve: { description: 'Bootstrap Stonyx and run the app', run: serve },
14
+ test: { description: 'Bootstrap Stonyx in test mode and run tests', run: test },
15
+ help: { description: 'Show available commands', run: help }
16
+ };
17
+
18
+ const args = process.argv.slice(2);
19
+ const commandName = aliases[args[0]] || args[0];
20
+ const commandArgs = args.slice(1);
21
+
22
+ async function main() {
23
+ if (!commandName || commandName === 'help') {
24
+ await help({ args: commandArgs, builtInCommands, loadModuleCommands });
25
+ return;
26
+ }
27
+
28
+ const builtIn = builtInCommands[commandName];
29
+
30
+ if (builtIn) {
31
+ await builtIn.run({ args: commandArgs });
32
+ return;
33
+ }
34
+
35
+ // Search module commands
36
+ const moduleCommands = await loadModuleCommands();
37
+ const moduleCommand = moduleCommands[commandName];
38
+
39
+ if (moduleCommand) {
40
+ const cwd = process.cwd();
41
+
42
+ if (moduleCommand.bootstrap) {
43
+ const { default: config } = await import(`${cwd}/config/environment.js`);
44
+ const { default: Stonyx } = await import('./main.js');
45
+ new Stonyx(config, cwd);
46
+ await Stonyx.ready;
47
+ }
48
+
49
+ await moduleCommand.run({ args: commandArgs, cwd });
50
+ return;
51
+ }
52
+
53
+ console.error(`Unknown command: ${args[0]}\n`);
54
+ await help({ args: [], builtInCommands, loadModuleCommands });
55
+ process.exit(1);
56
+ }
57
+
58
+ main().catch(error => {
59
+ console.error(error);
60
+ process.exit(1);
61
+ });
@@ -0,0 +1,17 @@
1
+ export async function runStartupHooks(modules) {
2
+ for (const module of modules) {
3
+ if (typeof module.startup === 'function') await module.startup();
4
+ }
5
+ }
6
+
7
+ export async function runShutdownHooks(modules) {
8
+ for (const module of [...modules].reverse()) {
9
+ if (typeof module.shutdown === 'function') {
10
+ try {
11
+ await module.shutdown();
12
+ } catch (error) {
13
+ console.error('Error during module shutdown:', error);
14
+ }
15
+ }
16
+ }
17
+ }
package/src/main.js CHANGED
@@ -17,6 +17,7 @@
17
17
  import Chronicle from 'node-chronicle';
18
18
  import loadModules from './modules.js';
19
19
  import { kebabCaseToCamelCase } from '@stonyx/utils/string';
20
+ import { mergeObject } from '@stonyx/utils/object';
20
21
 
21
22
  export default class Stonyx {
22
23
  static initialized = false
@@ -45,6 +46,16 @@ export default class Stonyx {
45
46
 
46
47
  Stonyx.initialized = true;
47
48
 
49
+ // Auto-merge test environment overrides (after initialized flag, before modules load)
50
+ // Uses in-place mutation to preserve existing references (e.g. stonyx/config export cache)
51
+ if (process.env.NODE_ENV === 'test') {
52
+ try {
53
+ const { default: testOverrides } = await import(`${rootPath}/test/config/environment.js`);
54
+ const merged = mergeObject(config, testOverrides);
55
+ Object.assign(config, merged);
56
+ } catch { /* no test overrides found */ }
57
+ }
58
+
48
59
  this.modules = await loadModules(config, rootPath, this.chronicle);
49
60
  this.configureUserLogs();
50
61
  }
package/src/bootstrap.cjs DELETED
@@ -1,9 +0,0 @@
1
- /**
2
- * commonJS Bootstrap loading - Stonyx must be loaded first, prior to the rest of the application
3
- */
4
- const { default:Stonyx } = require('stonyx');
5
- const { default:config } = require('./config/environment.js');
6
-
7
- new Stonyx(config, __dirname);
8
-
9
- module.exports = Stonyx;
@@ -1,9 +0,0 @@
1
- /**
2
- * commonJS Bootstrap loading - Stonyx must be loaded first, prior to the rest of the application
3
- */
4
- const { default:Stonyx } = require('stonyx');
5
- const { default:config } = require('./config/environment.js');
6
-
7
- new Stonyx(config, __dirname);
8
-
9
- module.exports = Stonyx;