diff --git a/cli/src/action/application/index.ts b/cli/src/action/application/index.ts index aade852f3d..c98b37cc88 100644 --- a/cli/src/action/application/index.ts +++ b/cli/src/action/application/index.ts @@ -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`) } diff --git a/cli/src/action/function/index.ts b/cli/src/action/function/index.ts index e980abe46f..8cbde080d4 100644 --- a/cli/src/action/function/index.ts +++ b/cli/src/action/function/index.ts @@ -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() 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`) + } + } + } + } } @@ -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) + } +} \ No newline at end of file diff --git a/cli/src/command/function/index.ts b/cli/src/command/function/index.ts index 2d6959b4b2..5564507405 100644 --- a/cli/src/command/function/index.ts +++ b/cli/src/command/function/index.ts @@ -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) } })