stonyx 0.2.3-beta.9 → 0.2.3-beta.90
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 +16 -5
- 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 +26 -0
- package/dist/cli/new.js +293 -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 +98 -0
- package/dist/modules.d.ts +5 -0
- package/dist/modules.js +93 -0
- package/dist/util/import-config.d.ts +1 -0
- package/dist/util/import-config.js +10 -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,23 +1,30 @@
|
|
|
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
25
|
stonyx serve # Bootstrap + run app.js
|
|
20
26
|
stonyx test # Bootstrap + run tests
|
|
27
|
+
stonyx help # Show all available commands
|
|
21
28
|
```
|
|
22
29
|
|
|
23
30
|
Stonyx reads `config/environment.js`, initializes all `@stonyx/*` modules from your `devDependencies`, and runs your application.
|
|
@@ -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.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,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,26 @@
|
|
|
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 generateTestEnvironmentTs(): string;
|
|
12
|
+
export declare function generateSetupTs(): string;
|
|
13
|
+
export declare function generateZzExitTestTs(): string;
|
|
14
|
+
export declare function generateEnvironmentExampleTs(): string;
|
|
15
|
+
export declare function generateGitignore(): string;
|
|
16
|
+
export declare function generateTsConfig(): string;
|
|
17
|
+
/**
|
|
18
|
+
* Write every scaffolded file to disk given a resolved project directory,
|
|
19
|
+
* app name, and pre-selected module options. Factored out of `newCommand`
|
|
20
|
+
* so tests can exercise the file-writing path without interactive prompts.
|
|
21
|
+
*/
|
|
22
|
+
export declare function scaffoldProject(projectDir: string, appName: string, selectedModules: ModuleOption[]): Promise<void>;
|
|
23
|
+
export default function newCommand({ args }: {
|
|
24
|
+
args: string[];
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
export {};
|
package/dist/cli/new.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
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
|
+
qunit: '^2.24.1',
|
|
54
|
+
stonyx: 'latest',
|
|
55
|
+
tsx: '^4.21.0',
|
|
56
|
+
typescript: '^5.8.3'
|
|
57
|
+
};
|
|
58
|
+
for (const mod of selectedModules) {
|
|
59
|
+
devDependencies[mod.package] = 'latest';
|
|
60
|
+
}
|
|
61
|
+
// Sort dependencies alphabetically
|
|
62
|
+
const sorted = Object.fromEntries(Object.entries(devDependencies).sort(([a], [b]) => a.localeCompare(b)));
|
|
63
|
+
return JSON.stringify({
|
|
64
|
+
name,
|
|
65
|
+
version: '0.1.0',
|
|
66
|
+
type: 'module',
|
|
67
|
+
private: true,
|
|
68
|
+
scripts: {
|
|
69
|
+
build: 'tsc',
|
|
70
|
+
serve: 'stonyx serve',
|
|
71
|
+
start: 'stonyx serve',
|
|
72
|
+
test: "NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'"
|
|
73
|
+
},
|
|
74
|
+
devDependencies: sorted
|
|
75
|
+
}, null, 2) + '\n';
|
|
76
|
+
}
|
|
77
|
+
export function generateAppTs() {
|
|
78
|
+
return `import log from 'stonyx/log';
|
|
79
|
+
|
|
80
|
+
export default class App {
|
|
81
|
+
static instance: App;
|
|
82
|
+
ready: Promise<void>;
|
|
83
|
+
|
|
84
|
+
constructor() {
|
|
85
|
+
if (App.instance) return App.instance;
|
|
86
|
+
App.instance = this;
|
|
87
|
+
|
|
88
|
+
this.ready = this.init();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async init(): Promise<void> {
|
|
92
|
+
log.info('Initializing Application');
|
|
93
|
+
|
|
94
|
+
// Application setup here
|
|
95
|
+
|
|
96
|
+
log.info('Application has been initialized');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
`;
|
|
100
|
+
}
|
|
101
|
+
export function generateEnvironmentTs() {
|
|
102
|
+
return `import type { StoynxConfig } from 'stonyx';
|
|
103
|
+
|
|
104
|
+
const config: StoynxConfig = {
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export default config;
|
|
108
|
+
`;
|
|
109
|
+
}
|
|
110
|
+
export function generateTestEnvironmentTs() {
|
|
111
|
+
return `import type { StoynxConfig } from 'stonyx';
|
|
112
|
+
|
|
113
|
+
// Test-specific config overrides
|
|
114
|
+
const config: Partial<StoynxConfig> = {
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export default config;
|
|
118
|
+
`;
|
|
119
|
+
}
|
|
120
|
+
export function generateSetupTs() {
|
|
121
|
+
return `// Test setup: bootstrap Stonyx with the consumer config before qunit runs.
|
|
122
|
+
// Sprint 44 pattern — loaded via \`node --import ./test/setup.ts\`.
|
|
123
|
+
import { pathToFileURL } from 'url';
|
|
124
|
+
|
|
125
|
+
const cwd = process.cwd();
|
|
126
|
+
|
|
127
|
+
const { default: Stonyx } = await import('stonyx');
|
|
128
|
+
const { default: config } = await import(pathToFileURL(\`\${cwd}/config/environment.ts\`).href);
|
|
129
|
+
|
|
130
|
+
new Stonyx(config, cwd);
|
|
131
|
+
|
|
132
|
+
await Stonyx.ready;
|
|
133
|
+
`;
|
|
134
|
+
}
|
|
135
|
+
export function generateZzExitTestTs() {
|
|
136
|
+
return `// Force-exit hook for qunit runs.
|
|
137
|
+
//
|
|
138
|
+
// Stonyx modules may keep listeners/servers open that would otherwise
|
|
139
|
+
// block the process from exiting after tests complete, causing CI to
|
|
140
|
+
// hit timeouts even though all tests passed.
|
|
141
|
+
//
|
|
142
|
+
// Named \`zz-\` so alphabetical test-file order places it last, after
|
|
143
|
+
// all real test files have registered with QUnit.
|
|
144
|
+
import QUnit from 'qunit';
|
|
145
|
+
|
|
146
|
+
QUnit.on('runEnd', () => {
|
|
147
|
+
setImmediate(() => process.exit(process.exitCode ?? 0));
|
|
148
|
+
});
|
|
149
|
+
`;
|
|
150
|
+
}
|
|
151
|
+
export function generateEnvironmentExampleTs() {
|
|
152
|
+
return `// Copy this file to environment.ts and fill in your values
|
|
153
|
+
// All values should use environment variables with ?? fallback defaults
|
|
154
|
+
|
|
155
|
+
const {
|
|
156
|
+
NODE_ENV,
|
|
157
|
+
} = process.env;
|
|
158
|
+
|
|
159
|
+
const environment: string = NODE_ENV ?? 'development';
|
|
160
|
+
|
|
161
|
+
export default {
|
|
162
|
+
}
|
|
163
|
+
`;
|
|
164
|
+
}
|
|
165
|
+
export function generateGitignore() {
|
|
166
|
+
return `node_modules/
|
|
167
|
+
.env
|
|
168
|
+
db.json
|
|
169
|
+
*.log
|
|
170
|
+
|
|
171
|
+
# Compiled TypeScript output (tsc compiles .ts to .js in-place)
|
|
172
|
+
*.js
|
|
173
|
+
*.d.ts
|
|
174
|
+
*.js.map
|
|
175
|
+
`;
|
|
176
|
+
}
|
|
177
|
+
export function generateTsConfig() {
|
|
178
|
+
return JSON.stringify({
|
|
179
|
+
compilerOptions: {
|
|
180
|
+
strict: true,
|
|
181
|
+
target: 'ES2022',
|
|
182
|
+
module: 'NodeNext',
|
|
183
|
+
moduleResolution: 'NodeNext',
|
|
184
|
+
outDir: '.',
|
|
185
|
+
rootDir: '.',
|
|
186
|
+
esModuleInterop: true,
|
|
187
|
+
skipLibCheck: true,
|
|
188
|
+
forceConsistentCasingInFileNames: true
|
|
189
|
+
},
|
|
190
|
+
include: ['**/*.ts'],
|
|
191
|
+
exclude: ['node_modules', 'test']
|
|
192
|
+
}, null, 2) + '\n';
|
|
193
|
+
}
|
|
194
|
+
function runPnpmInstall(projectDir) {
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
const child = spawn('pnpm', ['install'], {
|
|
197
|
+
cwd: projectDir,
|
|
198
|
+
stdio: 'inherit'
|
|
199
|
+
});
|
|
200
|
+
child.on('close', code => {
|
|
201
|
+
if (code === 0)
|
|
202
|
+
resolve();
|
|
203
|
+
else
|
|
204
|
+
reject(new Error(`pnpm install exited with code ${code}`));
|
|
205
|
+
});
|
|
206
|
+
child.on('error', reject);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Write every scaffolded file to disk given a resolved project directory,
|
|
211
|
+
* app name, and pre-selected module options. Factored out of `newCommand`
|
|
212
|
+
* so tests can exercise the file-writing path without interactive prompts.
|
|
213
|
+
*/
|
|
214
|
+
export async function scaffoldProject(projectDir, appName, selectedModules) {
|
|
215
|
+
// Create project directory
|
|
216
|
+
await createDirectory(projectDir);
|
|
217
|
+
// Copy .nvmrc from monorepo root
|
|
218
|
+
const monorepoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
|
|
219
|
+
const nvmrcSource = path.join(monorepoRoot, '.nvmrc');
|
|
220
|
+
if (await fileExists(nvmrcSource)) {
|
|
221
|
+
await copyFile(nvmrcSource, path.join(projectDir, '.nvmrc'));
|
|
222
|
+
}
|
|
223
|
+
// Generate core files
|
|
224
|
+
await createFile(path.join(projectDir, 'package.json'), generatePackageJson(appName, selectedModules));
|
|
225
|
+
await createFile(path.join(projectDir, 'app.ts'), generateAppTs());
|
|
226
|
+
await createFile(path.join(projectDir, 'tsconfig.json'), generateTsConfig());
|
|
227
|
+
await createFile(path.join(projectDir, '.gitignore'), generateGitignore());
|
|
228
|
+
// Create config directory and files
|
|
229
|
+
await createFile(path.join(projectDir, 'config', 'environment.ts'), generateEnvironmentTs());
|
|
230
|
+
await createFile(path.join(projectDir, 'config', 'environment.example.ts'), generateEnvironmentExampleTs());
|
|
231
|
+
// Create module-specific directories and files
|
|
232
|
+
for (const mod of selectedModules) {
|
|
233
|
+
if (mod.dirs) {
|
|
234
|
+
for (const dir of mod.dirs) {
|
|
235
|
+
await createDirectory(path.join(projectDir, dir));
|
|
236
|
+
// Create .gitkeep so empty dirs are tracked
|
|
237
|
+
await createFile(path.join(projectDir, dir, '.gitkeep'), '');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (mod.files) {
|
|
241
|
+
for (const [filePath, generator] of Object.entries(mod.files)) {
|
|
242
|
+
await createFile(path.join(projectDir, filePath), generator());
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Create test structure
|
|
247
|
+
await createDirectory(path.join(projectDir, 'test', 'unit'));
|
|
248
|
+
await createFile(path.join(projectDir, 'test', 'unit', '.gitkeep'), '');
|
|
249
|
+
await createDirectory(path.join(projectDir, 'test', 'integration'));
|
|
250
|
+
await createFile(path.join(projectDir, 'test', 'integration', '.gitkeep'), '');
|
|
251
|
+
await createDirectory(path.join(projectDir, 'test', 'acceptance'));
|
|
252
|
+
await createFile(path.join(projectDir, 'test', 'acceptance', '.gitkeep'), '');
|
|
253
|
+
// Create test config (TS)
|
|
254
|
+
await createFile(path.join(projectDir, 'test', 'config', 'environment.ts'), generateTestEnvironmentTs());
|
|
255
|
+
// Create Sprint 44 test harness files (setup bootstraps Stonyx, zz-exit-test drains event loop)
|
|
256
|
+
await createFile(path.join(projectDir, 'test', 'setup.ts'), generateSetupTs());
|
|
257
|
+
await createFile(path.join(projectDir, 'test', 'zz-exit-test.ts'), generateZzExitTestTs());
|
|
258
|
+
}
|
|
259
|
+
export default async function newCommand({ args }) {
|
|
260
|
+
let appName = args[0];
|
|
261
|
+
if (!appName) {
|
|
262
|
+
appName = await prompt('Project name:');
|
|
263
|
+
}
|
|
264
|
+
if (!appName) {
|
|
265
|
+
console.error('Project name is required.');
|
|
266
|
+
process.exit(1);
|
|
267
|
+
}
|
|
268
|
+
const projectDir = path.resolve(process.cwd(), appName);
|
|
269
|
+
if (await fileExists(projectDir)) {
|
|
270
|
+
console.error(`Directory "${appName}" already exists.`);
|
|
271
|
+
process.exit(1);
|
|
272
|
+
}
|
|
273
|
+
console.log(`\nScaffolding new Stonyx project: ${appName}\n`);
|
|
274
|
+
// Prompt for module selection
|
|
275
|
+
const selectedModules = [];
|
|
276
|
+
for (const mod of MODULE_OPTIONS) {
|
|
277
|
+
if (await confirm(mod.question)) {
|
|
278
|
+
selectedModules.push(mod);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
console.log('\nCreating project structure...\n');
|
|
282
|
+
await scaffoldProject(projectDir, appName, selectedModules);
|
|
283
|
+
console.log('Installing dependencies...\n');
|
|
284
|
+
try {
|
|
285
|
+
await runPnpmInstall(projectDir);
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
console.error('Failed to install dependencies. Run `pnpm install` manually in the project directory.');
|
|
289
|
+
}
|
|
290
|
+
console.log(`\n✓ Project "${appName}" created successfully!`);
|
|
291
|
+
console.log(`\n cd ${appName}`);
|
|
292
|
+
console.log(` stonyx serve\n`);
|
|
293
|
+
}
|
|
@@ -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 {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { importConfig } from '../util/import-config.js';
|
|
3
|
+
const cwd = process.cwd();
|
|
4
|
+
const { default: Stonyx } = await import('stonyx');
|
|
5
|
+
const config = await importConfig(`${cwd}/config/environment`);
|
|
6
|
+
new Stonyx(config, cwd);
|
|
7
|
+
// Drain the event loop once qunit finishes. Modules loaded by Stonyx
|
|
8
|
+
// (e.g. @stonyx/rest-server) may keep listeners/servers open that would
|
|
9
|
+
// otherwise block the process from exiting, causing CI to hit timeouts
|
|
10
|
+
// even though all tests passed.
|
|
11
|
+
//
|
|
12
|
+
// qunit's CLI bin does `delete require.cache[qunitPath]` and re-requires
|
|
13
|
+
// qunit before running, so any qunit instance we grab synchronously from
|
|
14
|
+
// this --import setup is a DIFFERENT object than the one qunit's CLI
|
|
15
|
+
// actually uses. We defer the hook registration with setImmediate so it
|
|
16
|
+
// runs AFTER the qunit bin has populated require.cache with its own
|
|
17
|
+
// instance; grabbing qunit via CJS require at that point returns the
|
|
18
|
+
// instance the tests are running on.
|
|
19
|
+
//
|
|
20
|
+
// See: https://github.com/abofs/stonyx/issues/55
|
|
21
|
+
const require = createRequire(import.meta.url);
|
|
22
|
+
setImmediate(() => {
|
|
23
|
+
const qunitPath = require.resolve('qunit', {
|
|
24
|
+
paths: [`${cwd}/node_modules`, cwd]
|
|
25
|
+
});
|
|
26
|
+
const QUnit = require(qunitPath);
|
|
27
|
+
QUnit.on('runEnd', () => {
|
|
28
|
+
setImmediate(() => process.exit(process.exitCode ?? 0));
|
|
29
|
+
});
|
|
30
|
+
});
|
package/dist/cli/test.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export const DEFAULT_TEST_GLOB = 'test/**/*-test.{js,ts}';
|
|
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
|
+
const testArgs = args.length > 0 ? args : [DEFAULT_TEST_GLOB];
|
|
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