在collection.json 同级目录,新建 migration.json 文件,并写入以下内容:
{
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"migration002": {
"version": "0.0.2",
"description": "更新angular-schematics-tutorial到0.0.2版本时执行的更新",
"factory": "./ng-
update/index#
update"
}
}
}
在 package.json 中声明 ug-update 配置
在 package.json 中,添加以下项目:
"ng-update": {
"migrations": "./schematics/migration.json"
},
其作用,就是在执行 ng update 时,能够找到对应的配置文件
在projects/my-lib/schematics/ 目录下新建index.ts
import { Rule, Tree, SchematicContext, SchematicsException } from '@angular-devkit/schematics';
import { buildDefaultPath } from '@schematics/angular/utility/project';
import * as ts from 'typescript';
export function update(): Rule {
return (_tree: Tree, _context: SchematicContext) => {
// 解析angular项目
const workspaceConfigBuffer = _tree.read('angular.json');
if (!workspaceConfigBuffer) {
throw new SchematicsException('Not an Angular CLI workspace');
}
return tree.create('index.md','我更新了')
}
}
完结撒花!!