import { strings } from '@angular-devkit/core' ; import { camelize } from '@angular-devkit/core/src/utils/strings' ; import { Rule , apply , applyTemplates , chain , mergeWith , move , url } from '@angular-devkit/schematics' ; import { SyntaxKind } from 'ts-morph' ; import { applyLintFix } from '../utils/lint-fix' ; import { addImportToFile } from '../utils/registration' ; import { createTsMorphProject } from '../utils/ts-morph' ; export function eslintRule ( options ) { const ruleName = options . name ; options . path = '/eslint-rules/' ; console . log ( 'eslint-rule schematic' , ruleName ) ; return async ( _ ) => { const operations = [ ] ; operations . push ( mergeWith ( apply ( url ( './files' ) , [ applyTemplates ( { ... strings , ... options , } ) , move ( options . path ) , ] ) ) ) ; operations . push ( addEslintRuleToObject ( options ) ) ; operations . push ( addImportToFile ( { module : '/eslint-rules/src/index.ts' , artifactName : `${ camelize ( options . name ) }Rule` , moduleImportPath : `/eslint-rules/src/rules/${ options . name }` , } ) ) ; operations . push ( applyLintFix ( ) ) ; return chain ( operations ) ; } ; } function addEslintRuleToObject ( options ) { return host => { const tsMorphProject = createTsMorphProject ( host ) ; tsMorphProject . addSourceFileAtPath ( '/eslint-rules/src/index.ts' ) ; const sourceFile = tsMorphProject . getSourceFile ( '/eslint-rules/src/index.ts' ) ; sourceFile . getFirstDescendantByKindOrThrow ( SyntaxKind . ObjectLiteralExpression ) . addPropertyAssignment ( { name : `'${ options . name }'` , initializer : `${ camelize ( options . name ) }Rule` , } ) ; host . overwrite ( '/eslint-rules/src/index.ts' , sourceFile . getText ( ) ) ; } ; }