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
+ * @param {object } param
66
+ * @returns
67
+ */
68
+
69
+ export async function handleInvokeFunctionCommand ( appid :string , functionName :string , param :object ) {
70
+
71
+ const functionsDir = path . resolve ( process . cwd ( ) , FUNCTIONS_DIR )
72
+
73
+ // get local code
74
+ const functionNameDir = path . resolve ( functionsDir , functionName )
75
+ const funcFile = path . resolve ( functionNameDir , FUNCTIONS_FILE )
76
+ const code = fs . readFileSync ( funcFile , 'utf8' )
77
+ const obj = {
78
+ func :{
79
+ appid : appid ,
80
+ code : code ,
81
+ name :functionName ,
82
+ compiledCode : compileTs2js ( code ) ,
83
+ debugParams : JSON . stringify ( param ) ,
84
+ } ,
85
+ param :param
86
+ }
87
+
88
+ const res = await debugFunction ( functionName , obj )
89
+ console . log ( res )
90
+
91
+ }
92
+
93
+
94
+ /**
95
+ * push fuction
96
+ * @param {string } appid
97
+ * @param {string } functionName
98
+ * @param {any } options
99
+ * @returns
100
+ */
101
+
102
+
103
+ export async function handlePushFunctionCommand ( appid :string , functionName :string , options :any ) {
104
+
105
+ const functionsDir = path . resolve ( process . cwd ( ) , FUNCTIONS_DIR )
106
+
107
+ // get local code
108
+ const functionNameDir = path . resolve ( functionsDir , functionName )
109
+ const funcFile = path . resolve ( functionNameDir , FUNCTIONS_FILE )
110
+ const code = fs . readFileSync ( funcFile , 'utf8' )
111
+
112
+ // get function
113
+ const record = await getFunctionByName ( appid , functionName )
114
+
115
+ //update function
116
+ if ( record . data ) {
117
+ if ( record . data . code !== code ) {
118
+
119
+ if ( options . forceOverwrite ) {
120
+ const data = {
121
+ code :code ,
122
+ debugParams :JSON . stringify ( { "code" :"laf" } ) ,
123
+ }
124
+ const res = await pushFunction ( appid , functionName , data )
125
+ if ( res . data ) {
126
+ console . log ( "push success" )
127
+ }
128
+ } else {
129
+
130
+ console . log ( "romote code is different with local" )
131
+ }
132
+ } else {
133
+ console . log ( "romote code is same with local" )
134
+ }
135
+
136
+ } else {
137
+ // create function
138
+ const data = {
139
+ code :code ,
140
+ name :functionName ,
141
+ label :"test" ,
142
+ status :1
143
+ }
144
+
145
+ const res = await createFunction ( appid , data )
146
+ if ( res . data ) {
147
+ console . log ( "push success" )
148
+ }
149
+ }
150
+
151
+ }
0 commit comments