Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions apps/studio/electron/main/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import type { TemplateNode } from '@onlook/models/element';
import { DEFAULT_IDE, IdeType } from '@onlook/models/ide';
import { dialog, shell } from 'electron';
import { mainWindow } from '..';

import { GENERATE_CODE_OPTIONS } from '../run/helpers';
import { PersistentStorage } from '../storage';
import { generateCode } from './diff/helpers';
import { formatContent, readFile, writeFile } from './files';
import { parseJsxCodeBlock } from './helpers';
import { IDE } from '/common/ide';

import fs from 'fs';
import { CopyStage, type CopyCallback } from '@onlook/models';
import path from 'path';

export async function readCodeBlock(
templateNode: TemplateNode,
stripIds: boolean = false,
Expand Down Expand Up @@ -129,6 +134,35 @@ export function openFileInIde(filePath: string, line?: number) {
shell.openExternal(command);
}

export async function moveFolderPath(
currentPath: string,
updatedPath: string,
onProgress: CopyCallback,
): Promise<void> {
try {
onProgress(CopyStage.STARTING);

if (!fs.existsSync(currentPath)) {
throw new Error('Could not find the source path');
}

const parentDir = path.dirname(updatedPath);
if (!fs.existsSync(parentDir)) {
await fs.promises.mkdir(parentDir, { recursive: true });
}

onProgress(CopyStage.COPYING);

await fs.promises.cp(currentPath, updatedPath, { recursive: true });

onProgress(CopyStage.COMPLETE);
} catch (error) {
onProgress(CopyStage.ERROR);
console.error(error);
throw error;
}
}

export function pickDirectory() {
return dialog.showOpenDialog({
properties: ['openDirectory', 'createDirectory'],
Expand Down
30 changes: 28 additions & 2 deletions apps/studio/electron/main/events/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ import {
scanTailwindConfig,
updateTailwindColorConfig,
} from '../assets/styles';
import { openFileInIde, openInIde, pickDirectory, readCodeBlock, writeCode } from '../code/';
import {
moveFolderPath,
openFileInIde,
openInIde,
pickDirectory,
readCodeBlock,
writeCode,
} from '../code/';
import { getTemplateNodeClass } from '../code/classes';
import { extractComponentsFromDirectory } from '../code/components';
import { getCodeDiffs } from '../code/diff';
import { isChildTextEditable } from '../code/diff/text';
import { readFile } from '../code/files';
import { getTemplateNodeProps } from '../code/props';
import { getTemplateNodeChild } from '../code/templateNode';
import runManager from '../run';
import { getFileContentWithoutIds } from '../run/cleanup';

import { getTemplateNodeProps } from '../code/props';
import type { CopyCallback, CopyStage } from '@onlook/models';
import { mainWindow } from '..';

const fontFileWatcher = new FontFileWatcher();

export function listenForCodeMessages() {
Expand Down Expand Up @@ -114,6 +124,22 @@ export function listenForCodeMessages() {
return result.filePaths.at(0) ?? null;
});

ipcMain.handle(
MainChannels.UPDATE_PROJECT_PATH,
async (e: Electron.IpcMainInvokeEvent, args) => {
const progressCallback: CopyCallback = (stage: CopyStage) => {
mainWindow?.webContents.send(MainChannels.COPY_PROJECT_CALLBACK, {
stage,
});
};
const { currentPath, updatedPath } = args as {
currentPath: string;
updatedPath: string;
};
return moveFolderPath(currentPath, updatedPath, progressCallback);
},
);

ipcMain.handle(MainChannels.GET_COMPONENTS, async (_, args) => {
if (typeof args !== 'string') {
throw new Error('`args` must be a string');
Expand Down
Loading