diff --git a/packages/create-nx-plugin/bin/create-nx-plugin.ts b/packages/create-nx-plugin/bin/create-nx-plugin.ts index 181819033d07e7..4c2c32fb3e64b3 100644 --- a/packages/create-nx-plugin/bin/create-nx-plugin.ts +++ b/packages/create-nx-plugin/bin/create-nx-plugin.ts @@ -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`}:`, @@ -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 @@ -64,8 +65,7 @@ function determinePluginName(parsedArgs: CreateNxPluginArguments) { } interface CreateNxPluginArguments { - name: string; - importPath: string; + pluginName: string; packageManager: PackageManager; ci: CI; allPrompts: boolean; @@ -117,11 +117,11 @@ export const commandsObject: yargs.Argv = yargs async function main(parsedArgs: yargs.Arguments) { const populatedArguments: CreateNxPluginArguments & CreateWorkspaceOptions = { ...parsedArgs, + name: parsedArgs.pluginName.includes('/') + ? parsedArgs.pluginName.split('/')[1] + : parsedArgs.pluginName, }; - await createWorkspace( - '@nrwl/nx-plugin', - populatedArguments - ); + await createWorkspace('@nrwl/nx-plugin', populatedArguments); } /** diff --git a/packages/nx-plugin/generators.json b/packages/nx-plugin/generators.json index b889b233cf3b86..4b1057fd01addf 100644 --- a/packages/nx-plugin/generators.json +++ b/packages/nx-plugin/generators.json @@ -1,7 +1,9 @@ { "name": "nx/plugin", "version": "0.1", - "extends": ["@nrwl/workspace"], + "extends": [ + "@nrwl/workspace" + ], "generators": { "plugin": { "factory": "./src/generators/plugin/plugin", @@ -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": { diff --git a/packages/nx-plugin/src/generators/preset/files/src/index.ts__template__ b/packages/nx-plugin/src/generators/preset/files/src/index.ts__template__ new file mode 100644 index 00000000000000..dde3cb697b7275 --- /dev/null +++ b/packages/nx-plugin/src/generators/preset/files/src/index.ts__template__ @@ -0,0 +1 @@ +const variable = "<%= projectName %>"; \ No newline at end of file diff --git a/packages/nx-plugin/src/generators/preset/generator.spec.ts b/packages/nx-plugin/src/generators/preset/generator.spec.ts new file mode 100644 index 00000000000000..97eceb7ecd9a5a --- /dev/null +++ b/packages/nx-plugin/src/generators/preset/generator.spec.ts @@ -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(); + }); +}); diff --git a/packages/nx-plugin/src/generators/preset/generator.ts b/packages/nx-plugin/src/generators/preset/generator.ts new file mode 100644 index 00000000000000..e04c262e0e4988 --- /dev/null +++ b/packages/nx-plugin/src/generators/preset/generator.ts @@ -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', + }); +} diff --git a/packages/nx-plugin/src/generators/preset/schema.d.ts b/packages/nx-plugin/src/generators/preset/schema.d.ts new file mode 100644 index 00000000000000..bff8df3c05dfbc --- /dev/null +++ b/packages/nx-plugin/src/generators/preset/schema.d.ts @@ -0,0 +1,3 @@ +export interface PresetGeneratorSchema { + pluginName: string; +} diff --git a/packages/nx-plugin/src/generators/preset/schema.json b/packages/nx-plugin/src/generators/preset/schema.json new file mode 100644 index 00000000000000..022c86107efa0c --- /dev/null +++ b/packages/nx-plugin/src/generators/preset/schema.json @@ -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"] +}