Skip to content

Commit

Permalink
Adds listdir mask
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarlosn committed Apr 28, 2024
1 parent 0971039 commit c21b26c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/protolib/bundles/os/context2/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {readFile} from './readFile'
import {writeFile} from './writeFile'
import { runCommand } from './RunCommand'
import {listDir} from './listDir'
export default {
readFile,
writeFile,
listDir,
runCommand
}
24 changes: 24 additions & 0 deletions packages/protolib/bundles/os/context2/listDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from 'fs/promises'
import {join} from 'path'

export const listDir = async(options:{
path: string,
done?: (list) => {},
error?: (err) => {}
}) => {
const path = options.path
const done = options.done || (() => {})
const error = options.error

try {
const list = await fs.readdir(join('../../', path))
done(list)
return list
} catch(err) {
if(error) {
error(err)
} else {
throw err
}
}
}
46 changes: 46 additions & 0 deletions packages/protolib/bundles/os/masks2/ListDir.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Node, NodeOutput, NodeParams, filterObject, restoreObject } from 'protoflow';
import { useColorFromPalette } from 'protoflow/src/diagram/Theme'
import { Folder } from 'lucide-react'

const ListDir = ({ node = {}, nodeData = {}, children }: any) => {
const color = useColorFromPalette(12)
return (
<Node icon={Folder} node={node} isPreview={!node.id} title='List Directory' color={color} id={node.id} skipCustom={true}>
<NodeParams id={node.id} params={[{ label: 'Path', field: 'mask-path', type: 'input' }]} />
<div style={{height: '30px'}} />
<NodeOutput id={node.id} type={'input'} label={'Done'} vars={['list']} handleId={'mask-done'} />
<NodeOutput id={node.id} type={'input'} label={'Error'} vars={['err']} handleId={'mask-error'} />
</Node>
)
}

export default {
id: 'os2.listDir',
type: 'CallExpression',
category: "OS",
keywords: ['fs', 'os', 'read', 'directory', 'list'],
check: (node, nodeData) => {
return node.type == "CallExpression" && nodeData.to?.startsWith('context.os2.listDir')
},
getComponent: (node, nodeData, children) => <ListDir node={node} nodeData={nodeData} children={children} />,
filterChildren: filterObject({keys: {
path: 'input',
done: 'output',
error: 'output'
}}),
restoreChildren: restoreObject({keys: {
path: 'input',
done: { params: {'param-done': { key: "list"}}},
error: { params: { 'param-error': { key: "err"}}}
}}),
getInitialData: () => {
return {
await: true,
to: 'context.os2.listDir',
"mask-path": {
value: "",
kind: "StringLiteral"
}
}
}
}
2 changes: 2 additions & 0 deletions packages/protolib/bundles/os/masks2/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import ReadFile from "./ReadFile";
import WriteFile from './WriteFile';
import RunCommand from "./RunCommand";
import ListDir from "./ListDir";
export default [
ReadFile,
WriteFile,
ListDir,
RunCommand
]

0 comments on commit c21b26c

Please sign in to comment.