Jetzt haben viele Frameworks CLI (Command Line Interface) in ihrem Arsenal, und Angular ist keine Ausnahme. Ich bin zum ersten Mal auf CLI-Dienstprogramme gestoĂen, als ich EmberJS ausprobiert habe, und dann schien es mir ein sehr praktisches Tool zu sein, das mir viel Zeit gespart hat. Leider sind sofort einsatzbereite CLI-Dienstprogramme nur fĂŒr einfache Heimprojekte geeignet.
Die Verwendung in groĂen Produktionsprojekten ohne Fertigstellung mit einer Datei ist nahezu unmöglich. Daher waren wir zu Beginn unseres Projekts aufgrund der Besonderheiten unserer Architektur gezwungen, seine Verwendung aufzugeben. Fast ein Jahr lang habe ich mich regelmĂ€Ăig mit der Angular CLI in Bezug auf ihre Entwicklung befasst und war verĂ€rgert, dass wir sie nicht verwenden konnten, da dies den Einstieg in das Projekt erheblich erleichtern wĂŒrde. Zu einem bestimmten Zeitpunkt veröffentlichte das Angular-Team jedoch das @ angle-devkit, das eine Reihe von Paketen enthĂ€lt, die vom Dienstprogramm cli verwendet werden. Dies gab uns die Möglichkeit, die Angular-CLI an unsere Anforderungen anzupassen. Das Repository enthĂ€lt derzeit etwa ein Dutzend Pakete. In diesem Artikel werden jedoch nur diejenigen berĂŒcksichtigt, die sich auf SchaltplĂ€ne beziehen.

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.