Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): opt func pull command #1271

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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