write multiple files from plugin

pull/5370/head
Tyler Sehr 2 years ago committed by Aniket
parent 137a81c026
commit 26ecb0d717
  1. 49
      apps/remix-ide/src/app/files/fileManager.ts

@ -1,3 +1,4 @@
'use strict'
import { saveAs } from 'file-saver'
import JSZip from 'jszip'
@ -21,7 +22,7 @@ const profile = {
icon: 'assets/img/fileManager.webp',
permission: true,
version: packageJson.version,
methods: ['closeAllFiles', 'closeFile', 'file', 'exists', 'open', 'writeFile', 'readFile', 'copyFile', 'copyDir', 'rename', 'mkdir',
methods: ['closeAllFiles', 'closeFile', 'file', 'exists', 'open', 'writeFile', 'writeMultipleFiles', 'readFile', 'copyFile', 'copyDir', 'rename', 'mkdir',
'readdir', 'dirList', 'fileList', 'remove', 'getCurrentFile', 'getFile', 'getFolder', 'setFile', 'switchFile', 'refresh',
'getProviderOf', 'getProviderByName', 'getPathFromUrl', 'getUrlFromPath', 'saveCurrentFile', 'setBatchFiles', 'isGitRepo'],
kind: 'file-system'
@ -217,6 +218,38 @@ class FileManager extends Plugin {
}
}
/**
* Set the content of multiple files
* @param {string[]} filePaths paths of the files
* @param {string[]} data content to write to each file
* @param {string} folderPath base folder path
* @returns {void}
*/
async writeMultipleFiles(filePaths, fileData, folderPath) {
try {
let alert = true
for (let i = 0; i < filePaths.length; i++) {
let installPath = folderPath + "/" + filePaths[i]
let path = this.normalize(installPath)
path = this.limitPluginScope(path)
if (await this.exists(path)) {
await this._handleIsFile(path, `Cannot write file ${path}`)
await this.setMultipleFileContent(path, fileData[i], folderPath, alert)
} else {
await this.setMultipleFileContent(path, fileData[i], folderPath, alert)
this.emit('fileAdded', path)
}
alert = false
}
} catch (e) {
throw new Error(e)
}
}
/**
* Return the content of a specific file
* @param {string} path path of the file
@ -572,13 +605,25 @@ class FileManager extends Plugin {
return await this._setFileInternal(path, content)
}
async setMultipleFileContent(path, content, folderPath, alert) {
if (this.currentRequest) {
const canCall = await this.askUserPermission(`writeFile`, `modifying ${folderPath} ...`)
const required = this.appManager.isRequired(this.currentRequest.from)
if (canCall && !required && alert) {
// inform the user about modification after permission is granted and even if permission was saved before
this.call('notification', 'toast', fileChangedToastMsg(this.currentRequest.from, folderPath))
}
}
return await this._setFileInternal(path, content)
}
_setFileInternal(path, content) {
const provider = this.fileProviderOf(path)
if (!provider) throw createError({ code: 'ENOENT', message: `${path} not available` })
// TODO : Add permission
// TODO : Change Provider to Promise
return new Promise((resolve, reject) => {
provider.set(path, content, (error) => {
provider.set(path, content, async (error) => {
if (error) reject(error)
this.syncEditor(path)
this.emit('fileSaved', path)

Loading…
Cancel
Save