create-forder.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * fun 结点:create-folder(脚本名 create-forder 为历史拼写)
  3. * inVars[0] 或字段 path:目录路径(相对当前流程目录或绝对路径)
  4. * 使用 fs.mkdirSync(..., { recursive: true })
  5. */
  6. const path = require('path')
  7. const fs = require('fs')
  8. function buildAbsolutePath (p, folderPath) {
  9. if (p == null || p === '') return null
  10. const s = typeof p === 'string' ? p.trim() : String(p).trim()
  11. if (!s) return null
  12. if (path.isAbsolute(s) || /^[A-Za-z]:/.test(s)) return path.normalize(s)
  13. return folderPath ? path.join(folderPath, s) : path.resolve(s)
  14. }
  15. /**
  16. * @param {{ path?: string, dirPath?: string, folderPath?: string }} input
  17. */
  18. async function executeCreateFolder ({ path: p, dirPath, folderPath }) {
  19. const rel = p != null && String(p).trim() !== '' ? p : dirPath
  20. if (rel == null || String(rel).trim() === '') {
  21. return { success: false, error: 'create-folder 缺少 path(inVars[0] 或字段 path)' }
  22. }
  23. const abs = buildAbsolutePath(String(rel).trim(), folderPath)
  24. if (!abs) return { success: false, error: 'create-folder 路径无效' }
  25. try {
  26. fs.mkdirSync(abs, { recursive: true })
  27. } catch (e) {
  28. return { success: false, error: e && e.message ? e.message : String(e) }
  29. }
  30. return {
  31. success: true,
  32. path: abs,
  33. value: abs,
  34. result: abs,
  35. }
  36. }
  37. module.exports = { executeCreateFolder }