Skip to content

Commit

Permalink
[DSDK-185] [TS] [SDK] Add scaffolding tool to repo (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman authored Feb 1, 2024
2 parents 1d326d1 + 25b4239 commit b4dd599
Show file tree
Hide file tree
Showing 19 changed files with 562 additions and 1 deletion.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,49 @@ This project uses Github CI.
In order to avoid task repetition, we can add some scripts the corresponding package's script folder, on in a root script folder if it concerns multiple packages.
A script is a `.mjs` file interpreted by [zx](https://github.com/google/zx).

# Templates

To kickly scaffold part of our code, we use `hygen` to help us kickstart our development process faster.
Each project can have it's own `_templates` folder, so **generators** are scoped.
The `_templates` folder contains the basic generators to create new ones.

[Hygen documentation](https://www.hygen.io/docs/quick-start/)

## Process for adding a new generator

The easiest way would be to use `hygen` from the root folder as so:

**Options**:
- `new`: creates a generator that take no input during creation (but can still access metadata)
- `with-prompt`: creates a generator that can take some input during creation (with access to metadata)

```
pnpm hygen generator with-prompt|new name
```

This command will create a new generator folder in the root `_templates` with the given `name`.
It's there so that we can modify this new generator.
When done, move the new generator to it's correct project `_templates` folder (again, so we can keep scope).
Finally, we should add a script in the correct `package.json` as a shortcut to trigger the new generator.
eg:

```
pnpm core module:create
```

Under the hood, the script looks like this:

```
pnpm hygen <name> with-prompt
```

- `name` is the name given during the creation of the generator.
- `with-prompt` to call the prompted version of the generator (there can be multiple targets, like `new`)


## Available templates

| workspace | script | description |
| - | - | - |
| 📦 core | `module:create` | scaffolds a new _src/internal_ module |

5 changes: 5 additions & 0 deletions _templates/generator/help/index.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
message: |
hygen {bold generator new} --name [NAME] --action [ACTION]
hygen {bold generator with-prompt} --name [NAME] --action [ACTION]
---
18 changes: 18 additions & 0 deletions _templates/generator/new/hello.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
to: _templates/<%= name %>/<%= action || 'new' %>/hello.ejs.t
---
---
to: app/hello.js
---
const hello = ```
Hello!
This is your first hygen template.

Learn what it can do here:

https://github.com/jondot/hygen
```

console.log(hello)


18 changes: 18 additions & 0 deletions _templates/generator/with-prompt/hello.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
to: _templates/<%= name %>/<%= action || 'new' %>/hello.ejs.t
---
---
to: app/hello.js
---
const hello = ```
Hello!
This is your first prompt based hygen template.

Learn what it can do here:

https://github.com/jondot/hygen
```

console.log(hello)


14 changes: 14 additions & 0 deletions _templates/generator/with-prompt/prompt.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
to: _templates/<%= name %>/<%= action || 'new' %>/prompt.js
---

// see types of prompts:
// https://github.com/enquirer/enquirer/tree/master/examples
//
module.exports = [
{
type: 'input',
name: 'message',
message: "What's your message?"
}
]
4 changes: 4 additions & 0 deletions _templates/init/repo/new-repo.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
setup: <%= name %>
force: true # this is because mostly, people init into existing folders is safe
---
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"eslint": "^8.56.0",
"hygen": "^6.2.11",
"jest": "^29.7.0",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
to: src/internal/<%= moduleName %>/data/<%= h.capitalize(moduleName) %>DataSource.ts
---
export interface <%= h.capitalize(moduleName) %>DataSource {}


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
to: src/internal/<%= moduleName %>/service/Default<%= h.capitalize(moduleName) %>Service.ts
---
import { injectable } from "inversify";
import { <%= h.capitalize(moduleName) %>Service } from "./<%= h.capitalize(moduleName) %>Service";

@injectable()
export class Default<%= h.capitalize(moduleName) %>Service implements <%= h.capitalize(moduleName) %>Service {
constructor() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
to: src/internal/<%= moduleName %>/service/Default<%= h.capitalize(moduleName) %>Service.test.ts
---
import { <%= h.capitalize(moduleName) %>Service } from "./<%= h.capitalize(moduleName) %>Service";
import { Default<%= h.capitalize(moduleName) %>Service } from "./Default<%= h.capitalize(moduleName) %>Service";

let service: <%= h.capitalize(moduleName) %>Service;
describe("<%= h.capitalize(moduleName) %>Service", () => {
beforeEach(() => {
service = new Default<%= h.capitalize(moduleName) %>Service();
});

it("should be defined", () => {
expect(service).toBeDefined();
});
});
15 changes: 15 additions & 0 deletions packages/core/_templates/core-module/with-prompt/di-module.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
to: src/internal/<%= moduleName %>/di/<%= moduleName %>Module.ts
---
import { ContainerModule } from "inversify";
import { Default<%= h.capitalize(moduleName) %>Service } from "../service/Default<%= h.capitalize(moduleName) %>Service";
import { types } from "./<%= moduleName %>Types";

type FactoryProps = {};

const <%= moduleName %>ModuleFactory = ({}: Partial<FactoryProps> = {}) =>
new ContainerModule((bind, _unbind, _isBound, _rebind, _unbindAsync, _onActivation, _onDeactivation) => {
bind(types.<%= h.capitalize(moduleName) %>Service).to(Default<%= h.capitalize(moduleName) %>Service);
});

export default <%= moduleName %>ModuleFactory;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
to: src/internal/<%= moduleName %>/di/<%= moduleName %>Module.test.ts
---
import { Container } from "inversify";
import <%= moduleName %>ModuleFactory from "./<%= moduleName %>Module";
import { types } from "./<%= moduleName %>Types";

describe("<%= moduleName %>ModuleFactory", () => {
describe("Default", () => {
let container: Container;
let mod: ReturnType<typeof <%= moduleName %>ModuleFactory>;
beforeEach(() => {
mod = <%= moduleName %>ModuleFactory();
container = new Container();
container.load(mod);
});

it("should return the config module", () => {
expect(mod).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
to: src/internal/<%= moduleName %>/di/<%= moduleName %>Types.ts
---
export const types = {
<%= h.capitalize(moduleName) %>Service: Symbol.for("<%= h.capitalize(moduleName) %>Service"),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
to: src/internal/<%= moduleName %>/model/.gitkeep
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
to: src/internal/<%= moduleName %>/usecase/.gitkeep
---
10 changes: 10 additions & 0 deletions packages/core/_templates/core-module/with-prompt/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// see types of prompts:
// https://github.com/enquirer/enquirer/tree/master/examples
//
module.exports = [
{
type: "input",
name: "moduleName",
message: "What's the name of the module (camelCase)?",
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
to: src/internal/<%= moduleName %>/service/<%= h.capitalize(moduleName) %>Service.ts
---
export interface <%= h.capitalize(moduleName) %>Service {}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "jest src",
"test:watch": "pnpm test -- --watch",
"test:coverage": "pnpm test -- --coverage",
"module:init": "zx scripts/add-module.mjs"
"module:create": "pnpm hygen core-module with-prompt"
},
"dependencies": {
"inversify": "^6.0.2",
Expand Down
Loading

0 comments on commit b4dd599

Please sign in to comment.