@necord/schematics 1.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Alexey Filippov
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/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @necord/schematics
2
+
3
+ This is a collection of schematics for Necord projects.
4
+
5
+ ### Testing
6
+
7
+ To test locally, install `@angular-devkit/schematics-cli` globally and use the `schematics` command
8
+ line tool. That tool acts the same as the `generate` command of the Angular CLI, but also has a
9
+ debug mode.
10
+
11
+ ```bash
12
+ npm i -g @angular-devkit/schematics-cli
13
+ ```
14
+
15
+ Check the documentation with
16
+
17
+ ```bash
18
+ schematics --help
19
+ ```
20
+
21
+ Now you can execute generation commands.
22
+
23
+ ```bash
24
+ schematics @necord/schematics:command
25
+ ```
26
+
27
+ ### Unit Testing
28
+
29
+ `npm run test` will run the unit tests, using Jasmine as a runner and test framework.
30
+
31
+ That's it!
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@necord/schematics",
3
+ "description": "A collection of schematics for Necord projects with NestJS",
4
+ "version": "1.1.0",
5
+ "scripts": {
6
+ "build": "rimraf -rf dist && tsc -p tsconfig.json",
7
+ "prepublish:npm": "npm run build",
8
+ "publish:npm": "release-it",
9
+ "prepublish:dev": "npm run build",
10
+ "publish:dev": "npm publish --access public --tag dev",
11
+ "prepare": "husky install .github/husky",
12
+ "format": "prettier --write \"src/**/*.ts\"",
13
+ "lint": "eslint --ignore-path .gitignore {integration,src}/**/*.ts",
14
+ "test": "jest"
15
+ },
16
+ "files": [
17
+ "src"
18
+ ],
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "schematics": "./src/collection.json",
23
+ "author": "Alexey Filippov <socket.someone@gmail.com>",
24
+ "license": "MIT",
25
+ "keywords": [
26
+ "schematics",
27
+ "nest",
28
+ "nestjs-cli",
29
+ "nestjs",
30
+ "cli",
31
+ "inquirer",
32
+ "necord"
33
+ ],
34
+ "dependencies": {
35
+ "@angular-devkit/core": "14.0.5",
36
+ "@angular-devkit/schematics": "14.0.5",
37
+ "rxjs": "7.8.0",
38
+ "@nestjs/schematics": "9.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@commitlint/cli": "17.5.0",
42
+ "@commitlint/config-angular": "17.4.4",
43
+ "@types/jest": "29.5.0",
44
+ "@types/node": "18.15.6",
45
+ "@typescript-eslint/eslint-plugin": "5.56.0",
46
+ "@typescript-eslint/parser": "5.56.0",
47
+ "eslint": "8.38.0",
48
+ "eslint-config-prettier": "8.8.0",
49
+ "eslint-plugin-import": "2.27.5",
50
+ "husky": "8.0.3",
51
+ "jest": "29.5.0",
52
+ "prettier": "2.8.7",
53
+ "release-it": "15.10.1",
54
+ "rimraf": "4.4.1",
55
+ "ts-jest": "29.0.5",
56
+ "typescript": "5.0.2"
57
+ },
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "git+https://github.com/necordjs/schematics.git"
61
+ },
62
+ "homepage": "https://github.com/necordjs/schematics.git#readme"
63
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
3
+ "schematics": {
4
+ "context-menu": {
5
+ "description": "Generates a context menu component.",
6
+ "factory": "./context-menu/index#contextMenu",
7
+ "schema": "./context-menu/schema.json",
8
+ "aliases": [
9
+ "cm"
10
+ ]
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "properties": {
3
+ "name": {
4
+ "type": "string",
5
+ "minLength": 1,
6
+ "x-prompt": "What is the questions set name?"
7
+ },
8
+ "path": {
9
+ "type": "string",
10
+ "format": "path",
11
+ "description": "The path to create the service."
12
+ },
13
+ "sourceRoot": {
14
+ "type": "string",
15
+ "description": "Nest service source root directory."
16
+ },
17
+ "flat": {
18
+ "type": "boolean",
19
+ "default": false,
20
+ "description": "Flag to indicate if a directory is created.",
21
+ "x-prompt": "Create a directory?"
22
+ },
23
+ "spec": {
24
+ "type": "boolean",
25
+ "default": true,
26
+ "description": "Specifies if a spec file is generated.",
27
+ "x-prompt": "Generate spec file as well?"
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,12 @@
1
+ import { Path } from '@angular-devkit/core';
2
+
3
+ export interface CommonOptions {
4
+ name: string;
5
+ spec: boolean;
6
+ flat: boolean;
7
+ sourceRoot: string;
8
+ path?: string | Path;
9
+ type?: string;
10
+ metadata?: string;
11
+ module?: Path | null;
12
+ }
@@ -0,0 +1,88 @@
1
+ import { Path, strings } from '@angular-devkit/core';
2
+ import {
3
+ apply,
4
+ applyTemplates,
5
+ branchAndMerge,
6
+ chain,
7
+ filter,
8
+ mergeWith,
9
+ move,
10
+ noop,
11
+ Rule,
12
+ SchematicContext,
13
+ SchematicsException,
14
+ Source,
15
+ Tree,
16
+ url
17
+ } from '@angular-devkit/schematics';
18
+ import {
19
+ DeclarationOptions,
20
+ Location,
21
+ mergeSourceRoot,
22
+ ModuleDeclarator,
23
+ ModuleFinder,
24
+ NameParser
25
+ } from '@nestjs/schematics';
26
+ import { join } from 'path';
27
+ import { CommonOptions } from './common-options.interface';
28
+
29
+ export * from './common-options.interface';
30
+
31
+ export class CommonSchematicFactory<T extends CommonOptions = CommonOptions> {
32
+ public templatePath = './files';
33
+ public type = 'service';
34
+ public metadata = 'providers';
35
+
36
+ public create(options: T): Rule {
37
+ options = this.transform(options);
38
+ return branchAndMerge(
39
+ chain([mergeWith(this.generate(options)), this.addDeclarationToModule(options), mergeSourceRoot(options)])
40
+ );
41
+ }
42
+
43
+ public generate(options: T): Source {
44
+ return (context: SchematicContext) =>
45
+ apply(url(join(this.templatePath as Path)), [
46
+ options.spec ? noop() : filter((path: string) => !path.endsWith('.spec.ts')),
47
+ applyTemplates({
48
+ ...strings,
49
+ ...options,
50
+ lowercase: (str: string) => str.toLowerCase()
51
+ }),
52
+ move(options.path ?? '')
53
+ ])(context);
54
+ }
55
+
56
+ public addDeclarationToModule(options: T): Rule {
57
+ return (tree: Tree) => {
58
+ options.module = new ModuleFinder(tree).find({
59
+ name: options.name,
60
+ path: options.path as Path
61
+ });
62
+ if (options.module === undefined || options.module === null) {
63
+ return tree;
64
+ }
65
+ const rawContent = tree.read(options.module);
66
+ const content = rawContent?.toString() ?? '';
67
+ const declarator: ModuleDeclarator = new ModuleDeclarator();
68
+ tree.overwrite(options.module, declarator.declare(content, options as DeclarationOptions));
69
+ return tree;
70
+ };
71
+ }
72
+
73
+ public transform(source: T): T {
74
+ const target: T = Object.assign({}, source);
75
+ target.metadata = this.metadata;
76
+ target.type = this.type;
77
+
78
+ if (target.name === null || target.name === undefined) {
79
+ throw new SchematicsException('Option (name) is required.');
80
+ }
81
+ const location: Location = new NameParser().parse(target);
82
+ target.name = strings.dasherize(location.name);
83
+ target.path = strings.dasherize(location.path);
84
+
85
+ target.path = target.flat ? target.path : join(target.path as Path, target.name);
86
+ return target;
87
+ }
88
+ }
@@ -0,0 +1,6 @@
1
+ import { CommonOptions } from '../common';
2
+ import { ContextMenuType } from './context-menu-type.enum';
3
+
4
+ export interface ContextMenuOptions extends CommonOptions {
5
+ type: ContextMenuType;
6
+ }
@@ -0,0 +1,4 @@
1
+ export enum ContextMenuType {
2
+ Message = 'message',
3
+ User = 'user'
4
+ }
@@ -0,0 +1,14 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ import { Context, UserCommand, UserCommandContext, TargetUser } from 'necord';
3
+ import { User } from 'discord.js';
4
+
5
+ @Injectable()
6
+ export class <%= classify(name) %>Commands {
7
+ @UserCommand({ name: <%= name.toLowerCase() %> })
8
+ public async <%= name %>(
9
+ @Context() [interaction]: UserCommandContext,
10
+ @TargetUser() user: User
11
+ ) {
12
+ return interaction.reply({ content: `Hello ${user.username}!` });
13
+ }
14
+ }
@@ -0,0 +1,26 @@
1
+ import { CommonSchematicFactory } from '../common';
2
+ import { ContextMenuOptions } from './context-menu-options.interface';
3
+ import { Rule, Source } from '@angular-devkit/schematics';
4
+ import { ContextMenuType } from './context-menu-type.enum';
5
+
6
+ class ContextMenuSchematicFactory extends CommonSchematicFactory<ContextMenuOptions> {
7
+ public type = 'context-menu';
8
+
9
+ generate(options: ContextMenuOptions): Source {
10
+ switch (options.type) {
11
+ case ContextMenuType.Message:
12
+ this.templatePath = './files/message-menu';
13
+ break;
14
+ case ContextMenuType.User:
15
+ this.templatePath = './files/user-menu';
16
+ break;
17
+ }
18
+
19
+ return super.generate(options);
20
+ }
21
+ }
22
+
23
+ export function contextMenu(options: ContextMenuOptions): Rule {
24
+ const contextMenuFactory = new ContextMenuSchematicFactory();
25
+ return contextMenuFactory.create(options);
26
+ }
@@ -0,0 +1,42 @@
1
+ {
2
+ "properties": {
3
+ "name": {
4
+ "type": "string",
5
+ "minLength": 1,
6
+ "description": "Command name",
7
+ "x-prompt": "What is the command name?"
8
+ },
9
+ "path": {
10
+ "type": "string",
11
+ "format": "path",
12
+ "description": "The path to create the service."
13
+ },
14
+ "sourceRoot": {
15
+ "type": "string",
16
+ "description": "Nest service source root directory."
17
+ },
18
+ "flat": {
19
+ "type": "boolean",
20
+ "default": false,
21
+ "description": "Flag to indicate if a directory is created.",
22
+ "x-prompt": "Create a directory?"
23
+ },
24
+ "spec": {
25
+ "type": "boolean",
26
+ "default": true,
27
+ "description": "Specifies if a spec file is generated.",
28
+ "x-prompt": "Generate spec file as well?"
29
+ },
30
+ "type": {
31
+ "type": "string",
32
+ "description": "The type of context menu to create.",
33
+ "enum": [
34
+ "message",
35
+ "user"
36
+ ]
37
+ }
38
+ },
39
+ "required": [
40
+ "type"
41
+ ]
42
+ }
package/src/index.ts ADDED
File without changes
@@ -0,0 +1,30 @@
1
+ {
2
+ "properties": {
3
+ "name": {
4
+ "type": "string",
5
+ "minLength": 1,
6
+ "x-prompt": "What is the questions set name?"
7
+ },
8
+ "path": {
9
+ "type": "string",
10
+ "format": "path",
11
+ "description": "The path to create the service."
12
+ },
13
+ "sourceRoot": {
14
+ "type": "string",
15
+ "description": "Nest service source root directory."
16
+ },
17
+ "flat": {
18
+ "type": "boolean",
19
+ "default": false,
20
+ "description": "Flag to indicate if a directory is created.",
21
+ "x-prompt": "Create a directory?"
22
+ },
23
+ "spec": {
24
+ "type": "boolean",
25
+ "default": true,
26
+ "description": "Specifies if a spec file is generated.",
27
+ "x-prompt": "Generate spec file as well?"
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "properties": {
3
+ "name": {
4
+ "type": "string",
5
+ "minLength": 1,
6
+ "x-prompt": "What is the questions set name?"
7
+ },
8
+ "path": {
9
+ "type": "string",
10
+ "format": "path",
11
+ "description": "The path to create the service."
12
+ },
13
+ "sourceRoot": {
14
+ "type": "string",
15
+ "description": "Nest service source root directory."
16
+ },
17
+ "flat": {
18
+ "type": "boolean",
19
+ "default": false,
20
+ "description": "Flag to indicate if a directory is created.",
21
+ "x-prompt": "Create a directory?"
22
+ },
23
+ "spec": {
24
+ "type": "boolean",
25
+ "default": true,
26
+ "description": "Specifies if a spec file is generated.",
27
+ "x-prompt": "Generate spec file as well?"
28
+ }
29
+ }
30
+ }