Skip to content

Commit 65cb7a7

Browse files
dingzhenznen丁振振
and
丁振振
authored
feat(cli): add oss/server command (#154)
* 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 * add command to index * add oss command Co-authored-by: 丁振振 <[email protected]>
1 parent e31d99e commit 65cb7a7

File tree

18 files changed

+836
-416
lines changed

18 files changed

+836
-416
lines changed

packages/cli/src/actions/function.ts

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}

packages/cli/src/actions/init.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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} appName
13+
* @param {string} appid
14+
* @param {string} endPoint
15+
* @param {string} ossEndpoint
16+
* @returns
17+
*/
18+
19+
export async function handleInitAppCommand(appName:string,appid:string,endPoint:string,ossEndpoint) {
20+
21+
const appPath = path.resolve(process.cwd(), appName)
22+
checkDir(appPath)
23+
const lafFile = path.resolve(appPath, LAF_FILE)
24+
// write data
25+
fs.writeFileSync(lafFile, JSON.stringify({ appid: appid, root: appPath ,endPoint,ossEndpoint}))
26+
27+
}
28+
29+
30+
/**
31+
* sync app
32+
* @param {string} appName
33+
* @param {any} data
34+
* @returns
35+
*/
36+
37+
export async function handleSyncAppCommand(appName:string,data:any) {
38+
39+
const appPath = path.resolve(process.cwd(), appName)
40+
41+
const appZip = appName + '.zip'
42+
const appZipPath = path.resolve(process.cwd(), appZip)
43+
const writer = fs.createWriteStream(appZipPath)
44+
await pipeline(data.data,writer);
45+
46+
// unzip
47+
const file = new AdmZip(appZipPath)
48+
file.extractAllTo(appPath)
49+
50+
fs.unlinkSync(appZipPath)
51+
console.log('success')
52+
53+
}

0 commit comments

Comments
 (0)