Skip to content

Commit

Permalink
fix(cli): opt func pull command (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Jun 16, 2023
1 parent e077f76 commit 75deeb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/src/action/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function init(appid: string, options: { sync: boolean }) {
// pull policies
policyPull()
// pull functions
funcPull()
funcPull({ force: true })
}
console.log(`${getEmoji('🚀')} application ${app.name} init success`)
}
Expand Down
31 changes: 30 additions & 1 deletion cli/src/action/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,31 @@ async function pull(funcName: string) {
fs.writeFileSync(codePath, func.source.code)
}

export async function pullAll() {
export async function pullAll(options: { force: boolean }) {
const appSchema = AppSchema.read()
const funcs = await functionControllerFindAll(appSchema.appid)
const serverFuncMap = new Map<string, boolean>()
for (let func of funcs) {
await pull(func.name)
console.log(`${getEmoji('✅')} function ${func.name} pulled`)
serverFuncMap.set(func.name, true)
}
// remove remote not exist function
const localFuncs = getLocalFuncs()
for (let item of localFuncs) {
if (!serverFuncMap.has(item)) {
if (options.force) {
removeFunction(item)
console.log(`${getEmoji('✅')} function ${item} deleted`)
} else {
const res = await confirm('confirm remove function in local ' + item + '?')
if (res.value) {
removeFunction(item)
console.log(`${getEmoji('✅')} function ${item} deleted`)
}
}
}

}
}

Expand Down Expand Up @@ -266,3 +285,13 @@ function getLocalFuncs() {
const funcs = files.filter((file) => file.endsWith('.ts')).map((file) => file.replace('.ts', ''))
return funcs
}

function removeFunction(name: string) {
if (FunctionSchema.exist(name)) {
FunctionSchema.delete(name)
}
const funcPath = path.join(getBaseDir(), 'functions', name + '.ts')
if (exist(funcPath)) {
remove(funcPath)
}
}
5 changes: 3 additions & 2 deletions cli/src/command/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ export function command(): Command {
cmd
.command('pull')
.argument('[funcName]', 'funcName')
.option('-f, --force', 'force to overwrite the local', false)
.description('pull function, if funcName does not exist, pull all')
.action((funcName) => {
.action((funcName, options) => {
if (funcName) {
pullOne(funcName)
} else {
pullAll()
pullAll(options)
}
})

Expand Down

0 comments on commit 75deeb3

Please sign in to comment.