stonyx 0.2.3-alpha.5 → 0.2.3-alpha.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -7
- package/config/{environment.js → environment copy.ts } +16 -12
- package/dist/cli/help.d.ts +8 -0
- package/dist/cli/help.js +43 -0
- package/dist/cli/load-commands.d.ts +10 -0
- package/dist/cli/load-commands.js +49 -0
- package/dist/cli/new.d.ts +49 -0
- package/dist/cli/new.js +341 -0
- package/dist/cli/serve.d.ts +6 -0
- package/dist/cli/serve.js +49 -0
- package/dist/cli/test-setup.d.ts +1 -0
- package/dist/cli/test-setup.js +30 -0
- package/dist/cli/test.d.ts +4 -0
- package/dist/cli/test.js +23 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +53 -0
- package/dist/exports/config.d.ts +2 -0
- package/dist/exports/config.js +2 -0
- package/dist/exports/log.d.ts +2 -0
- package/{src → dist}/exports/log.js +1 -2
- package/dist/exports/test-helpers.d.ts +5 -0
- package/dist/exports/test-helpers.js +7 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.js +18 -0
- package/dist/main.d.ts +22 -0
- package/dist/main.js +102 -0
- package/dist/modules.d.ts +5 -0
- package/dist/modules.js +93 -0
- package/dist/util/import-config.d.ts +52 -0
- package/dist/util/import-config.js +256 -0
- package/dist/util/resolve-entry-point.d.ts +1 -0
- package/dist/util/resolve-entry-point.js +14 -0
- package/dist/util/resolve-module-name.d.ts +14 -0
- package/dist/util/resolve-module-name.js +33 -0
- package/package.json +41 -12
- package/scripts/postinstall.js +24 -2
- package/.claude/index.md +0 -96
- package/.github/workflows/ci.yml +0 -16
- package/.github/workflows/publish.yml +0 -51
- package/docs/api.md +0 -89
- package/docs/cli.md +0 -78
- package/docs/configuration.md +0 -72
- package/docs/conventions/discord-conventions.md +0 -62
- package/docs/conventions/framework-modules.md +0 -127
- package/docs/conventions/index.md +0 -24
- package/docs/conventions/orm-conventions.md +0 -158
- package/docs/conventions/project-structure.md +0 -105
- package/docs/conventions/rest-conventions.md +0 -96
- package/docs/conventions/testing-conventions.md +0 -56
- package/docs/developing-modules.md +0 -108
- package/docs/index.md +0 -15
- package/docs/lifecycle.md +0 -47
- package/docs/logging.md +0 -52
- package/docs/modules.md +0 -78
- package/docs/testing.md +0 -64
- package/src/cli/help.js +0 -48
- package/src/cli/load-commands.js +0 -56
- package/src/cli/new.js +0 -237
- package/src/cli/serve.js +0 -38
- package/src/cli/test-setup.js +0 -8
- package/src/cli/test.js +0 -27
- package/src/cli.js +0 -63
- package/src/exports/config.js +0 -3
- package/src/exports/test-helpers.js +0 -8
- package/src/lifecycle.js +0 -17
- package/src/main.js +0 -101
- package/src/modules.js +0 -112
package/README.md
CHANGED
|
@@ -1,26 +1,33 @@
|
|
|
1
|
+
[](https://github.com/abofs/stonyx/actions/workflows/ci.yml)
|
|
2
|
+
[](https://www.npmjs.com/package/stonyx)
|
|
3
|
+
[](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.
|
|
4
8
|
|
|
5
|
-
- 100% JavaScript (ES Modules)
|
|
6
9
|
- Drop-in module system — no boilerplate
|
|
7
10
|
- Automatic async module loading and initialization
|
|
8
|
-
- Built-in CLI for bootstrapping and testing
|
|
11
|
+
- Built-in CLI for scaffolding, bootstrapping, and testing
|
|
9
12
|
|
|
10
13
|
## Quick Start
|
|
11
14
|
|
|
12
15
|
```bash
|
|
13
|
-
npm install stonyx
|
|
16
|
+
npm install -g stonyx
|
|
17
|
+
stonyx new my-app
|
|
18
|
+
cd my-app
|
|
19
|
+
stonyx serve
|
|
14
20
|
```
|
|
15
21
|
|
|
16
|
-
The
|
|
22
|
+
The `new` command walks you through module selection and generates a ready-to-run project. From there, the CLI handles bootstrapping:
|
|
17
23
|
|
|
18
24
|
```bash
|
|
19
|
-
stonyx serve # Bootstrap + run app.js
|
|
25
|
+
stonyx serve # Bootstrap + run app.ts (or app.js)
|
|
20
26
|
stonyx test # Bootstrap + run tests
|
|
27
|
+
stonyx help # Show all available commands
|
|
21
28
|
```
|
|
22
29
|
|
|
23
|
-
Stonyx reads `config/environment.js`, initializes all `@stonyx/*` modules from your `devDependencies`, and runs your application.
|
|
30
|
+
Stonyx reads `config/environment.ts` (preferred) or `config/environment.js`, initializes all `@stonyx/*` modules from your `devDependencies`, and runs your application.
|
|
24
31
|
|
|
25
32
|
## Documentation
|
|
26
33
|
|
|
@@ -40,8 +47,12 @@ Stonyx reads `config/environment.js`, initializes all `@stonyx/*` modules from y
|
|
|
40
47
|
| Module | Description |
|
|
41
48
|
|--------|-------------|
|
|
42
49
|
| [@stonyx/cron](https://github.com/abofs/stonyx-cron) | Lightweight async job scheduling |
|
|
43
|
-
| [@stonyx/
|
|
50
|
+
| [@stonyx/discord](https://github.com/abofs/stonyx-discord) | Discord bot with command and event handler auto-discovery |
|
|
51
|
+
| [@stonyx/events](https://github.com/abofs/stonyx-events) | Pub/sub event system |
|
|
52
|
+
| [@stonyx/oauth](https://github.com/abofs/stonyx-oauth) | OAuth provider integration |
|
|
44
53
|
| [@stonyx/orm](https://github.com/abofs/stonyx-orm) | ORM with models, relationships, and serializers |
|
|
54
|
+
| [@stonyx/rest-server](https://github.com/abofs/stonyx-rest-server) | Dynamic REST server with auto-route registration |
|
|
55
|
+
| [@stonyx/sockets](https://github.com/abofs/stonyx-sockets) | WebSocket server and client |
|
|
45
56
|
|
|
46
57
|
## License
|
|
47
58
|
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import type { StoynxConfig } from 'stonyx';
|
|
2
|
+
|
|
3
|
+
// project configuration, override-able by listed environment variables
|
|
4
|
+
const {
|
|
5
|
+
DEBUG,
|
|
6
|
+
NODE_ENV,
|
|
7
|
+
} = process.env;
|
|
8
|
+
|
|
9
|
+
const environment = NODE_ENV ?? 'development';
|
|
10
|
+
|
|
11
|
+
const config: StoynxConfig = {
|
|
12
|
+
environment,
|
|
13
|
+
debug: DEBUG ?? environment === 'development',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default config;
|
|
@@ -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 {};
|
package/dist/cli/help.js
ADDED
|
@@ -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.ts (or app.js)');
|
|
27
|
+
console.log(' Config: config/environment.ts (or .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,49 @@
|
|
|
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
|
+
if (!commandsModule.default || typeof commandsModule.default !== 'object')
|
|
34
|
+
continue;
|
|
35
|
+
const moduleCommands = commandsModule.default;
|
|
36
|
+
for (const [name, command] of Object.entries(moduleCommands)) {
|
|
37
|
+
if (commands[name]) {
|
|
38
|
+
console.warn(`Warning: Command "${name}" from ${moduleName} conflicts with existing command. Skipping.`);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
commands[name] = { ...command, module: moduleName };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return commands;
|
|
49
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
interface ModuleOption {
|
|
2
|
+
question: string;
|
|
3
|
+
package: string;
|
|
4
|
+
dirs?: string[];
|
|
5
|
+
files?: Record<string, () => string>;
|
|
6
|
+
}
|
|
7
|
+
export declare const MODULE_OPTIONS: ModuleOption[];
|
|
8
|
+
export declare function generateDbSchema(): string;
|
|
9
|
+
/**
|
|
10
|
+
* The version of the `stonyx` package running this generator, read from its own
|
|
11
|
+
* `package.json` at call time.
|
|
12
|
+
*
|
|
13
|
+
* Read rather than hardcoded on purpose: a literal would be correct on the day
|
|
14
|
+
* it was written and silently stale on every day after (abofs/stonyx#113).
|
|
15
|
+
*/
|
|
16
|
+
export declare function readCoreVersion(): string;
|
|
17
|
+
/**
|
|
18
|
+
* The npm dist-tag naming the release line a version belongs to:
|
|
19
|
+
* `0.2.3-beta.96` -> `beta`, `0.2.3-alpha.4` -> `alpha`, `0.2.2` -> `latest`.
|
|
20
|
+
*
|
|
21
|
+
* Every scaffolded `@stonyx/*` module is requested on the same line as the core
|
|
22
|
+
* that scaffolded it, so the generated project cannot mix a prerelease core with
|
|
23
|
+
* modules from the stable line (or the reverse).
|
|
24
|
+
*
|
|
25
|
+
* Bound: this assumes a prerelease identifier is also a published dist-tag name.
|
|
26
|
+
* True for `beta` and `alpha`, which are the only two lines the fleet publishes.
|
|
27
|
+
* A core released as e.g. `0.2.3-rc.1` with no `rc` dist-tag would scaffold an
|
|
28
|
+
* unresolvable module specifier -- loudly, at install, not silently.
|
|
29
|
+
*/
|
|
30
|
+
export declare function releaseTagFor(version: string): string;
|
|
31
|
+
export declare function generatePackageJson(name: string, selectedModules: ModuleOption[], coreVersion?: string): string;
|
|
32
|
+
export declare function generateAppTs(): string;
|
|
33
|
+
export declare function generateEnvironmentTs(): string;
|
|
34
|
+
export declare function generateTestEnvironmentTs(): string;
|
|
35
|
+
export declare function generateSetupTs(): string;
|
|
36
|
+
export declare function generateZzExitTestTs(): string;
|
|
37
|
+
export declare function generateEnvironmentExampleTs(): string;
|
|
38
|
+
export declare function generateGitignore(): string;
|
|
39
|
+
export declare function generateTsConfig(): string;
|
|
40
|
+
/**
|
|
41
|
+
* Write every scaffolded file to disk given a resolved project directory,
|
|
42
|
+
* app name, and pre-selected module options. Factored out of `newCommand`
|
|
43
|
+
* so tests can exercise the file-writing path without interactive prompts.
|
|
44
|
+
*/
|
|
45
|
+
export declare function scaffoldProject(projectDir: string, appName: string, selectedModules: ModuleOption[]): Promise<void>;
|
|
46
|
+
export default function newCommand({ args }: {
|
|
47
|
+
args: string[];
|
|
48
|
+
}): Promise<void>;
|
|
49
|
+
export {};
|
package/dist/cli/new.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
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 { readFileSync } from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
export const MODULE_OPTIONS = [
|
|
8
|
+
{
|
|
9
|
+
question: 'Will this project need a REST server?',
|
|
10
|
+
package: '@stonyx/rest-server',
|
|
11
|
+
dirs: ['requests']
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
question: 'Will this project need WebSockets?',
|
|
15
|
+
package: '@stonyx/sockets',
|
|
16
|
+
dirs: ['socket-handlers']
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
question: 'Will this project need data management?',
|
|
20
|
+
package: '@stonyx/orm',
|
|
21
|
+
dirs: ['models', 'serializers', 'access', 'transforms', 'hooks'],
|
|
22
|
+
files: { 'config/db-schema.ts': generateDbSchema }
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
question: 'Will this project need scheduled tasks (cron)?',
|
|
26
|
+
package: '@stonyx/cron',
|
|
27
|
+
dirs: ['crons']
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
question: 'Will this project need OAuth?',
|
|
31
|
+
package: '@stonyx/oauth'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
question: 'Will this project need pub/sub events?',
|
|
35
|
+
package: '@stonyx/events'
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
question: 'Will this project need a Discord bot?',
|
|
39
|
+
package: '@stonyx/discord',
|
|
40
|
+
dirs: ['discord-commands', 'discord-events']
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
export function generateDbSchema() {
|
|
44
|
+
return `import { Model, hasMany, type HasMany } from '@stonyx/orm';
|
|
45
|
+
|
|
46
|
+
export default class DBModel extends Model {
|
|
47
|
+
// Define your collections here
|
|
48
|
+
// examples: HasMany = hasMany('example');
|
|
49
|
+
}
|
|
50
|
+
`;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The npm dist-tag that points at a package's stable release line. Only ever
|
|
54
|
+
* used as a *release line* selector for `@stonyx/*` modules (see
|
|
55
|
+
* `releaseTagFor`) -- never as the specifier for the core, which is always
|
|
56
|
+
* pinned exactly.
|
|
57
|
+
*/
|
|
58
|
+
const STABLE_DIST_TAG = 'latest';
|
|
59
|
+
/** Absolute path to this package's own root, from both `src/` and `dist/`. */
|
|
60
|
+
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
61
|
+
/**
|
|
62
|
+
* The version of the `stonyx` package running this generator, read from its own
|
|
63
|
+
* `package.json` at call time.
|
|
64
|
+
*
|
|
65
|
+
* Read rather than hardcoded on purpose: a literal would be correct on the day
|
|
66
|
+
* it was written and silently stale on every day after (abofs/stonyx#113).
|
|
67
|
+
*/
|
|
68
|
+
export function readCoreVersion() {
|
|
69
|
+
const raw = readFileSync(path.join(packageRoot, 'package.json'), 'utf8');
|
|
70
|
+
const version = JSON.parse(raw).version;
|
|
71
|
+
if (typeof version !== 'string' || !version) {
|
|
72
|
+
throw new Error(`Could not read the stonyx version from ${path.join(packageRoot, 'package.json')}`);
|
|
73
|
+
}
|
|
74
|
+
return version;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The npm dist-tag naming the release line a version belongs to:
|
|
78
|
+
* `0.2.3-beta.96` -> `beta`, `0.2.3-alpha.4` -> `alpha`, `0.2.2` -> `latest`.
|
|
79
|
+
*
|
|
80
|
+
* Every scaffolded `@stonyx/*` module is requested on the same line as the core
|
|
81
|
+
* that scaffolded it, so the generated project cannot mix a prerelease core with
|
|
82
|
+
* modules from the stable line (or the reverse).
|
|
83
|
+
*
|
|
84
|
+
* Bound: this assumes a prerelease identifier is also a published dist-tag name.
|
|
85
|
+
* True for `beta` and `alpha`, which are the only two lines the fleet publishes.
|
|
86
|
+
* A core released as e.g. `0.2.3-rc.1` with no `rc` dist-tag would scaffold an
|
|
87
|
+
* unresolvable module specifier -- loudly, at install, not silently.
|
|
88
|
+
*/
|
|
89
|
+
export function releaseTagFor(version) {
|
|
90
|
+
const prerelease = /^\d+\.\d+\.\d+-([0-9A-Za-z-]+)(?:\.|$)/.exec(version);
|
|
91
|
+
return prerelease ? prerelease[1] : STABLE_DIST_TAG;
|
|
92
|
+
}
|
|
93
|
+
export function generatePackageJson(name, selectedModules, coreVersion = readCoreVersion()) {
|
|
94
|
+
// The framework is a runtime dependency of the application, pinned exactly to
|
|
95
|
+
// the core that generated the project.
|
|
96
|
+
const dependencies = { stonyx: coreVersion };
|
|
97
|
+
const devDependencies = {
|
|
98
|
+
qunit: '^2.24.1',
|
|
99
|
+
tsx: '^4.21.0',
|
|
100
|
+
typescript: '^5.8.3'
|
|
101
|
+
};
|
|
102
|
+
// Modules are discovered from devDependencies (see docs/modules.md), and are
|
|
103
|
+
// requested on the core's own release line rather than at `latest`.
|
|
104
|
+
const moduleTag = releaseTagFor(coreVersion);
|
|
105
|
+
for (const mod of selectedModules) {
|
|
106
|
+
devDependencies[mod.package] = moduleTag;
|
|
107
|
+
}
|
|
108
|
+
// Sort dependencies alphabetically
|
|
109
|
+
const sorted = Object.fromEntries(Object.entries(devDependencies).sort(([a], [b]) => a.localeCompare(b)));
|
|
110
|
+
return JSON.stringify({
|
|
111
|
+
name,
|
|
112
|
+
version: '0.1.0',
|
|
113
|
+
type: 'module',
|
|
114
|
+
private: true,
|
|
115
|
+
scripts: {
|
|
116
|
+
build: 'tsc',
|
|
117
|
+
serve: 'stonyx serve',
|
|
118
|
+
start: 'stonyx serve',
|
|
119
|
+
test: "NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'"
|
|
120
|
+
},
|
|
121
|
+
dependencies,
|
|
122
|
+
devDependencies: sorted
|
|
123
|
+
}, null, 2) + '\n';
|
|
124
|
+
}
|
|
125
|
+
export function generateAppTs() {
|
|
126
|
+
return `import log from 'stonyx/log';
|
|
127
|
+
|
|
128
|
+
export default class App {
|
|
129
|
+
static instance: App;
|
|
130
|
+
ready: Promise<void>;
|
|
131
|
+
|
|
132
|
+
constructor() {
|
|
133
|
+
if (App.instance) return App.instance;
|
|
134
|
+
App.instance = this;
|
|
135
|
+
|
|
136
|
+
this.ready = this.init();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async init(): Promise<void> {
|
|
140
|
+
log.info('Initializing Application');
|
|
141
|
+
|
|
142
|
+
// Application setup here
|
|
143
|
+
|
|
144
|
+
log.info('Application has been initialized');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
export function generateEnvironmentTs() {
|
|
150
|
+
return `import type { StoynxConfig } from 'stonyx';
|
|
151
|
+
|
|
152
|
+
const config: StoynxConfig = {
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export default config;
|
|
156
|
+
`;
|
|
157
|
+
}
|
|
158
|
+
export function generateTestEnvironmentTs() {
|
|
159
|
+
return `import type { StoynxConfig } from 'stonyx';
|
|
160
|
+
|
|
161
|
+
// Test-specific config overrides
|
|
162
|
+
const config: Partial<StoynxConfig> = {
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export default config;
|
|
166
|
+
`;
|
|
167
|
+
}
|
|
168
|
+
export function generateSetupTs() {
|
|
169
|
+
return `// Test setup: bootstrap Stonyx with the consumer config before qunit runs.
|
|
170
|
+
// Sprint 44 pattern — loaded via \`node --import ./test/setup.ts\`.
|
|
171
|
+
import { pathToFileURL } from 'url';
|
|
172
|
+
|
|
173
|
+
const cwd = process.cwd();
|
|
174
|
+
|
|
175
|
+
const { default: Stonyx } = await import('stonyx');
|
|
176
|
+
const { default: config } = await import(pathToFileURL(\`\${cwd}/config/environment.ts\`).href);
|
|
177
|
+
|
|
178
|
+
new Stonyx(config, cwd);
|
|
179
|
+
|
|
180
|
+
await Stonyx.ready;
|
|
181
|
+
`;
|
|
182
|
+
}
|
|
183
|
+
export function generateZzExitTestTs() {
|
|
184
|
+
return `// Force-exit hook for qunit runs.
|
|
185
|
+
//
|
|
186
|
+
// Stonyx modules may keep listeners/servers open that would otherwise
|
|
187
|
+
// block the process from exiting after tests complete, causing CI to
|
|
188
|
+
// hit timeouts even though all tests passed.
|
|
189
|
+
//
|
|
190
|
+
// Named \`zz-\` so alphabetical test-file order places it last, after
|
|
191
|
+
// all real test files have registered with QUnit.
|
|
192
|
+
import QUnit from 'qunit';
|
|
193
|
+
|
|
194
|
+
QUnit.on('runEnd', () => {
|
|
195
|
+
setImmediate(() => process.exit(process.exitCode ?? 0));
|
|
196
|
+
});
|
|
197
|
+
`;
|
|
198
|
+
}
|
|
199
|
+
export function generateEnvironmentExampleTs() {
|
|
200
|
+
return `// Copy this file to environment.ts and fill in your values
|
|
201
|
+
// All values should use environment variables with ?? fallback defaults
|
|
202
|
+
|
|
203
|
+
const {
|
|
204
|
+
NODE_ENV,
|
|
205
|
+
} = process.env;
|
|
206
|
+
|
|
207
|
+
const environment: string = NODE_ENV ?? 'development';
|
|
208
|
+
|
|
209
|
+
export default {
|
|
210
|
+
}
|
|
211
|
+
`;
|
|
212
|
+
}
|
|
213
|
+
export function generateGitignore() {
|
|
214
|
+
return `node_modules/
|
|
215
|
+
.env
|
|
216
|
+
db.json
|
|
217
|
+
*.log
|
|
218
|
+
|
|
219
|
+
# Compiled TypeScript output (tsc compiles .ts to .js in-place)
|
|
220
|
+
*.js
|
|
221
|
+
*.d.ts
|
|
222
|
+
*.js.map
|
|
223
|
+
`;
|
|
224
|
+
}
|
|
225
|
+
export function generateTsConfig() {
|
|
226
|
+
return JSON.stringify({
|
|
227
|
+
compilerOptions: {
|
|
228
|
+
strict: true,
|
|
229
|
+
target: 'ES2022',
|
|
230
|
+
module: 'NodeNext',
|
|
231
|
+
moduleResolution: 'NodeNext',
|
|
232
|
+
outDir: '.',
|
|
233
|
+
rootDir: '.',
|
|
234
|
+
esModuleInterop: true,
|
|
235
|
+
skipLibCheck: true,
|
|
236
|
+
forceConsistentCasingInFileNames: true
|
|
237
|
+
},
|
|
238
|
+
include: ['**/*.ts'],
|
|
239
|
+
exclude: ['node_modules', 'test']
|
|
240
|
+
}, null, 2) + '\n';
|
|
241
|
+
}
|
|
242
|
+
function runPnpmInstall(projectDir) {
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
const child = spawn('pnpm', ['install'], {
|
|
245
|
+
cwd: projectDir,
|
|
246
|
+
stdio: 'inherit'
|
|
247
|
+
});
|
|
248
|
+
child.on('close', code => {
|
|
249
|
+
if (code === 0)
|
|
250
|
+
resolve();
|
|
251
|
+
else
|
|
252
|
+
reject(new Error(`pnpm install exited with code ${code}`));
|
|
253
|
+
});
|
|
254
|
+
child.on('error', reject);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Write every scaffolded file to disk given a resolved project directory,
|
|
259
|
+
* app name, and pre-selected module options. Factored out of `newCommand`
|
|
260
|
+
* so tests can exercise the file-writing path without interactive prompts.
|
|
261
|
+
*/
|
|
262
|
+
export async function scaffoldProject(projectDir, appName, selectedModules) {
|
|
263
|
+
// Create project directory
|
|
264
|
+
await createDirectory(projectDir);
|
|
265
|
+
// Copy .nvmrc from monorepo root
|
|
266
|
+
const monorepoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
|
|
267
|
+
const nvmrcSource = path.join(monorepoRoot, '.nvmrc');
|
|
268
|
+
if (await fileExists(nvmrcSource)) {
|
|
269
|
+
await copyFile(nvmrcSource, path.join(projectDir, '.nvmrc'));
|
|
270
|
+
}
|
|
271
|
+
// Generate core files
|
|
272
|
+
await createFile(path.join(projectDir, 'package.json'), generatePackageJson(appName, selectedModules));
|
|
273
|
+
await createFile(path.join(projectDir, 'app.ts'), generateAppTs());
|
|
274
|
+
await createFile(path.join(projectDir, 'tsconfig.json'), generateTsConfig());
|
|
275
|
+
await createFile(path.join(projectDir, '.gitignore'), generateGitignore());
|
|
276
|
+
// Create config directory and files
|
|
277
|
+
await createFile(path.join(projectDir, 'config', 'environment.ts'), generateEnvironmentTs());
|
|
278
|
+
await createFile(path.join(projectDir, 'config', 'environment.example.ts'), generateEnvironmentExampleTs());
|
|
279
|
+
// Create module-specific directories and files
|
|
280
|
+
for (const mod of selectedModules) {
|
|
281
|
+
if (mod.dirs) {
|
|
282
|
+
for (const dir of mod.dirs) {
|
|
283
|
+
await createDirectory(path.join(projectDir, dir));
|
|
284
|
+
// Create .gitkeep so empty dirs are tracked
|
|
285
|
+
await createFile(path.join(projectDir, dir, '.gitkeep'), '');
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (mod.files) {
|
|
289
|
+
for (const [filePath, generator] of Object.entries(mod.files)) {
|
|
290
|
+
await createFile(path.join(projectDir, filePath), generator());
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// Create test structure
|
|
295
|
+
await createDirectory(path.join(projectDir, 'test', 'unit'));
|
|
296
|
+
await createFile(path.join(projectDir, 'test', 'unit', '.gitkeep'), '');
|
|
297
|
+
await createDirectory(path.join(projectDir, 'test', 'integration'));
|
|
298
|
+
await createFile(path.join(projectDir, 'test', 'integration', '.gitkeep'), '');
|
|
299
|
+
await createDirectory(path.join(projectDir, 'test', 'acceptance'));
|
|
300
|
+
await createFile(path.join(projectDir, 'test', 'acceptance', '.gitkeep'), '');
|
|
301
|
+
// Create test config (TS)
|
|
302
|
+
await createFile(path.join(projectDir, 'test', 'config', 'environment.ts'), generateTestEnvironmentTs());
|
|
303
|
+
// Create Sprint 44 test harness files (setup bootstraps Stonyx, zz-exit-test drains event loop)
|
|
304
|
+
await createFile(path.join(projectDir, 'test', 'setup.ts'), generateSetupTs());
|
|
305
|
+
await createFile(path.join(projectDir, 'test', 'zz-exit-test.ts'), generateZzExitTestTs());
|
|
306
|
+
}
|
|
307
|
+
export default async function newCommand({ args }) {
|
|
308
|
+
let appName = args[0];
|
|
309
|
+
if (!appName) {
|
|
310
|
+
appName = await prompt('Project name:');
|
|
311
|
+
}
|
|
312
|
+
if (!appName) {
|
|
313
|
+
console.error('Project name is required.');
|
|
314
|
+
process.exit(1);
|
|
315
|
+
}
|
|
316
|
+
const projectDir = path.resolve(process.cwd(), appName);
|
|
317
|
+
if (await fileExists(projectDir)) {
|
|
318
|
+
console.error(`Directory "${appName}" already exists.`);
|
|
319
|
+
process.exit(1);
|
|
320
|
+
}
|
|
321
|
+
console.log(`\nScaffolding new Stonyx project: ${appName}\n`);
|
|
322
|
+
// Prompt for module selection
|
|
323
|
+
const selectedModules = [];
|
|
324
|
+
for (const mod of MODULE_OPTIONS) {
|
|
325
|
+
if (await confirm(mod.question)) {
|
|
326
|
+
selectedModules.push(mod);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
console.log('\nCreating project structure...\n');
|
|
330
|
+
await scaffoldProject(projectDir, appName, selectedModules);
|
|
331
|
+
console.log('Installing dependencies...\n');
|
|
332
|
+
try {
|
|
333
|
+
await runPnpmInstall(projectDir);
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
console.error('Failed to install dependencies. Run `pnpm install` manually in the project directory.');
|
|
337
|
+
}
|
|
338
|
+
console.log(`\n✓ Project "${appName}" created successfully!`);
|
|
339
|
+
console.log(`\n cd ${appName}`);
|
|
340
|
+
console.log(` stonyx serve\n`);
|
|
341
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type StoynxModule } from '../lifecycle.js';
|
|
2
|
+
export declare function createShutdownHandler(modules: StoynxModule[]): () => Promise<void>;
|
|
3
|
+
export declare function maybeRegisterAppShutdown(modules: StoynxModule[], appInstance: unknown, originalShutdown: () => Promise<void>): void;
|
|
4
|
+
export default function serve({ args }: {
|
|
5
|
+
args: string[];
|
|
6
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { resolve } from 'path';
|
|
2
|
+
import { pathToFileURL } from 'url';
|
|
3
|
+
import { runStartupHooks, runShutdownHooks } from '../lifecycle.js';
|
|
4
|
+
import { importConfig } from '../util/import-config.js';
|
|
5
|
+
import { resolveEntryPoint } from '../util/resolve-entry-point.js';
|
|
6
|
+
export function createShutdownHandler(modules) {
|
|
7
|
+
let shuttingDown = false;
|
|
8
|
+
return async () => {
|
|
9
|
+
if (shuttingDown)
|
|
10
|
+
return;
|
|
11
|
+
shuttingDown = true;
|
|
12
|
+
await runShutdownHooks(modules);
|
|
13
|
+
process.exit(0);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function maybeRegisterAppShutdown(modules, appInstance, originalShutdown) {
|
|
17
|
+
if (appInstance &&
|
|
18
|
+
typeof appInstance.shutdown === 'function') {
|
|
19
|
+
const appShutdown = createShutdownHandler([
|
|
20
|
+
...modules,
|
|
21
|
+
appInstance
|
|
22
|
+
]);
|
|
23
|
+
process.removeListener('SIGTERM', originalShutdown);
|
|
24
|
+
process.removeListener('SIGINT', originalShutdown);
|
|
25
|
+
process.on('SIGTERM', appShutdown);
|
|
26
|
+
process.on('SIGINT', appShutdown);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export default async function serve({ args }) {
|
|
30
|
+
const cwd = process.cwd();
|
|
31
|
+
const entryFlag = args.indexOf('--entry');
|
|
32
|
+
const entryArg = (entryFlag !== -1 && entryFlag + 1 < args.length) ? args[entryFlag + 1] : null;
|
|
33
|
+
const entryPoint = entryArg !== null ? resolve(cwd, entryArg) : resolveEntryPoint(`${cwd}/app`);
|
|
34
|
+
const config = await importConfig(`${cwd}/config/environment`);
|
|
35
|
+
const { default: Stonyx } = await import('../main.js');
|
|
36
|
+
new Stonyx(config, cwd);
|
|
37
|
+
await Stonyx.ready;
|
|
38
|
+
const { modules } = Stonyx.instance;
|
|
39
|
+
await runStartupHooks(modules);
|
|
40
|
+
const shutdown = createShutdownHandler(modules);
|
|
41
|
+
process.on('SIGTERM', shutdown);
|
|
42
|
+
process.on('SIGINT', shutdown);
|
|
43
|
+
const entryModule = await import(pathToFileURL(entryPoint).href);
|
|
44
|
+
let appInstance;
|
|
45
|
+
if (entryModule.default) {
|
|
46
|
+
appInstance = new entryModule.default();
|
|
47
|
+
}
|
|
48
|
+
maybeRegisterAppShutdown(modules, appInstance, shutdown);
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|