@@ -10,60 +10,109 @@ import { json, workspaces } from '@angular-devkit/core';
1010import { Rule , Tree , noop } from '@angular-devkit/schematics' ;
1111import { ProjectType } from './workspace-models' ;
1212
13- function createHost ( tree : Tree ) : workspaces . WorkspaceHost {
14- return {
15- async readFile ( path : string ) : Promise < string > {
16- return tree . readText ( path ) ;
17- } ,
18- async writeFile ( path : string , data : string ) : Promise < void > {
19- return tree . overwrite ( path , data ) ;
20- } ,
21- async isDirectory ( path : string ) : Promise < boolean > {
22- // approximate a directory check
23- return ! tree . exists ( path ) && tree . getDir ( path ) . subfiles . length > 0 ;
24- } ,
25- async isFile ( path : string ) : Promise < boolean > {
26- return tree . exists ( path ) ;
27- } ,
28- } ;
13+ const DEFAULT_WORKSPACE_PATH = '/angular.json' ;
14+
15+ // re-export the workspace definition types for convenience
16+ export type WorkspaceDefinition = workspaces . WorkspaceDefinition ;
17+ export type ProjectDefinition = workspaces . ProjectDefinition ;
18+ export type TargetDefinition = workspaces . TargetDefinition ;
19+
20+ /**
21+ * A {@link workspaces.WorkspaceHost} backed by a Schematics {@link Tree} instance.
22+ */
23+ class TreeWorkspaceHost implements workspaces . WorkspaceHost {
24+ constructor ( private readonly tree : Tree ) { }
25+
26+ async readFile ( path : string ) : Promise < string > {
27+ return this . tree . readText ( path ) ;
28+ }
29+
30+ async writeFile ( path : string , data : string ) : Promise < void > {
31+ if ( this . tree . exists ( path ) ) {
32+ this . tree . overwrite ( path , data ) ;
33+ } else {
34+ this . tree . create ( path , data ) ;
35+ }
36+ }
37+
38+ async isDirectory ( path : string ) : Promise < boolean > {
39+ // approximate a directory check
40+ return ! this . tree . exists ( path ) && this . tree . getDir ( path ) . subfiles . length > 0 ;
41+ }
42+
43+ async isFile ( path : string ) : Promise < boolean > {
44+ return this . tree . exists ( path ) ;
45+ }
2946}
3047
48+ /**
49+ * Updates the workspace file (`angular.json`) found within the root of the schematic's tree.
50+ * The workspace object model can be directly modified within the provided updater function
51+ * with changes being written to the workspace file after the updater function returns.
52+ * The spacing and overall layout of the file (including comments) will be maintained where
53+ * possible when updating the file.
54+ *
55+ * @param updater An update function that can be used to modify the object model for the
56+ * workspace. A {@link WorkspaceDefinition} is provided as the first argument to the function.
57+ */
3158export function updateWorkspace (
32- updater : ( workspace : workspaces . WorkspaceDefinition ) => void | Rule | PromiseLike < void | Rule > ,
33- ) : Rule ;
34- export function updateWorkspace ( workspace : workspaces . WorkspaceDefinition ) : Rule ;
35- export function updateWorkspace (
36- updaterOrWorkspace :
37- | workspaces . WorkspaceDefinition
38- | ( ( workspace : workspaces . WorkspaceDefinition ) => void | Rule | PromiseLike < void | Rule > ) ,
59+ updater : ( workspace : WorkspaceDefinition ) => void | Rule | PromiseLike < void | Rule > ,
3960) : Rule {
4061 return async ( tree : Tree ) => {
41- const host = createHost ( tree ) ;
62+ const host = new TreeWorkspaceHost ( tree ) ;
4263
43- if ( typeof updaterOrWorkspace === 'function' ) {
44- const { workspace } = await workspaces . readWorkspace ( '/' , host ) ;
64+ const { workspace } = await workspaces . readWorkspace ( DEFAULT_WORKSPACE_PATH , host ) ;
4565
46- const result = await updaterOrWorkspace ( workspace ) ;
66+ const result = await updater ( workspace ) ;
4767
48- await workspaces . writeWorkspace ( workspace , host ) ;
68+ await workspaces . writeWorkspace ( workspace , host ) ;
4969
50- return result || noop ;
51- } else {
52- await workspaces . writeWorkspace ( updaterOrWorkspace , host ) ;
53-
54- return noop ;
55- }
70+ return result || noop ;
5671 } ;
5772}
5873
59- export async function getWorkspace ( tree : Tree , path = '/' ) {
60- const host = createHost ( tree ) ;
74+ // TODO: This should be renamed `readWorkspace` once deep imports are restricted (already exported from `utility` with that name)
75+ /**
76+ * Reads a workspace file (`angular.json`) from the provided {@link Tree} instance.
77+ *
78+ * @param tree A schematics {@link Tree} instance used to access the workspace file.
79+ * @param path The path where a workspace file should be found. If a file is specified, the file
80+ * path will be used. If a directory is specified, the file `angular.json` will be used from
81+ * within the specified directory. Defaults to `/angular.json`.
82+ * @returns A {@link WorkspaceDefinition} representing the workspace found at the specified path.
83+ */
84+ export async function getWorkspace (
85+ tree : Tree ,
86+ path = DEFAULT_WORKSPACE_PATH ,
87+ ) : Promise < WorkspaceDefinition > {
88+ const host = new TreeWorkspaceHost ( tree ) ;
6189
6290 const { workspace } = await workspaces . readWorkspace ( path , host ) ;
6391
6492 return workspace ;
6593}
6694
95+ /**
96+ * Writes a workspace file (`angular.json`) to the provided {@link Tree} instance.
97+ * The spacing and overall layout of an exisitng file (including comments) will be maintained where
98+ * possible when writing the file.
99+ *
100+ * @param tree A schematics {@link Tree} instance used to access the workspace file.
101+ * @param workspace The {@link WorkspaceDefinition} to write.
102+ * @param path The path where a workspace file should be written. If a file is specified, the file
103+ * path will be used. If not provided, the definition's underlying file path stored during reading
104+ * will be used.
105+ */
106+ export async function writeWorkspace (
107+ tree : Tree ,
108+ workspace : WorkspaceDefinition ,
109+ path ?: string ,
110+ ) : Promise < void > {
111+ const host = new TreeWorkspaceHost ( tree ) ;
112+
113+ return workspaces . writeWorkspace ( workspace , host , path ) ;
114+ }
115+
67116/**
68117 * Build a default project path for generating.
69118 * @param project The project which will have its default path generated.
0 commit comments