Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([issue-1840]): use canonical ensureDir instead of raw mkdir recursive

[._.]/ Adam Eivy5044871629c0
diff --git a/server/services/chatgptZipImport.js b/server/services/chatgptZipImport.js
index 7f8e266eda..079959476b 100644
--- a/server/services/chatgptZipImport.js
+++ b/server/services/chatgptZipImport.js
@@ -30,9 +30,9 @@
*/
import { createReadStream, createWriteStream } from 'fs';
-import { mkdir, rename, unlink } from 'fs/promises';
+import { rename, unlink } from 'fs/promises';
import { Writable } from 'stream';
-import { PATHS, getMimeType } from '../lib/fileUtils.js';
+import { PATHS, getMimeType, ensureDir } from '../lib/fileUtils.js';
import { parseZip, collectZipEntry, MAX_ZIP_MEMBER_BYTES } from '../lib/zipStream.js';
import { parseExport, importConversations, assetPointerId } from './chatgptImport.js';
import { ServerError } from '../lib/errorHandler.js';
@@ -153,7 +153,7 @@ const streamAssetToFile = (entry, filePath, max) => new Promise((resolve, reject
* at `/data/brain-imports/<assetId><ext>`.
*/
export async function extractChatgptZip(zipPath, { assetDir = PATHS.brainImportAssets } = {}) {
- await mkdir(assetDir, { recursive: true });
+ await ensureDir(assetDir);
const convoBuffers = []; // { path, buffer }
let assetNameMap = {};
diff --git a/server/services/dataManager.js b/server/services/dataManager.js
index 66ea3fecaa..a4c958d218 100644
--- a/server/services/dataManager.js
+++ b/server/services/dataManager.js
@@ -1,9 +1,9 @@
-import { readdir, stat, rm, mkdir, writeFile as fsWriteFile } from 'fs/promises';
+import { readdir, stat, rm, writeFile as fsWriteFile } from 'fs/promises';
import { join, relative, resolve, isAbsolute } from 'path';
import { existsSync } from 'fs';
import { execFile } from 'child_process';
import { promisify } from 'util';
-import { PATHS } from '../lib/fileUtils.js';
+import { PATHS, ensureDir } from '../lib/fileUtils.js';
const execFileAsync = promisify(execFile);
const DATA_DIR = PATHS.data;
@@ -122,7 +122,7 @@ export async function archiveCategory(categoryKey, options = {}) {
if (!existsSync(dirPath)) throw new Error(`Category directory not found: ${categoryKey}`);
const backupDir = join(DATA_DIR, 'backup');
- await mkdir(backupDir, { recursive: true });
+ await ensureDir(backupDir);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const archiveName = `${categoryKey}-${timestamp}.tar.gz`;
diff --git a/server/services/lmStudioManager.js b/server/services/lmStudioManager.js
index 02031cdea5..38fc8ce88b 100644
--- a/server/services/lmStudioManager.js
+++ b/server/services/lmStudioManager.js
@@ -8,7 +8,8 @@
import { homedir } from 'os'
import { join, basename, resolve, relative, isAbsolute, sep } from 'path'
import { existsSync } from 'fs'
-import { readdir, stat, mkdir, copyFile, link, rm, rmdir } from 'fs/promises'
+import { readdir, stat, copyFile, link, rm, rmdir } from 'fs/promises'
+import { ensureDir } from '../lib/fileUtils.js'
import { cosEvents } from './cosEvents.js'
import { fetchWithTimeout } from '../lib/fetchWithTimeout.js'
import {
@@ -664,7 +665,7 @@ async function importModelFromGguf({ lmstudioId, ggufPath, projectorPath, mode =
return false
}
let linked = false
- const r = await mkdir(destDir, { recursive: true })
+ const r = await ensureDir(destDir)
.then(async () => {
const base = basename(ggufPath)
const destName = /\.gguf$/i.test(base) ? base : `${repo}.gguf`
diff --git a/server/services/localLlm.js b/server/services/localLlm.js
index b74354a1eb..a7f124130d 100644
--- a/server/services/localLlm.js
+++ b/server/services/localLlm.js
@@ -23,12 +23,12 @@
import { execFile, spawn } from 'child_process'
import { promisify } from 'util'
import { readFileSync, createWriteStream } from 'fs'
-import { stat, mkdir, rm } from 'fs/promises'
+import { stat, rm } from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'
import { pipeline } from 'stream/promises'
import { Readable } from 'stream'
-import { PATHS, atomicWrite } from '../lib/fileUtils.js'
+import { PATHS, atomicWrite, ensureDir } from '../lib/fileUtils.js'
import { compareSemver } from '../lib/versionUtils.js'
import { isBackend, mapModelToBackend } from '../lib/localLlmCatalog.js'
import { sanitizeOllamaName } from '../lib/localLlmDisk.js'
@@ -456,7 +456,7 @@ async function upgradeOllamaMacApp(emit) {
const tmpDir = join(tmpdir(), `portos-ollama-upgrade-${Date.now()}`)
const zipPath = join(tmpDir, 'Ollama-darwin.zip')
- await mkdir(tmpDir, { recursive: true })
+ await ensureDir(tmpDir)
emit(`Downloading Ollama ${release.tag_name} (${Math.round(asset.size / 1024 / 1024)} MB)…`)
// No timeout signal here: it would cover the entire multi-hundred-MB body
diff --git a/server/services/loraTraining/index.js b/server/services/loraTraining/index.js
index feb40cb307..eb01656ab6 100644
--- a/server/services/loraTraining/index.js
+++ b/server/services/loraTraining/index.js
@@ -15,7 +15,7 @@
import { spawn } from 'child_process';
import { existsSync } from 'fs';
-import { copyFile, mkdir, writeFile } from 'fs/promises';
+import { copyFile, writeFile } from 'fs/promises';
import { join, basename, dirname } from 'path';
import { platform } from 'os';
import { PATHS, ensureDir, atomicWrite, shortId } from '../../lib/fileUtils.js';
@@ -510,8 +510,8 @@ export async function runTraining({ jobId, runId, pythonPath = null, resumeCheck
// existence check (TOCTOU), or disk-full — would otherwise propagate to the
// queue's catch (no crash) but leave the run record `running` forever.
try {
- await mkdir(checkpointsDir, { recursive: true });
- await mkdir(samplesDir, { recursive: true });
+ await ensureDir(checkpointsDir);
+ await ensureDir(samplesDir);
if (run.runtime === TRAINING_RUNTIMES.FLUX2) {
bin = resolveFlux2Python();
@@ -542,7 +542,7 @@ export async function runTraining({ jobId, runId, pythonPath = null, resumeCheck
// NNNN.txt caption pairs, plus preview_1.txt for the periodic sample
// render. mflux resolves everything from the config's `data` dir.
const dataDir = join(dir, 'data');
- await mkdir(dataDir, { recursive: true });
+ await ensureDir(dataDir);
for (let i = 0; i < manifest.images.length; i += 1) {
const stem = String(i + 1).padStart(4, '0');
await copyFile(manifest.images[i].path, join(dataDir, `${stem}.png`));
diff --git a/server/services/ollamaManager.js b/server/services/ollamaManager.js
index a2cfb35149..04e0a7be99 100644
--- a/server/services/ollamaManager.js
+++ b/server/services/ollamaManager.js
@@ -16,12 +16,12 @@
import { homedir } from 'os'
import { join, dirname } from 'path'
-import { readdir, stat, link, mkdir } from 'fs/promises'
+import { readdir, stat, link } from 'fs/promises'
import { execFile, spawn } from 'child_process'
import { promisify } from 'util'
import { fetchWithTimeout } from '../lib/fetchWithTimeout.js'
import { readResponseJson } from '../lib/readResponseJson.js'
-import { readJSONFile, sha256File, safeJSONParse } from '../lib/fileUtils.js'
+import { readJSONFile, sha256File, safeJSONParse, ensureDir } from '../lib/fileUtils.js'
import {
parseOllamaManifest, parseOllamaModelRef, ollamaManifestRelPath, digestToBlobFilename, buildModelfile
} from '../lib/localLlmDisk.js'
@@ -918,7 +918,7 @@ async function prelinkBlob(ggufPath) {
const hex = await sha256File(ggufPath)
const blobPath = join(getModelsDir(), 'blobs', digestToBlobFilename(`sha256:${hex}`))
if (await fileExists(blobPath)) return true // already present (content-addressed dedup)
- await mkdir(dirname(blobPath), { recursive: true })
+ await ensureDir(dirname(blobPath))
await link(ggufPath, blobPath)
return true
}