diff --git a/aio/content/examples/schematics-for-libraries/projects/my-lib/schematics/my-service/index.ts b/aio/content/examples/schematics-for-libraries/projects/my-lib/schematics/my-service/index.ts index a36cf29acd..a94cde3174 100644 --- a/aio/content/examples/schematics-for-libraries/projects/my-lib/schematics/my-service/index.ts +++ b/aio/content/examples/schematics-for-libraries/projects/my-lib/schematics/my-service/index.ts @@ -6,37 +6,53 @@ import { chain, mergeWith } from '@angular-devkit/schematics'; -import { strings, normalize, experimental } from '@angular-devkit/core'; +import { strings, normalize, virtualFs, workspaces } from '@angular-devkit/core'; // #enddocregion schematics-imports import { Schema as MyServiceSchema } from './schema'; // #enddocregion schema-imports +function createHost(tree: Tree): workspaces.WorkspaceHost { + return { + async readFile(path: string): Promise { + const data = tree.read(path); + if (!data) { + throw new SchematicsException('File not found.'); + } + return virtualFs.fileBufferToString(data); + }, + async writeFile(path: string, data: string): Promise { + return tree.overwrite(path, data); + }, + async isDirectory(path: string): Promise { + return !tree.exists(path) && tree.getDir(path).subfiles.length > 0; + }, + async isFile(path: string): Promise { + return tree.exists(path); + }, + }; +} + export function myService(options: MyServiceSchema): Rule { - return (tree: Tree) => { - const workspaceConfig = tree.read('/angular.json'); - if (!workspaceConfig) { - throw new SchematicsException('Could not find Angular workspace configuration'); - } + return async (tree: Tree) => { + const host = createHost(tree); + const { workspace } = await workspaces.readWorkspace('/', host); - // convert workspace to string - const workspaceContent = workspaceConfig.toString(); - - // parse workspace string into JSON object - const workspace: experimental.workspace.WorkspaceSchema = JSON.parse(workspaceContent); // #enddocregion workspace + +// #docregion project-info // #docregion project-fallback if (!options.project) { - options.project = workspace.defaultProject; + options.project = workspace.extensions.defaultProject; } // #enddocregion project-fallback -// #docregion project-info - const projectName = options.project as string; + const project = workspace.projects.get(options.project); + if (!project) { + throw new SchematicsException(`Invalid project name: ${options.project}`); + } - const project = workspace.projects[projectName]; - - const projectType = project.projectType === 'application' ? 'app' : 'lib'; + const projectType = project.extensions.projectType === 'application' ? 'app' : 'lib'; // #enddocregion project-info // #docregion path diff --git a/aio/content/guide/schematics-for-libraries.md b/aio/content/guide/schematics-for-libraries.md index e2520c5520..727bb9c54a 100644 --- a/aio/content/guide/schematics-for-libraries.md +++ b/aio/content/guide/schematics-for-libraries.md @@ -218,7 +218,8 @@ The `Tree` methods give you access to the complete file tree in your workspace, ### Get the project configuration -1. To determine the destination project, use the `Tree.read()` method to read the contents of the workspace configuration file, `angular.json`, at the root of the workspace. +1. To determine the destination project, use the `workspaces.readWorkspace` method to read the contents of the workspace configuration file, `angular.json`. + To use `workspaces.readWorkspace` you need to create a `workspaces.WorkspaceHost` from the `Tree`. Add the following code to your factory function. @@ -226,9 +227,7 @@ The `Tree` methods give you access to the complete file tree in your workspace, * Be sure to check that the context exists and throw the appropriate error. - * After reading the contents into a string, parse the configuration into a JSON object, typed to the `WorkspaceSchema`. - -1. The `WorkspaceSchema` contains all the properties of the workspace configuration, including a `defaultProject` value for determining which project to use if not provided. +1. The `WorkspaceDefinition`, `extensions` property includes a `defaultProject` value for determining which project to use if not provided. We will use that value as a fallback, if no project is explicitly specified in the `ng generate` command.