Actualice su CLI

Ahora, muchos frameworks tienen CLI (Command Line Interface) en su arsenal y angular no es una excepción. Encontré las utilidades CLI por primera vez cuando probé EmberJS, y luego me pareció una herramienta muy conveniente que me ahorró mucho tiempo. Pero, desafortunadamente, las utilidades CLI listas para usar solo son adecuadas para proyectos domésticos simples.



Usarlos en grandes proyectos de producción sin terminar con un archivo es casi imposible. Por ello, al inicio de nuestro proyecto, nos vimos obligados a abandonar su uso debido a las peculiaridades de nuestra arquitectura. Durante casi un año, miré periódicamente hacia la CLI de Angular en términos de su desarrollo y me molestó que no pudiéramos usarla, ya que facilitaría mucho la entrada al proyecto. Pero en un momento, el equipo de Angular lanzó @ angular-devkit, que contiene un conjunto de paquetes usados ​​por la utilidad cli, y esto nos dio la oportunidad de personalizar la CLI de Angular para nuestras necesidades. El repositorio contiene actualmente alrededor de una docena de paquetes, pero en este artículo consideraremos solo los relacionados con esquemas.



imagen



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). :



imagen



- , 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 . , :



imagen



, npm , src — collection.json, index.ts index_spec.ts.



collection.json schematics . . :



imagen



schematics :



imagen



, schematics, . :



  • schema – json , , .
  • factory — , .
  • description — , , angular-CLI.
  • hidden — CLI- .
  • aliases — .
  • extends — schematic , . , , .


factory description , .



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



index.ts, :



imagen



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



  • base –
  • staging area – , base


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



:



imagen



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



, :



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


imagen



CLI , . , ‘.’.



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



imagen



schema.json:



imagen



. , :



imagen



imagen



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



, :



imagen



, , , , 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;


:



imagen



, . , 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 – . , , . :



imagen



, files . , – . files . , :



__name@dasherize__.service.ts


template , . , ‘_’. , . name. name – @dasherize. , name .



. dasherize – , template . , :



__name@dasherize__.service.ts 


imagen



__name@dasherize__.service.spec.ts


imagen



, template , . if for:



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


, template:



imagen



, . , 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 . :



imagen



, 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:



imagen



, , , , . , .





, , . , , , . , ?



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



  • ,
  • providers


schematics Angular:



findModuleFromOptions – Angular, .



buildRelativePath – .



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



, . , , ast :



imagen



:



imagen



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



, , :



imagen



, Angular, , , .





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



, github — https://github.com/KyKyPy3/schematics.



: KyKyPy3uK




All Articles