Tingkatkan CLI Anda

Sekarang banyak kerangka kerja memiliki CLI (Command Line Interface) di gudang senjata mereka, dan angular tidak terkecuali. Saya pertama kali menemukan utilitas CLI ketika saya mencoba EmberJS, dan kemudian bagi saya itu alat yang sangat nyaman yang menghemat banyak waktu saya. Namun, sayangnya, utilitas CLI di luar kotak hanya cocok untuk proyek rumah sederhana.



Hampir tidak mungkin menggunakannya dalam proyek produksi besar tanpa menyelesaikan file. Oleh karena itu, pada awal proyek kami, kami terpaksa meninggalkan penggunaannya karena kekhasan arsitektur kami. Selama hampir setahun, saya secara berkala melihat ke arah CLI Angular dalam hal pengembangannya dan kecewa karena kami tidak dapat menggunakannya, karena itu akan membuat memasuki proyek jauh lebih mudah. Tetapi pada satu titik, tim Angular merilis @ angular-devkit, yang berisi sekumpulan paket yang digunakan oleh utilitas cli, dan ini memberi kami kesempatan untuk menyesuaikan CLI Angular untuk kebutuhan kami. Repositori saat ini berisi sekitar selusin paket, tetapi dalam artikel ini kami hanya akan mempertimbangkan yang terkait dengan skema.



gambar



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



gambar



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



gambar



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



collection.json schematics . . :



gambar



schematics :



gambar



, schematics, . :



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


factory description , .



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



index.ts, :



gambar



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



  • base –
  • staging area – , base


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



:



gambar



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



, :



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


gambar



CLI , . , β€˜.’.



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



gambar



schema.json:



gambar



. , :



gambar



gambar



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



, :



gambar



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


:



gambar



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



gambar



, files . , – . files . , :



__name@dasherize__.service.ts


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



. dasherize – , template . , :



__name@dasherize__.service.ts 


gambar



__name@dasherize__.service.spec.ts


gambar



, template , . if for:



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


, template:



gambar



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



gambar



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



gambar



, , , , . , .





, , . , , , . , ?



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



  • ,
  • providers


schematics Angular:



findModuleFromOptions – Angular, .



buildRelativePath – .



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



, . , , ast :



gambar



:



gambar



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



, , :



gambar



, Angular, , , .





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



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



: KyKyPy3uK




All Articles