Skip to content

Commit

Permalink
feat(nx-plugin): add nx-plugin preset generator
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Mar 17, 2023
1 parent a8261e8 commit 6c0bdcb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
16 changes: 8 additions & 8 deletions packages/create-nx-plugin/bin/create-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { output } from 'create-nx-workspace/src/utils/output';
import { CI } from 'create-nx-workspace/src/utils/ci/ci-list';
import { CreateWorkspaceOptions } from 'create-nx-workspace/src/create-workspace-options';
import { PackageManager } from 'create-nx-workspace/src/utils/package-manager';
import { parse } from 'path';

export const yargsDecorator = {
'Options:': `${chalk.green`Options`}:`,
Expand All @@ -38,8 +39,8 @@ export const yargsDecorator = {
const nxVersion = require('../package.json').version;

function determinePluginName(parsedArgs: CreateNxPluginArguments) {
if (parsedArgs.name) {
return Promise.resolve(parsedArgs.name);
if (parsedArgs.pluginName) {
return Promise.resolve(parsedArgs.pluginName);
}

return enquirer
Expand All @@ -64,8 +65,7 @@ function determinePluginName(parsedArgs: CreateNxPluginArguments) {
}

interface CreateNxPluginArguments {
name: string;
importPath: string;
pluginName: string;
packageManager: PackageManager;
ci: CI;
allPrompts: boolean;
Expand Down Expand Up @@ -117,11 +117,11 @@ export const commandsObject: yargs.Argv<CreateNxPluginArguments> = yargs
async function main(parsedArgs: yargs.Arguments<CreateNxPluginArguments>) {
const populatedArguments: CreateNxPluginArguments & CreateWorkspaceOptions = {
...parsedArgs,
name: parsedArgs.pluginName.includes('/')
? parsedArgs.pluginName.split('/')[1]
: parsedArgs.pluginName,
};
await createWorkspace<CreateNxPluginArguments>(
'@nrwl/nx-plugin',
populatedArguments
);
await createWorkspace('@nrwl/nx-plugin', populatedArguments);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "nx/plugin",
"version": "0.1",
"extends": ["@nrwl/workspace"],
"extends": [
"@nrwl/workspace"
],
"generators": {
"plugin": {
"factory": "./src/generators/plugin/plugin",
Expand Down Expand Up @@ -33,6 +35,13 @@
"factory": "./src/generators/lint-checks/generator",
"schema": "./src/generators/lint-checks/schema.json",
"description": "Adds linting configuration to validate common json files for nx plugins."
},
"preset": {
"factory": "./src/generators/preset/generator",
"schema": "./src/generators/preset/schema.json",
"description": "preset generator",
"hidden": true,
"x-use-standalone-layout": true
}
},
"schematics": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= projectName %>";
20 changes: 20 additions & 0 deletions packages/nx-plugin/src/generators/preset/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 { PresetGeneratorSchema } from './schema';

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

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace({layout: 'apps-libs'});
});

it('should run successfully', async () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'test');
expect(config).toBeDefined();
});
});
16 changes: 16 additions & 0 deletions packages/nx-plugin/src/generators/preset/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Tree } from '@nrwl/devkit';
import { Linter } from '@nrwl/linter';
import { pluginGenerator } from '../plugin/plugin';
import { PresetGeneratorSchema } from './schema';

export default async function (tree: Tree, options: PresetGeneratorSchema) {
return pluginGenerator(tree, {
compiler: 'tsc',
linter: Linter.EsLint,
name: options.pluginName,
skipFormat: false,
skipLintChecks: false,
skipTsConfig: false,
unitTestRunner: 'jest',
});
}
3 changes: 3 additions & 0 deletions packages/nx-plugin/src/generators/preset/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PresetGeneratorSchema {
pluginName: string;
}
16 changes: 16 additions & 0 deletions packages/nx-plugin/src/generators/preset/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginPreset",
"title": "Preset for @nrwl/nx-plugin",
"description": "Initializes a workspace with an nx-plugin inside of it. Use as: `create-nx-workspace --preset @nrwl/nx-plugin`",
"type": "object",
"properties": {
"pluginName": {
"type": "string",
"description": "Plugin name",
"aliases": ["name"]
}
},
"required": ["pluginName"]
}

0 comments on commit 6c0bdcb

Please sign in to comment.