| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- /**
- * fun 结点:remove-folder(脚本名 remove-forder 为历史拼写)
- * inVars[0] 或字段 path:目录路径(相对当前流程目录或绝对路径)
- * 默认递归删除目录及内容(等同 rm -rf)。若仅需删除空目录,设 recursive 为 0 / false / no / empty-only
- */
- const path = require('path')
- const fs = require('fs')
- function buildAbsolutePath (p, folderPath) {
- if (p == null || p === '') return null
- const s = typeof p === 'string' ? p.trim() : String(p).trim()
- if (!s) return null
- if (path.isAbsolute(s) || /^[A-Za-z]:/.test(s)) return path.normalize(s)
- return folderPath ? path.join(folderPath, s) : path.resolve(s)
- }
- /** 未指定时默认 true(非空目录也可删);0 / false / no / empty-only 则仅删空目录 */
- function parseRecursive (raw) {
- if (raw == null || raw === '') return true
- const t = String(raw).trim().toLowerCase()
- if (t === '') return true
- if (t === '0' || t === 'false' || t === 'no' || t === 'empty-only') return false
- return true
- }
- /**
- * @param {{ path?: string, dirPath?: string, recursive?: string, folderPath?: string }} input
- */
- async function executeRemoveFolder ({ path: p, dirPath, recursive, folderPath }) {
- const rel = p != null && String(p).trim() !== '' ? p : dirPath
- if (rel == null || String(rel).trim() === '') {
- return { success: false, error: 'remove-folder 缺少 path(inVars[0] 或字段 path)' }
- }
- const abs = buildAbsolutePath(String(rel).trim(), folderPath)
- if (!abs) return { success: false, error: 'remove-folder 路径无效' }
- const rec = parseRecursive(recursive)
- try {
- if (!fs.existsSync(abs)) return { success: true, skipped: true, path: abs }
- const st = fs.statSync(abs)
- if (!st.isDirectory()) return { success: false, error: 'remove-folder 目标不是目录' }
- fs.rmSync(abs, { recursive: rec, force: false })
- } catch (e) {
- if (!rec && (e.code === 'ENOTEMPTY' || e.code === 'EISDIR')) {
- return { success: false, error: '目录非空或无法以非递归方式删除:请去掉 recursive=false/0,或不要设置 recursive(默认递归删除整目录)' }
- }
- return { success: false, error: e && e.message ? e.message : String(e) }
- }
- return { success: true, path: abs }
- }
- module.exports = { executeRemoveFolder }
|