1
+
2
+ import * as path from 'node:path'
3
+ import * as fs from 'node:fs'
4
+ import { compileTs2js } from '../utils/util-lang'
5
+
6
+ import { FUNCTIONS_DIR , FUNCTIONS_FILE } from '../utils/constants'
7
+ import { checkDir } from '../utils/util'
8
+
9
+ import { debugFunction , getFunctionByName , pushFunction , createFunction } from '../api/functions'
10
+
11
+
12
+
13
+ /**
14
+ * pull function
15
+ * @param {any } data
16
+ * @param {any } options
17
+ * @returns
18
+ */
19
+
20
+ export async function handlePullFunctionCommand ( data :any , options :any ) {
21
+
22
+ // functions dir
23
+ const functionsDir = path . resolve ( process . cwd ( ) , FUNCTIONS_DIR )
24
+
25
+ checkDir ( functionsDir )
26
+
27
+ data . forEach ( element => {
28
+
29
+ //fuction name
30
+ const funcName = element . name ;
31
+ const funcNameDir = path . resolve ( functionsDir , funcName )
32
+
33
+ checkDir ( funcNameDir )
34
+
35
+ const funcFile = path . resolve ( funcNameDir , FUNCTIONS_FILE )
36
+ try {
37
+ // check if exist function file
38
+ fs . accessSync ( funcFile )
39
+ const currentCode = fs . readFileSync ( funcFile , 'utf-8' )
40
+
41
+ if ( currentCode ) {
42
+ // forceOverwrite
43
+ if ( options . forceOverwrite ) {
44
+ fs . writeFileSync ( funcFile , element . code )
45
+ }
46
+ } else {
47
+ fs . writeFileSync ( funcFile , element . code )
48
+ }
49
+ } catch ( err ) {
50
+
51
+ fs . writeFileSync ( funcFile , element . code )
52
+
53
+ }
54
+
55
+ console . log ( 'pull success' )
56
+ } )
57
+
58
+
59
+ }
60
+
61
+ /**
62
+ * invoke function
63
+ * @param {string } appid
64
+ * @param {string } functionName
65
+ * @returns
66
+ */
67
+
68
+ export async function handleInvokeFunctionCommand ( appid :string , functionName :string , param :object ) {
69
+
70
+ const functionsDir = path . resolve ( process . cwd ( ) , FUNCTIONS_DIR )
71
+
72
+ // get local code
73
+ const functionNameDir = path . resolve ( functionsDir , functionName )
74
+ const funcFile = path . resolve ( functionNameDir , FUNCTIONS_FILE )
75
+ const code = fs . readFileSync ( funcFile , 'utf8' )
76
+ const obj = {
77
+ func :{
78
+ appid : appid ,
79
+ code : code ,
80
+ name :functionName ,
81
+ compiledCode : compileTs2js ( code ) ,
82
+ debugParams : JSON . stringify ( param ) ,
83
+ } ,
84
+ param :param
85
+ }
86
+
87
+ const res = await debugFunction ( appid , 'test' , obj )
88
+ console . log ( res )
89
+
90
+ }
91
+
92
+
93
+ /**
94
+ * push fuction
95
+ * @param {string } appid
96
+ * @param {string } functionName
97
+ * @param {any } options
98
+ * @returns
99
+ */
100
+
101
+
102
+ export async function handlePushFunctionCommand ( appid :string , functionName :string , options :any ) {
103
+
104
+ const functionsDir = path . resolve ( process . cwd ( ) , FUNCTIONS_DIR )
105
+
106
+ // get local code
107
+ const functionNameDir = path . resolve ( functionsDir , functionName )
108
+ const funcFile = path . resolve ( functionNameDir , FUNCTIONS_FILE )
109
+ const code = fs . readFileSync ( funcFile , 'utf8' )
110
+
111
+ // get function
112
+ const record = await getFunctionByName ( appid , functionName )
113
+
114
+ //update function
115
+ if ( record . data ) {
116
+ if ( record . data . code !== code ) {
117
+
118
+ if ( options . forceOverwrite ) {
119
+ const data = {
120
+ code :code ,
121
+ debugParams :JSON . stringify ( { "code" :"laf" } ) ,
122
+ }
123
+ const res = await pushFunction ( appid , functionName , data )
124
+ if ( res . data ) {
125
+ console . log ( "push success" )
126
+ }
127
+ }
128
+ } else {
129
+ console . log ( "push success1" )
130
+ }
131
+
132
+ } else {
133
+ // create function
134
+ const data = {
135
+ code :code ,
136
+ name :functionName ,
137
+ label :"test" ,
138
+ }
139
+
140
+ const res = await createFunction ( appid , data )
141
+ if ( res . data ) {
142
+ console . log ( "push success" )
143
+ }
144
+ }
145
+
146
+ }
0 commit comments