forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add oss/server command (labring#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: 丁振振 <dingzhenzhen@dingzhenzhendeMacBook-Pro.local>
- Loading branch information
1 parent
5e481f2
commit 9a14fc6
Showing
15 changed files
with
834 additions
and
294 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
|
||
import * as path from 'node:path' | ||
import * as fs from 'node:fs' | ||
import { compileTs2js } from '../utils/util-lang' | ||
|
||
import { FUNCTIONS_DIR ,FUNCTIONS_FILE} from '../utils/constants' | ||
import { checkDir} from '../utils/util' | ||
|
||
import { debugFunction,getFunctionByName,pushFunction,createFunction} from '../api/functions' | ||
|
||
|
||
|
||
/** | ||
* pull function | ||
* @param {any} data | ||
* @param {any} options | ||
* @returns | ||
*/ | ||
|
||
export async function handlePullFunctionCommand(data:any,options:any) { | ||
|
||
// functions dir | ||
const functionsDir = path.resolve(process.cwd(), FUNCTIONS_DIR) | ||
|
||
checkDir(functionsDir) | ||
|
||
data.forEach(element => { | ||
|
||
//fuction name | ||
const funcName =element.name; | ||
const funcNameDir = path.resolve(functionsDir, funcName) | ||
|
||
checkDir(funcNameDir) | ||
|
||
const funcFile= path.resolve(funcNameDir, FUNCTIONS_FILE) | ||
try{ | ||
// check if exist function file | ||
fs.accessSync(funcFile) | ||
const currentCode =fs.readFileSync(funcFile,'utf-8') | ||
|
||
if(currentCode){ | ||
// forceOverwrite | ||
if(options.forceOverwrite){ | ||
fs.writeFileSync(funcFile, element.code) | ||
} | ||
}else{ | ||
fs.writeFileSync(funcFile, element.code) | ||
} | ||
}catch(err){ | ||
|
||
fs.writeFileSync(funcFile, element.code) | ||
|
||
} | ||
|
||
console.log('pull success') | ||
}) | ||
|
||
|
||
} | ||
|
||
/** | ||
* invoke function | ||
* @param {string} appid | ||
* @param {string} functionName | ||
* @param {object} param | ||
* @returns | ||
*/ | ||
|
||
export async function handleInvokeFunctionCommand(appid:string,functionName:string,param:object) { | ||
|
||
const functionsDir = path.resolve(process.cwd(), FUNCTIONS_DIR) | ||
|
||
// get local code | ||
const functionNameDir = path.resolve(functionsDir, functionName) | ||
const funcFile= path.resolve(functionNameDir, FUNCTIONS_FILE) | ||
const code = fs.readFileSync(funcFile, 'utf8') | ||
const obj = { | ||
func:{ | ||
appid: appid, | ||
code: code, | ||
name:functionName, | ||
compiledCode: compileTs2js(code), | ||
debugParams: JSON.stringify(param), | ||
}, | ||
param:param | ||
} | ||
|
||
const res = await debugFunction(functionName,obj) | ||
console.log(res) | ||
|
||
} | ||
|
||
|
||
/** | ||
* push fuction | ||
* @param {string} appid | ||
* @param {string} functionName | ||
* @param {any} options | ||
* @returns | ||
*/ | ||
|
||
|
||
export async function handlePushFunctionCommand(appid:string,functionName:string,options:any) { | ||
|
||
const functionsDir = path.resolve(process.cwd(), FUNCTIONS_DIR) | ||
|
||
// get local code | ||
const functionNameDir = path.resolve(functionsDir, functionName) | ||
const funcFile= path.resolve(functionNameDir, FUNCTIONS_FILE) | ||
const code = fs.readFileSync(funcFile, 'utf8') | ||
|
||
// get function | ||
const record = await getFunctionByName(appid,functionName) | ||
|
||
//update function | ||
if(record.data){ | ||
if(record.data.code!==code){ | ||
|
||
if(options.forceOverwrite){ | ||
const data = { | ||
code:code, | ||
debugParams:JSON.stringify({"code":"laf"}), | ||
} | ||
const res = await pushFunction(appid,functionName,data) | ||
if(res.data){ | ||
console.log("push success") | ||
} | ||
}else{ | ||
|
||
console.log("romote code is different with local") | ||
} | ||
}else{ | ||
console.log("romote code is same with local") | ||
} | ||
|
||
}else{ | ||
// create function | ||
const data = { | ||
code:code, | ||
name:functionName, | ||
label:"test", | ||
status:1 | ||
} | ||
|
||
const res = await createFunction(appid,data) | ||
if(res.data){ | ||
console.log("push success") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as fs from 'node:fs' | ||
import * as path from 'node:path' | ||
import { LAF_FILE } from '../utils/constants' | ||
import { checkDir } from '../utils/util' | ||
|
||
import * as AdmZip from 'adm-zip' | ||
import { pipeline } from 'node:stream/promises' | ||
|
||
|
||
/** | ||
* init app | ||
* @param {string} appName | ||
* @param {string} appid | ||
* @param {string} endPoint | ||
* @param {string} ossEndpoint | ||
* @returns | ||
*/ | ||
|
||
export async function handleInitAppCommand(appName:string,appid:string,endPoint:string,ossEndpoint) { | ||
|
||
const appPath = path.resolve(process.cwd(), appName) | ||
checkDir(appPath) | ||
const lafFile = path.resolve(appPath, LAF_FILE) | ||
// write data | ||
fs.writeFileSync(lafFile, JSON.stringify({ appid: appid, root: appPath ,endPoint,ossEndpoint})) | ||
|
||
} | ||
|
||
|
||
/** | ||
* sync app | ||
* @param {string} appName | ||
* @param {any} data | ||
* @returns | ||
*/ | ||
|
||
export async function handleSyncAppCommand(appName:string,data:any) { | ||
|
||
const appPath = path.resolve(process.cwd(), appName) | ||
|
||
const appZip = appName + '.zip' | ||
const appZipPath = path.resolve(process.cwd(), appZip) | ||
const writer = fs.createWriteStream(appZipPath) | ||
await pipeline(data.data,writer); | ||
|
||
// unzip | ||
const file = new AdmZip(appZipPath) | ||
file.extractAllTo(appPath) | ||
|
||
fs.unlinkSync(appZipPath) | ||
console.log('success') | ||
|
||
} |
Oops, something went wrong.