diff --git a/packages/cli/README.md b/packages/cli/README.md index 4099aca389..bea49ba301 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -8,13 +8,23 @@ npm i laf-cli -g ## Usage -#### 1. Generate a env file template for laf-cli: `.env` +### 1. login your account ```bash - laf init --access-key xxxxx --access-secret xxxxxx --bucket xxxxxx + laf login -u maslow -p password -r https://console.lafyun.com ``` -#### 2. run `laf-cli sync ./dist` command to sync files to OSS bucket +### 2. show your applications list + +```bash + laf list +``` + +### 3. init in your local project + +```bash + laf init -s YOUR_APPID +``` ## Upgrade laf-cli diff --git a/packages/cli/src/actions/app.ts b/packages/cli/src/actions/app.ts index f1121b807a..336c8facf7 100644 --- a/packages/cli/src/actions/app.ts +++ b/packages/cli/src/actions/app.ts @@ -1,37 +1,33 @@ -import * as Table from 'cli-table2' - -import {appList} from '../api/apps' +import * as Table from 'cli-table2' +import { appList } from '../api/apps' /** * apps list * @returns */ - export async function handleAppListCommand() { - // get list const response = await appList() //init table const table = new Table({ - head: ['appid', 'name','status'], - }); + head: ['appid', 'name', 'status'], + }) // user create app - if(response.data.created){ + if (response.data.created) { response.data.created.forEach(app => { - table.push([app.appid,app.name,app.status]) - }); + table.push([app.appid, app.name, app.status]) + }) } // user join app - if(response.data.joined){ + if (response.data.joined) { response.data.joined.forEach(app => { - table.push([app.appid,app.name,app.status]) - }); + table.push([app.appid, app.name, app.status]) + }) } - + // show table console.log(table.toString()) - } \ No newline at end of file diff --git a/packages/cli/src/actions/function-invoke.ts b/packages/cli/src/actions/function-invoke.ts index 560f03bbe9..f664234196 100644 --- a/packages/cli/src/actions/function-invoke.ts +++ b/packages/cli/src/actions/function-invoke.ts @@ -1,11 +1,8 @@ -import { PROJECT_DIR } from '../utils/constants' - import * as path from 'node:path' import * as fs from 'node:fs' import { compileTs2js } from '../utils/util-lang' - +import { PROJECT_DIR } from '../utils/constants' import { FUNCTIONS_DIR, FUNCTIONS_FILE } from '../utils/constants' - import { debugFunction } from '../api/functions' /** @@ -15,7 +12,6 @@ import { debugFunction } from '../api/functions' * @param {object} param * @returns */ - export async function handleInvokeFunctionCommand(appid: string, functionName: string, param: object) { // get local code @@ -35,5 +31,4 @@ export async function handleInvokeFunctionCommand(appid: string, functionName: s const res = await debugFunction(functionName, obj) console.log(res) - } \ No newline at end of file diff --git a/packages/cli/src/actions/function-pull-list.ts b/packages/cli/src/actions/function-pull-list.ts index 8c7e809e35..478e29963a 100644 --- a/packages/cli/src/actions/function-pull-list.ts +++ b/packages/cli/src/actions/function-pull-list.ts @@ -1,6 +1,5 @@ -import { pullFunction } from "../api/functions" import * as path from 'node:path' - +import { pullFunction } from "../api/functions" import { FUNCTIONS_DIR, PROJECT_DIR } from '../utils/constants' import { ensureDirectory } from '../utils/util' import { getLocalList, getRemoteList } from "../utils/function-lists" diff --git a/packages/cli/src/actions/function-pull-one.ts b/packages/cli/src/actions/function-pull-one.ts index c659a18605..0db60eb9e8 100644 --- a/packages/cli/src/actions/function-pull-one.ts +++ b/packages/cli/src/actions/function-pull-one.ts @@ -1,9 +1,8 @@ -import { pullFunction } from "../api/functions" import * as path from 'node:path' import * as fs from 'node:fs' import { FUNCTIONS_DIR, PROJECT_DIR } from '../utils/constants' import { ensureDirectory } from '../utils/util' - +import { pullFunction } from "../api/functions" import { createfn, updatefn } from "../utils/function-pull" @@ -31,5 +30,4 @@ export async function handlePullOneCommand(appid: string, functionName: string) createfn(response.data[0]) } } - } \ No newline at end of file diff --git a/packages/cli/src/actions/function-push-list.ts b/packages/cli/src/actions/function-push-list.ts index 67d72b5162..cd9585b1af 100644 --- a/packages/cli/src/actions/function-push-list.ts +++ b/packages/cli/src/actions/function-push-list.ts @@ -16,19 +16,15 @@ export async function handlePushListCommand(appid: string, options: any) { // functions dir const functionsDir = path.resolve(PROJECT_DIR, FUNCTIONS_DIR) - ensureDirectory(functionsDir) const response = await pullFunction(appid, '') - - if (!response.data) { return false } const remoteList = getRemoteList(response.data) - - const localList = await getLocalList(functionsDir) + const localList = getLocalList(functionsDir) // create list const createList = getCreateList(remoteList, localList) @@ -36,7 +32,6 @@ export async function handlePushListCommand(appid: string, options: any) { createList.forEach(async (item) => { await createRemoteFn(appid, item.key) }) - } //update list @@ -45,7 +40,6 @@ export async function handlePushListCommand(appid: string, options: any) { updateList.forEach(async (item2) => { await updateRemoteFn(appid, item2.key) }) - } // delete list @@ -56,7 +50,5 @@ export async function handlePushListCommand(appid: string, options: any) { await deleteRemoteFn(appid, item3.value._id) }) } - } - } \ No newline at end of file diff --git a/packages/cli/src/actions/funtcion-push-one.ts b/packages/cli/src/actions/funtcion-push-one.ts index e4df595bf4..f4b887dc7f 100644 --- a/packages/cli/src/actions/funtcion-push-one.ts +++ b/packages/cli/src/actions/funtcion-push-one.ts @@ -1,30 +1,17 @@ import { getFunctionByName, } from "../api/functions" - import { createRemoteFn, updateRemoteFn } from "../utils/function-push" - - /** * pull function - * @param { appid} string - * @param { functionName } string * @returns */ export async function handlePushOneCommand(appid: string, functionName: string) { - - // get remote function const record = await getFunctionByName(appid, functionName) - - if (!record.data) { - createRemoteFn(appid, functionName) } else { - updateRemoteFn(appid, functionName) - } - } \ No newline at end of file diff --git a/packages/cli/src/actions/init.ts b/packages/cli/src/actions/init.ts index 4b2d3a6ea2..c735a0f4c8 100644 --- a/packages/cli/src/actions/init.ts +++ b/packages/cli/src/actions/init.ts @@ -1,10 +1,9 @@ -import { GLOBAL_FILE, PACKAGE_FILE, PROJECT_DIR, RESPONSE_FILE, TEMPLATES_DIR, TSCONFIG_FILE, TYPE_DIR } from './../utils/constants' +import * as path from 'node:path' import * as fs from 'node:fs' +import { GLOBAL_FILE, PACKAGE_FILE, PROJECT_DIR, RESPONSE_FILE, TEMPLATES_DIR, TSCONFIG_FILE, TYPE_DIR } from './../utils/constants' import { LAF_CONFIG_FILE } from '../utils/constants' import { ensureDirectory } from '../utils/util' - import { handlePullListCommand } from './function-pull-list' -import * as path from 'node:path' /** @@ -13,9 +12,12 @@ import * as path from 'node:path' * @param {string} endpoint * @param {string} oss_endpoint */ - export async function handleInitAppCommand(appid: string, endpoint: string, oss_endpoint: string) { + // add laf.json to project fs.writeFileSync(LAF_CONFIG_FILE, JSON.stringify({ appid: appid, endpoint: endpoint, oss_endpoint: oss_endpoint })) + + // add config file + addConfigFromTemplates() } @@ -26,35 +28,33 @@ export async function handleInitAppCommand(appid: string, endpoint: string, oss_ export async function handleSyncAppCommand(appid: string) { ensureDirectory(PROJECT_DIR) - // pull function - await handlePullListCommand(appid, []) - // add config file - addConfigFromTemplates() + // pull functions + await handlePullListCommand(appid, []) } function addConfigFromTemplates() { - ensureDirectory(TYPE_DIR) - //from templates dir + + // from templates dir const templates_dir = path.resolve(__dirname, '../../', TEMPLATES_DIR) - // global + // generate global.d.ts const from_globle_file = path.resolve(templates_dir, GLOBAL_FILE) const out_globe_file = path.resolve(TYPE_DIR, GLOBAL_FILE) fs.writeFileSync(out_globe_file, fs.readFileSync(from_globle_file, 'utf-8')) - // response + // generate response.d.ts const from_response_file = path.resolve(templates_dir, RESPONSE_FILE) const out_response_file = path.resolve(TYPE_DIR, RESPONSE_FILE) fs.writeFileSync(out_response_file, fs.readFileSync(from_response_file, 'utf-8')) - // package + // generate package.json const from_package_file = path.resolve(templates_dir, PACKAGE_FILE) const out_package_file = path.resolve(PROJECT_DIR, PACKAGE_FILE) fs.writeFileSync(out_package_file, fs.readFileSync(from_package_file, 'utf-8')) - // tsconfig + // generate tsconfig.json const from_tsconfig_file = path.resolve(templates_dir, TSCONFIG_FILE) const out_tsconfig_file = path.resolve(PROJECT_DIR, TSCONFIG_FILE) fs.writeFileSync(out_tsconfig_file, fs.readFileSync(from_tsconfig_file, 'utf-8')) diff --git a/packages/cli/src/actions/oss.ts b/packages/cli/src/actions/oss.ts index 7c3f849c16..a83a94cc98 100644 --- a/packages/cli/src/actions/oss.ts +++ b/packages/cli/src/actions/oss.ts @@ -2,9 +2,9 @@ import * as fs from 'node:fs' import * as path from 'node:path' import { createHash } from 'node:crypto' import * as mime from 'mime' -import { ensureDirectory, getS3Client } from '../utils/util' import axios from 'axios' import { pipeline } from 'node:stream/promises' +import { ensureDirectory, getS3Client } from '../utils/util' /** * push files @@ -31,7 +31,7 @@ export async function handlePushCommand(credentials: any, options: any) { // get source files const sourceFiles = readdirRecursive(source).map(file => { return { - key: path.relative(abs_source, file), + key: path.relative(abs_source, file).split(path.sep).join('/'), abs_path: path.resolve(file), } }) @@ -48,7 +48,7 @@ export async function handlePushCommand(credentials: any, options: any) { Bucket: options.bucketName, Key: file.key, Body: fs.readFileSync(path.resolve(source, file.abs_path)), - ContentType: mime.getType(file.key) + ContentType: mime.getType(file.key), }).promise() console.log(path.resolve(source, file.abs_path)) diff --git a/packages/cli/src/actions/user.ts b/packages/cli/src/actions/user.ts index 3955ec4691..d6112bac86 100644 --- a/packages/cli/src/actions/user.ts +++ b/packages/cli/src/actions/user.ts @@ -1,7 +1,7 @@ -import {loginApi} from '../api/user' import * as fs from 'node:fs' -import {AUTH_FILE} from '../utils/constants' +import { AUTH_FILE } from '../utils/constants' import { checkCredentialsDir } from '../utils/util' +import { loginApi } from '../api/user' /** * login @@ -10,23 +10,21 @@ import { checkCredentialsDir } from '../utils/util' * @param {string} password * @returns */ - -export async function handleLoginCommand(remote:string,username:string,password:string) { +export async function handleLoginCommand(remote: string, username: string, password: string) { // check auth dir checkCredentialsDir() // login - const result = await loginApi(remote,{username,password}) - - if(!result){ + const result = await loginApi(remote, { username, password }) + if (!result) { console.error('username or password is wrong') process.exit(1) } - const content = {access_token:result.access_token,expire_time:result.expire_time,remote:remote} - + + const content = { access_token: result.access_token, expire_time: result.expire_time, remote: remote } + // write to file fs.writeFileSync(AUTH_FILE, JSON.stringify(content)) console.log(`Generated: ${AUTH_FILE}`) - } \ No newline at end of file diff --git a/packages/cli/src/api/apps.ts b/packages/cli/src/api/apps.ts index f3c3ad7acb..587056e1e3 100644 --- a/packages/cli/src/api/apps.ts +++ b/packages/cli/src/api/apps.ts @@ -1,26 +1,23 @@ import { requestData } from "./request" - /** * 根据 appid 获取应用 * @param {string} appid * @returns 返回应用数据 */ - export async function getApplicationByAppid(appid:string) { +export async function getApplicationByAppid(appid: string) { const res = await requestData({ - url: `/sys-api/apps/${appid}`, - method: 'get' + url: `/sys-api/apps/${appid}`, + method: 'get' }) - - return res.data - } + return res.data +} /** * 获取应用列表 * @returns 返回应用数据 */ - export async function appList() { const url = `/sys-api/apps/my` const obj = { @@ -36,7 +33,6 @@ export async function appList() { * @param {string} appid * @returns */ - export async function appStop(appid: string) { const url = `/sys-api/apps/${appid}/instance/stop` const obj = { @@ -69,7 +65,6 @@ export async function appStart(appid: string) { * @param {string} appid * @returns */ - export async function appRestart(appid: string) { const url = `/sys-api/apps/${appid}/instance/start` const obj = { diff --git a/packages/cli/src/api/functions.ts b/packages/cli/src/api/functions.ts index caa319af8f..f34daaa5ea 100644 --- a/packages/cli/src/api/functions.ts +++ b/packages/cli/src/api/functions.ts @@ -14,13 +14,12 @@ import axios from 'axios' export async function pullFunction(appid: string, functionName: string) { let url = '' - if (functionName) { url = `/sys-api/apps/${appid}/function?page=1&limit=1&keyword=${functionName}` } else { url = `/sys-api/apps/${appid}/function?page=1&limit=200` - } + const obj = { method: "GET", url @@ -36,33 +35,26 @@ export async function pullFunction(appid: string, functionName: string) { * @param {Object} obj * @returns */ - export async function debugFunction(functionName: string, obj: Object) { const appData = getAppData() - const url = `${appData.endpoint}/debug/${functionName}` try { - const debug_token = await getDebugToken() const headers = { "authorization": `Bearer ${debug_token}` } const result = await axios.post(url, obj, { headers: headers }) - const response = result.data if (response.error) { console.error(response.error) process.exit(1) } return response - } catch (err) { - console.error(err.message) process.exit(1) } - } /** @@ -71,12 +63,8 @@ export async function debugFunction(functionName: string, obj: Object) { * @param {Object} data * @returns */ - export async function createFunction(appid: string, data: Object) { - - const url = `/sys-api/apps/${appid}/function/create` - const obj = { method: "POST", url, @@ -94,7 +82,6 @@ export async function createFunction(appid: string, data: Object) { * @param {string} functionName * @returns */ - export async function getFunctionByName(appid: string, functionName: string) { const url = `/sys-api/apps/${appid}/function/detail/${functionName}` const obj = { @@ -114,7 +101,6 @@ export async function getFunctionByName(appid: string, functionName: string) { */ export async function pushFunction(appid: string, functionName: string, data: object) { const url = `/sys-api/apps/${appid}/function/save/${functionName}` - const obj = { method: "POST", url, @@ -151,7 +137,6 @@ export async function publishFunction(appid: string, functionName: string) { * @param {string} functionName * @returns */ - export async function publishAllFunction(appid: string) { const url = `/sys-api/apps/${appid}/function/publish` @@ -171,7 +156,6 @@ export async function publishAllFunction(appid: string) { * @param {string} functionId * @returns */ - export async function editFunction(appid: string, functionId: string, data: any) { const url = `/sys-api/apps/${appid}/function/${functionId}/info` @@ -184,16 +168,12 @@ export async function editFunction(appid: string, functionId: string, data: any) return result.data } - - - /** * 删除函数 * @param {string} appid * @param {string} functionId * @returns */ - export async function deleteFunction(appid: string, functionId: string) { const url = `/sys-api/apps/${appid}/function/${functionId}` diff --git a/packages/cli/src/api/oss.ts b/packages/cli/src/api/oss.ts index 779d07e53d..ce662a6f02 100644 --- a/packages/cli/src/api/oss.ts +++ b/packages/cli/src/api/oss.ts @@ -1,14 +1,12 @@ import { requestData } from "./request" - /** * 根据 appid 同步数据 * @param {string} appid * @param {string} bucketName * @returns */ - -export async function detail(appid: string,bucketName:string) { +export async function detail(appid: string, bucketName: string) { const url = `/sys-api/apps/${appid}/oss/buckets/${bucketName}` const obj = { diff --git a/packages/cli/src/api/request.ts b/packages/cli/src/api/request.ts index 1b5f0ddd3e..5e0af7bcf2 100644 --- a/packages/cli/src/api/request.ts +++ b/packages/cli/src/api/request.ts @@ -1,7 +1,5 @@ import axios from 'axios' - import { getRemoteServer } from '../utils/util' - import { getAccessToken } from '../utils/tokens' @@ -13,7 +11,6 @@ export const request = axios.create({ // http request request.interceptors.request.use( async (config) => { - const token = await getAccessToken() if (token) { config.headers.Authorization = `Bearer ${token}` @@ -80,9 +77,7 @@ request.interceptors.response.use( * @param {Object} obj */ export function requestData(obj: object) { - return request.request(obj) - } diff --git a/packages/cli/src/api/sync.ts b/packages/cli/src/api/sync.ts index 25bade79f8..31d58ef607 100644 --- a/packages/cli/src/api/sync.ts +++ b/packages/cli/src/api/sync.ts @@ -6,9 +6,7 @@ import { requestData } from "./request" * @param {string} appid * @returns */ - export async function syncApp(appid: string) { - const url = `/sys-api/apps/${appid}/export` const obj = { method: "GET", diff --git a/packages/cli/src/api/user.ts b/packages/cli/src/api/user.ts index d51c51b595..364ea01352 100644 --- a/packages/cli/src/api/user.ts +++ b/packages/cli/src/api/user.ts @@ -6,25 +6,19 @@ import axios from 'axios' * @param {Object} obj * @returns */ - -export async function loginApi(server:string,obj:Object) { +export async function loginApi(server: string, obj: Object) { // remote server login url - const url = `${server}/sys-api/account/login`; - - try{ - - const result = await axios.post(url,obj,{timeout:5000}) + const url = `${server}/sys-api/account/login` - const response = result.data - - if(response.code!=0){ + try { + const result = await axios.post(url, obj, { timeout: 5000 }) + const response = result.data + if (response.code != 0) { return false } - return { access_token:response.data.access_token,expire_time:response.data.expire } - - }catch(err){ - + return { access_token: response.data.access_token, expire_time: response.data.expire } + } catch (err) { console.error(err.message) process.exit(1) } diff --git a/packages/cli/src/functions.ts b/packages/cli/src/functions.ts index 675e7ac9ea..117214e5a5 100644 --- a/packages/cli/src/functions.ts +++ b/packages/cli/src/functions.ts @@ -3,7 +3,6 @@ import { Command } from 'commander' import { getFunctionByName, publishAllFunction, publishFunction } from './api/functions' import { getAppData, checkFuncNameDir, ensureDirectory } from './utils/util' import { handleInvokeFunctionCommand } from './actions/function-invoke' - import { handlePullOneCommand } from './actions/function-pull-one' import { handlePullListCommand } from './actions/function-pull-list' import { handlePushOneCommand } from './actions/funtcion-push-one' @@ -11,7 +10,6 @@ import { handlePushListCommand } from './actions/function-push-list' import { FUNCTIONS_DIR } from './utils/constants' export function makeFnCommand() { - const fn = new Command('fn') fn @@ -19,18 +17,13 @@ export function makeFnCommand() { .argument('[function-name]', "functionname") .option('-f, --force-overwrite', 'force to file ignore if modified', false) .action(async (functionName, options) => { - const appData = getAppData() if (functionName) { - await handlePullOneCommand(appData.appid, functionName) } else { - await handlePullListCommand(appData.appid, options) - } - }) fn @@ -46,13 +39,10 @@ export function makeFnCommand() { checkFuncNameDir(functionName) await handleInvokeFunctionCommand(appData.appid, functionName, debugParams) - } catch (err) { console.error(err.message) process.exit(1) - } - }) @@ -66,12 +56,9 @@ export function makeFnCommand() { checkFuncNameDir(functionName) handlePushOneCommand(appData.appid, functionName) } else { - ensureDirectory(FUNCTIONS_DIR) handlePushListCommand(appData.appid, options) - } - // check fucntion }) @@ -79,7 +66,6 @@ export function makeFnCommand() { .command('publish') .argument('[function-name]', "functionname") .action(async (functionName) => { - const appData = getAppData() if (functionName) { // get function @@ -94,7 +80,6 @@ export function makeFnCommand() { } else { console.log(`${functionName} funtion not exist`) } - } else { const result = await publishAllFunction(appData.appid) @@ -102,10 +87,8 @@ export function makeFnCommand() { console.log('publish success') } } - }) return fn - } diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 78b754e721..967b313efb 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -14,7 +14,6 @@ import { makeFnCommand } from './functions' import { makeOssCommand } from './oss' - program .option('-v, --version', 'output version') .action((options) => { @@ -33,7 +32,6 @@ program .option('-p, --password ', 'password') .option('-r, --remote ', 'remote server', "https://console.lafyun.com") .action(async (options) => { - // check params const username = options.username const password = options.password @@ -48,8 +46,6 @@ program // login await handleLoginCommand(options.remote, username, password) - - }) @@ -70,8 +66,7 @@ program //sync app (now only pull function) await handleSyncAppCommand(appid) } - } - catch (err) { + } catch (err) { console.log(err.message) } }) @@ -79,17 +74,15 @@ program program .command('list') + .description('show application list') .action(async () => { - await handleAppListCommand() - }) program .command('stop ') .option('--env ', `the file name to generate`, '.env') .action(async (appid) => { - const response = await appStop(appid) if (response.data.result) { @@ -102,9 +95,7 @@ program program .command('start ') - .option('--env ', `the file name to generate`, '.env') .action(async (appid) => { - const response = await appStart(appid) if (response.data.result) { @@ -117,9 +108,7 @@ program program .command('restart ') - .option('--env ', `the file name to generate`, '.env') .action(async (appid) => { - const response = await appRestart(appid) if (response.data.result) { @@ -130,9 +119,7 @@ program }) program.addCommand(makeFnCommand()) - program.addCommand(makeOssCommand()) - program.parse(process.argv) diff --git a/packages/cli/src/oss.ts b/packages/cli/src/oss.ts index 1d9ce349b1..ea928d5fab 100644 --- a/packages/cli/src/oss.ts +++ b/packages/cli/src/oss.ts @@ -1,11 +1,9 @@ import { Command } from 'commander' import { detail } from './api/oss' import { handlePushCommand, handlePullCommand } from './actions/oss' - import { getAppData } from "./utils/util" export function makeOssCommand() { - const oss = new Command('oss') oss @@ -14,15 +12,14 @@ export function makeOssCommand() { .argument('out-path', "out-path") .option('-f, --force-overwrite', 'force to file ignore if modified', false) .action(async (bucket, outPath, options) => { - const appData = getAppData() + //get bucket detail const buckets = await detail(appData.appid, bucket) options.outPath = outPath options.bucketName = `${appData.appid}-${bucket}` options.endpoint = appData.oss_endpoint await handlePullCommand(buckets.data.credentials, options) - }) oss @@ -30,9 +27,7 @@ export function makeOssCommand() { .argument('input-path', "input-path") .argument('bucket', "bucket") .option('-f, --force-overwrite', 'force to file ignore if modified', false) - .action(async (inputPath, bucket, options) => { - const appData = getAppData() // get bucket detail const buckets = await detail(appData.appid, bucket) @@ -40,9 +35,7 @@ export function makeOssCommand() { options.bucketName = `${appData.appid}-${bucket}` options.endpoint = appData.oss_endpoint await handlePushCommand(buckets.data.credentials, options) - }) return oss - } diff --git a/packages/cli/src/utils/function-lists.ts b/packages/cli/src/utils/function-lists.ts index 0f5567568a..e33df50203 100644 --- a/packages/cli/src/utils/function-lists.ts +++ b/packages/cli/src/utils/function-lists.ts @@ -8,9 +8,7 @@ import { FUNCTIONS_FILE, META_FILE } from './constants' * @returns */ export function getRemoteList(data: any) { - return data.map((item: any) => { - return { key: item.name, version: item.version, value: item } }) } diff --git a/packages/cli/src/utils/function-pull.ts b/packages/cli/src/utils/function-pull.ts index 0cd43fdef4..cb7fe4f253 100644 --- a/packages/cli/src/utils/function-pull.ts +++ b/packages/cli/src/utils/function-pull.ts @@ -1,23 +1,17 @@ -import { ensureDirectory } from "./util" import * as path from 'node:path' -import { FUNCTIONS_DIR, FUNCTIONS_FILE, META_FILE, PROJECT_DIR } from "./constants" import * as fs from 'node:fs' - +import { ensureDirectory } from "./util" +import { FUNCTIONS_DIR, FUNCTIONS_FILE, META_FILE, PROJECT_DIR } from "./constants" const functionsDir = path.resolve(PROJECT_DIR, FUNCTIONS_DIR) - /** * create function list * @param { localList } any * @param { remoteList } any * @returns */ - export function getCreateList(remoteList: any, localList: any) { - - - const createFunction = remoteList.filter(remote => { const local = localList.find(local => local.key === remote.key) @@ -36,7 +30,6 @@ export function getCreateList(remoteList: any, localList: any) { * @param { remoteList } any * @returns */ - export function getUpdateList(remoteList: any, localList: any,) { const updateFunction = remoteList.filter(local => { const remote = localList.find(remote => local.key === remote.key && local.version != remote.version) @@ -45,7 +38,6 @@ export function getUpdateList(remoteList: any, localList: any,) { } }) return updateFunction - } @@ -55,7 +47,6 @@ export function getUpdateList(remoteList: any, localList: any,) { * @param { remoteList } any * @returns */ - export function getDeleteList(remoteList: any, localList: any) { const deleteFunction = localList.filter(local => { const remote = remoteList.find(remote => local.key === remote.key) @@ -64,12 +55,8 @@ export function getDeleteList(remoteList: any, localList: any) { } }) return deleteFunction - } - - - /** * create function * @param { data } any @@ -97,7 +84,6 @@ export function createfn(data: any) { * @param { data } any * @returns */ - export function updatefn(data: any) { const funcNameDir = path.resolve(functionsDir, data.name) @@ -120,7 +106,6 @@ export function updatefn(data: any) { fs.writeFileSync(metaFile, JSON.stringify(meta)) return true } - } @@ -129,7 +114,6 @@ export function updatefn(data: any) { * @param { funcName } string * @returns */ - export function deletefn(funcName: string) { const funcNameDir = path.resolve(functionsDir, funcName) diff --git a/packages/cli/src/utils/function-push.ts b/packages/cli/src/utils/function-push.ts index 8e019f3c78..5335f54c56 100644 --- a/packages/cli/src/utils/function-push.ts +++ b/packages/cli/src/utils/function-push.ts @@ -1,25 +1,19 @@ import * as path from 'node:path' -import { FUNCTIONS_DIR, FUNCTIONS_FILE, META_FILE, PROJECT_DIR } from "./constants" import * as fs from 'node:fs' import { compileTs2js } from "./util-lang" import { createFunction, deleteFunction, editFunction, getFunctionByName, pushFunction } from "../api/functions" - +import { FUNCTIONS_DIR, FUNCTIONS_FILE, META_FILE, PROJECT_DIR } from "./constants" const functionsDir = path.resolve(PROJECT_DIR, FUNCTIONS_DIR) - /** * create function list * @param { localList } any * @param { remoteList } any * @returns */ - export function getCreateList(remoteList: any, localList: any) { - - - const createFunction = localList.filter(local => { const remote = remoteList.find(remote => local.key === remote.key) @@ -38,7 +32,6 @@ export function getCreateList(remoteList: any, localList: any) { * @param { remoteList } any * @returns */ - export function getUpdateList(remoteList: any, localList: any,) { const updateFunction = remoteList.filter(local => { const remote = localList.find(remote => local.key === remote.key && local.version != remote.version) @@ -57,7 +50,6 @@ export function getUpdateList(remoteList: any, localList: any,) { * @param { remoteList } any * @returns */ - export function getDeleteList(remoteList: any, localList: any) { const deleteFunction = remoteList.filter(remote => { const local = localList.find(local => local.key === remote.key) @@ -66,7 +58,6 @@ export function getDeleteList(remoteList: any, localList: any) { } }) return deleteFunction - } @@ -100,13 +91,11 @@ export async function createRemoteFn(appid: string, functionName: string) { } const res = await createFunction(appid, data) - if (res.data) { // create meta file await createMetaFile(appid, functionName) console.log("create success") } - } /** @@ -114,7 +103,6 @@ export async function createRemoteFn(appid: string, functionName: string) { * @param { data } any * @returns */ - export async function updateRemoteFn(appid: string, functionName: string) { const functionNameDir = path.resolve(functionsDir, functionName) @@ -143,12 +131,10 @@ export async function updateRemoteFn(appid: string, functionName: string) { * @param { funcName } string * @returns */ - export async function deleteRemoteFn(appid: string, functionId: string) { const editResult = await editFunction(appid, functionId, { 'status': 0 }) if (!editResult.data.modifiedCount) { console.error('修改失败') - } const res = await deleteFunction(appid, functionId,) if (!res.data.deletedCount) { @@ -163,7 +149,6 @@ export async function deleteRemoteFn(appid: string, functionId: string) { * @param { functionName } string * @returns */ - async function createMetaFile(appid: string, functionName: string) { // get function @@ -175,7 +160,6 @@ async function createMetaFile(appid: string, functionName: string) { const metaFile = path.resolve(funcNameDir, META_FILE) fs.writeFileSync(metaFile, JSON.stringify(meta)) - } @@ -197,5 +181,4 @@ function getMetaData(data: any) { triggers: data.triggers, version: data.version } - } \ No newline at end of file diff --git a/packages/cli/src/utils/tokens.ts b/packages/cli/src/utils/tokens.ts index 943ee6146f..fcd16d9b85 100644 --- a/packages/cli/src/utils/tokens.ts +++ b/packages/cli/src/utils/tokens.ts @@ -9,7 +9,6 @@ import { getAppData } from './util' * get access token * @returns */ - export async function getAccessToken() { // read data const authData = JSON.parse(fs.readFileSync(AUTH_FILE, 'utf8')) @@ -32,5 +31,4 @@ export async function getDebugToken() { const appData = getAppData() const response = await getApplicationByAppid(appData.appid) return response.data.debug_token - } diff --git a/packages/cli/src/utils/util-lang.ts b/packages/cli/src/utils/util-lang.ts index 03184f7777..6bf8b4af78 100644 --- a/packages/cli/src/utils/util-lang.ts +++ b/packages/cli/src/utils/util-lang.ts @@ -1,5 +1,4 @@ - import * as ts from 'typescript' diff --git a/packages/cli/src/utils/util.ts b/packages/cli/src/utils/util.ts index 8a57b7118c..22608b56c3 100644 --- a/packages/cli/src/utils/util.ts +++ b/packages/cli/src/utils/util.ts @@ -2,7 +2,7 @@ import * as fs from 'node:fs' import * as path from 'node:path' import { CREDENTIALS_DIR, DEFAULT_SERVER, PROJECT_DIR } from './constants' import { AUTH_FILE, LAF_CONFIG_FILE, FUNCTIONS_DIR } from '../utils/constants' -const AWS = require('aws-sdk') +import * as AWS from 'aws-sdk' /** * check auth dir