33 * Licensed under the MIT License. See LICENSE file in the project root for license information.
44 *-----------------------------------------------------------------------------------------------*/
55
6- import * as cp from 'child_process' ;
76import * as fs from 'fs-extra' ;
8- import { dump } from 'js-yaml' ;
97import * as path from 'path' ;
10- import { Disposable , Uri , window , workspace } from 'vscode' ;
8+ import { Disposable , Uri , workspace } from 'vscode' ;
119import { CommandText } from '../base/command' ;
1210import { CliChannel } from '../cli' ;
1311import { DeploymentConfig } from '../k8s/deploymentConfig' ;
14- import { OdoImpl } from '../odo' ;
15- import { CliExitData } from '../util/childProcessUtil' ;
16- import { VsCommandError } from '../vscommand' ;
17- import { ServerlessCommand , Utils } from './commands' ;
12+ import { Utils } from './commands' ;
1813import { Functions } from './functions' ;
1914import { DeployedFunction , FunctionContent , FunctionObject , FunctionStatus } from './types' ;
20- import { ServerlessFunctionView } from './view' ;
2115
22- export interface ServerlessFunction extends Disposable {
23- getLocalFunctions ( ) : Promise < FunctionObject [ ] > ;
24- createFunction ( language : string , template : string , location : string , image : string ) : Promise < CliExitData > ;
16+ export interface View {
17+ refresh ( ) : void ;
2518}
2619
27- export class ServerlessFunctionImpl implements ServerlessFunction {
28- private static instance : ServerlessFunction = new ServerlessFunctionImpl ( ) ;
20+ export class ServerlessFunctionModel implements Disposable {
2921
3022 private watchers : fs . FSWatcher [ ] = [ ] ;
3123 private workspaceWatcher : Disposable ;
24+ private view : View ;
3225
33- public static get Instance ( ) : ServerlessFunction {
34- return ServerlessFunctionImpl . instance ;
35- }
36-
37- private constructor ( ) {
26+ public constructor ( view : View ) {
27+ this . view = view ;
3828 this . addWatchers ( ) ;
3929 this . workspaceWatcher = workspace . onDidChangeWorkspaceFolders ( ( _e ) => {
4030 for ( const watcher of this . watchers ) {
4131 watcher . close ( ) ;
4232 }
43- ServerlessFunctionView . getInstance ( ) . refresh ( ) ;
33+ this . view . refresh ( ) ;
4434 this . addWatchers ( ) ;
4535 } ) ;
4636 }
4737
48- private addWatchers ( ) {
49- if ( workspace . workspaceFolders ) {
50- for ( const workspaceFolder of workspace . workspaceFolders ) {
51- this . watchers . push (
52- fs . watch ( workspaceFolder . uri . fsPath , ( _event , filename ) => {
53- if ( filename === 'func.yaml' ) {
54- ServerlessFunctionView . getInstance ( ) . refresh ( ) ;
55- }
56- } ) ,
57- ) ;
58- }
59- }
60- }
61-
62- private async getListItems ( command : CommandText , fail = false ) {
63- const listCliExitData = await CliChannel . getInstance ( ) . executeTool (
64- command ,
65- undefined ,
66- fail ,
67- ) ;
68- try {
69- return JSON . parse ( listCliExitData . stdout ) as FunctionObject [ ] ;
70- } catch ( err ) {
71- return [ ] ;
72- }
73- }
74-
75- private async getDeployedFunctions ( ) : Promise < FunctionObject [ ] > {
76- return this . getListItems ( DeploymentConfig . command . getDeploymentFunctions ( ) ) ;
77- }
78-
79- async createFunction (
80- language : string ,
81- template : string ,
82- location : string ,
83- image : string ,
84- ) : Promise < CliExitData > {
85- let functionResponse : CliExitData ;
86- try {
87- const response = await OdoImpl . Instance . execute (
88- ServerlessCommand . createFunction ( language , template , location ) ,
89- ) ;
90- if ( response && ! response . error ) {
91- const yamlContent = await Utils . getFuncYamlContent ( location ) ;
92- if ( yamlContent ) {
93- yamlContent . image = image ;
94- await fs . rm ( path . join ( location , 'func.yaml' ) ) ;
95- await fs . writeFile (
96- path . join ( location , 'func.yaml' ) ,
97- dump ( yamlContent ) ,
98- 'utf-8' ,
99- ) ;
100- functionResponse = {
101- error : undefined ,
102- stderr : '' ,
103- stdout : 'Success' ,
104- } ;
105- }
106- } else {
107- await fs . rmdir ( location ) ;
108- functionResponse = response ;
109- }
110- } catch ( err ) {
111- if ( err instanceof VsCommandError ) {
112- void window . showErrorMessage ( err . message ) ;
113- }
114- await fs . rmdir ( location ) ;
115- functionResponse = {
116- error : err as cp . ExecException ,
117- stderr : '' ,
118- stdout : '' ,
119- } ;
120- }
121- return functionResponse ;
122- }
123-
124- async getLocalFunctions ( ) : Promise < FunctionObject [ ] > {
38+ public async getLocalFunctions ( ) : Promise < FunctionObject [ ] > {
12539 const functionList : FunctionObject [ ] = [ ] ;
12640 const folders : Uri [ ] = [ ] ;
12741 if ( workspace . workspaceFolders ) {
@@ -172,7 +86,14 @@ export class ServerlessFunctionImpl implements ServerlessFunction {
17286 return functionList ;
17387 }
17488
175- getDeployFunction (
89+ public dispose ( ) {
90+ for ( const watcher of this . watchers ) {
91+ watcher . close ( ) ;
92+ }
93+ this . workspaceWatcher . dispose ( ) ;
94+ }
95+
96+ private getDeployFunction (
17697 funcData : FunctionContent ,
17798 deployedFunctions : FunctionObject [ ] ,
17899 ) : DeployedFunction {
@@ -192,19 +113,39 @@ export class ServerlessFunctionImpl implements ServerlessFunction {
192113 return { status : FunctionStatus . LOCALONLY , url : '' } as DeployedFunction ;
193114 }
194115
195- dispose ( ) {
196- for ( const watcher of this . watchers ) {
197- watcher . close ( ) ;
116+ private async checkImage ( folderUri : Uri ) : Promise < boolean > {
117+ const yamlContent = await Utils . getFuncYamlContent ( folderUri . fsPath ) ;
118+ return Functions . imageRegex . test ( yamlContent ?. image ) ;
119+ }
120+
121+ private addWatchers ( ) {
122+ if ( workspace . workspaceFolders ) {
123+ for ( const workspaceFolder of workspace . workspaceFolders ) {
124+ this . watchers . push (
125+ fs . watch ( workspaceFolder . uri . fsPath , ( _event , filename ) => {
126+ if ( filename === 'func.yaml' ) {
127+ this . view . refresh ( ) ;
128+ }
129+ } ) ,
130+ ) ;
131+ }
198132 }
199- this . workspaceWatcher . dispose ( ) ;
200133 }
201134
202- async checkImage ( folderUri : Uri ) : Promise < boolean > {
203- const yamlContent = await Utils . getFuncYamlContent ( folderUri . fsPath ) ;
204- return Functions . imageRegex . test ( yamlContent ?. image ) ;
135+ private async getListItems ( command : CommandText , fail = false ) {
136+ const listCliExitData = await CliChannel . getInstance ( ) . executeTool (
137+ command ,
138+ undefined ,
139+ fail ,
140+ ) ;
141+ try {
142+ return JSON . parse ( listCliExitData . stdout ) as FunctionObject [ ] ;
143+ } catch ( err ) {
144+ return [ ] ;
145+ }
205146 }
206- }
207147
208- export function serverlessInstance ( ) : ServerlessFunction {
209- return ServerlessFunctionImpl . Instance ;
148+ private async getDeployedFunctions ( ) : Promise < FunctionObject [ ] > {
149+ return this . getListItems ( DeploymentConfig . command . getDeploymentFunctions ( ) ) ;
150+ }
210151}
0 commit comments