Skip to content

Commit

Permalink
Merge branch 'ajthinking:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-lyl authored Oct 28, 2024
2 parents fde7798 + f12b9b2 commit 08bd33f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/core",
"version": "0.0.105",
"version": "0.0.107",
"main": "dist/main/index.js",
"type": "commonjs",
"types": "dist/main/index.d.ts",
Expand Down
Binary file added packages/ds-ext/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions packages/ds-ext/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "ds-ext",
"displayName": "ds-ext",
"displayName": "data-story",
"description": "",
"version": "0.0.105",
"version": "0.0.107",
"publisher": "ajthinking",
"icon": "icon.png",
"engines": {
"vscode": "^1.80.0"
},
Expand Down
59 changes: 55 additions & 4 deletions packages/ds-ext/src/app/onDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,69 @@ import { NodeDescription, str } from '@data-story/core';
export const onDrop = (event: any, addNodeFromDescription: any) => {
event.preventDefault();

if(isDropFromOs(event)) return onDropFromOs(
event,
addNodeFromDescription
);

if(isDropFromExplorer(event)) return onDropFromExplorer(
event,
addNodeFromDescription
);
};

export const isDropFromOs = (event: any) => {
return event.dataTransfer.files.length;
};

export const isDropFromExplorer = (event: any) => {
return event.dataTransfer.getData('text/plain');
};

export const onDropFromOs = (event: any, addNodeFromDescription: any) => {
const file = event.dataTransfer.files[0];
const path = file.path;
const filename = path.split('/').pop();
const extention = filename.split('.').pop();

if (extention !== 'json') {
console.warn({
message: 'Currently, only JSON files are supported.',
path,
filename,
extention,
});

return;
}

const description = createJsonFileReadDescription(filename, path);
addNodeFromDescription(description);
};

export const onDropFromExplorer = (event: any, addNodeFromDescription: any) => {
// DROP FROM EXPLORER
const path = event.dataTransfer.getData('text/plain');
const filename = path.split('/').pop();
const extention = filename.split('.').pop();

if (extention !== 'json') {
console.warn('Currently, only JSON files are supported.');
console.warn({
message: 'Currently, only JSON files are supported.',
path,
filename,
extention,
});

return;
}

const description: NodeDescription = {
const description = createJsonFileReadDescription(filename, path);
addNodeFromDescription(description);
};

export const createJsonFileReadDescription = (filename: string, path: string): NodeDescription => {
return {
name: 'JsonFile.read',
label: filename,
inputs: [],
Expand Down Expand Up @@ -40,6 +93,4 @@ export const onDrop = (event: any, addNodeFromDescription: any) => {
}),
],
};

addNodeFromDescription(description);
};
2 changes: 1 addition & 1 deletion packages/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/nodejs",
"version": "0.0.105",
"version": "0.0.107",
"main": "dist/index.js",
"type": "commonjs",
"types": "dist/index.d.ts",
Expand Down
31 changes: 24 additions & 7 deletions packages/nodejs/src/computers/JsonFileRead/JsonFileRead.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as glob from 'glob';
import fs from 'fs';
import path from 'path'; // Import path module
import { Computer, get, serializeError, str } from '@data-story/core';

export const JsonFileRead: Computer = {
Expand Down Expand Up @@ -33,15 +34,31 @@ export const JsonFileRead: Computer = {
async *run({ output, params }) {
const pathPattern = params.file_path as string;
const itemsPath = params.items_path as string;

// Check if the provided path is absolute
const isAbsolutePath = path.isAbsolute(pathPattern);

let files: string[] = [];
try {
// Use glob to get all matching file paths
const files = glob.sync(pathPattern, {
cwd: process.env.WORKSPACE_FOLDER_PATH,
ignore: ['**/node_modules/**'],
root: process.env.WORKSPACE_FOLDER_PATH,
absolute: true,
});
if (isAbsolutePath) {
// If it's an absolute path, use it directly with glob
files = glob.sync(pathPattern, {
ignore: ['**/node_modules/**'],
absolute: true,
});
} else {
// If it's a relative path, resolve using the workspace folder
const cwd = process.env.WORKSPACE_FOLDER_PATH as string;
files = glob.sync(pathPattern, {
cwd, // Resolve relative paths from the workspace folder
ignore: ['**/node_modules/**'],
absolute: true,
});
}

console.log({ files }); // Debug output

// Process each file found by glob
for (const file of files) {
try {
const content = fs.readFileSync(file, 'utf-8');
Expand Down
32 changes: 22 additions & 10 deletions packages/nodejs/src/computers/JsonFileWrite/JsonFileWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,32 @@ export const JsonFileWrite: Computer = {
],

canRun({ input }) {
return input.haveAllItemsAtInput('input')
return input.haveAllItemsAtInput('input');
},

async *run({ input, params }) {
const incoming = input.pull()
const incoming = input.pull();
const filePath = params.file_path as string;
const content = JSON.stringify(incoming.map(i => i.value), null, 2);

const filePath = params.file_path as string
const content = JSON.stringify(incoming.map(i => i.value), null, 2)
// Determine if the path is absolute
const isAbsolutePath = path.isAbsolute(filePath);

const root = process.env.WORKSPACE_FOLDER_PATH;
if(!root) throw new Error('WORKSPACE_FOLDER_PATH not set')
// Resolve the full path based on whether it's absolute or relative
const fullPath = isAbsolutePath
? filePath // Use the absolute path directly
: path.join(process.env.WORKSPACE_FOLDER_PATH as string, filePath); // Prepend the workspace root for relative paths

const fullPath = path.join(root, filePath)
await fs.mkdir(path.dirname(fullPath), { recursive: true })
await fs.writeFile(fullPath, content, 'utf-8')
console.log({ fullPath }); // Debug output to check resolved path

try {
// Create the directory recursively if it doesn't exist
await fs.mkdir(path.dirname(fullPath), { recursive: true });
// Write the file content
await fs.writeFile(fullPath, content, 'utf-8');
} catch (error: any) {
console.error('Error writing file:', error);
throw new Error(`Failed to write file: ${error.message}`);
}
},
};
};
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/ui",
"version": "0.0.105",
"version": "0.0.107",
"main": "./dist/bundle.js",
"types": "./dist/src/index.d.ts",
"module": "./dist/bundle.mjs",
Expand Down
11 changes: 0 additions & 11 deletions packages/ui/src/components/Node/TableNodeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,6 @@ const TableNodeComponent = ({ id, data }: {
<div
ref={tableRef}
className="shadow-xl bg-gray-50 border rounded border-gray-300 text-xxxs"
onDoubleClick={() => {
const blob = new Blob([
JSON.stringify(tableInstance.options.data, null, 2)
], { type: 'application/json' });

const url = URL.createObjectURL(blob);

if (typeof window !== 'undefined') {
const handle = window.open(url, '_blank');
}
}}
>
<CustomHandle id={input.id} isConnectable={true} isInput={true} />

Expand Down

0 comments on commit 08bd33f

Please sign in to comment.