tshex-cli 1.0.13 → 1.0.15

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 (38) hide show
  1. package/LICENSE +21 -21
  2. package/build/main.js +21 -46
  3. package/package.json +50 -52
  4. package/readme.md +205 -76
  5. package/source/main.ts +84 -145
  6. package/templates/ctx/index.ts +9 -9
  7. package/templates/lib/index.d.ts +1 -1
  8. package/templates/lib/main.ts +5 -5
  9. package/templates/lib/shared/application/databases.ts +81 -0
  10. package/templates/lib/shared/application/events.ts +121 -0
  11. package/templates/lib/shared/application/{Response.ts → http.ts} +48 -52
  12. package/templates/{cls.example → lib/shared/application/loggers.ts} +50 -50
  13. package/templates/lib/shared/application/{Service.ts → services.ts} +43 -47
  14. package/templates/lib/shared/domain/{Aggregate.ts → aggregates.ts} +44 -48
  15. package/templates/lib/shared/domain/{Entity.ts → entities.ts} +46 -50
  16. package/templates/lib/shared/{application/DataTransferObject.ts → domain/errors.ts} +46 -46
  17. package/templates/lib/shared/domain/value-objects.ts +194 -0
  18. package/templates/ctx/ports/.gitkeep +0 -0
  19. package/templates/lib/shared/adapters/.gitkeep +0 -0
  20. package/templates/lib/shared/application/Logger.ts +0 -54
  21. package/templates/lib/shared/application/data/DataManager.ts +0 -50
  22. package/templates/lib/shared/application/data/Repository.ts +0 -55
  23. package/templates/lib/shared/application/errors/.gitkeep +0 -0
  24. package/templates/lib/shared/application/events/Event.ts +0 -56
  25. package/templates/lib/shared/application/events/EventDispatcher.ts +0 -55
  26. package/templates/lib/shared/application/events/EventHandler.ts +0 -50
  27. package/templates/lib/shared/constants/.gitkeep +0 -0
  28. package/templates/lib/shared/domain/errors/ValueError.ts +0 -69
  29. package/templates/lib/shared/domain/utils/.gitkeep +0 -0
  30. package/templates/lib/shared/domain/value-objects/BooleanValueObject.ts +0 -87
  31. package/templates/lib/shared/domain/value-objects/DateValueObject.ts +0 -100
  32. package/templates/lib/shared/domain/value-objects/EmailValueObject.ts +0 -89
  33. package/templates/lib/shared/domain/value-objects/NullableBooleanValueObject.ts +0 -87
  34. package/templates/lib/shared/domain/value-objects/NumericValueObject.ts +0 -136
  35. package/templates/lib/shared/domain/value-objects/StringValueObject.ts +0 -87
  36. package/templates/lib/shared/domain/value-objects/SymbolValueObject.ts +0 -82
  37. package/templates/lib/shared/domain/value-objects/ValueObject.ts +0 -57
  38. package/templates/rfc.example +0 -42
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 virtualitems
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 virtualitems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/build/main.js CHANGED
@@ -1,22 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  import { program } from 'commander';
3
- import fs from 'fs';
4
- import path from 'path';
5
- var packageJson = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, '..', 'package.json'), 'utf-8'));
6
- program
7
- .name('tshex')
8
- .version(packageJson.version)
9
- .option('--lib <name>', 'creates a new library with it\'s shared directory')
10
- .option('--ctx <name>', 'creates a new context')
11
- .option('--cls <name>', 'creates a new class')
12
- .option('--rfc <name>', 'creates a new react functional component')
13
- .option('--dir <path>', 'sets the directory to create the new item')
14
- .parse(process.argv);
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ function readPackageJson() {
6
+ return JSON.parse(fs.readFileSync(path.join(import.meta.dirname, '..', 'package.json'), 'utf-8'));
7
+ }
15
8
  function executeCreateLib(templatesDir, libraryDir) {
16
9
  try {
17
10
  fs.cpSync(path.join(templatesDir, 'lib'), libraryDir, {
18
11
  recursive: true,
19
- filter: function (src) { return (!src.endsWith('.gitkeep')); }
12
+ filter: (src) => !src.endsWith('.gitkeep')
20
13
  });
21
14
  console.log('Library created successfully');
22
15
  }
@@ -28,7 +21,7 @@ function executeCreateContext(templatesDir, contextDir) {
28
21
  try {
29
22
  fs.cpSync(path.join(templatesDir, 'ctx'), contextDir, {
30
23
  recursive: true,
31
- filter: function (src) { return (!src.endsWith('.gitkeep')); }
24
+ filter: (src) => !src.endsWith('.gitkeep')
32
25
  });
33
26
  console.log('Context created successfully');
34
27
  }
@@ -36,29 +29,10 @@ function executeCreateContext(templatesDir, contextDir) {
36
29
  console.error(err);
37
30
  }
38
31
  }
39
- function executeCreateClass(templatesDir, classDir) {
40
- try {
41
- fs.cpSync(path.join(templatesDir, 'cls.example'), classDir, {});
42
- console.log('Class created successfully');
43
- }
44
- catch (err) {
45
- console.error(err);
46
- }
47
- }
48
- function executeCreateReactFunctionalComponent(templatesDir, classDir) {
49
- try {
50
- fs.cpSync(path.join(templatesDir, 'rfc.example'), classDir, {});
51
- console.log('React component created successfully');
52
- }
53
- catch (err) {
54
- console.error(err);
55
- }
56
- }
57
- (function () {
58
- var _a;
59
- var templatesDir = path.join(import.meta.dirname, '..', 'templates');
60
- var options = program.opts();
61
- var targetDir = path.resolve((_a = options.dir) !== null && _a !== void 0 ? _a : process.cwd());
32
+ function main(program) {
33
+ const templatesDir = path.join(import.meta.dirname, '..', 'templates');
34
+ const options = program.opts();
35
+ let targetDir = path.resolve(options.dir ?? process.cwd());
62
36
  if (Object.keys(options).length === 0) {
63
37
  program.help();
64
38
  return;
@@ -74,12 +48,13 @@ function executeCreateReactFunctionalComponent(templatesDir, classDir) {
74
48
  targetDir = path.join(targetDir, options.ctx);
75
49
  executeCreateContext(templatesDir, targetDir);
76
50
  }
77
- if (options.cls !== undefined) {
78
- var target = path.join(targetDir, options.cls + '.ts');
79
- executeCreateClass(templatesDir, target);
80
- }
81
- if (options.rfc !== undefined) {
82
- var target = path.join(targetDir, options.rfc + '.tsx');
83
- executeCreateReactFunctionalComponent(templatesDir, target);
84
- }
85
- })();
51
+ }
52
+ const packageJson = readPackageJson();
53
+ program
54
+ .name('tshex')
55
+ .version(packageJson.version)
56
+ .option('--lib <name>', "creates a new library with it's shared directory")
57
+ .option('--ctx <name>', 'creates a new context')
58
+ .option('--dir <path>', 'sets the directory to create the new item')
59
+ .parse(process.argv);
60
+ main(program);
package/package.json CHANGED
@@ -1,52 +1,50 @@
1
- {
2
- "name": "tshex-cli",
3
- "version": "1.0.13",
4
- "author": "https://github.com/virtualitems/",
5
- "description": "Typescript Hexagonal Architecture CLI",
6
- "type": "module",
7
- "main": "./build/main.js",
8
- "keywords": [
9
- "architecture",
10
- "clean-architecture",
11
- "clean",
12
- "cli",
13
- "ddd",
14
- "design-patterns",
15
- "design",
16
- "domain-driven-design",
17
- "hexagonal",
18
- "pattern",
19
- "software-architecture",
20
- "tshex",
21
- "tshex-cli",
22
- "typescript",
23
- "virtualitems"
24
- ],
25
- "homepage": "https://github.com/virtualitems/tshex-cli",
26
- "bugs": {
27
- "url": "https://github.com/virtualitems/tshex-cli/issues"
28
- },
29
- "files": [
30
- "./build",
31
- "./source",
32
- "./templates"
33
- ],
34
- "types": "./source/index.d.ts",
35
- "scripts": {
36
- "examples": "tsc -p examples",
37
- "build": "tsc && tsc-alias",
38
- "dev": "tsc -w"
39
- },
40
- "devDependencies": {
41
- "@types/commander": "^2.12.0",
42
- "@types/node": "^22.5.4",
43
- "tsc-alias": "^1.8.10",
44
- "typescript": "^5.4.5"
45
- },
46
- "dependencies": {
47
- "commander": "^12.1.0"
48
- },
49
- "bin": {
50
- "tshex": "./build/main.js"
51
- }
52
- }
1
+ {
2
+ "name": "tshex-cli",
3
+ "version": "1.0.15",
4
+ "author": "https://github.com/virtualitems/",
5
+ "description": "Typescript Hexagonal Architecture CLI",
6
+ "type": "module",
7
+ "main": "./build/main.js",
8
+ "keywords": [
9
+ "architecture",
10
+ "clean-architecture",
11
+ "clean",
12
+ "cli",
13
+ "ddd",
14
+ "design-patterns",
15
+ "design",
16
+ "domain-driven-design",
17
+ "hexagonal",
18
+ "pattern",
19
+ "software-architecture",
20
+ "tshex",
21
+ "tshex-cli",
22
+ "typescript",
23
+ "virtualitems"
24
+ ],
25
+ "homepage": "https://github.com/virtualitems/tshex-cli",
26
+ "bugs": {
27
+ "url": "https://github.com/virtualitems/tshex-cli/issues"
28
+ },
29
+ "files": [
30
+ "./build",
31
+ "./source",
32
+ "./templates"
33
+ ],
34
+ "types": "./source/index.d.ts",
35
+ "devDependencies": {
36
+ "@types/node": "^22.5.4",
37
+ "typescript": "^5.4.5"
38
+ },
39
+ "dependencies": {
40
+ "commander": "^12.1.0"
41
+ },
42
+ "bin": {
43
+ "tshex": "./build/main.js"
44
+ },
45
+ "scripts": {
46
+ "examples": "tsc -p examples",
47
+ "build": "tsc",
48
+ "dev": "tsc -w"
49
+ }
50
+ }
package/readme.md CHANGED
@@ -1,76 +1,205 @@
1
- # Hexagonal Architecture CLI
2
-
3
- This CLI will help you to generate a hexagonal architecture structure.
4
-
5
- # Why?
6
-
7
- Hexagonal architecture is a software design pattern that separates the internal domain of the application from the external dependencies.. This separation is achieved by dividing the application into layers. Each layer has a specific responsibility and interacts with the other layers in a specific way.
8
-
9
- Because what we are trying to achieve is a separation between the domain code and the installed dependencies, a Hexagonal Architecture Framework is a step in the opposite direction, as it couples the domain code with the framework. This is why we need a tool to help us to create the structure with our own codebase.
10
-
11
- # Usage example
12
-
13
- https://github.com/virtualitems/tshex-cli
14
-
15
- # Note!!!
16
-
17
- This tool and its templates are in constant development, so be aware that some changes may occur and be careful when updating the tool.
18
-
19
- If you have any suggestions or improvements, please let me know.
20
-
21
- https://github.com/virtualitems/tshex-cli/issues
22
-
23
- # Installation
24
-
25
- ```bash
26
- npm install -g tshex-cli
27
- ```
28
-
29
- # Usage
30
-
31
- > Remember to replace placeholders `<...>` with the actual data.
32
- >
33
-
34
- ## Getting started
35
-
36
- ```bash
37
- tshex --lib <library-name> --ctx <context-name>
38
- ```
39
-
40
- ## Help
41
-
42
- ```bash
43
- tshex -h
44
-
45
- tshex --help
46
- ```
47
-
48
- ## Create a new library
49
-
50
- ```bash
51
- tshex --lib <name>
52
- ```
53
-
54
- ## Create a new context
55
-
56
- ```bash
57
- tshex --ctx <name>
58
- ```
59
-
60
- ## Create a new class
61
-
62
- ```bash
63
- tshex --cls <name>
64
- ```
65
-
66
- ## Create a new react functional component
67
-
68
- ```bash
69
- tshex --rfc <name>
70
- ```
71
-
72
- ## Set the directory to create the new items
73
-
74
- ```bash
75
- tshex --lib <lib-name> --ctx <ctx-name> --dir <path>
76
- ```
1
+ # Hexagonal Architecture CLI
2
+
3
+ This CLI will help you to generate a hexagonal architecture structure.
4
+
5
+ # Why?
6
+
7
+ Hexagonal architecture is a software design pattern that separates the internal domain of the application from the external dependencies. This separation is achieved by dividing the application into layers. Each layer has a specific responsibility and interacts with the other layers in a specific way.
8
+
9
+ The goal is to separate the domain code from installed dependencies. A Hexagonal Architecture _framework_ would work against that goal, since it couples the domain code to the framework itself. That is why this CLI exists: it scaffolds the structure for you, using your own codebase instead of a framework.
10
+
11
+ # Note
12
+
13
+ This tool and its templates are in constant development, so be aware that some changes may occur and be careful when updating the tool.
14
+
15
+ If you have any suggestions or improvements, please let me know.
16
+
17
+ https://github.com/virtualitems/tshex-cli/issues
18
+
19
+ # Getting started
20
+
21
+ `tshex` scaffolds folders and files for a hexagonal architecture project. It does not write business logic for you; it only generates the structure, so you can fill it in with your own code.
22
+
23
+ There are two things you can generate:
24
+
25
+ - A **library** (`--lib`): the shared foundation of a project. It contains the domain and application contracts (entities, value objects, services, etc.) that the rest of the code depends on.
26
+ - A **context** (`--ctx`): a specific business area (e.g. `users`, `billing`, `orders`). It contains the `domain/`, `application/`, and `adapters/` folders where you implement that area's logic.
27
+
28
+ A typical project has one library and one or more contexts inside it.
29
+
30
+ ## Install
31
+
32
+ Install the CLI globally:
33
+
34
+ ```bash
35
+ npm install -g tshex-cli
36
+ ```
37
+
38
+ Check the installation:
39
+
40
+ ```bash
41
+ tshex --version
42
+ ```
43
+
44
+ Show the command options:
45
+
46
+ ```bash
47
+ tshex --help
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ ```bash
53
+ tshex [--lib <name>] [--ctx <name>] [--dir <path>]
54
+ ```
55
+
56
+ | Option | Purpose |
57
+ | -------------- | ------------------------------------------------------------------- |
58
+ | `--lib <name>` | Generate a library called `<name>`, based on `templates/lib`. |
59
+ | `--ctx <name>` | Generate a context called `<name>`, based on `templates/ctx`. |
60
+ | `--dir <path>` | Parent directory where the library/context will be created. Defaults to the current directory. |
61
+ | `--help` | Show the command options. |
62
+ | `--version` | Show the installed version. |
63
+
64
+ `--lib` and `--ctx` can be used together in a single command (see [Create a library and a context together](#create-a-library-and-a-context-together)). Running `tshex` without any options just prints the help text — it does not generate anything.
65
+
66
+ ## Create a library
67
+
68
+ Use this when you are starting a new project and need the shared contracts that the rest of your code will build on:
69
+
70
+ ```bash
71
+ tshex --lib core
72
+ ```
73
+
74
+ This generates a `core/` folder in the current directory:
75
+
76
+ ```text
77
+ core/
78
+ ├── index.d.ts
79
+ ├── main.ts
80
+ └── shared/
81
+ ├── application/
82
+ │ ├── databases.ts
83
+ │ ├── events.ts
84
+ │ ├── http.ts
85
+ │ ├── loggers.ts
86
+ │ └── services.ts
87
+ └── domain/
88
+ ├── aggregates.ts
89
+ ├── entities.ts
90
+ ├── errors.ts
91
+ └── value-objects.ts
92
+ ```
93
+
94
+ What each file is for:
95
+
96
+ | File | Purpose |
97
+ | ------------------ | --------------------------------------------- |
98
+ | `index.d.ts` | Declare library types. |
99
+ | `main.ts` | Export the library API. |
100
+ | `entities.ts` | Define entity contracts. |
101
+ | `value-objects.ts` | Define value objects and validation. |
102
+ | `aggregates.ts` | Define aggregate contracts. |
103
+ | `errors.ts` | Define domain errors. |
104
+ | `services.ts` | Define application service contracts. |
105
+ | `databases.ts` | Define data manager and repository contracts. |
106
+ | `events.ts` | Define event contracts. |
107
+ | `http.ts` | Define HTTP response contracts. |
108
+ | `loggers.ts` | Define logger contracts. |
109
+
110
+ `main.ts` is generated with a placeholder `throw new Error('Not implemented yet')`, so the library fails loudly if you try to use it before filling it in. To start using it, open `main.ts`, remove that line, and export your own code, built on top of the shared contracts. For example:
111
+
112
+ ```ts
113
+ export { Entity } from './shared/domain/entities.js'
114
+ export { ValueObject, Email } from './shared/domain/value-objects.js'
115
+
116
+ class User extends Entity {
117
+ constructor(public readonly email: Email) {
118
+ super()
119
+ }
120
+ }
121
+ ```
122
+
123
+ ## Create a context
124
+
125
+ Use this to scaffold a specific business area, such as `users`, inside an existing library (or on its own):
126
+
127
+ ```bash
128
+ tshex --ctx users
129
+ ```
130
+
131
+ This generates a `users/` folder in the current directory:
132
+
133
+ ```text
134
+ users/
135
+ ├── adapters/
136
+ ├── application/
137
+ ├── domain/
138
+ └── index.ts
139
+ ```
140
+
141
+ What each part is for:
142
+
143
+ | Path | Purpose |
144
+ | -------------- | ---------------------------------------------------------------- |
145
+ | `domain/` | Entities, value objects, aggregates, and domain errors. |
146
+ | `application/` | Use cases, services, DTOs, and contracts. |
147
+ | `adapters/` | Database, HTTP, event, file, and service implementations. |
148
+ | `index.ts` | Export or compose the context's public API. |
149
+
150
+ ## Choose the destination folder
151
+
152
+ By default, `tshex` creates files in your current directory. Use `--dir` to choose a different parent directory instead.
153
+
154
+ Create a library inside `./src`:
155
+
156
+ ```bash
157
+ tshex --lib core --dir ./src
158
+ ```
159
+
160
+ This generates the library at:
161
+
162
+ ```text
163
+ ./src/core
164
+ ```
165
+
166
+ Create a context inside that library:
167
+
168
+ ```bash
169
+ tshex --ctx users --dir ./src/core
170
+ ```
171
+
172
+ This generates the context at:
173
+
174
+ ```text
175
+ ./src/core/users
176
+ ```
177
+
178
+ You can also use an absolute path:
179
+
180
+ ```bash
181
+ tshex --ctx users --dir /workspace/project/src
182
+ ```
183
+
184
+ ## Create a library and a context together
185
+
186
+ You can generate both in a single command. `tshex` creates the library first, then creates the context inside it:
187
+
188
+ ```bash
189
+ tshex --dir ./src --lib core --ctx users
190
+ ```
191
+
192
+ Result:
193
+
194
+ ```text
195
+ src/
196
+ └── core/
197
+ ├── index.d.ts
198
+ ├── main.ts
199
+ ├── shared/
200
+ └── users/
201
+ ├── adapters/
202
+ ├── application/
203
+ ├── domain/
204
+ └── index.ts
205
+ ```