Skip to content

Commit e31d99e

Browse files
dingzhenznen丁振振
and
丁振振
authored
chore(cli): add remote server & remarks (#150)
* add login to index.ts * add remarks * add remote server * add remote server * add remote server * add response check * fix init function * fix init function * fix command * add server api Co-authored-by: 丁振振 <[email protected]>
1 parent 385a0dc commit e31d99e

21 files changed

+773
-135
lines changed

packages/cli/src/actions/appAction.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import * as Table from 'cli-table2'
22

33
import {appList} from '../api/apps'
44

5-
export async function appListCommand() {
5+
/**
6+
* apps list
7+
* @returns
8+
*/
9+
10+
export async function handleAppListCommand() {
611

712
// get list
813
const response = await appList()
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as fs from 'node:fs'
2+
import * as path from 'node:path'
3+
import { LAF_FILE } from '../utils/constants'
4+
import { checkDir } from '../utils/util'
5+
6+
import * as AdmZip from 'adm-zip'
7+
import { pipeline } from 'node:stream/promises'
8+
9+
10+
/**
11+
* init app
12+
* @param {string} funcName
13+
* @param {string} appid
14+
* @param {string} endPoint
15+
* @returns
16+
*/
17+
18+
export async function handleInitAppCommand(appName:string,appid:string,endPoint:string) {
19+
20+
const appPath = path.resolve(process.cwd(), appName)
21+
checkDir(appPath)
22+
const lafFile = path.resolve(appPath, LAF_FILE)
23+
// write data
24+
fs.writeFileSync(lafFile, JSON.stringify({ appid: appid, root: appPath ,endPoint}))
25+
26+
}
27+
28+
29+
/**
30+
* sync app
31+
* @param {string} funcName
32+
* @param {any} data
33+
* @returns
34+
*/
35+
36+
export async function handleSyncAppCommand(appName:string,data:any) {
37+
38+
const appPath = path.resolve(process.cwd(), appName)
39+
40+
const appZip = appName + '.zip'
41+
const appZipPath = path.resolve(process.cwd(), appZip)
42+
const writer = fs.createWriteStream(appZipPath)
43+
await pipeline(data.data,writer);
44+
45+
// unzip
46+
const file = new AdmZip(appZipPath)
47+
file.extractAllTo(appPath)
48+
49+
fs.unlinkSync(appZipPath)
50+
51+
}

packages/cli/src/actions/userAction.ts

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import * as fs from 'node:fs'
33
import {AUTH_FILE} from '../utils/constants'
44
import { checkCredentialsDir } from '../utils/util'
55

6+
/**
7+
* login
8+
* @param {string} remote
9+
* @param {string} username
10+
* @param {string} password
11+
* @returns
12+
*/
13+
614
export async function handleLoginCommand(remote:string,username:string,password:string) {
715

816
// check auth dir

packages/cli/src/api/apps.ts

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
import { requestData } from "./request"
22

3+
4+
/**
5+
* 根据 appid 获取应用
6+
* @param {string} appid
7+
* @returns 返回应用数据
8+
*/
9+
export async function getApplicationByAppid(appid:string) {
10+
const res = await requestData({
11+
url: `/sys-api/apps/${appid}`,
12+
method: 'get'
13+
})
14+
15+
return res.data
16+
}
17+
18+
19+
/**
20+
* 获取应用列表
21+
* @returns 返回应用数据
22+
*/
23+
324
export async function appList() {
425
const url = `/sys-api/apps/my`
526
const obj = {
@@ -10,6 +31,12 @@ export async function appList() {
1031
return result.data
1132
}
1233

34+
/**
35+
* 根据 appid stop 应用
36+
* @param {string} appid
37+
* @returns
38+
*/
39+
1340
export async function appStop(appid: string) {
1441
const url = `/sys-api/apps/${appid}/instance/stop`
1542
const obj = {
@@ -20,6 +47,12 @@ export async function appStop(appid: string) {
2047
return result.data
2148
}
2249

50+
/**
51+
* 根据 appid start 应用
52+
* @param {string} appid
53+
* @returns
54+
*/
55+
2356
export async function appStart(appid: string) {
2457
const url = `/sys-api/apps/${appid}/instance/start`
2558
const obj = {
@@ -31,6 +64,12 @@ export async function appStart(appid: string) {
3164
}
3265

3366

67+
/**
68+
* 根据 appid restart 应用
69+
* @param {string} appid
70+
* @returns
71+
*/
72+
3473
export async function appRestart(appid: string) {
3574
const url = `/sys-api/apps/${appid}/instance/start`
3675
const obj = {

0 commit comments

Comments
 (0)