Skip to content

Commit

Permalink
feat(cli): support multi folder for functions (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Jul 31, 2023
1 parent d223f5f commit 3f387ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
35 changes: 26 additions & 9 deletions cli/src/action/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { FUNCTION_SCHEMA_DIRECTORY } from '../../common/constant'
import { confirm } from '../../common/prompts'
import { AppSchema } from '../../schema/app'
import { FunctionSchema } from '../../schema/function'
import * as urlencode from 'urlencode'
import { lstatSync } from 'fs'

export async function create(
funcName: string,
Expand Down Expand Up @@ -64,7 +66,7 @@ export async function list() {

export async function del(funcName: string) {
const appSchema = AppSchema.read()
await functionControllerRemove(appSchema.appid, funcName)
await functionControllerRemove(appSchema.appid, urlencode(funcName))
if (FunctionSchema.exist(funcName)) {
FunctionSchema.delete(funcName)
}
Expand All @@ -77,7 +79,7 @@ export async function del(funcName: string) {

async function pull(funcName: string) {
const appSchema = AppSchema.read()
const func = await functionControllerFindOne(appSchema.appid, funcName)
const func = await functionControllerFindOne(appSchema.appid, urlencode(funcName))
const functionSchema: FunctionSchema = {
name: func.name,
description: func.description,
Expand Down Expand Up @@ -143,7 +145,7 @@ async function push(funcName: string, isCreate: boolean) {
code,
tags: funcSchema.tags,
}
await functionControllerUpdate(appSchema.appid, funcName, updateDto)
await functionControllerUpdate(appSchema.appid, urlencode(funcName), updateDto)
}
}

Expand All @@ -169,12 +171,12 @@ export async function pushAll(options: { force: boolean }) {
for (let item of serverFuncs) {
if (!localFuncMap.has(item.name)) {
if (options.force) {
await functionControllerRemove(appSchema.appid, item.name)
await functionControllerRemove(appSchema.appid, urlencode(item.name))
console.log(`${getEmoji('✅')} function ${item.name} deleted`)
} else {
const res = await confirm('confirm remove function ' + item.name + '?')
if (res.value) {
await functionControllerRemove(appSchema.appid, item.name)
await functionControllerRemove(appSchema.appid, urlencode(item.name))
console.log(`${getEmoji('✅')} function ${item.name} deleted`)
} else {
console.log(`${getEmoji('🎃')} cancel remove function ${item.name}`)
Expand Down Expand Up @@ -222,7 +224,7 @@ export async function exec(
code,
}
const appSchema = AppSchema.read()
const func = await functionControllerCompile(appSchema.appid, funcName, compileDto)
const func = await functionControllerCompile(appSchema.appid, urlencode(funcName), compileDto)

// transform headers json string to object. -H '{"Content-Type": "application/json"}'
if (options.headers) {
Expand Down Expand Up @@ -279,13 +281,28 @@ async function printLog(appid: string, requestId: string) {
}
}

function getLocalFuncs() {
function getLocalFuncs(): string[] {
const funcDir = path.join(getBaseDir(), FUNCTION_SCHEMA_DIRECTORY)
const files = fs.readdirSync(funcDir)
const funcs = files.filter((file) => file.endsWith('.ts')).map((file) => file.replace('.ts', ''))
const funcs = getLocalFunction(funcDir, '')
return funcs
}

function getLocalFunction(dir: string, prefix: string): string[] {
const files = fs.readdirSync(dir)
const funcNames: string[] =[]
files.forEach((file) => {
const filePath = path.join(dir, file)
const stat = lstatSync(filePath)
if (stat.isDirectory()) {
funcNames.push(...getLocalFunction(filePath, path.join(prefix || '', file)))
}
if (stat.isFile() && file.endsWith('.ts')) {
funcNames.push(path.join(prefix || '', file).replace(/\.ts$/, ''))
}
})
return funcNames
}

function removeFunction(name: string) {
if (FunctionSchema.exist(name)) {
FunctionSchema.delete(name)
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function command(): Command {
.option('-t --tags <items...>', 'tags', [])
.option('-d --description <description>', 'function description', '')
.action((funcName, options) => {
if (!/^[_A-Za-z][A-Za-z0-9-_]+$/.test(funcName)) {
return console.log('Function names can only contain English letters or underscores (_).')
if (!/^[a-zA-Z0-9_.\-/]{1,256}$/.test(funcName)) {
return console.log('Function names must consist of letters, numbers, periods (.), and hyphens (-), matching the regex: /^[a-zA-Z0-9.-]{1,128}$/.')
}
create(funcName, options)
})
Expand Down
7 changes: 7 additions & 0 deletions cli/src/schema/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path'
import { FUNCTION_SCHEMA_DIRECTORY, FUNCTION_SCHEMA_SUFFIX } from '../common/constant'
import { exist, loadYamlFile, remove, writeYamlFile } from '../util/file'
import { getBaseDir } from '../util/sys'
import { mkdirSync } from 'fs'

export class FunctionSchema {
name: string
Expand All @@ -15,6 +16,12 @@ export class FunctionSchema {
}

static write(name: string, schema: FunctionSchema): void {
if (path.dirname(name) !== '.') {
const dir = path.join(getBaseDir(), FUNCTION_SCHEMA_DIRECTORY, path.dirname(name))
if (!exist(dir)) {
mkdirSync(dir, { recursive: true })
}
}
const funcConfigPath = path.join(getBaseDir(), FUNCTION_SCHEMA_DIRECTORY, name + FUNCTION_SCHEMA_SUFFIX)
return writeYamlFile(funcConfigPath, schema)
}
Expand Down

0 comments on commit 3f387ec

Please sign in to comment.