1. Install schematics cli:

npm i -g @angular-devkit/schematics-cli@latest

2. Then run schematics to create a new blank project:

schematics blank --name=my-component

It creates folder with number of files for you.

CREATE /my-component/README.md (639 bytes)
CREATE /my-component/package.json (539 bytes)
CREATE /my-component/tsconfig.json (656 bytes)
CREATE /my-component/.gitignore (191 bytes)
CREATE /my-component/.npmignore (64 bytes)
CREATE /my-component/src/collection.json (231 bytes)
CREATE /my-component/src/my-component/index.ts (318 bytes)
CREATE /my-component/src/my-component/index_spec.ts (474 bytes)

3. One important thing to do now, open .npmignore, remove line:

*.ts

It is because when you do `npm pack` to publish your schematics, it will remove all ts files, which is not what we want.

4. src/collection.json:

{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"my-component": {
"description": "A blank schematic.",
"factory": "./my-component/index#myComponent"
}
}
}

The highlighted part, is the name of your schematics. Run the following command from the my-component directory.

schematics .:my-component

By default schematics run in staging mode, which means it won't generate any files for you. Instaed you can run:

schematics .:my-component --dry-run=false

5. Let’s do something slightly more interesting than making sure it runs and create a hello.ts file. Modify my-component/index.ts to have a tree.create() command.

import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

export function myComponent(_options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {
tree.create('hello.ts', 'console.log("Hello, World")');
return tree;
};
}
describe('my-component', () => {
it('works', () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = runner.runSchematic('my-component', {}, Tree.empty()); expect(tree.files).toEqual(['/hello.ts']);
});
});

[Schematics] 0. Schematics "Hello World"的更多相关文章

  1. Angular Schematics 三部曲之 Add

    前言 因工作繁忙,差不多有三个月没有写过技术文章了,自八月份第一次编写 schematics 以来,我一直打算分享关于 schematics 的编写技巧,无奈还是拖到了年底. Angular Sche ...

  2. 在库中使用schematics——ng add与ng update

    起步 创建一个angular库 ng new demo --create-application=false ng g library my-lib 可见如下目录结构 ├── node_modules ...

  3. ng 服务端渲染

    官网文档 教学视屏 ng-toolkit @ng-toolkit/universal 注意: 使用 npm和yarn安装项目依赖,不要使用cnpm ng add @ng-toolkit/univers ...

  4. 试用 Angular v6 的 Ivy compiler

    “Ivy” 是 Angular v6 的新一代渲染器.从 v6.0.0-beta.1 开始,Ivy 已经作为体验 API 发布. 作为下一代的 Angular 的视图引擎,重点在于彻底缩减代码尺寸并增 ...

  5. Vivado Design Suite用户指南之约束的使用第二部分(约束方法论)

    Constraints Methodology(约束方法论) 关于约束方法论 设计约束定义了编译流程必须满足的要求,以使设计在板上起作用. 并非所有步骤都使用所有约束在编译流程中. 例如,物理约束仅在 ...

  6. Angular CLI: 1.6.7 入门

    当你使用npm或者yarn也安装不了angular-cli时,请使用淘宝镜像. Step1 npm i -g cnpm --registry=https://registry.npm.taobao.o ...

  7. 为什么angular library的build不能将assets静态资源打包进去(转)

    Versions Angular CLI: 6.0.7 Node: 9.3.0 OS: darwin x64 Angular: 6.0.3 ... animations, common, compil ...

  8. Angular7.1.4+Typescript3.1框架学习(二)

    接着第一部分,这篇文章就 Angular cli进行介绍总结: 1. ng g:列出当前命令 ng g 需在angular工程文件夹下执行: C:\Users\zb\angulardemo\heroe ...

  9. 从 10.x 到 ArcGIS Pro 的 Python 迁移

    与 ArcGIS Pro 结合使用 Python 的方式与包括 ArcGIS Desktop.ArcGIS Server 以及 ArcGIS Engine 在内的其他 ArcGIS 产品不同. 地理处 ...

随机推荐

  1. golang - 映射 ini 配置文件

    使用:setting.AppSetting.PageSize 包:go get github.com/go-ini/ini

  2. Modelsim——显示状态机名称的方法

    方法在本人博客<状态机的Verilog写法>已经写明,为了方便查看,特意拎出来. 方法1: Testbench 设计文件含有状态机时,对应的仿真文件testbench里增加一段参数转ASC ...

  3. SAS学习笔记58 单元格格式化设计

    单元格行_row_ 对于行单元格,主要就通过_row_这么一个自动变量的方式,来对单元格所有行进行格式化设计 例如,对性别为“男”的单元格所在行颜色设定为红色: 单元格列_col_ 将_row_改成_ ...

  4. robotframework_javaScript定位

    整理笔记才发现,只有在rebotframework才用过js定位,那么如果有小伙伴在使用js遇到问题,给我留言吧 通过Id定位   name定位 通过标签名查找 HTML 元素 本例查找 id=&qu ...

  5. java之hibernate之基于主键的双向一对一关联映射

    这篇 基于主键的双向一对一关联映射 1.依然考察人和身份证的一对一关系,如果采用主键关联,那么其表结构为: 2.类结构 Person.java public class Person implemen ...

  6. java之初识hibernate

    1. 使用jdbc进行数据库操作:获取数据库连接,编写sql语句,执行sql操作,关闭连接. 比如:每次创建连接,释放资源----使的执行效率降低: 解决方案:连接池. 编写sql语句动作----简单 ...

  7. IIS配置文件的XML格式不正确 applicationHost.config崩溃

    错误提示如图: 检查C:\Windows\System32\inetsrv\config目录下的applicationHost.config文件,备份一份. 可使用IIS提供的AppCmd.exe的r ...

  8. 【转载】C#中string类使用Remove方法来移除指定位置的字符

    在C#的字符串操作过程中,有时候需要将字符串中指定位置的字符移除,此时就可能使用到字符串类string类中的Remove方法,此方法允许指定移除开始的开始的索引位置,以及移除的长度信息等,共有2个重载 ...

  9. React 中的 定义组件的 两种方式

    React 中创建 Components 的方式有两种:Function and Class 定义一个组件最简单的方法就是写一个 JavaScript 函数 function Welcome(prop ...

  10. 0001-代码仓库-mvn

    暂缺 基本介绍 web管理 ifsvnadmin