Skip to content

Commit

Permalink
Lab #10
Browse files Browse the repository at this point in the history
  • Loading branch information
christianliebel committed Oct 7, 2024
1 parent 9fbd00e commit fbe1f40
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions tools/my-plugin/src/generators/my-step/generator.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit fbe1f40

Please sign in to comment.