Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plugin #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions apps/my-app/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = (async () => {
resolver: {
assetExts: assetExts.filter((ext) => ext !== 'svg'),
sourceExts: [...sourceExts, 'svg'],
blacklistRE: /libs\/myplugin\/package.json/
},
},
{
Expand Down
14 changes: 14 additions & 0 deletions apps/myplugin-e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'myplugin-e2e',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/myplugin-e2e',
};
16 changes: 16 additions & 0 deletions apps/myplugin-e2e/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"root": "apps/myplugin-e2e",
"projectType": "application",
"sourceRoot": "apps/myplugin-e2e/src",
"targets": {
"e2e": {
"executor": "@nrwl/nx-plugin:e2e",
"options": {
"target": "myplugin:build",
"jestConfig": "apps/myplugin-e2e/jest.config.js"
}
}
},
"tags": [],
"implicitDependencies": ["myplugin"]
}
50 changes: 50 additions & 0 deletions apps/myplugin-e2e/tests/myplugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
checkFilesExist,
ensureNxProject,
readJson,
runNxCommandAsync,
uniq,
} from '@nrwl/nx-plugin/testing';
describe('myplugin e2e', () => {
it('should create myplugin', async () => {
const plugin = uniq('myplugin');
ensureNxProject('@nx-react-native-example/myplugin', 'dist/libs/myplugin');
await runNxCommandAsync(
`generate @nx-react-native-example/myplugin:myplugin ${plugin}`
);

const result = await runNxCommandAsync(`build ${plugin}`);
expect(result.stdout).toContain('Executor ran');
}, 120000);

describe('--directory', () => {
it('should create src in the specified directory', async () => {
const plugin = uniq('myplugin');
ensureNxProject(
'@nx-react-native-example/myplugin',
'dist/libs/myplugin'
);
await runNxCommandAsync(
`generate @nx-react-native-example/myplugin:myplugin ${plugin} --directory subdir`
);
expect(() =>
checkFilesExist(`libs/subdir/${plugin}/src/index.ts`)
).not.toThrow();
}, 120000);
});

describe('--tags', () => {
it('should add tags to the project', async () => {
const plugin = uniq('myplugin');
ensureNxProject(
'@nx-react-native-example/myplugin',
'dist/libs/myplugin'
);
await runNxCommandAsync(
`generate @nx-react-native-example/myplugin:myplugin ${plugin} --tags e2etag,e2ePackage`
);
const project = readJson(`libs/${plugin}/project.json`);
expect(project.tags).toEqual(['e2etag', 'e2ePackage']);
}, 120000);
});
});
13 changes: 13 additions & 0 deletions apps/myplugin-e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.e2e.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
9 changes: 9 additions & 0 deletions apps/myplugin-e2e/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
3 changes: 3 additions & 0 deletions libs/myplugin/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
18 changes: 18 additions & 0 deletions libs/myplugin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/myplugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# myplugin

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

## Running unit tests

Run `nx test myplugin` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions libs/myplugin/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/schema",
"executors": {
"build": {
"implementation": "./src/executors/build/executor",
"schema": "./src/executors/build/schema.json",
"description": "build executor"
}
}
}
12 changes: 12 additions & 0 deletions libs/myplugin/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/schema",
"name": "myplugin",
"version": "0.0.1",
"generators": {
"myplugin": {
"factory": "./src/generators/myplugin/generator",
"schema": "./src/generators/myplugin/schema.json",
"description": "myplugin generator"
}
}
}
15 changes: 15 additions & 0 deletions libs/myplugin/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
displayName: 'myplugin',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/myplugin',
};
7 changes: 7 additions & 0 deletions libs/myplugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@nx-react-native-example/myplugin",
"version": "0.0.1",
"main": "src/index.js",
"generators": "./generators.json",
"executors": "./executors.json"
}
56 changes: 56 additions & 0 deletions libs/myplugin/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"root": "libs/myplugin",
"sourceRoot": "libs/myplugin/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/myplugin/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/myplugin"],
"options": {
"jestConfig": "libs/myplugin/jest.config.js",
"passWithNoTests": true
}
},
"build": {
"executor": "@nrwl/node:package",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/myplugin",
"tsConfig": "libs/myplugin/tsconfig.lib.json",
"packageJson": "libs/myplugin/package.json",
"main": "libs/myplugin/src/index.ts",
"assets": [
"libs/myplugin/*.md",
{
"input": "./libs/myplugin/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./libs/myplugin/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./libs/myplugin",
"glob": "generators.json",
"output": "."
},
{
"input": "./libs/myplugin",
"glob": "executors.json",
"output": "."
}
]
}
}
},
"tags": []
}
11 changes: 11 additions & 0 deletions libs/myplugin/src/executors/build/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BuildExecutorSchema } from './schema';
import executor from './executor';

const options: BuildExecutorSchema = {};

describe('Build Executor', () => {
it('can run', async () => {
const output = await executor(options);
expect(output.success).toBe(true);
});
});
8 changes: 8 additions & 0 deletions libs/myplugin/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BuildExecutorSchema } from './schema';

export default async function runExecutor(options: BuildExecutorSchema) {
console.log('Executor ran for Build', options);
return {
success: true,
};
}
1 change: 1 addition & 0 deletions libs/myplugin/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface BuildExecutorSchema {} // eslint-disable-line
9 changes: 9 additions & 0 deletions libs/myplugin/src/executors/build/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Build executor",
"description": "",
"type": "object",
"properties": {},
"required": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= projectName %>";
20 changes: 20 additions & 0 deletions libs/myplugin/src/generators/myplugin/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';

import generator from './generator';
import { MypluginGeneratorSchema } from './schema';

describe('myplugin generator', () => {
let appTree: Tree;
const options: MypluginGeneratorSchema = { name: 'test' };

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

it('should run successfully', async () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'test');
expect(config).toBeDefined();
});
});
73 changes: 73 additions & 0 deletions libs/myplugin/src/generators/myplugin/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
addProjectConfiguration,
formatFiles,
generateFiles,
getWorkspaceLayout,
names,
offsetFromRoot,
Tree,
} from '@nrwl/devkit';
import * as path from 'path';
import { MypluginGeneratorSchema } from './schema';

interface NormalizedSchema extends MypluginGeneratorSchema {
projectName: string;
projectRoot: string;
projectDirectory: string;
parsedTags: string[];
}

function normalizeOptions(
tree: Tree,
options: MypluginGeneratorSchema
): NormalizedSchema {
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const projectRoot = `${getWorkspaceLayout(tree).libsDir}/${projectDirectory}`;
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];

return {
...options,
projectName,
projectRoot,
projectDirectory,
parsedTags,
};
}

function addFiles(tree: Tree, options: NormalizedSchema) {
const templateOptions = {
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot),
template: '',
};
generateFiles(
tree,
path.join(__dirname, 'files'),
options.projectRoot,
templateOptions
);
}

export default async function (tree: Tree, options: MypluginGeneratorSchema) {
const normalizedOptions = normalizeOptions(tree, options);
addProjectConfiguration(tree, normalizedOptions.projectName, {
root: normalizedOptions.projectRoot,
projectType: 'library',
sourceRoot: `${normalizedOptions.projectRoot}/src`,
targets: {
build: {
executor: '@nx-react-native-example/myplugin:build',
},
},
tags: normalizedOptions.parsedTags,
});
addFiles(tree, normalizedOptions);
await formatFiles(tree);
}
5 changes: 5 additions & 0 deletions libs/myplugin/src/generators/myplugin/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface MypluginGeneratorSchema {
name: string;
tags?: string;
directory?: string;
}
Loading