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