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. Linux08 文件系统

    对于磁盘等各类存储设备中所有的数据都以0和1的概念,但对于用户来说,0和1是没有任何意义的,这时候就需要一种类似于“翻译”的机制存在于用户和磁盘之间,Linux中采用的是文件系统+虚拟文件系统(Vir ...

  2. Governing sand(主席树/贪心)(2019牛客暑期多校训练营(第七场))

    示例:输入:25 1 11 10 125 1 23 2 3输出:12 题意:n种树,第i种树有P[i]颗,砍掉每颗树的代价是C[i], 高度是H[i].需要用最小的花费砍掉一些树,让最高的树超过一半. ...

  3. SpringCloud入门使用

    目的: 1.springcloud简介 入门案例 2.注册中心eureka springcloud简介 推荐一个springcloud讲解详细的博客:https://blog.csdn.net/qq3 ...

  4. JavaNetty

    Netty的简单使用: import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import io.netty.ch ...

  5. 【洛谷 P5341】 [TJOI2019]甲苯先生和大中锋的字符串(后缀自动机)

    题目链接 建出\(sam\),求出parent tree上每个点的\(endpos\)集合大小. 如果等于\(k\),说明到达这个点的都可以.给\((len[fa(i)],len[i]]\)的\(cn ...

  6. 深入理解JVM(二)--对象的创建

    Java是一门面向对象的语言,在Java程序运行的过程中,无时无刻都会有对象被创建出来,在程序语言中,创建对象(例如克隆,反序列化)通常仅仅是一个new关键字,但是在虚拟机中是怎样的呢?本文主要了解一 ...

  7. python 内建属性

    在python中创建一个类,它不仅有我们自定义的属性和方法,还有与生俱来的一些属性和方法,我们叫它内建属性. 下面是类常用内建属性列表. 常用专有属性 说明 触发方式 __init__ 构造初始化函数 ...

  8. Delphi-RzDbgrid-绘制表格式设置某行颜色或者其他格式-以及隔行换色的属性

    参考文章:https://www.cnblogs.com/OSKnown/p/8568740.html 在DbgridEh和原生的Dbgrid直接在DrawColumnCell事件中写重绘代码就好了, ...

  9. redis哨兵配置 总结

    本文内容涵盖 windows下单机部署redis多实例(docker.linux下的配置也可参考本文) redis主从配置 redis哨兵配置 以spring boot redis demo下一个存a ...

  10. Linux路由:CentOS6的多种玩法

    将一台Linux主机作路由器使用,这本是件很容易的事情,利用Linux主机强大的网络功能,很轻松就实现了.这里在虚拟机环境下设定一台CentOS主机通过另一台CentOS主机路由接入Internet网 ...