NX工作空间生成器在使用目录属性时出现异常



我创建了一个NXworkspace-generator来创建自定义设置的Angular库。

一切正常,我可以使用libraryGenerator()方法生成库。

然而,如果我传递--directory参数,我得到以下异常:

Cannot find configuration for 'myLib' in /workspace.json.
The terminal process "/bin/zsh '-c', 
'npx nx workspace-generator ng-library myLib --teamName=myTeam --directory=parentDir --dry-run'" 
terminated with exit code: 1.

知道为什么传递目录属性会触发这样的错误吗?我可以移动库到parentDir中,但我想避免它,因为在给定的libraryGenerator()方法中指定目录似乎是合法的方式。我使用"@nrwl/angular": "13.1.5",我不能(目前)更新到一个较新的版本。

我的<<p> strong> index.ts 文件:
import {
Tree,
formatFiles,
installPackagesTask,
readProjectConfiguration,
generateFiles,
joinPathFragments,
updateProjectConfiguration,
getProjects,
ProjectConfiguration
} from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/angular/generators';
import { LibSchemaModel } from './lib-schema.model';
export default async function (tree: Tree, schema: LibSchemaModel) {
const libraryName = schema.name;
await libraryGenerator(tree, {
name: libraryName,
importPath: `@${schema.importPathPrefix}/${libraryName}`,
publishable: schema.libraryType === 'publishable',
buildable: schema.libraryType === 'buildable',
directory: schema.directory // <- If I comment this line, all works fine
});
...
}

Mygenerator schema.json:

{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "ng-library",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the library.",
"x-prompt": "Which name do you want to use for the library?",
"$default": {
"$source": "argv",
"index": 0
}
},
"teamName": {
"type": "string",
"description": "The team name.",
"x-prompt": "Which is the library's team name?"
},
"importPathPrefix": {
"type": "string",
"description": "The prefix used for the library importPath."
},
"directory": {
"type": "string",
"description": "The directory that will contain the new library."
},
"libraryType": {
"description": "Library type: `Publishable` to publish it outside the workspace or `Buildable` to use it internally in the workspace.",
"type": "string",
"default": "publishable",
"enum": [
"publishable",
"buildable"
],
"x-prompt": {
"message": "Which library type do you want to generate?",
"type": "list",
"items": [
{
"value": "publishable",
"label": "Publishable"
},
{
"value": "buildable",
"label": "Buildable"
}
]
}
},
"installDeps": {
"type": "boolean",
"description": "Install dependencies",
"default": true
}
},
"required": ["name", "teamName", "teamEmail"]
}

我假设您在使用libraryGenerator()创建库后使用readProjectConfiguration()读取库数据。

readProjectConfiguration()接受库/项目名作为第二个参数。但是库/项目名不等于schema.name。如果设置了目录,那么你的库/项目名称等于${schema.directory}-${schema.name}

所以在你的例子中,正确的代码应该是这样的:
export default async function (tree: Tree, schema: LibSchemaModel) {
const libraryName = schema.directory ? `${schema.directory}-${schema.name}` : schema.name;
await libraryGenerator(tree, {
name: schema.name,
importPath: `@${schema.importPathPrefix}/${libraryName}`,
publishable: schema.libraryType === 'publishable',
buildable: schema.libraryType === 'buildable',
directory: schema.directory
});
// When you read the data use libraryName instead of schema.name
const libConfig = readProjectConfiguration(tree, libraryName);
// ... do your other stuff
}

最新更新