Skip to content

Commit

Permalink
local plugin with generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Roozenboom committed Apr 23, 2024
1 parent 005bd49 commit 56c1526
Show file tree
Hide file tree
Showing 21 changed files with 841 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"ignorePatterns": ["**/*"],
"plugins": ["@nx"],
"overrides": [
{
"files": "*.json",
"parser": "jsonc-eslint-parser",
"rules": {}
},
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.validate": ["json"]
}
5 changes: 5 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"@nx/eslint:lint": {
"inputs": ["default", "{workspaceRoot}/.eslintrc.json"],
"cache": true
},
"@nx/js:tsc": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
}
},
"namedInputs": {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"react-router-dom": "6.11.2",
"react-test-renderer": "18.2.0",
"rxjs": "7.8.0",
"tslib": "^2.0.0",
"tslib": "^2.3.0",
"zone.js": "0.14.4"
},
"devDependencies": {
Expand All @@ -75,12 +75,17 @@
"@nx/eslint-plugin": "18.3.0",
"@nx/jest": "18.3.0",
"@nx/js": "18.3.0",
"@nx/plugin": "18.3.0",
"@nx/react": "18.3.0",
"@nx/web": "18.3.0",
"@nx/workspace": "18.3.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@schematics/angular": "17.3.5",
"@svgr/webpack": "^6.1.2",
"@swc-node/register": "~1.8.0",
"@swc/cli": "~0.1.62",
"@swc/core": "~1.3.85",
"@swc/helpers": "~0.5.2",
"@testing-library/react": "13.4.0",
"@types/jest": "29.4.0",
"@types/node": "^18.16.9",
Expand Down
32 changes: 32 additions & 0 deletions tools/my-plugin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
},
{
"files": ["./package.json", "./generators.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions tools/my-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# my-plugin

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build my-plugin` to build the library.

## Running unit tests

Run `nx test my-plugin` to execute the unit tests via [Jest](https://jestjs.io).
9 changes: 9 additions & 0 deletions tools/my-plugin/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"generators": {
"my-generator": {
"factory": "./src/generators/my-generator",
"schema": "./src/generators/my-generator/schema.json",
"description": "my-generator generator"
}
}
}
10 changes: 10 additions & 0 deletions tools/my-plugin/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'my-plugin',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/tools/my-plugin',
};
13 changes: 13 additions & 0 deletions tools/my-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@nx-example/my-plugin",
"version": "0.0.1",
"dependencies": {
"@nx/devkit": "18.3.0",
"tslib": "^2.3.0"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true,
"generators": "./generators.json"
}
51 changes: 51 additions & 0 deletions tools/my-plugin/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "my-plugin",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "tools/my-plugin/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/tools/my-plugin",
"main": "tools/my-plugin/src/index.ts",
"tsConfig": "tools/my-plugin/tsconfig.lib.json",
"assets": [
"tools/my-plugin/*.md",
{
"input": "./tools/my-plugin/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./tools/my-plugin/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./tools/my-plugin",
"glob": "generators.json",
"output": "."
},
{
"input": "./tools/my-plugin",
"glob": "executors.json",
"output": "."
}
]
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "tools/my-plugin/jest.config.ts"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= name %>";
20 changes: 20 additions & 0 deletions tools/my-plugin/src/generators/my-generator/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree, readProjectConfiguration } from '@nx/devkit';

import { myGeneratorGenerator } from './generator';
import { MyGeneratorGeneratorSchema } from './schema';

describe('my-generator generator', () => {
let tree: Tree;
const options: MyGeneratorGeneratorSchema = { name: 'test' };

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it('should run successfully', async () => {
await myGeneratorGenerator(tree, options);
const config = readProjectConfiguration(tree, 'test');
expect(config).toBeDefined();
});
});
25 changes: 25 additions & 0 deletions tools/my-plugin/src/generators/my-generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
addProjectConfiguration,
formatFiles,
generateFiles,
Tree,
} from '@nx/devkit';
import * as path from 'path';
import { MyGeneratorGeneratorSchema } from './schema';

export async function myGeneratorGenerator(
tree: Tree,
options: MyGeneratorGeneratorSchema
) {
const projectRoot = `libs/${options.name}`;
addProjectConfiguration(tree, options.name, {
root: projectRoot,
projectType: 'library',
sourceRoot: `${projectRoot}/src`,
targets: {},
});
generateFiles(tree, path.join(__dirname, 'files'), projectRoot, options);
await formatFiles(tree);
}

export default myGeneratorGenerator;
3 changes: 3 additions & 0 deletions tools/my-plugin/src/generators/my-generator/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface MyGeneratorGeneratorSchema {
name: string;
}
18 changes: 18 additions & 0 deletions tools/my-plugin/src/generators/my-generator/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "MyGenerator",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
}
},
"required": ["name"]
}
Empty file added tools/my-plugin/src/index.ts
Empty file.
16 changes: 16 additions & 0 deletions tools/my-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions tools/my-plugin/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions tools/my-plugin/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"baseUrl": ".",
"paths": {
"@nx-example/cart/cart-page": ["libs/cart/cart-page/src/index.ts"],
"@nx-example/my-plugin": ["tools/my-plugin/src/index.ts"],
"@nx-example/products/home-page": [
"libs/products/home-page/src/index.ts"
],
Expand Down

0 comments on commit 56c1526

Please sign in to comment.