Skip to content

Commit

Permalink
Handle contract import
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 committed Nov 29, 2023
1 parent 89b6991 commit 28d612f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
21 changes: 15 additions & 6 deletions src/hooks/project.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function useProjectActions() {
createNewItem,
updateFileContent,
deleteItem,
projectFiles,
} = useWorkspaceActions();
const { isContractDebugEnabled } = useSettingAction();

Expand Down Expand Up @@ -172,11 +173,17 @@ export function useProjectActions() {

let filesToProcess = [file?.path];

projectFiles(projectId).forEach((f) => {
if (f.name.includes('.tact') && !filesToProcess.includes(f.path)) {
filesToProcess.push(f.path);
}
});

while (filesToProcess.length !== 0) {
const fileToProcess = filesToProcess.pop();
const file = await getFileByPath(fileToProcess, projectId);
if (file?.content) {
fileList[file.path!!] = file.content;
fileList['/' + file.path!!] = file.content;
}
if (!file?.content) {
continue;
Expand Down Expand Up @@ -313,18 +320,20 @@ export function useProjectActions() {

updateProjectById(data, projectId);

const scriptFile = await getFileByPath(
output.contractScript.name,
projectId
);
let scriptPath = output.contractScript.name;
if (scriptPath.startsWith('/')) {
scriptPath = scriptPath.substring(1);
}

const scriptFile = await getFileByPath(scriptPath, projectId);

let fileToReRender = scriptFile;

if (!scriptFile?.id) {
let distDirectory = await getFileByPath('dist', projectId);
fileToReRender = await createNewItem(
distDirectory?.id!!,
output.contractScript.name,
scriptPath,
'file',
projectId,
output.contractScript.value.toString()
Expand Down
44 changes: 37 additions & 7 deletions src/utility/OverwritableVirtualFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,30 @@ export class OverwritableVirtualFileSystem implements VirtualFileSystem {
}

resolve(...path: string[]): string {
return path.join('/').replace('./', '');
const _path = this.root + path.join('/').replace('./', '');
return this.normalize(_path);
}

exists(path: string): boolean {
return typeof this.overwrites.get(path) === 'string';
exists(_path: string): boolean {
let path = this.normalize(_path);

if (typeof this.overwrites.get(path) === 'string') {
return true;
} else if (typeof this.projectFiles[path] === 'string') {
return true;
}
return false;
}

readFile(path: string): Buffer {
return (
this.overwrites.get(path) ?? Buffer.from(this.projectFiles[path], 'utf-8')
);
readFile(_path: string): any {
let path = this.normalize(_path);

if (this.overwrites.get(path)) {
return this.overwrites.get(path)
? Buffer.from(this.projectFiles[path], 'utf-8')
: Buffer.from('');
}
return this.projectFiles[path];
}

writeFile(path: string, content: string | Buffer): void {
Expand All @@ -30,4 +43,21 @@ export class OverwritableVirtualFileSystem implements VirtualFileSystem {
typeof content === 'string' ? Buffer.from(content, 'utf-8') : content
);
}

normalize(path: string): string {
// TODO: Was getting base file path also in beginning. so added this hack. Need to resolve in future
let pathArray = path.split('/');

const tactIndex = pathArray.findIndex((item) => item.endsWith('.tact'));

// Check if '.tact' is found and it's not the last element
if (tactIndex !== -1 && tactIndex < pathArray.length - 1) {
pathArray.splice(0, 1);
}

let newPath = pathArray.join('/');
newPath = newPath.startsWith('.') ? newPath.replace('.', '/') : newPath;

return newPath;
}
}

0 comments on commit 28d612f

Please sign in to comment.