From be768ca19dad130767e53c54d4fe161751467cef Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Fri, 21 Apr 2023 12:26:35 -0400 Subject: [PATCH] fix(misc): cleanup migration to workspace-plugin --- .../packages/plugin/generators/plugin.json | 5 +++ .../plugin/src/generators/plugin/plugin.ts | 2 +- .../plugin/src/generators/plugin/schema.d.ts | 1 + .../plugin/src/generators/plugin/schema.json | 5 +++ .../plugin/utils/normalize-schema.ts | 2 + .../plugin/src/generators/preset/generator.ts | 1 + ...ve-workspace-generators-to-local-plugin.ts | 40 ++++++++++++------- 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/docs/generated/packages/plugin/generators/plugin.json b/docs/generated/packages/plugin/generators/plugin.json index bf906b753cd6e..d0fa0cbfbbae7 100644 --- a/docs/generated/packages/plugin/generators/plugin.json +++ b/docs/generated/packages/plugin/generators/plugin.json @@ -87,6 +87,11 @@ "enum": ["tsc", "swc"], "default": "tsc", "description": "The compiler used by the build and test targets." + }, + "publishable": { + "type": "boolean", + "description": "Generates a boilerplate for publishing the plugin to npm.", + "default": false } }, "required": ["name"], diff --git a/packages/plugin/src/generators/plugin/plugin.ts b/packages/plugin/src/generators/plugin/plugin.ts index ae89ef1be729d..c53355f96b15d 100644 --- a/packages/plugin/src/generators/plugin/plugin.ts +++ b/packages/plugin/src/generators/plugin/plugin.ts @@ -83,7 +83,7 @@ export async function pluginGenerator(host: Tree, schema: Schema) { ...schema, config: 'project', bundler: options.bundler, - publishable: true, + publishable: options.publishable, importPath: options.npmPackageName, skipFormat: true, }) diff --git a/packages/plugin/src/generators/plugin/schema.d.ts b/packages/plugin/src/generators/plugin/schema.d.ts index 294249c817f5b..32a5d3cb8e9a8 100644 --- a/packages/plugin/src/generators/plugin/schema.d.ts +++ b/packages/plugin/src/generators/plugin/schema.d.ts @@ -14,4 +14,5 @@ export interface Schema { setParserOptionsProject?: boolean; compiler: 'swc' | 'tsc'; rootProject?: boolean; + publishable?: boolean; } diff --git a/packages/plugin/src/generators/plugin/schema.json b/packages/plugin/src/generators/plugin/schema.json index f8ef7fadfa248..d3fde1578fb81 100644 --- a/packages/plugin/src/generators/plugin/schema.json +++ b/packages/plugin/src/generators/plugin/schema.json @@ -87,6 +87,11 @@ "enum": ["tsc", "swc"], "default": "tsc", "description": "The compiler used by the build and test targets." + }, + "publishable": { + "type": "boolean", + "description": "Generates a boilerplate for publishing the plugin to npm.", + "default": false } }, "required": ["name"] diff --git a/packages/plugin/src/generators/plugin/utils/normalize-schema.ts b/packages/plugin/src/generators/plugin/utils/normalize-schema.ts index 18fa3ae47e6ef..91b0191cf6697 100644 --- a/packages/plugin/src/generators/plugin/utils/normalize-schema.ts +++ b/packages/plugin/src/generators/plugin/utils/normalize-schema.ts @@ -18,6 +18,7 @@ export interface NormalizedSchema extends Schema { npmScope: string; npmPackageName: string; bundler: 'swc' | 'tsc'; + publishable: boolean; } export function normalizeOptions( host: Tree, @@ -58,5 +59,6 @@ export function normalizeOptions( projectDirectory: fullProjectDirectory, parsedTags, npmPackageName, + publishable: options.publishable ?? false, }; } diff --git a/packages/plugin/src/generators/preset/generator.ts b/packages/plugin/src/generators/preset/generator.ts index 0002c2bda340c..780f17b168aef 100644 --- a/packages/plugin/src/generators/preset/generator.ts +++ b/packages/plugin/src/generators/preset/generator.ts @@ -26,6 +26,7 @@ export default async function (tree: Tree, options: PresetGeneratorSchema) { importPath: options.pluginName, rootProject: true, e2eTestRunner: 'jest', + publishable: true, }); tasks.push(pluginTask); diff --git a/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts b/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts index 1c097e59b9223..b27ce66ba1a71 100644 --- a/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts +++ b/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts @@ -28,11 +28,24 @@ const PROJECT_NAME = 'workspace-plugin'; const DESTINATION = `tools/${PROJECT_NAME}`; export default async function (tree: Tree) { - const tasks = []; - if (!tree.children('tools/generators').length) { + if (!tree.exists('tools/generators')) { return; } + const tasks = []; + if (hasWorkspaceGenerators(tree)) { + tasks.push(...(await moveWorkspaceGeneratorsToLocalPlugin(tree))); + } + removeToolsGeneratorsIfEmpty(tree); + await formatFiles(tree); + return () => { + for (const task of tasks) { + task(); + } + }; +} +async function moveWorkspaceGeneratorsToLocalPlugin(tree: Tree) { + const tasks = []; let project = getProjects(tree).get(PROJECT_NAME); if (!project) { await createNewPlugin(tree); @@ -48,21 +61,19 @@ export default async function (tree: Tree) { project = readProjectConfiguration(tree, PROJECT_NAME); } await updateExistingPlugin(tree, project); - removeToolsGeneratorsIfEmpty(tree); - await formatFiles(tree); - return () => { - for (const task of tasks) { - task(); - } - }; + return tasks; } -function removeToolsGeneratorsIfEmpty(tree: Tree) { +function hasWorkspaceGenerators(tree: Tree) { const children = tree.children('tools/generators'); - if ( - children.length === 0 || - (children.length === 1 && children[0] === '.gitkeep') - ) { + return ( + children.length > 0 && + !(children.length === 1 && children[0] === '.gitkeep') + ); +} + +function removeToolsGeneratorsIfEmpty(tree: Tree) { + if (!hasWorkspaceGenerators(tree)) { tree.delete('tools/generators'); } } @@ -174,6 +185,7 @@ async function createNewPlugin(tree: Tree) { skipLintChecks: false, unitTestRunner: 'jest', e2eTestRunner: 'none', + publishable: false, }); getCreateGeneratorsJson()( tree,