stonyx 0.2.3-beta.4 → 0.2.3-beta.41

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 (50) hide show
  1. package/README.md +16 -4
  2. package/dist/cli/help.d.ts +8 -0
  3. package/dist/cli/help.js +43 -0
  4. package/dist/cli/load-commands.d.ts +10 -0
  5. package/dist/cli/load-commands.js +47 -0
  6. package/dist/cli/new.d.ts +17 -0
  7. package/dist/cli/new.js +236 -0
  8. package/dist/cli/serve.d.ts +5 -0
  9. package/dist/cli/serve.js +29 -0
  10. package/dist/cli/test-setup.d.ts +1 -0
  11. package/{src → dist}/cli/test-setup.js +5 -8
  12. package/dist/cli/test.d.ts +3 -0
  13. package/dist/cli/test.js +23 -0
  14. package/dist/cli.d.ts +2 -0
  15. package/dist/cli.js +52 -0
  16. package/dist/exports/config.d.ts +2 -0
  17. package/dist/exports/config.js +2 -0
  18. package/dist/exports/log.d.ts +2 -0
  19. package/{src → dist}/exports/log.js +1 -2
  20. package/dist/exports/test-helpers.d.ts +5 -0
  21. package/dist/exports/test-helpers.js +7 -0
  22. package/dist/lifecycle.d.ts +7 -0
  23. package/dist/lifecycle.js +18 -0
  24. package/dist/main.d.ts +21 -0
  25. package/dist/main.js +88 -0
  26. package/dist/modules.d.ts +5 -0
  27. package/dist/modules.js +92 -0
  28. package/package.json +39 -12
  29. package/.github/workflows/ci.yml +0 -16
  30. package/.github/workflows/publish.yml +0 -51
  31. package/docs/api.md +0 -89
  32. package/docs/cli.md +0 -78
  33. package/docs/configuration.md +0 -72
  34. package/docs/developing-modules.md +0 -108
  35. package/docs/index.md +0 -15
  36. package/docs/lifecycle.md +0 -47
  37. package/docs/logging.md +0 -52
  38. package/docs/modules.md +0 -78
  39. package/docs/testing.md +0 -64
  40. package/scripts/postinstall.js +0 -11
  41. package/src/cli/help.js +0 -29
  42. package/src/cli/load-commands.js +0 -56
  43. package/src/cli/serve.js +0 -38
  44. package/src/cli/test.js +0 -27
  45. package/src/cli.js +0 -61
  46. package/src/exports/config.js +0 -3
  47. package/src/exports/test-helpers.js +0 -8
  48. package/src/lifecycle.js +0 -17
  49. package/src/main.js +0 -101
  50. package/src/modules.js +0 -112
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ [![CI](https://github.com/abofs/stonyx/actions/workflows/ci.yml/badge.svg)](https://github.com/abofs/stonyx/actions/workflows/ci.yml)
2
+ [![npm version](https://img.shields.io/npm/v/stonyx.svg)](https://www.npmjs.com/package/stonyx)
3
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
4
+
1
5
  # Stonyx
2
6
 
3
7
  **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.
@@ -5,19 +9,23 @@
5
9
  - 100% JavaScript (ES Modules)
6
10
  - Drop-in module system — no boilerplate
7
11
  - Automatic async module loading and initialization
8
- - Built-in CLI for bootstrapping and testing
12
+ - Built-in CLI for scaffolding, bootstrapping, and testing
9
13
 
10
14
  ## Quick Start
11
15
 
12
16
  ```bash
13
- npm install stonyx
17
+ npm install -g stonyx
18
+ stonyx new my-app
19
+ cd my-app
20
+ stonyx serve
14
21
  ```
15
22
 
16
- The CLI handles everything no manual `new Stonyx()` calls needed:
23
+ The `new` command walks you through module selection and generates a ready-to-run project. From there, the CLI handles bootstrapping:
17
24
 
18
25
  ```bash
19
26
  stonyx serve # Bootstrap + run app.js
20
27
  stonyx test # Bootstrap + run tests
28
+ stonyx help # Show all available commands
21
29
  ```
22
30
 
23
31
  Stonyx reads `config/environment.js`, initializes all `@stonyx/*` modules from your `devDependencies`, and runs your application.
@@ -40,8 +48,12 @@ Stonyx reads `config/environment.js`, initializes all `@stonyx/*` modules from y
40
48
  | Module | Description |
41
49
  |--------|-------------|
42
50
  | [@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 |
51
+ | [@stonyx/discord](https://github.com/abofs/stonyx-discord) | Discord bot with command and event handler auto-discovery |
52
+ | [@stonyx/events](https://github.com/abofs/stonyx-events) | Pub/sub event system |
53
+ | [@stonyx/oauth](https://github.com/abofs/stonyx-oauth) | OAuth provider integration |
44
54
  | [@stonyx/orm](https://github.com/abofs/stonyx-orm) | ORM with models, relationships, and serializers |
55
+ | [@stonyx/rest-server](https://github.com/abofs/stonyx-rest-server) | Dynamic REST server with auto-route registration |
56
+ | [@stonyx/sockets](https://github.com/abofs/stonyx-sockets) | WebSocket server and client |
45
57
 
46
58
  ## License
47
59
 
@@ -0,0 +1,8 @@
1
+ import type { CommandDefinition } from './load-commands.js';
2
+ interface HelpOptions {
3
+ args?: string[];
4
+ builtInCommands?: Record<string, CommandDefinition>;
5
+ loadModuleCommands?: () => Promise<Record<string, CommandDefinition>>;
6
+ }
7
+ export default function help({ args, builtInCommands, loadModuleCommands }?: HelpOptions): Promise<void>;
8
+ export {};
@@ -0,0 +1,43 @@
1
+ export default async function help({ args, builtInCommands, loadModuleCommands } = {}) {
2
+ console.log('\nUsage: stonyx <command> [...args]\n');
3
+ console.log('Commands:\n');
4
+ if (builtInCommands) {
5
+ for (const [name, { description }] of Object.entries(builtInCommands)) {
6
+ console.log(` ${name.padEnd(20)} ${description}`);
7
+ }
8
+ }
9
+ if (loadModuleCommands) {
10
+ try {
11
+ const moduleCommands = await loadModuleCommands();
12
+ const entries = Object.entries(moduleCommands);
13
+ if (entries.length) {
14
+ console.log('\nModule commands:\n');
15
+ for (const [name, { description }] of entries) {
16
+ console.log(` ${name.padEnd(20)} ${description}`);
17
+ }
18
+ }
19
+ }
20
+ catch {
21
+ // Module commands not available (e.g., no project context)
22
+ }
23
+ }
24
+ console.log('\nAliases: s=serve, t=test, n=new, h=help\n');
25
+ console.log('Project conventions:\n');
26
+ console.log(' Entry point: app.js');
27
+ console.log(' Config: config/environment.js');
28
+ console.log(' DB schema: config/db-schema.js');
29
+ console.log(' Models: models/');
30
+ console.log(' Serializers: serializers/');
31
+ console.log(' Access control: access/');
32
+ console.log(' Transforms: transforms/');
33
+ console.log(' Hooks: hooks/');
34
+ console.log(' REST requests: requests/');
35
+ console.log(' Cron jobs: crons/');
36
+ console.log(' Tests: test/{unit,integration,acceptance}/');
37
+ console.log('');
38
+ console.log(' Logging: import log from \'stonyx/log\' (never console.log)');
39
+ console.log(' Utilities: import from \'@stonyx/utils/*\' (never raw fs)');
40
+ console.log(' Lint config: import from \'@abofs/code-conventions\'');
41
+ console.log(' Package manager: pnpm');
42
+ console.log('');
43
+ }
@@ -0,0 +1,10 @@
1
+ export interface CommandDefinition {
2
+ description: string;
3
+ run: (ctx: {
4
+ args: string[];
5
+ cwd?: string;
6
+ }) => Promise<void>;
7
+ bootstrap?: boolean;
8
+ module?: string;
9
+ }
10
+ export default function loadModuleCommands(): Promise<Record<string, CommandDefinition>>;
@@ -0,0 +1,47 @@
1
+ import { readFile } from 'fs/promises';
2
+ import path from 'path';
3
+ export default async function loadModuleCommands() {
4
+ const cwd = process.cwd();
5
+ const commands = {};
6
+ let projectPackage;
7
+ try {
8
+ const raw = await readFile(path.join(cwd, 'package.json'), 'utf8');
9
+ projectPackage = JSON.parse(raw);
10
+ }
11
+ catch {
12
+ return commands;
13
+ }
14
+ const allDeps = {
15
+ ...projectPackage.dependencies,
16
+ ...projectPackage.devDependencies
17
+ };
18
+ const stonyxModules = Object.keys(allDeps).filter(name => name.startsWith('@stonyx/'));
19
+ for (const moduleName of stonyxModules) {
20
+ let modulePackage;
21
+ try {
22
+ const raw = await readFile(path.join(cwd, 'node_modules', moduleName, 'package.json'), 'utf8');
23
+ modulePackage = JSON.parse(raw);
24
+ }
25
+ catch {
26
+ continue;
27
+ }
28
+ const moduleExports = modulePackage.exports;
29
+ if (!moduleExports || !moduleExports['./commands'])
30
+ continue;
31
+ try {
32
+ const commandsModule = await import(path.join(cwd, 'node_modules', moduleName, moduleExports['./commands']));
33
+ const moduleCommands = commandsModule.default;
34
+ for (const [name, command] of Object.entries(moduleCommands)) {
35
+ if (commands[name]) {
36
+ console.warn(`Warning: Command "${name}" from ${moduleName} conflicts with existing command. Skipping.`);
37
+ continue;
38
+ }
39
+ commands[name] = { ...command, module: moduleName };
40
+ }
41
+ }
42
+ catch {
43
+ continue;
44
+ }
45
+ }
46
+ return commands;
47
+ }
@@ -0,0 +1,17 @@
1
+ interface ModuleOption {
2
+ question: string;
3
+ package: string;
4
+ dirs?: string[];
5
+ files?: Record<string, () => string>;
6
+ }
7
+ export declare function generateDbSchema(): string;
8
+ export declare function generatePackageJson(name: string, selectedModules: ModuleOption[]): string;
9
+ export declare function generateAppTs(): string;
10
+ export declare function generateEnvironmentTs(): string;
11
+ export declare function generateEnvironmentExampleTs(): string;
12
+ export declare function generateGitignore(): string;
13
+ export declare function generateTsConfig(): string;
14
+ export default function newCommand({ args }: {
15
+ args: string[];
16
+ }): Promise<void>;
17
+ export {};
@@ -0,0 +1,236 @@
1
+ import { confirm, prompt } from '@stonyx/utils/prompt';
2
+ import { createFile, createDirectory, copyFile, fileExists } from '@stonyx/utils/file';
3
+ import { spawn } from 'child_process';
4
+ import path from 'path';
5
+ import { fileURLToPath } from 'url';
6
+ const MODULE_OPTIONS = [
7
+ {
8
+ question: 'Will this project need a REST server?',
9
+ package: '@stonyx/rest-server',
10
+ dirs: ['requests']
11
+ },
12
+ {
13
+ question: 'Will this project need WebSockets?',
14
+ package: '@stonyx/sockets',
15
+ dirs: ['socket-handlers']
16
+ },
17
+ {
18
+ question: 'Will this project need data management?',
19
+ package: '@stonyx/orm',
20
+ dirs: ['models', 'serializers', 'access', 'transforms', 'hooks'],
21
+ files: { 'config/db-schema.ts': generateDbSchema }
22
+ },
23
+ {
24
+ question: 'Will this project need scheduled tasks (cron)?',
25
+ package: '@stonyx/cron',
26
+ dirs: ['crons']
27
+ },
28
+ {
29
+ question: 'Will this project need OAuth?',
30
+ package: '@stonyx/oauth'
31
+ },
32
+ {
33
+ question: 'Will this project need pub/sub events?',
34
+ package: '@stonyx/events'
35
+ },
36
+ {
37
+ question: 'Will this project need a Discord bot?',
38
+ package: '@stonyx/discord',
39
+ dirs: ['discord-commands', 'discord-events']
40
+ }
41
+ ];
42
+ export function generateDbSchema() {
43
+ return `import { Model, hasMany, type HasMany } from '@stonyx/orm';
44
+
45
+ export default class DBModel extends Model {
46
+ // Define your collections here
47
+ // examples: HasMany = hasMany('example');
48
+ }
49
+ `;
50
+ }
51
+ export function generatePackageJson(name, selectedModules) {
52
+ const devDependencies = {
53
+ stonyx: 'latest',
54
+ typescript: '^5.8.3'
55
+ };
56
+ for (const mod of selectedModules) {
57
+ devDependencies[mod.package] = 'latest';
58
+ }
59
+ // Sort dependencies alphabetically
60
+ const sorted = Object.fromEntries(Object.entries(devDependencies).sort(([a], [b]) => a.localeCompare(b)));
61
+ return JSON.stringify({
62
+ name,
63
+ version: '0.1.0',
64
+ type: 'module',
65
+ private: true,
66
+ scripts: {
67
+ build: 'tsc',
68
+ start: 'stonyx serve',
69
+ test: 'stonyx test'
70
+ },
71
+ devDependencies: sorted
72
+ }, null, 2) + '\n';
73
+ }
74
+ export function generateAppTs() {
75
+ return `import log from 'stonyx/log';
76
+
77
+ export default class App {
78
+ static instance: App;
79
+ ready: Promise<void>;
80
+
81
+ constructor() {
82
+ if (App.instance) return App.instance;
83
+ App.instance = this;
84
+
85
+ this.ready = this.init();
86
+ }
87
+
88
+ async init(): Promise<void> {
89
+ log.info('Initializing Application');
90
+
91
+ // Application setup here
92
+
93
+ log.info('Application has been initialized');
94
+ }
95
+ }
96
+ `;
97
+ }
98
+ export function generateEnvironmentTs() {
99
+ return `export default {
100
+ }
101
+ `;
102
+ }
103
+ export function generateEnvironmentExampleTs() {
104
+ return `// Copy this file to environment.ts and fill in your values
105
+ // All values should use environment variables with ?? fallback defaults
106
+
107
+ const {
108
+ NODE_ENV,
109
+ } = process.env;
110
+
111
+ const environment: string = NODE_ENV ?? 'development';
112
+
113
+ export default {
114
+ }
115
+ `;
116
+ }
117
+ export function generateGitignore() {
118
+ return `node_modules/
119
+ .env
120
+ db.json
121
+ *.log
122
+
123
+ # Compiled TypeScript output (tsc compiles .ts to .js in-place)
124
+ *.js
125
+ *.d.ts
126
+ *.js.map
127
+ # Keep test config (JavaScript)
128
+ !test/**/*.js
129
+ `;
130
+ }
131
+ export function generateTsConfig() {
132
+ return JSON.stringify({
133
+ compilerOptions: {
134
+ strict: true,
135
+ target: 'ES2022',
136
+ module: 'NodeNext',
137
+ moduleResolution: 'NodeNext',
138
+ outDir: '.',
139
+ rootDir: '.',
140
+ esModuleInterop: true,
141
+ skipLibCheck: true,
142
+ forceConsistentCasingInFileNames: true
143
+ },
144
+ include: ['**/*.ts'],
145
+ exclude: ['node_modules', 'test']
146
+ }, null, 2) + '\n';
147
+ }
148
+ function runPnpmInstall(projectDir) {
149
+ return new Promise((resolve, reject) => {
150
+ const child = spawn('pnpm', ['install'], {
151
+ cwd: projectDir,
152
+ stdio: 'inherit'
153
+ });
154
+ child.on('close', code => {
155
+ if (code === 0)
156
+ resolve();
157
+ else
158
+ reject(new Error(`pnpm install exited with code ${code}`));
159
+ });
160
+ child.on('error', reject);
161
+ });
162
+ }
163
+ export default async function newCommand({ args }) {
164
+ let appName = args[0];
165
+ if (!appName) {
166
+ appName = await prompt('Project name:');
167
+ }
168
+ if (!appName) {
169
+ console.error('Project name is required.');
170
+ process.exit(1);
171
+ }
172
+ const projectDir = path.resolve(process.cwd(), appName);
173
+ if (await fileExists(projectDir)) {
174
+ console.error(`Directory "${appName}" already exists.`);
175
+ process.exit(1);
176
+ }
177
+ console.log(`\nScaffolding new Stonyx project: ${appName}\n`);
178
+ // Prompt for module selection
179
+ const selectedModules = [];
180
+ for (const mod of MODULE_OPTIONS) {
181
+ if (await confirm(mod.question)) {
182
+ selectedModules.push(mod);
183
+ }
184
+ }
185
+ console.log('\nCreating project structure...\n');
186
+ // Create project directory
187
+ await createDirectory(projectDir);
188
+ // Copy .nvmrc from monorepo root
189
+ const monorepoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
190
+ const nvmrcSource = path.join(monorepoRoot, '.nvmrc');
191
+ if (await fileExists(nvmrcSource)) {
192
+ await copyFile(nvmrcSource, path.join(projectDir, '.nvmrc'));
193
+ }
194
+ // Generate core files
195
+ await createFile(path.join(projectDir, 'package.json'), generatePackageJson(appName, selectedModules));
196
+ await createFile(path.join(projectDir, 'app.ts'), generateAppTs());
197
+ await createFile(path.join(projectDir, 'tsconfig.json'), generateTsConfig());
198
+ await createFile(path.join(projectDir, '.gitignore'), generateGitignore());
199
+ // Create config directory and files
200
+ await createFile(path.join(projectDir, 'config', 'environment.ts'), generateEnvironmentTs());
201
+ await createFile(path.join(projectDir, 'config', 'environment.example.ts'), generateEnvironmentExampleTs());
202
+ // Create module-specific directories and files
203
+ for (const mod of selectedModules) {
204
+ if (mod.dirs) {
205
+ for (const dir of mod.dirs) {
206
+ await createDirectory(path.join(projectDir, dir));
207
+ // Create .gitkeep so empty dirs are tracked
208
+ await createFile(path.join(projectDir, dir, '.gitkeep'), '');
209
+ }
210
+ }
211
+ if (mod.files) {
212
+ for (const [filePath, generator] of Object.entries(mod.files)) {
213
+ await createFile(path.join(projectDir, filePath), generator());
214
+ }
215
+ }
216
+ }
217
+ // Create test structure
218
+ await createDirectory(path.join(projectDir, 'test', 'unit'));
219
+ await createFile(path.join(projectDir, 'test', 'unit', '.gitkeep'), '');
220
+ await createDirectory(path.join(projectDir, 'test', 'integration'));
221
+ await createFile(path.join(projectDir, 'test', 'integration', '.gitkeep'), '');
222
+ await createDirectory(path.join(projectDir, 'test', 'acceptance'));
223
+ await createFile(path.join(projectDir, 'test', 'acceptance', '.gitkeep'), '');
224
+ // Create test config
225
+ await createFile(path.join(projectDir, 'test', 'config', 'environment.js'), `export default {\n // Test-specific config overrides\n}\n`);
226
+ console.log('Installing dependencies...\n');
227
+ try {
228
+ await runPnpmInstall(projectDir);
229
+ }
230
+ catch (error) {
231
+ console.error('Failed to install dependencies. Run `pnpm install` manually in the project directory.');
232
+ }
233
+ console.log(`\n✓ Project "${appName}" created successfully!`);
234
+ console.log(`\n cd ${appName}`);
235
+ console.log(` stonyx serve\n`);
236
+ }
@@ -0,0 +1,5 @@
1
+ import { type StoynxModule } from '../lifecycle.js';
2
+ export declare function createShutdownHandler(modules: StoynxModule[]): () => Promise<void>;
3
+ export default function serve({ args }: {
4
+ args: string[];
5
+ }): Promise<void>;
@@ -0,0 +1,29 @@
1
+ import { runStartupHooks, runShutdownHooks } from '../lifecycle.js';
2
+ export function createShutdownHandler(modules) {
3
+ let shuttingDown = false;
4
+ return async () => {
5
+ if (shuttingDown)
6
+ return;
7
+ shuttingDown = true;
8
+ await runShutdownHooks(modules);
9
+ process.exit(0);
10
+ };
11
+ }
12
+ export default async function serve({ args }) {
13
+ const cwd = process.cwd();
14
+ const entryFlag = args.indexOf('--entry');
15
+ const entryPoint = entryFlag !== -1 ? args[entryFlag + 1] : 'app.js';
16
+ const { default: config } = await import(`${cwd}/config/environment.js`);
17
+ const { default: Stonyx } = await import('../main.js');
18
+ new Stonyx(config, cwd);
19
+ await Stonyx.ready;
20
+ const { modules } = Stonyx.instance;
21
+ await runStartupHooks(modules);
22
+ const shutdown = createShutdownHandler(modules);
23
+ process.on('SIGTERM', shutdown);
24
+ process.on('SIGINT', shutdown);
25
+ const entryModule = await import(`${cwd}/${entryPoint}`);
26
+ if (entryModule.default) {
27
+ new entryModule.default();
28
+ }
29
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,8 +1,5 @@
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);
1
+ import { pathToFileURL } from 'url';
2
+ const cwd = process.cwd();
3
+ const { default: Stonyx } = await import('stonyx');
4
+ const { default: config } = await import(pathToFileURL(`${cwd}/config/environment.js`).href);
5
+ new Stonyx(config, cwd);
@@ -0,0 +1,3 @@
1
+ export default function test({ args }: {
2
+ args: string[];
3
+ }): Promise<void>;
@@ -0,0 +1,23 @@
1
+ import { spawn } from 'child_process';
2
+ import { fileURLToPath, pathToFileURL } from 'url';
3
+ import path from 'path';
4
+ export default async function test({ args }) {
5
+ const cwd = process.cwd();
6
+ const setupFile = path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'test-setup.js');
7
+ const setupFileUrl = pathToFileURL(setupFile).href;
8
+ const qunitBin = path.resolve(cwd, 'node_modules/qunit/bin/qunit.js');
9
+ // Default to conventional test glob if no args provided
10
+ const testArgs = args.length > 0 ? args : ['test/**/*-test.js'];
11
+ const child = spawn(process.execPath, [
12
+ '--import', setupFileUrl,
13
+ qunitBin,
14
+ ...testArgs
15
+ ], {
16
+ cwd,
17
+ stdio: 'inherit',
18
+ env: { ...process.env, NODE_ENV: 'test' }
19
+ });
20
+ child.on('close', (code) => {
21
+ process.exit(code ?? 1);
22
+ });
23
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env node
2
+ import serve from './cli/serve.js';
3
+ import test from './cli/test.js';
4
+ import help from './cli/help.js';
5
+ import newCommand from './cli/new.js';
6
+ import loadModuleCommands from './cli/load-commands.js';
7
+ try {
8
+ process.loadEnvFile();
9
+ }
10
+ catch { /* no .env file */ }
11
+ const aliases = { s: 'serve', t: 'test', h: 'help', n: 'new' };
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
+ new: { description: 'Scaffold a new Stonyx project', run: newCommand },
16
+ help: { description: 'Show available commands', run: help }
17
+ };
18
+ const args = process.argv.slice(2);
19
+ const commandName = aliases[args[0]] || args[0];
20
+ const commandArgs = args.slice(1);
21
+ async function main() {
22
+ if (!commandName || commandName === 'help') {
23
+ await help({ args: commandArgs, builtInCommands, loadModuleCommands });
24
+ return;
25
+ }
26
+ const builtIn = builtInCommands[commandName];
27
+ if (builtIn) {
28
+ await builtIn.run({ args: commandArgs });
29
+ return;
30
+ }
31
+ // Search module commands
32
+ const moduleCommands = await loadModuleCommands();
33
+ const moduleCommand = moduleCommands[commandName];
34
+ if (moduleCommand) {
35
+ const cwd = process.cwd();
36
+ if (moduleCommand.bootstrap) {
37
+ const { default: config } = await import(`${cwd}/config/environment.js`);
38
+ const { default: Stonyx } = await import('./main.js');
39
+ new Stonyx(config, cwd);
40
+ await Stonyx.ready;
41
+ }
42
+ await moduleCommand.run({ args: commandArgs, cwd });
43
+ return;
44
+ }
45
+ console.error(`Unknown command: ${args[0]}\n`);
46
+ await help({ args: [], builtInCommands, loadModuleCommands });
47
+ process.exit(1);
48
+ }
49
+ main().catch(error => {
50
+ console.error(error);
51
+ process.exit(1);
52
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../modules.js").StoynxConfig;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ import Stonyx from 'stonyx';
2
+ export default Stonyx.config;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("@stonyx/logs").default;
2
+ export default _default;
@@ -1,3 +1,2 @@
1
1
  import Stonyx from 'stonyx';
2
-
3
- export default Stonyx.log;
2
+ export default Stonyx.log;
@@ -0,0 +1,5 @@
1
+ interface TestHooks {
2
+ before(fn: () => Promise<void>): void;
3
+ }
4
+ export declare function setupIntegrationTests(hooks: TestHooks): void;
5
+ export {};
@@ -0,0 +1,7 @@
1
+ import Stonyx from 'stonyx';
2
+ export function setupIntegrationTests(hooks) {
3
+ hooks.before(async function () {
4
+ await Stonyx.ready;
5
+ console.log('Stonyx ready');
6
+ });
7
+ }
@@ -0,0 +1,7 @@
1
+ export interface StoynxModule {
2
+ init?(): Promise<void>;
3
+ startup?(): Promise<void>;
4
+ shutdown?(): Promise<void>;
5
+ }
6
+ export declare function runStartupHooks(modules: StoynxModule[]): Promise<void>;
7
+ export declare function runShutdownHooks(modules: StoynxModule[]): Promise<void>;
@@ -0,0 +1,18 @@
1
+ export async function runStartupHooks(modules) {
2
+ for (const module of modules) {
3
+ if (typeof module.startup === 'function')
4
+ await module.startup();
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
+ }
13
+ catch (error) {
14
+ console.error('Error during module shutdown:', error);
15
+ }
16
+ }
17
+ }
18
+ }
package/dist/main.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import Chronicle from '@stonyx/logs';
2
+ import type { StoynxModule } from './lifecycle.js';
3
+ import type { StoynxConfig } from './modules.js';
4
+ export default class Stonyx {
5
+ static initialized: boolean;
6
+ static modulePromises: Record<string, unknown>;
7
+ static instance: Stonyx;
8
+ static ready: Promise<void>;
9
+ config: StoynxConfig;
10
+ chronicle: Chronicle;
11
+ modules: StoynxModule[];
12
+ constructor(config: StoynxConfig, rootPath: string);
13
+ start(config: StoynxConfig, rootPath: string): Promise<void>;
14
+ /**
15
+ * Allows users to define their own log for any extra class via environment config
16
+ */
17
+ configureUserLogs(): void;
18
+ static get log(): Chronicle;
19
+ static get config(): StoynxConfig;
20
+ }
21
+ export { waitForModule } from './modules.js';