Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions demo/my-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Demo Plugin

After the plugin and generators/executors are present, follow the below steps:

1. Setup the plugin in the workspace

```bash
nx g @push-based/zod2nx-schema-nx-plugin:init --registerSyncGenerator --registerPlugin
```

2. Add configuration to your project `zod2nx-schema.config.ts`

```bash
# manual generator usage
nx g @push-based/zod2nx-schema-nx-plugin:configuration --project=my-plugin
# or via registered plugin (when init used with --registerPlugin)
nx run my-plugin:zod2nx-schema--configuration
```

3. Generate the JSON schema

```bash
nx run my-plugin:zod2nx-schema
# over global sync generator (when init used with --registerSyncGenerator)
nx run my-plugin:build
```
19 changes: 19 additions & 0 deletions demo/my-plugin/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"generators": {
"my-generator": {
"factory": "./src/generators/my-generator/my-generator",
"schema": "./src/generators/my-generator/schema.json",
"description": "my-generator generator"
},
"other-generator": {
"factory": "./src/generators/other-generator/other-generator",
"schema": "./src/generators/other-generator/schema.json",
"description": "other-generator generator"
},
"good-generator": {
"factory": "./src/generators/good-generator/good-generator",
"schema": "./src/generators/good-generator/schema.json",
"description": "good-generator generator"
}
}
}
13 changes: 13 additions & 0 deletions demo/my-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@push-based/my-plugin",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": "./src/index.js",
"types": "./src/index.d.ts",
"dependencies": {
"@nx/devkit": "22.3.3",
"tslib": "^2.3.0"
},
"generators": "./generators.json"
}
8 changes: 8 additions & 0 deletions demo/my-plugin/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "my-plugin",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "demo/my-plugin/src",
"projectType": "library",
"tags": [],
"targets": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= name %>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Tree, readProjectConfiguration } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { goodGeneratorGenerator } from './good-generator';
import { GoodGeneratorGeneratorSchema } from './schema';

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

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

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

export async function goodGeneratorGenerator(
tree: Tree,
options: GoodGeneratorGeneratorSchema,
) {
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 goodGeneratorGenerator;
3 changes: 3 additions & 0 deletions demo/my-plugin/src/generators/good-generator/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface GoodGeneratorGeneratorSchema {
name: string;
}
18 changes: 18 additions & 0 deletions demo/my-plugin/src/generators/good-generator/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "GoodGenerator",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
}
},
"required": ["name"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= name %>";
19 changes: 19 additions & 0 deletions demo/my-plugin/src/generators/my-generator/my-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Tree, readProjectConfiguration } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { myGeneratorGenerator } from './my-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 demo/my-plugin/src/generators/my-generator/my-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Tree,
addProjectConfiguration,
formatFiles,
generateFiles,
} 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 demo/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 demo/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"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= name %>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Tree, readProjectConfiguration } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { otherGeneratorGenerator } from './other-generator';
import { OtherGeneratorGeneratorSchema } from './schema';

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

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

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

export async function otherGeneratorGenerator(
tree: Tree,
options: OtherGeneratorGeneratorSchema,
) {
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 otherGeneratorGenerator;
3 changes: 3 additions & 0 deletions demo/my-plugin/src/generators/other-generator/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface OtherGeneratorGeneratorSchema {
name: string;
}
18 changes: 18 additions & 0 deletions demo/my-plugin/src/generators/other-generator/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "OtherGenerator",
"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 demo/my-plugin/src/index.ts
Empty file.
13 changes: 13 additions & 0 deletions demo/my-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
}
]
}
9 changes: 9 additions & 0 deletions demo/my-plugin/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { type GenerateZod2NxSchemaOptions } from '@push-based/zod2nx-schema';

export default [] satisfies GenerateZod2NxSchemaOptions[];
export default [];
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ describe('nx plugin executor - cli', () => {
});
});

// afterEach(async () => {
// await teardownTestFolder(testFileDir);
// });

afterAll(() => {
Object.entries(processEnvCP).forEach(([k, v]) => {
// eslint-disable-next-line functional/immutable-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('zod2nx-schema-nx-plugin g configuration', () => {
'g',
'@push-based/zod2nx-schema-nx-plugin:configuration',
project,
'--targetName=zod2nx-schema',
],
cwd,
});
Expand Down Expand Up @@ -157,4 +156,46 @@ describe('zod2nx-schema-nx-plugin g configuration', () => {
'NOTE: The "dryRun" flag means no changes were made.',
);
});

it('should support registering sync generator locally', async () => {
const cwd = path.join(testFileDir, 'configure-with-sync');
const mockDir = path.join(import.meta.dirname, '../mocks/nx-monorepo');
await setupTestWorkspace(mockDir, cwd);

const { code, stdout } = await executeProcess({
command: 'npx',
args: [
'nx',
'g',
'@push-based/zod2nx-schema-nx-plugin:configuration',
project,
'--registerSyncGenerator',
'--taskName=build',
],
cwd,
});

expect(code).toBe(0);

const cleanedStdout = removeColorCodes(stdout);
expect(cleanedStdout).toContain(
'NX Generating @push-based/zod2nx-schema-nx-plugin:configuration',
);

await expect(
readFile(
path.join(cwd, 'libs', project, 'zod2nx-schema.config.ts'),
'utf8',
),
).resolves.not.toThrow();
const projectJson = await readFile(
path.join(cwd, 'libs', project, 'project.json'),
'utf8',
);

const projectConfig = JSON.parse(projectJson);
expect(projectConfig.targets?.build?.syncGenerators).toContain(
'@push-based/zod2nx-schema-nx-plugin:sync-schemas',
);
});
});
Loading