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']);
});
});

最新文章

  1. Titanium.App.Properties 对象
  2. 让JNLP应用程序从Firefox浏览器启动起来
  3. [Java] Java IO Files
  4. JDK1.5新特性随手记
  5. Java7新特性(一)Coin
  6. WinForm中嵌入WebBrowser,并且支持C#和JS方法的相互调用
  7. Linux系统迁移
  8. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
  9. C#之FTP上传下载(一)
  10. path和classpath的区别
  11. 列表(list) ----python
  12. buaaoo_second_improvement
  13. sessionStorage数组、对象的存储和读取
  14. angular.isElement()
  15. 【转】dos下 和 批处理中的 for 语句的基本用法
  16. MOSFET的半桥驱动电路设计要领详解
  17. too many open files(打开的文件过多)解决方法
  18. SQL DATE_FORMAT() 函数
  19. python type的用法
  20. 经典算法分析:n与lgn

热门文章

  1. Servlet知识点小结
  2. C程序设计语言练习 第二章
  3. laravel中一些非常常用的php artisan命令
  4. adb命令查看连接PC的移动设备
  5. spring boot使用@Aspect记录日志(请求参数,响应结果)
  6. Oracle scott解锁 以及连接数据库
  7. GC偏好
  8. 前端开发 ECMAScript-1概述
  9. number与string的转换
  10. Tomcat - 启动闪退