ClassOptions 架构 - Angular 架构
ClassOptions Schema - Angular Schematics
在@angular-devkit/schematics readme 中,我对 ClassOptions 的作用感到困惑。
如果将代码添加到项目中,将显示错误,因为该文件不存在。
关于它的用途的想法+示例?
import { strings } from '@angular-devkit/core';
import {
Rule, SchematicContext, SchematicsException, Tree,
apply, branchAndMerge, mergeWith, template, url,
} from '@angular-devkit/schematics';
import { Schema as ClassOptions } from './schema';
export default function (options: ClassOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
if (!options.name) {
throw new SchematicsException('Option (name) is required.');
}
const templateSource = apply(
url('./files'),
[
template({
...strings,
...options,
}),
]
);
return branchAndMerge(mergeWith(templateSource));
};
}
示例中的schema
文件是用this function during a build process in angular-cli
. It's a pattern used in angular-cli
and the example won't work as it is. You can see a complete example of the source for a schematic like this in one of the official angular schematics like service生成的打字稿文件。
但是请记住,ClassOptions
只是一个为 Angular 示意图指定选项的对象。它可以来自任何地方,并且与原理图库解耦。它是 responsibility of the tooling 提供原理图库的选项。
这是一个使用 reference cli 的类似示例。
生成简单原理图:
schematics blank --name=test
将架构文件的路径添加到 collection.json
:
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"test": {
"description": "A blank schematic.",
"factory": "./test/index#test",
"schema": "./test/schema.json"
}
}
}
创建 schema.json
:
{
"$schema": "http://json-schema.org/schema",
"id": "MySchema",
"title": "My Schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"default": "cactus"
}
}
}
index.ts
中的工厂现在将从 schema.json
文件中获取默认选项。
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function test(_options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {
tree.create(_options.name || "hello", "world");
return tree;
};
}
运行 默认值来自 schema.json
:
的 cli
schematics .:test --dry-run=false
...
CREATE /cactus (5 bytes)
运行 具有用户定义选项的 cli:
schematics .:test --name=bean--dry-run=false
...
CREATE /bean (5 bytes)
在@angular-devkit/schematics readme 中,我对 ClassOptions 的作用感到困惑。
如果将代码添加到项目中,将显示错误,因为该文件不存在。
关于它的用途的想法+示例?
import { strings } from '@angular-devkit/core';
import {
Rule, SchematicContext, SchematicsException, Tree,
apply, branchAndMerge, mergeWith, template, url,
} from '@angular-devkit/schematics';
import { Schema as ClassOptions } from './schema';
export default function (options: ClassOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
if (!options.name) {
throw new SchematicsException('Option (name) is required.');
}
const templateSource = apply(
url('./files'),
[
template({
...strings,
...options,
}),
]
);
return branchAndMerge(mergeWith(templateSource));
};
}
示例中的schema
文件是用this function during a build process in angular-cli
. It's a pattern used in angular-cli
and the example won't work as it is. You can see a complete example of the source for a schematic like this in one of the official angular schematics like service生成的打字稿文件。
但是请记住,ClassOptions
只是一个为 Angular 示意图指定选项的对象。它可以来自任何地方,并且与原理图库解耦。它是 responsibility of the tooling 提供原理图库的选项。
这是一个使用 reference cli 的类似示例。
生成简单原理图:
schematics blank --name=test
将架构文件的路径添加到 collection.json
:
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"test": {
"description": "A blank schematic.",
"factory": "./test/index#test",
"schema": "./test/schema.json"
}
}
}
创建 schema.json
:
{
"$schema": "http://json-schema.org/schema",
"id": "MySchema",
"title": "My Schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"default": "cactus"
}
}
}
index.ts
中的工厂现在将从 schema.json
文件中获取默认选项。
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function test(_options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {
tree.create(_options.name || "hello", "world");
return tree;
};
}
运行 默认值来自 schema.json
:
schematics .:test --dry-run=false
...
CREATE /cactus (5 bytes)
运行 具有用户定义选项的 cli:
schematics .:test --name=bean--dry-run=false
...
CREATE /bean (5 bytes)