From fbe1f40a99912874b204898f328510427c119ac8 Mon Sep 17 00:00:00 2001 From: Christian Liebel Date: Mon, 7 Oct 2024 12:15:49 -0400 Subject: [PATCH] Lab #10 --- .../src/generators/my-step/generator.ts | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/tools/my-plugin/src/generators/my-step/generator.ts b/tools/my-plugin/src/generators/my-step/generator.ts index cd95e38..f107c94 100644 --- a/tools/my-plugin/src/generators/my-step/generator.ts +++ b/tools/my-plugin/src/generators/my-step/generator.ts @@ -1,17 +1,53 @@ -import { Tree } from '@nx/devkit'; -import { dasherize } from '@nx/devkit/src/utils/string-utils'; +import { + applyChangesToString, + ChangeType, + joinPathFragments, + readProjectConfiguration, + Tree, +} from '@nx/devkit'; +import { classify, dasherize } from '@nx/devkit/src/utils/string-utils'; import { wrapAngularDevkitSchematic } from 'nx/src/adapter/ngcli-adapter'; +import { Identifier, isIdentifier, isVariableStatement } from 'typescript'; +import { getSourceFile } from '../utils/get-source-file'; import { MyStepGeneratorSchema } from './schema'; export async function myStepGenerator( tree: Tree, options: MyStepGeneratorSchema ) { - const componentSchematic = wrapAngularDevkitSchematic('@schematics/angular', 'component'); + const componentSchematic = wrapAngularDevkitSchematic( + '@schematics/angular', + 'component' + ); await componentSchematic(tree, { name: dasherize(options.name), project: options.project, }); + + const projectDir = readProjectConfiguration(tree, options.project).root; + const workflowFileName = joinPathFragments( + projectDir, + 'src', + 'app', + 'app.workflow.ts' + ); + const sourceFile = getSourceFile(tree, workflowFileName); + + const stepsDeclaration = sourceFile.statements + .filter(isVariableStatement) + .map((s) => s.declarationList.declarations[0]) + .filter((d) => isIdentifier(d.name)) + .find((d) => (d.name as Identifier).escapedText === 'loginSteps'); + + const stepToAdd = `"${classify(options.name)}",`; + const newContents = applyChangesToString(sourceFile.text, [ + { + type: ChangeType.Insert, + index: stepsDeclaration.end - 1, + text: stepToAdd + }, + ]); + tree.write(workflowFileName, newContents); } export default myStepGenerator;