Upgrade your CLI

Now many frameworks have CLI (Command Line Interface) in their arsenal and angular is no exception. I first encountered CLI utilities when I tried EmberJS and then it seemed to me a very convenient tool that saved my time a lot. But, unfortunately, out of the box CLI utilities are only suitable for simple, home projects.



Using them in large production projects without finishing with a file is almost impossible. Therefore, at the start of our project, we were forced to abandon its use due to the peculiarities of our architecture. For almost a year, I periodically looked towards the Angular CLI in terms of its development and was upset that we could not use it, as it would make the entry into the project much easier. But at one point, the Angular team released @ angular-devkit, which contains a set of packages used by the cli utility, and this gave us the opportunity to customize the Angular CLI for our needs. The repository currently contains about a dozen packages, but in this article we will consider only those related to schematics.



image



Schematics CLI , . , β€˜ng new’, , β€˜ng generate’ - , schematics. Angular , schematics. nrwl.io, , ngrx, redux- . schematics CLI , ? , :



  • , Angular CLI. schematics .
  • (breaking changes) .
  • IDE schematics. , schematics, , , .
  • , , , .


, , shematics. , , schematics.



schematics?



Schematics – @angular-devkit, : , , , .



, , , schematics : (). schematics-, , in-memory , , . git – (staging area), , (commit). :



image



- , schematics (Action List), . , Sink, .





, , schematics . schematics , schematics-cli:



npm install -g @angular-devkit/schematics-cli


schematics Angular CLI, npm . schematics. schematics:



schematics blank --name=sample


, CLI . , :



image



, npm , src β€” collection.json, index.ts index_spec.ts.



collection.json schematics . . :



image



schematics :



image



, schematics, . :



  • schema – json , , .
  • factory β€” , .
  • description β€” , , angular-CLI.
  • hidden β€” CLI- .
  • aliases β€” .
  • extends β€” schematic , . , , .


factory description , .



package.json, , schematics, . , CLI- , .



index.ts, :



image



, . options, , CLI, schematics . Rule. , , . Tree, . Tree :



  • base –
  • staging area – , base


, base , staging area. , git. Rule. Rule – , (Tree), . Rule – schematic , , . Rule, , , .



:



image



. , , . , .. schematics , console.log . Rule , . , , , Observable, . , , npm, package.json.



, :



npm run build
schematics .:sample --name=test


image



CLI , . , β€˜.’.



, . , options, , any. , , , CLI , , . . – schema.json schema.ts. angular-CLI, , . . scheme.ts:



image



schema.json:



image



. , :



image



image



, . , . quiet , . , - , . . CLI , , . , , . x-promt , . , , .



, :



image



, , , , schematics.



schematics



schematics , . , , – , , . Tree, Rule, :



create(path: string, content: Buffer | string): void;
delete(path: string): void;
rename(from: string, to: string): void;
overwrite(path: string, content: Buffer | string): void;
read(path: string): Buffer | null;
exists(path: string): boolean;


:



image



, . , schematics – , , . schematics . , , β€˜β€“dry-run=false’:



schematics .:sample --name=test –dry-run=false


.



, , , , , , , β€” . , . schematics :



contentTemplate<T>(options: T)
pathTemplate<T>(options: T)
template<T>(options: T)


contentTemplate , pathTemplate , template . ? (Action) . :



  • template null, DeleteAction
  • , RenameAction
  • , OverwriteAction


template – . , , . :



image



, files . , – . files . , :



__name@dasherize__.service.ts


template , . , β€˜_’. , . name. name – @dasherize. , name .



. dasherize – , template . , :



__name@dasherize__.service.ts 


image



__name@dasherize__.service.spec.ts


image



, template , . if for:



<% if (a) { %>b<% } %>
<% for (let i = 0; i < value; i++) { %>1<% } %>


, template:



image



, . , template , apply. Source Rule, .



Source , Tree, base. Source , url, . Rule filter, , . spec , noop, Tree, spec . template, , , , strings, , dasherize classify. angular, . move, .



apply Source , . – chain, mergeWith, branchAndMerge. chain Rule Rule, rule. mergeWith Tree , apply. , rebase git-. . branchAndMerge Tree . , dry-run=false, – , .





schematics , . , , , . API , schematics – externalSchematic schematic. , – .



. , .



, , , component Angular. , . Angular . :



image



, externalSchematic, Angular. Angular, , schematics . Angular, staging area β€˜ts’ . kind β€˜c’.



, , Invalid source: undefined. , angular-CLI. , .



CLI



schematics Angular CLI, :



ng new my-project


:



npm link $PATH_TO_SCHEMATIC_PROJECT


schematics , , , npm . , CLI:



image



, , , , . , .





, , . , , , . , ?



, . , , typescript json , , , ast . , ast typescript, Angular. Angular schematics CLI, . , , , . , . :



  • ,
  • providers


schematics Angular:



findModuleFromOptions – Angular, .



buildRelativePath – .



addProviderToModule – providers . Angular addExportToModule, addImportToModule, addDeclarationToModule, addBootstrapToModule, etc.



, . , , ast :



image



:



image



, buildRelativePath addProviderToModule . addProviderToModule , InsertChange. API schematics – beginUpdate, insertLeft commitUpdate. , , .



, , :



image



, Angular, , , .





, schematics – , . , , Angular , . , @angular-devkit.



, github β€” https://github.com/KyKyPy3/schematics.



: KyKyPy3uK




All Articles