Revert "feat(live-values): atomic writer for tui-live-values.json"
This reverts commit c565888ff5.
This commit is contained in:
parent
c565888ff5
commit
4c955886ff
2 changed files with 0 additions and 89 deletions
|
|
@ -1,41 +0,0 @@
|
|||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface LiveValueSnapshotIn {
|
||||
value: string;
|
||||
type?: string;
|
||||
ts: number;
|
||||
}
|
||||
|
||||
export interface LiveValuesPayloadIn {
|
||||
device: string;
|
||||
pou_name: string;
|
||||
values: Record<string, LiveValueSnapshotIn>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Server-side counterpart to the TUI's readLiveValues.
|
||||
*
|
||||
* Wraps the caller's payload in the v1 envelope, writes atomically via
|
||||
* `<file>.<pid>.tmp` + rename, and creates parent dirs as needed. Mirrors
|
||||
* src/tui/shared/state-write.ts (the selection writer); kept separate
|
||||
* because that one's ESM and the server is CJS.
|
||||
*/
|
||||
export async function writeLiveValues(
|
||||
filePath: string,
|
||||
projectDir: string,
|
||||
payload: LiveValuesPayloadIn
|
||||
): Promise<void> {
|
||||
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
||||
const envelope = {
|
||||
version: 1,
|
||||
updated_at: new Date().toISOString(),
|
||||
project_dir: projectDir,
|
||||
device: payload.device,
|
||||
pou_name: payload.pou_name,
|
||||
values: payload.values,
|
||||
};
|
||||
const tmp = `${filePath}.${process.pid}.tmp`;
|
||||
await fs.writeFile(tmp, JSON.stringify(envelope, null, 2), 'utf8');
|
||||
await fs.rename(tmp, filePath);
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
import { describe, it, expect } from 'vitest';
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import { writeLiveValues } from '../../src/live-values-write';
|
||||
|
||||
async function tmpDir(): Promise<string> {
|
||||
return fs.mkdtemp(path.join(os.tmpdir(), 'phobics-lvw-'));
|
||||
}
|
||||
|
||||
const sample = {
|
||||
device: 'D1',
|
||||
pou_name: 'PLC_PRG',
|
||||
values: {
|
||||
counter: { value: '47', type: 'INT', ts: Date.now() },
|
||||
},
|
||||
};
|
||||
|
||||
describe('writeLiveValues', () => {
|
||||
it('writes the v1 envelope with required fields', async () => {
|
||||
const dir = await tmpDir();
|
||||
const target = path.join(dir, 'tui-live-values.json');
|
||||
await writeLiveValues(target, '/abs/project', sample);
|
||||
|
||||
const parsed = JSON.parse(await fs.readFile(target, 'utf8'));
|
||||
expect(parsed.version).toBe(1);
|
||||
expect(parsed.project_dir).toBe('/abs/project');
|
||||
expect(parsed.device).toBe('D1');
|
||||
expect(parsed.pou_name).toBe('PLC_PRG');
|
||||
expect(parsed.values.counter.value).toBe('47');
|
||||
expect(typeof parsed.updated_at).toBe('string');
|
||||
});
|
||||
|
||||
it('creates parent dirs as needed', async () => {
|
||||
const dir = await tmpDir();
|
||||
const target = path.join(dir, 'a', 'b', 'tui-live-values.json');
|
||||
await writeLiveValues(target, '/abs/project', sample);
|
||||
expect((await fs.stat(target)).isFile()).toBe(true);
|
||||
});
|
||||
|
||||
it('does not leave .tmp residue on success', async () => {
|
||||
const dir = await tmpDir();
|
||||
const target = path.join(dir, 'tui-live-values.json');
|
||||
await writeLiveValues(target, '/abs/project', sample);
|
||||
const entries = await fs.readdir(dir);
|
||||
expect(entries.filter((e) => e.endsWith('.tmp'))).toEqual([]);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue