Skip to content

Commit

Permalink
feat(web-test-runner): add init generator
Browse files Browse the repository at this point in the history
  • Loading branch information
robby rabbitman committed Jun 29, 2024
1 parent 291520d commit f77acfc
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 16 deletions.
1 change: 0 additions & 1 deletion packages/web-test-runner-plugin/README.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/web-test-runner-plugin/src/index.ts

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions packages/web-test-runner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# web-test-runner
11 changes: 11 additions & 0 deletions packages/web-test-runner/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"generators": {
"init": {
"factory": "./src/generators/init",
"schema": "./src/generators/init.schema.json",
"description": "Registers @robby-rabbitman/web-test-runner/plugin in the nx.json.",
"hidden": true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
export default {
displayName: 'web-test-runner-plugin',
displayName: 'web-test-runner',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "@robby-rabbitman/web-test-runner-plugin",
"name": "@robby-rabbitman/web-test-runner",
"version": "0.0.0",
"description": "Web Test Runner Plugin for Nx workspaces.",
"description": "Web Test Runner Generator for Nx workspaces.",
"repository": {
"type": "git",
"url": "https://github.com/RobbyRabbitman/nx-plus.git",
"directory": "packages/web-test-runner-plugin"
"directory": "packages/web-test-runner"
},
"keywords": [
"Nx",
"Web Test Runner",
"Plugin",
"Monorepo",
"Nrwl",
"Plugin",
"Generator",
"Test",
"Web"
],
Expand All @@ -26,7 +27,12 @@
"tslib": "^2.3.0",
"@nx/devkit": "^19.3.0"
},
"generators": "./generators.json",
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
"typings": "./src/index.d.ts",
"exports": {
".": "./src/index.js",
"./plugin": "./src/plugin.js"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "web-test-runner-plugin",
"name": "web-test-runner",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"tags": ["scope:node", "type:plugin"],
"targets": {
Expand All @@ -26,11 +26,6 @@
"input": "{projectRoot}",
"glob": "generators.json",
"output": "."
},
{
"input": "{projectRoot}",
"glob": "executors.json",
"output": "."
}
]
}
Expand Down
25 changes: 25 additions & 0 deletions packages/web-test-runner/src/generators/init.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"cli": "nx",
"$id": "RobbyRabbitmanWebTestRunnerInit",
"title": "Initializes the @robby-rabbitman/web-test-runner in a nx workspace.",
"description": "Registers @robby-rabbitman/web-test-runner/plugin in the nx.json.",
"type": "object",
"additionalProperties": false,
"properties": {
"targetName": {
"description": "The name of the web-test-runner target",
"type": "string",
"default": "test"
},
"skipFormat": {
"type": "boolean",
"default": false
},
"skipAddPlugin": {
"type": "boolean",
"default": false
}
},
"required": []
}
52 changes: 52 additions & 0 deletions packages/web-test-runner/src/generators/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
ExpandedPluginConfiguration,
Generator,
formatFiles,
readNxJson,
updateNxJson,
} from '@nx/devkit';

export const webTestRunnerPluginPath =
'@robby-rabbitman/web-test-runner/plugin';

export interface WebTestRunnerInitGeneratorSchema {
testTarget?: string;
skipAddPlugin?: boolean;
skipFormat?: boolean;
}

export const initGenerator: Generator<
WebTestRunnerInitGeneratorSchema
> = async (tree, schema) => {
if (!schema?.skipAddPlugin) {
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];

const hasPlugin = nxJson.plugins.some((pluginConfig) =>
typeof pluginConfig === 'string'
? pluginConfig === webTestRunnerPluginPath
: pluginConfig.plugin === webTestRunnerPluginPath
);

if (!hasPlugin) {
nxJson.plugins.push(webTestRunnerPluginConfiguration(schema));
updateNxJson(tree, nxJson);
}
}

if (!schema?.skipFormat) {
await formatFiles(tree);
}
};

export default initGenerator;

const webTestRunnerPluginConfiguration = (
schema?: WebTestRunnerInitGeneratorSchema
) =>
({
plugin: webTestRunnerPluginPath,
options: {
targetName: schema?.testTarget ?? 'test',
},
} as ExpandedPluginConfiguration);
2 changes: 2 additions & 0 deletions packages/web-test-runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { initGenerator as generator } from './generators/init';
export { createNodesV2 } from './plugin';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"paths": {
"@robby-rabbitman/web-test-runner-plugin": [
"packages/web-test-runner-plugin/src/index.ts"
"@robby-rabbitman/web-test-runner": [
"packages/web-test-runner/src/index.ts"
],
"@robby-rabbitman/web-test-runner/plugin": [
"packages/web-test-runner/src/plugin.ts"
]
}
},
Expand Down

0 comments on commit f77acfc

Please sign in to comment.