diff --git a/packages/server/src/sdk/app/backups/exports.ts b/packages/server/src/sdk/app/backups/exports.ts index 813f813177..8a67ba2dae 100644 --- a/packages/server/src/sdk/app/backups/exports.ts +++ b/packages/server/src/sdk/app/backups/exports.ts @@ -14,6 +14,7 @@ import { ATTACHMENT_DIRECTORY, } from "./constants" import fs from "fs" +import fsp from "fs/promises" import { join } from "path" import env from "../../../environment" import { v4 as uuid } from "uuid" @@ -117,7 +118,7 @@ export async function exportApp(appId: string, config?: ExportOpts) { ObjectStoreBuckets.APPS, join(appPath, path) ) - fs.writeFileSync(join(tmpPath, path), contents) + await fsp.writeFile(join(tmpPath, path), contents) } } // get all the files @@ -131,14 +132,14 @@ export async function exportApp(appId: string, config?: ExportOpts) { const downloadedPath = join(tmpPath, appPath) if (fs.existsSync(downloadedPath)) { - const allFiles = fs.readdirSync(downloadedPath) + const allFiles = await fsp.readdir(downloadedPath) for (let file of allFiles) { const path = join(downloadedPath, file) // move out of app directory, simplify structure - fs.renameSync(path, join(downloadedPath, "..", file)) + await fsp.rename(path, join(downloadedPath, "..", file)) } // remove the old app directory created by object export - fs.rmdirSync(downloadedPath) + await fsp.rmdir(downloadedPath) } // enforce an export of app DB to the tmp path const dbPath = join(tmpPath, DB_EXPORT_FILE) @@ -148,7 +149,7 @@ export async function exportApp(appId: string, config?: ExportOpts) { }) if (config?.encryptPassword) { - for (let file of fs.readdirSync(tmpPath)) { + for (let file of await fsp.readdir(tmpPath)) { const path = join(tmpPath, file) // skip the attachments - too big to encrypt @@ -157,7 +158,7 @@ export async function exportApp(appId: string, config?: ExportOpts) { { dir: tmpPath, filename: file }, config.encryptPassword ) - fs.rmSync(path) + await fsp.rm(path) } } } @@ -165,9 +166,9 @@ export async function exportApp(appId: string, config?: ExportOpts) { // if tar requested, return where the tarball is if (config?.tar) { // now the tmpPath contains both the DB export and attachments, tar this - const tarPath = await tarFilesToTmp(tmpPath, fs.readdirSync(tmpPath)) + const tarPath = await tarFilesToTmp(tmpPath, await fsp.readdir(tmpPath)) // cleanup the tmp export files as tarball returned - fs.rmSync(tmpPath, { recursive: true, force: true }) + await fsp.rm(tmpPath, { recursive: true, force: true }) return tarPath } diff --git a/packages/server/src/sdk/app/backups/imports.ts b/packages/server/src/sdk/app/backups/imports.ts index 4c5c1e659d..75c17dfa95 100644 --- a/packages/server/src/sdk/app/backups/imports.ts +++ b/packages/server/src/sdk/app/backups/imports.ts @@ -17,6 +17,7 @@ import { downloadTemplate } from "../../../utilities/fileSystem" import { ObjectStoreBuckets } from "../../../constants" import { join } from "path" import fs from "fs" +import fsp from "fs/promises" import sdk from "../../" import { v4 as uuid } from "uuid" import tar from "tar" @@ -119,7 +120,7 @@ async function getTemplateStream(template: TemplateType) { export async function untarFile(file: { path: string }) { const tmpPath = join(budibaseTempDir(), uuid()) - fs.mkdirSync(tmpPath) + await fsp.mkdir(tmpPath) // extract the tarball await tar.extract({ cwd: tmpPath, @@ -130,12 +131,12 @@ export async function untarFile(file: { path: string }) { async function decryptFiles(path: string, password: string) { try { - for (let file of fs.readdirSync(path)) { + for (let file of await fsp.readdir(path)) { const inputPath = join(path, file) if (!inputPath.endsWith(ATTACHMENT_DIRECTORY)) { const outputPath = inputPath.replace(/\.enc$/, "") await encryption.decryptFile(inputPath, outputPath, password) - fs.rmSync(inputPath) + await fsp.rm(inputPath) } } } catch (err: any) { @@ -164,14 +165,14 @@ export async function importApp( let dbStream: any const isTar = template.file && template?.file?.type?.endsWith("gzip") const isDirectory = - template.file && fs.lstatSync(template.file.path).isDirectory() + template.file && (await fsp.lstat(template.file.path)).isDirectory() let tmpPath: string | undefined = undefined if (template.file && (isTar || isDirectory)) { tmpPath = isTar ? await untarFile(template.file) : template.file.path if (isTar && template.file.password) { await decryptFiles(tmpPath, template.file.password) } - const contents = fs.readdirSync(tmpPath) + const contents = await fsp.readdir(tmpPath) // have to handle object import if (contents.length && opts.importObjStoreContents) { let promises = [] @@ -182,7 +183,7 @@ export async function importApp( continue } filename = join(prodAppId, filename) - if (fs.lstatSync(path).isDirectory()) { + if ((await fsp.lstat(path)).isDirectory()) { promises.push( objectStore.uploadDirectory(ObjectStoreBuckets.APPS, path, filename) ) @@ -211,7 +212,7 @@ export async function importApp( await updateAutomations(prodAppId, db) // clear up afterward if (tmpPath) { - fs.rmSync(tmpPath, { recursive: true, force: true }) + await fsp.rm(tmpPath, { recursive: true, force: true }) } return ok }