diff --git a/tools/workspace-plugin/generators.json b/tools/workspace-plugin/generators.json index 5a8a25356e1bd3..c1bcb21a4c5b27 100644 --- a/tools/workspace-plugin/generators.json +++ b/tools/workspace-plugin/generators.json @@ -89,6 +89,11 @@ "factory": "./src/generators/bundle-size-configuration/generator", "schema": "./src/generators/bundle-size-configuration/schema.json", "description": "bundle-size-configuration generator" + }, + "split-library-in-two": { + "factory": "./src/generators/split-library-in-two/generator", + "schema": "./src/generators/split-library-in-two/schema.json", + "description": "split-library-in-two generator" } } } diff --git a/tools/workspace-plugin/src/generators/split-library-in-two/files/src/index.ts.template b/tools/workspace-plugin/src/generators/split-library-in-two/files/src/index.ts.template new file mode 100644 index 00000000000000..877d430279d9e5 --- /dev/null +++ b/tools/workspace-plugin/src/generators/split-library-in-two/files/src/index.ts.template @@ -0,0 +1 @@ +const variable = "<%= name %>"; \ No newline at end of file diff --git a/tools/workspace-plugin/src/generators/split-library-in-two/generator.spec.ts b/tools/workspace-plugin/src/generators/split-library-in-two/generator.spec.ts new file mode 100644 index 00000000000000..1be7abbcbc2657 --- /dev/null +++ b/tools/workspace-plugin/src/generators/split-library-in-two/generator.spec.ts @@ -0,0 +1,20 @@ +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import { Tree, readProjectConfiguration } from '@nx/devkit'; + +import { splitLibraryInTwoGenerator } from './generator'; +import { SplitLibraryInTwoGeneratorSchema } from './schema'; + +describe('split-library-in-two generator', () => { + let tree: Tree; + const options: SplitLibraryInTwoGeneratorSchema = { name: 'test' }; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should run successfully', async () => { + await splitLibraryInTwoGenerator(tree, options); + const config = readProjectConfiguration(tree, 'test'); + expect(config).toBeDefined(); + }); +}); diff --git a/tools/workspace-plugin/src/generators/split-library-in-two/generator.ts b/tools/workspace-plugin/src/generators/split-library-in-two/generator.ts new file mode 100644 index 00000000000000..38e408d746bbae --- /dev/null +++ b/tools/workspace-plugin/src/generators/split-library-in-two/generator.ts @@ -0,0 +1,17 @@ +import { addProjectConfiguration, formatFiles, generateFiles, Tree } from '@nx/devkit'; +import * as path from 'path'; +import { SplitLibraryInTwoGeneratorSchema } from './schema'; + +export async function splitLibraryInTwoGenerator(tree: Tree, options: SplitLibraryInTwoGeneratorSchema) { + 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 splitLibraryInTwoGenerator; diff --git a/tools/workspace-plugin/src/generators/split-library-in-two/schema.d.ts b/tools/workspace-plugin/src/generators/split-library-in-two/schema.d.ts new file mode 100644 index 00000000000000..0f5f303f5fc0fd --- /dev/null +++ b/tools/workspace-plugin/src/generators/split-library-in-two/schema.d.ts @@ -0,0 +1,3 @@ +export interface SplitLibraryInTwoGeneratorSchema { + name: string; +} diff --git a/tools/workspace-plugin/src/generators/split-library-in-two/schema.json b/tools/workspace-plugin/src/generators/split-library-in-two/schema.json new file mode 100644 index 00000000000000..33e4b3c1d1fe1c --- /dev/null +++ b/tools/workspace-plugin/src/generators/split-library-in-two/schema.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://json-schema.org/schema", + "$id": "SplitLibraryInTwo", + "title": "", + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "", + "$default": { + "$source": "argv", + "index": 0 + }, + "x-prompt": "What name would you like to use?" + } + }, + "required": ["name"] +}