Skip to content

Commit 6d3040e

Browse files
authored
feat: add browser-safe fallback entry with no-op modules for frontend compatibility (#41)
Co-authored-by: Mr Stone <pierre.evens16@gmail.com>
1 parent 9b5a76c commit 6d3040e

File tree

10 files changed

+119
-22
lines changed

10 files changed

+119
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ All notable changes to the "Stone.js Node Cli Adapter" extension will be documen
1313
* implement error handler and update lifecycle hooks ([4ca37b2](https://github.com/stone-foundation/stone-js-node-cli-adapter/commit/4ca37b2b0c5fee68c5c4db257f745b084b64de79))
1414
* major internal restructuring and cleanup ([#39](https://github.com/stone-foundation/stone-js-node-cli-adapter/issues/39)) ([1828efe](https://github.com/stone-foundation/stone-js-node-cli-adapter/commit/1828efeb9754a95231051030a3437cc1a5b17700))
1515

16-
## [0.0.21](https://github.com/stonemjs/node-cli-adapter/compare/v0.0.2...v0.0.21) (2024-12-07)
16+
## [0.0.21](https://github.com/stone-foundation/stone-js-node-cli-adapter/compare/v0.0.2...v0.0.21) (2024-12-07)
1717

1818

1919
### Miscellaneous Chores
2020

21-
* change the way commands are handled ([9cc05f0](https://github.com/stonemjs/node-cli-adapter/commit/9cc05f016f2877068f98d788dc5671f7a043d65c))
21+
* change the way commands are handled ([9cc05f0](https://github.com/stone-foundation/stone-js-node-cli-adapter/commit/9cc05f016f2877068f98d788dc5671f7a043d65c))
2222

2323
## 0.0.2 (2024-12-05)
2424

2525

2626
### Features
2727

28-
* implement node cli adapter ([30743f7](https://github.com/stonemjs/node-cli-adapter/commit/30743f7aaaae46db17826e810be4549d56406b6f))
28+
* implement node cli adapter ([30743f7](https://github.com/stone-foundation/stone-js-node-cli-adapter/commit/30743f7aaaae46db17826e810be4549d56406b6f))
29+

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"license": "MIT",
77
"repository": {
88
"type": "git",
9-
"url": "git+ssh://git@github.com/stone-js/node-cli-adapter.git"
9+
"url": "git+ssh://git@github.com/stone-foundation/stone-js-node-cli-adapter.git"
1010
},
1111
"homepage": "https://stonejs.dev",
1212
"bugs": {
13-
"url": "https://github.com/stone-js/node-cli-adapter/issues"
13+
"url": "https://github.com/stone-foundation/stone-js-node-cli-adapter/issues"
1414
},
1515
"keywords": [
1616
"cli",
@@ -31,12 +31,18 @@
3131
"types": "./dist/index.d.ts",
3232
"exports": {
3333
".": {
34-
"types": "./dist/index.d.ts",
35-
"default": "./dist/index.js"
34+
"browser": {
35+
"types": "./dist/index.d.ts",
36+
"default": "./dist/browser.js"
37+
},
38+
"default": {
39+
"types": "./dist/index.d.ts",
40+
"default": "./dist/index.js"
41+
}
3642
}
3743
},
3844
"engines": {
39-
"node": ">=18.0.0"
45+
"node": ">=18.17.0"
4046
},
4147
"scripts": {
4248
"lint": "ts-standard src",

rollup.config.mjs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import nodeExternals from 'rollup-plugin-node-externals'
99

1010
export default [
1111
{
12-
input: 'src/**/*.ts',
12+
input: [
13+
'src/**/*.ts',
14+
'!src/browser/**/*'
15+
],
1316
output: [
1417
{ format: 'es', file: 'dist/index.js' }
1518
],
@@ -29,7 +32,36 @@ export default [
2932
]
3033
},
3134
{
32-
input: 'dist/**/*.d.ts',
35+
input: [
36+
'src/constants.ts',
37+
'src/errors/**/*.ts',
38+
'src/browser/**/*.ts',
39+
'src/blueprint/**/*.ts',
40+
'src/decorators/Command.ts'
41+
],
42+
output: [
43+
{ format: 'es', file: 'dist/browser.js' }
44+
],
45+
plugins: [
46+
multi(),
47+
nodeExternals(), // Must always be before `nodeResolve()`.
48+
nodeResolve({
49+
extensions: ['.js', '.mjs', '.ts'],
50+
exportConditions: ['node', 'import', 'require', 'default']
51+
}),
52+
json(),
53+
typescript({
54+
noEmitOnError: true,
55+
tsconfig: './tsconfig.build.json',
56+
}),
57+
commonjs()
58+
]
59+
},
60+
{
61+
input: [
62+
'dist/**/*.d.ts',
63+
'!dist/browser/**/*.d.ts'
64+
],
3365
output: [{ format: 'es' , file: 'dist/index.d.ts' }],
3466
plugins: [
3567
multi(),

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sonar.projectName=Node CLI Adapter
1+
sonar.projectName=Stone.js - Node CLI Adapter
22
sonar.organization=stone-foundation
33
sonar.projectKey=stone-foundation_stone-js-node-cli-adapter
44
sonar.coverage.exclusions=**/*.spec.ts, **/*.test.ts
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { classDecoratorLegacyWrapper, ClassType } from '@stone-js/core'
2+
import { NodeConsoleAdapterAdapterConfig } from '../../options/NodeConsoleAdapterBlueprint'
3+
4+
/**
5+
* Configuration options for the `NodeConsole` decorator.
6+
* These options extend the default Node Cli adapter configuration.
7+
*/
8+
export interface NodeConsoleOptions extends Partial<NodeConsoleAdapterAdapterConfig> {}
9+
10+
/**
11+
* A Stone.js decorator that integrates the Node Cli Adapter with a class.
12+
*
13+
* This decorator modifies the class to seamlessly enable Node Cli as the
14+
* execution environment for a Stone.js application. By applying this decorator,
15+
* the class is automatically configured with the necessary blueprint for Node Cli.
16+
*
17+
* NB: This decorator is stubbed for browser environments compatibility and does not
18+
* perform any actual functionality in the browser. It is intended for use in Node.js environments
19+
* where the Node.js HTTP adapter is applicable.
20+
*
21+
* @template T - The type of the class being decorated. Defaults to `ClassType`.
22+
* @param options - Optional configuration to customize the Node Cli Adapter.
23+
*
24+
* @returns A class decorator that applies the Node Cli adapter configuration.
25+
*
26+
* @example
27+
* ```typescript
28+
* import { NodeConsole } from '@stone-js/node-cli-adapter';
29+
*
30+
* @NodeConsole({
31+
* alias: 'NodeConsole',
32+
* })
33+
* class App {
34+
* // Your application logic here
35+
* }
36+
* ```
37+
*/
38+
export const NodeConsole = <T extends ClassType = ClassType>(_options: NodeConsoleOptions = {}): ClassDecorator => {
39+
return classDecoratorLegacyWrapper<T>((_target: T, _context: ClassDecoratorContext<T>): undefined => {})
40+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NodeConsoleAdapterBlueprint } from '../../options/NodeConsoleAdapterBlueprint'
2+
3+
/**
4+
* Default blueprint configuration for the Node Cli Adapter.
5+
*
6+
* This blueprint defines the initial configuration for the Node Cli adapter
7+
* within the Stone.js framework. It includes:
8+
* - An alias for the Node Cli platform (`Node_CLI_PLATFORM`).
9+
* - A default resolver function (currently a placeholder).
10+
* - Middleware, hooks, and state flags (`current`, `default`, `preferred`).
11+
*/
12+
export const nodeConsoleAdapterBlueprint: NodeConsoleAdapterBlueprint = { stone: { adapters: [] } }

tests/blueprint/BlueprintUtils.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ const mockOptions = {
1111

1212
const createMockBlueprint = (isMatch: boolean, add = vi.fn()): IBlueprint => ({
1313
add,
14-
is: vi.fn((_key: string, _value: string) => isMatch),
14+
is: vi.fn((_key: string, _value: string) => isMatch)
1515
}) as unknown as IBlueprint
1616

1717
const next = vi.fn()
1818

1919
describe('defineCommand', () => {
2020
it('should define a functional command (no isFactory)', async () => {
2121
const handler = async (_event: IncomingEvent): Promise<void> => {}
22-
22+
2323
const blueprint = createMockBlueprint(true)
2424
next.mockResolvedValueOnce(blueprint)
2525

tests/decorators/decorators.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { COMMAND_KEY } from '../../src/decorators/constants'
2-
import { addBlueprint, setClassMetadata } from '@stone-js/core'
32
import { Command, CommandOptions } from '../../src/decorators/Command'
43
import { NodeConsoleOptions, NodeConsole } from '../../src/decorators/NodeConsole'
54
import { nodeConsoleAdapterBlueprint } from '../../src/options/NodeConsoleAdapterBlueprint'
5+
import { NodeConsole as BrowserNodeConsole } from '../../src/browser/decorators/NodeConsole'
6+
import { addBlueprint, classDecoratorLegacyWrapper, setClassMetadata } from '@stone-js/core'
7+
import { nodeConsoleAdapterBlueprint as baseNodeConsoleAdapterBlueprint } from '../../src/browser/options/NodeConsoleAdapterBlueprint'
68

79
/* eslint-disable @typescript-eslint/no-extraneous-class */
810

@@ -13,18 +15,21 @@ vi.mock('@stone-js/core', async (importOriginal) => {
1315
...actual,
1416
addBlueprint: vi.fn(() => {}),
1517
setClassMetadata: vi.fn(() => {}),
16-
classDecoratorLegacyWrapper: (fn: Function) => {
18+
classDecoratorLegacyWrapper: vi.fn((fn: Function) => {
1719
fn()
1820
return fn
19-
}
21+
})
2022
}
2123
})
2224

2325
describe('NodeConsole', () => {
2426
it('should call addBlueprint with correct parameters', () => {
2527
const options: NodeConsoleOptions = nodeConsoleAdapterBlueprint.stone.adapters?.[0] ?? {}
2628
NodeConsole(options)(class {})
29+
BrowserNodeConsole()(class {})
2730
expect(addBlueprint).toHaveBeenCalled()
31+
expect(classDecoratorLegacyWrapper).toHaveBeenCalledTimes(2)
32+
expect(addBlueprint).not.toHaveBeenCalledWith(expect.any(Function), expect.any(Object), baseNodeConsoleAdapterBlueprint)
2833
})
2934

3035
it('should call addBlueprint with default options if none are provided', () => {

typedoc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"entryPoints": ["src/**/*.ts"], // Entry point for your project, change accordingly
3+
"exclude": ["src/browser/**/*"], // Exclude browser-specific files if any
34
"out": "docs", // Output directory for the generated documentation
45
"name": "Node CLI Adapter", // Project name used in the docs
56
"includeVersion": false, // Optional: Include project version in the output

0 commit comments

Comments
 (0)