Skip to content

Commit

Permalink
upd: optimize installing files from extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
solvedDev committed Sep 27, 2022
1 parent 4bc6be4 commit baaa77b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/components/Extensions/InstallFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TPackTypeId } from '../Data/PackType'
import { App } from '/@/App'
import { FileSystem } from '/@/components/FileSystem/FileSystem'
import { iterateDir } from '/@/utils/iterateDir'
import { iterateDir, iterateDirParallel } from '/@/utils/iterateDir'
import { join } from '/@/utils/path'

export class InstallFiles {
Expand All @@ -18,6 +18,8 @@ export class InstallFiles {

await app.projectManager.projectReady.fired

const promises = []

for (const [from, to] of Object.entries(this.contributeFiles)) {
const projects = isGlobal ? app.projects : [app.project]

Expand All @@ -30,32 +32,42 @@ export class InstallFiles {
to.pack,
to.path
)
await app.fileSystem.writeFile(
target,
await file.arrayBuffer()
promises.push(
app.fileSystem.writeFile(
target,
await file.arrayBuffer()
)
)
}
} else if (await this.fileSystem.directoryExists(from)) {
// Handle directory contributions
await iterateDir(
await this.fileSystem.getDirectoryHandle(from),
async (fileHandle, filePath) => {
for (const project of projects) {
const target = project.config.resolvePackPath(
to.pack,
to.path
)
const newFileHandle =
await app.fileSystem.getFileHandle(
join(target, filePath),
true
promises.push(
iterateDirParallel(
await this.fileSystem.getDirectoryHandle(from),
async (fileHandle, filePath) => {
const copyPromises = []

for (const project of projects) {
const target = project.config.resolvePackPath(
to.pack,
to.path
)
const newFileHandle =
await app.fileSystem.getFileHandle(
join(target, filePath),
true
)
copyPromises.push(
app.fileSystem.copyFileHandle(
fileHandle,
newFileHandle
)
)
await app.fileSystem.copyFileHandle(
fileHandle,
newFileHandle
)
}

await Promise.all(copyPromises)
}
}
)
)
} else {
console.warn(
Expand All @@ -65,6 +77,8 @@ export class InstallFiles {
}
}

await Promise.all(promises)

// Refresh the pack explorer once files have been added
app.actionManager.trigger('bridge.action.refreshProject')
}
Expand Down
33 changes: 33 additions & 0 deletions src/utils/iterateDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,36 @@ export async function iterateDir(
}
}
}

export async function iterateDirParallel(
baseDirectory: AnyDirectoryHandle,
callback: (
fileHandle: AnyFileHandle,
path: string,
fromDirectory: AnyDirectoryHandle
) => Promise<void> | void,
ignoreFolders: Set<string> = new Set(),
path = ''
) {
const promises = []

for await (const handle of baseDirectory.values()) {
const currentPath =
path.length === 0 ? handle.name : `${path}/${handle.name}`

if (handle.kind === 'file') {
if (handle.name[0] === '.') continue

promises.push(callback(handle, currentPath, baseDirectory))
} else if (
handle.kind === 'directory' &&
!ignoreFolders.has(currentPath)
) {
promises.push(
iterateDirParallel(handle, callback, ignoreFolders, currentPath)
)
}
}

await Promise.all(promises)
}

0 comments on commit baaa77b

Please sign in to comment.