From 151921fb509d9935526b5b23eeaea1794dc5b576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noem=C3=AD=20Bl=C3=A1zquez?= Date: Mon, 29 Apr 2024 00:23:05 +0200 Subject: [PATCH] Adds mkdir mask --- .../protolib/bundles/os/context2/index.ts | 4 +- .../protolib/bundles/os/context2/makeDir.ts | 24 ++++++++++ .../protolib/bundles/os/masks/MkdirSync.tsx | 1 + .../protolib/bundles/os/masks2/MakeDir.tsx | 46 +++++++++++++++++++ packages/protolib/bundles/os/masks2/index.tsx | 4 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 packages/protolib/bundles/os/context2/makeDir.ts create mode 100644 packages/protolib/bundles/os/masks2/MakeDir.tsx diff --git a/packages/protolib/bundles/os/context2/index.ts b/packages/protolib/bundles/os/context2/index.ts index 8a20cd1f9..032abf545 100644 --- a/packages/protolib/bundles/os/context2/index.ts +++ b/packages/protolib/bundles/os/context2/index.ts @@ -2,9 +2,11 @@ import {readFile} from './readFile' import {writeFile} from './writeFile' import { runCommand } from './RunCommand' import {listDir} from './listDir' +import { makeDir } from './makeDir' export default { readFile, writeFile, listDir, - runCommand + runCommand, + makeDir } \ No newline at end of file diff --git a/packages/protolib/bundles/os/context2/makeDir.ts b/packages/protolib/bundles/os/context2/makeDir.ts new file mode 100644 index 000000000..c31137d65 --- /dev/null +++ b/packages/protolib/bundles/os/context2/makeDir.ts @@ -0,0 +1,24 @@ +import fs from 'fs/promises'; +import { join } from 'path'; + +export const makeDir = async(options: { + path: string, + done?: (dirPath) => {}, + error?: (err) => {} +}) => { + const path = options.path; + const done = options.done || (() => {}); + const error = options.error; + + try { + await fs.mkdir(join('../../', path), { recursive: true }); + done(path) + return path; + } catch(err) { + if (error) { + error(err); + } else { + throw err; + } + } +} \ No newline at end of file diff --git a/packages/protolib/bundles/os/masks/MkdirSync.tsx b/packages/protolib/bundles/os/masks/MkdirSync.tsx index ddb95236e..521820448 100644 --- a/packages/protolib/bundles/os/masks/MkdirSync.tsx +++ b/packages/protolib/bundles/os/masks/MkdirSync.tsx @@ -29,6 +29,7 @@ export default { id: 'CreateFolder', type: 'CallExpression', category: 'OS', + hidden: true, keywords: ['fs', 'os', 'make', 'dir', 'mkdir'], check: (node, nodeData) => { return node.type == "CallExpression" && nodeData.to?.startsWith('context.os.createFolder') diff --git a/packages/protolib/bundles/os/masks2/MakeDir.tsx b/packages/protolib/bundles/os/masks2/MakeDir.tsx new file mode 100644 index 000000000..a349894b8 --- /dev/null +++ b/packages/protolib/bundles/os/masks2/MakeDir.tsx @@ -0,0 +1,46 @@ +import { Node, NodeOutput, NodeParams, filterObject, restoreObject } from 'protoflow'; +import { useColorFromPalette } from 'protoflow/src/diagram/Theme' +import { FolderPlus } from 'lucide-react' + +const MakeDir = ({ node = {}, nodeData = {}, children }: any) => { + const color = useColorFromPalette(10) // Cambio de color para diferenciar del ListDir + return ( + + +
+ + + + ) +} + +export default { + id: 'os2.makeDir', + type: 'CallExpression', + category: "OS", + keywords: ['fs', 'os', 'create', 'directory', 'mkdir'], + check: (node, nodeData) => { + return node.type == "CallExpression" && nodeData.to?.startsWith('context.os2.makeDir') + }, + getComponent: (node, nodeData, children) => , + filterChildren: filterObject({keys: { + path: 'input', + done: 'output', + error: 'output' + }}), + restoreChildren: restoreObject({keys: { + path: 'input', + done: { params: {'param-done': { key: "path"}}}, + error: { params: { 'param-error': { key: "err"}}} + }}), + getInitialData: () => { + return { + await: true, + to: 'context.os2.makeDir', + "mask-path": { + value: "", + kind: "StringLiteral" + } + } + } +} \ No newline at end of file diff --git a/packages/protolib/bundles/os/masks2/index.tsx b/packages/protolib/bundles/os/masks2/index.tsx index 57ec3794c..67aac630b 100644 --- a/packages/protolib/bundles/os/masks2/index.tsx +++ b/packages/protolib/bundles/os/masks2/index.tsx @@ -2,9 +2,11 @@ import ReadFile from "./ReadFile"; import WriteFile from './WriteFile'; import RunCommand from "./RunCommand"; import ListDir from "./ListDir"; +import MakeDir from "./MakeDir"; export default [ ReadFile, WriteFile, ListDir, - RunCommand + RunCommand, + MakeDir ] \ No newline at end of file