The brackets at the end of each step indicate the alias’s or IntelliJ Live Templates to use. You can find the template definitions at mraible/idea-live-templates.
-
Install Angular CLI:
npm install -g @angular/cli -
Create a new Angular project:
ng new my-cool-app --routing -
Add Okta for authentication:
ng add @oktadev/schematics
-
Install the Schematics CLI
npm i -g @angular-devkit/schematics-cli@0.13.1
-
Run
schematicsto create a new blank projectschematics blank --name=my-component
-
Show
collection.json,index.ts, andindex_spec.ts -
Run
npm test
-
Modify
index.tsto create ahello.tsfiletree.create('hello.ts', 'console.log("Hello, World")'); -
Update
index_spec.tsto verify this file is createdexpect(tree.files).toEqual(['/hello.ts']);
-
Run
npm testto verify test passes -
Run your schematic
schematics .:my-component
-
Turn off debug mode and run it
schematics .:my-component --dry-run=false
-
Create a directory to hold your templates
mkdir -p src/my-component/files/src/app
-
Create an
app.component.tsfile insrc/my-component/files/src/app[schematics-app-ts]import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { name = '<%= name %>'; } -
Create an
app.component.htmlfile to read thenamevariable [schematics-app-html]<div style="text-align:center"> <h1> Hello, {{ name }} </h1> </div> <router-outlet></router-outlet> -
Create a
src/my-component/schema.jsonfile to define anameprompt [schematics-schema]{ "$schema": "http://json-schema.org/schema", "id": "SchematicsMyComponent", "title": "My Component Schema", "type": "object", "properties": { "name": { "type": "string", "description": "Your Name", "x-prompt": "What is your name?" } }, "required": ["name"] } -
Update
src/collection.jsonto reference this file in aschemaproperty{ "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json", "schematics": { "my-component": { "description": "A blank schematic.", "factory": "./my-component/index#myComponent", "schema": "./my-component/schema.json" } } } -
Install
@schematics/angularto allow getting a workspace and project in your schematicnpm i -D @schematics/angular@7.3.0
-
Modify
index.tsto get the generated project’s path, and copy templates [schematics-setup,schematics-move]
-
Update
index_spec.tsto create a workspace, project, and pass in options [schematics-test] -
Run
npm testand rejoice in your victory! -
Run your schematics with Angular CLI
ng new my-test-app --routing --style css cd my-test-app npm link ../my-component
-
Run your schematic with the
ng gcommandng g my-component:my-component
-
Fix
already existserror [schematics-move-fix] -
Run
npm run buildand try again
-
Modify
.npmignoreso it doesn’t exclude your template files. -
Publish using
npm publish -
You can use
npm unpublishif you make a mistake (within the first 72 hours)
-
Add
ng-addtocollection.json[schematics-ng-add]"ng-add": { "factory": "./ng-add/index", "description": "Add schematic", "schema": "./my-component/schema.json" } -
Create
src/ng-add/index.tsto invoke themy-componentschematic [schematics-ng-add-index] -
Run
npm run build -
Use
ng add my-componentinmy-test-app -
Fini!