升级您的CLI

如今,许多框架在其工具库中都具有CLI(命令行界面),而且也不例外。我在尝试EmberJS时首先遇到CLI实用程序,然后在我看来这是一个非常方便的工具,节省了很多时间。不幸的是,现成的CLI实用程序仅适用于简单的家庭项目。



在大型生产项目中使用它们而不完成文件几乎是不可能的。因此,在我们的项目开始时,由于我们的体系结构的特殊性,我们被迫放弃使用它。在将近一年的时间里,我定期对Angular CLI进行开发,并感到沮丧,因为我们无法使用它,因为它会使进入项目变得更加容易。但是有一次,Angular团队发布了@ angular-devkit,其中包含cli实用程序使用的一组软件包,这使我们有机会根据自己的需要自定义Angular CLI。该存储库当前包含大约十二个软件包,但是在本文中,我们将仅考虑与原理图相关的那些软件包。



图片



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



图片



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



图片



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



collection.json schematics . . :



图片



schematics :



图片



, schematics, . :



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


factory description , .



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



index.ts, :



图片



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



  • base –
  • staging area – , base


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



:



图片



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



, :



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


图片



CLI , . , ‘.’.



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



图片



schema.json:



图片



. , :



图片



图片



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



, :



图片



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


:



图片



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



图片



, files . , – . files . , :



__name@dasherize__.service.ts


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



. dasherize – , template . , :



__name@dasherize__.service.ts 


图片



__name@dasherize__.service.spec.ts


图片



, template , . if for:



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


, template:



图片



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



图片



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



图片



, , , , . , .





, , . , , , . , ?



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



  • ,
  • providers


schematics Angular:



findModuleFromOptions – Angular, .



buildRelativePath – .



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



, . , , ast :



图片



:



图片



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



, , :



图片



, Angular, , , .





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



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



: KyKyPy3uK




All Articles