@@ -2,12 +2,14 @@ import { NodeJsSyncHost } from '@angular-devkit/core/node';
22import {
33 createWorkspaceHost ,
44 readWorkspace as devKitReadWorkspace ,
5+ writeWorkspace as devKitWriteWorkspace ,
56 ProjectDefinition ,
67 TargetDefinition ,
78 WorkspaceDefinition ,
89} from '@angular-devkit/core/src/workspace' ;
910import {
1011 BadRequestException ,
12+ HttpException ,
1113 Injectable ,
1214 InternalServerErrorException ,
1315 Logger ,
@@ -16,73 +18,143 @@ import {
1618
1719import { SessionService } from '../session/session.service' ;
1820
21+ import { ProjectDto , UpdateProjectDto } from './dto' ;
1922import {
2023 ANGULAR_WORKSPACE_NOT_FOUND_EXCEPTION ,
2124 BAD_PATH_EXCEPTION ,
2225 NOT_ANGULAR_WORKSPACE_EXCEPTION ,
2326} from './entities' ;
2427
25- export const ng = 'npx ng new ' ;
28+ const ANGULAR_JSON = '/angular.json ' ;
2629
2730@Injectable ( )
2831export class WorkspaceService {
2932 private readonly logger = new Logger ( WorkspaceService . name ) ;
30- readonly ng = ng ;
33+ private readonly nodeJsSyncHost = new NodeJsSyncHost ( ) ;
34+
3135 constructor ( private sessionService : SessionService ) { }
3236
33- async readWorkspace ( path : string ) : Promise < WorkspaceDefinition > {
37+ async isAngularWorkspace ( path : string ) : Promise < boolean > {
3438 try {
35- return (
36- await devKitReadWorkspace (
37- path ,
38- createWorkspaceHost ( new NodeJsSyncHost ( ) )
39- )
40- ) . workspace ;
39+ return ! ! ( await devKitReadWorkspace (
40+ path ,
41+ createWorkspaceHost ( this . nodeJsSyncHost )
42+ ) ) ;
43+ } catch {
44+ return false ;
45+ }
46+ }
47+
48+ async connect ( path : string ) : Promise < void > {
49+ try {
50+ await devKitReadWorkspace ( path , createWorkspaceHost ( this . nodeJsSyncHost ) ) ;
51+ this . sessionService . setCwd ( path ) ;
4152 // eslint-disable-next-line @typescript-eslint/no-explicit-any
4253 } catch ( err : any ) {
43- const errorMessage = err [ 'message' ] ;
44- switch ( errorMessage ) {
45- case BAD_PATH_EXCEPTION :
46- throw new BadRequestException ( BAD_PATH_EXCEPTION ) ;
47- case NOT_ANGULAR_WORKSPACE_EXCEPTION :
48- throw new NotFoundException ( ANGULAR_WORKSPACE_NOT_FOUND_EXCEPTION ) ;
49- default :
50- this . logger . error ( errorMessage ) ;
51- throw new InternalServerErrorException ( err ) ;
52- }
54+ throw this . handleReadWorkspaceException ( err ) ;
5355 }
5456 }
5557
5658 async readWorkspaceProjectNames ( ) : Promise < string [ ] > {
57- const workspace = await this . readWorkspace ( this . sessionService . cwd ) ;
59+ const workspaceDefinition = await this . readWorkspaceDefinition ( ) ;
5860
59- return Array . from ( workspace . projects . keys ( ) ) ;
61+ return Array . from ( workspaceDefinition . projects . keys ( ) ) ;
6062 }
6163
62- async readWorkspaceProject (
63- name : string
64- ) : Promise < ProjectDefinition | undefined > {
65- const workspace = await this . readWorkspace ( this . sessionService . cwd ) ;
64+ async readWorkspaceProject ( name : string ) : Promise < ProjectDto > {
65+ return new ProjectDto ( await this . readProjectDefinition ( name ) ) ;
66+ }
67+
68+ async updateWorkspaceProject (
69+ name : string ,
70+ updateProjectDto : UpdateProjectDto
71+ ) : Promise < ProjectDto > {
72+ const workspaceDefinition = await this . readWorkspaceDefinition ( ) ;
73+ const projectDefinition = workspaceDefinition . projects . get ( name ) ;
74+
75+ if ( ! projectDefinition ) {
76+ throw new NotFoundException ( `Project ${ name } not found` ) ;
77+ }
78+
79+ Object . assign ( projectDefinition , updateProjectDto ) ;
80+ await this . writeWorkspace ( workspaceDefinition ) ;
6681
67- return workspace ?. projects ?. get ( name ) ;
82+ return this . readWorkspaceProject ( name ) ;
6883 }
6984
7085 async readWorkspaceProjectTargetNames ( name : string ) : Promise < string [ ] > {
71- const project = await this . readWorkspaceProject ( name ) ;
86+ const projectDefinition = await this . readProjectDefinition ( name ) ;
7287
73- if ( ! project ?. targets ?. size ) {
88+ if ( ! projectDefinition ?. targets ?. size ) {
7489 return [ ] ;
7590 }
7691
77- return Array . from ( project . targets . keys ( ) ) ;
92+ return Array . from ( projectDefinition . targets . keys ( ) ) ;
7893 }
7994
8095 async readWorkspaceProjectTarget (
8196 projectName : string ,
8297 targetName : string
83- ) : Promise < TargetDefinition | undefined > {
84- const project = await this . readWorkspaceProject ( projectName ) ;
98+ ) : Promise < TargetDefinition > {
99+ const projectDefinition = await this . readProjectDefinition ( projectName ) ;
100+ const targetDefinition = projectDefinition . targets . get ( targetName ) ;
101+
102+ if ( ! targetDefinition ) {
103+ throw new NotFoundException ( `Target ${ targetName } not found` ) ;
104+ }
105+
106+ return targetDefinition ;
107+ }
85108
86- return project ?. targets . get ( targetName ) ;
109+ private async readWorkspaceDefinition ( ) : Promise < WorkspaceDefinition > {
110+ try {
111+ return (
112+ await devKitReadWorkspace (
113+ this . sessionService . cwd ,
114+ createWorkspaceHost ( this . nodeJsSyncHost )
115+ )
116+ ) . workspace ;
117+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
118+ } catch ( err : any ) {
119+ throw this . handleReadWorkspaceException ( err ) ;
120+ }
121+ }
122+
123+ private async writeWorkspace (
124+ workspaceDefinition : WorkspaceDefinition
125+ ) : Promise < void > {
126+ await devKitWriteWorkspace (
127+ workspaceDefinition ,
128+ createWorkspaceHost ( this . nodeJsSyncHost ) ,
129+ `${ this . sessionService . cwd } ${ ANGULAR_JSON } `
130+ ) ;
131+ }
132+
133+ private async readProjectDefinition (
134+ name : string
135+ ) : Promise < ProjectDefinition > {
136+ const workspaceDefinition = await this . readWorkspaceDefinition ( ) ;
137+ const projectDefinition = workspaceDefinition . projects . get ( name ) ;
138+
139+ if ( ! projectDefinition ) {
140+ throw new NotFoundException ( `Project ${ name } not found` ) ;
141+ }
142+
143+ return projectDefinition ;
144+ }
145+
146+ private handleReadWorkspaceException ( err : {
147+ message : string ;
148+ } ) : HttpException {
149+ const errorMessage = err [ 'message' ] ;
150+ switch ( errorMessage ) {
151+ case BAD_PATH_EXCEPTION :
152+ return new BadRequestException ( BAD_PATH_EXCEPTION ) ;
153+ case NOT_ANGULAR_WORKSPACE_EXCEPTION :
154+ return new NotFoundException ( ANGULAR_WORKSPACE_NOT_FOUND_EXCEPTION ) ;
155+ default :
156+ this . logger . error ( errorMessage ) ;
157+ return new InternalServerErrorException ( err ) ;
158+ }
87159 }
88160}
0 commit comments