mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-10-27 07:51:52 +00:00
Refactor: Move ensureDir to src/utils.mjs
Moved the `ensureDir` utility function from `src/ops.mjs` to `src/utils.mjs` to promote code reuse and centralization. Updated `src/ops.mjs` and `src/services/memory/index.js` to use the shared utility. Added unit tests for `ensureDir` in `test/utils.test.mjs`. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
cea50da512
commit
ba5d86142c
4 files changed
+47
-11
No files matched your search
+1
-6
@@ -5,6 +5,7 @@ import crypto from 'node:crypto';
|
||||
import readline from 'node:readline/promises';
|
||||
import { stdin as input, stdout as output } from 'node:process';
|
||||
import { DEFAULT_RELAYS } from './constants.mjs';
|
||||
import { ensureDir } from './utils.mjs';
|
||||
|
||||
const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
|
||||
@@ -36,12 +37,6 @@ function getPaths(root, installDirName) {
|
||||
};
|
||||
}
|
||||
|
||||
function ensureDir(dir) {
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
function copyDir(src, dest) {
|
||||
if (fs.existsSync(src)) {
|
||||
fs.cpSync(src, dest, { recursive: true });
|
||||
|
||||
@@ -7,6 +7,7 @@ import { getMemoryPruneMode, isMemoryIngestEnabled } from './feature-flags.js';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import util from 'node:util';
|
||||
import { ensureDir } from '../../utils.mjs';
|
||||
|
||||
const MEMORY_FILE_PATH = path.join(process.cwd(), '.scheduler-memory', 'memory-store.json');
|
||||
const debug = util.debuglog('torch-memory');
|
||||
@@ -29,9 +30,7 @@ function loadMemoryStore() {
|
||||
function saveMemoryStore(store) {
|
||||
try {
|
||||
const dir = path.dirname(MEMORY_FILE_PATH);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
ensureDir(dir);
|
||||
const entries = [...store.entries()];
|
||||
fs.writeFileSync(MEMORY_FILE_PATH, JSON.stringify(entries, null, 2), 'utf8');
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
export function todayDateStr() {
|
||||
return new Date().toISOString().slice(0, 10);
|
||||
}
|
||||
@@ -34,3 +36,9 @@ export function detectPlatform() {
|
||||
if (process.env.GOOSE_API_KEY || process.env.GOOSE_SESSION_ID) return 'goose';
|
||||
return null;
|
||||
}
|
||||
|
||||
export function ensureDir(dir) {
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
+36
-2
@@ -1,6 +1,8 @@
|
||||
import { describe, it } from 'node:test';
|
||||
import { describe, it, after } from 'node:test';
|
||||
import assert from 'node:assert';
|
||||
import { getIsoWeekStr } from '../src/utils.mjs';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { getIsoWeekStr, ensureDir } from '../src/utils.mjs';
|
||||
|
||||
describe('Date Utilities', () => {
|
||||
describe('getIsoWeekStr', () => {
|
||||
@@ -34,3 +36,35 @@ describe('Date Utilities', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('File Utilities', () => {
|
||||
describe('ensureDir', () => {
|
||||
const tempBase = fs.mkdtempSync(path.join(process.cwd(), 'test-utils-'));
|
||||
|
||||
after(() => {
|
||||
fs.rmSync(tempBase, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('creates directory if it does not exist', () => {
|
||||
const targetDir = path.join(tempBase, 'new-dir');
|
||||
assert.strictEqual(fs.existsSync(targetDir), false);
|
||||
ensureDir(targetDir);
|
||||
assert.strictEqual(fs.existsSync(targetDir), true);
|
||||
});
|
||||
|
||||
it('does nothing if directory already exists', () => {
|
||||
const targetDir = path.join(tempBase, 'existing-dir');
|
||||
fs.mkdirSync(targetDir);
|
||||
assert.strictEqual(fs.existsSync(targetDir), true);
|
||||
ensureDir(targetDir);
|
||||
assert.strictEqual(fs.existsSync(targetDir), true);
|
||||
});
|
||||
|
||||
it('creates nested directories recursively', () => {
|
||||
const targetDir = path.join(tempBase, 'nested', 'deep', 'dir');
|
||||
assert.strictEqual(fs.existsSync(targetDir), false);
|
||||
ensureDir(targetDir);
|
||||
assert.strictEqual(fs.existsSync(targetDir), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user