Repository navigation
Expand file tree
/
Copy pathJSONCTCReadFileOp.ts
More file actions
114 lines (103 loc) · 2.73 KB
/
Copy pathJSONCTCReadFileOp.ts
File metadata and controls
114 lines (103 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bun
import * as fs from 'node:fs';
import process from 'node:process';
import type { IOContext } from './IOContext.ts';
import { JSONCTCObject } from './JSONCTCObject.ts';
import { Op } from './Op.ts';
import type { Failure, Success } from './Outcome.ts';
/**
* Read a JSONCTC file and return a JSONCTCObject wrapper
*
* This Op reads a JSONCTC file from disk and wraps it in a JSONCTCObject,
* which provides a Proxy-based interface for tracking changes while preserving
* comments and formatting.
*
* @example
* ```typescript
* const op = new JSONCTCReadFileOp('/path/to/config.jsonctc');
* const result = await op.run();
*
* if (result.ok) {
* const obj = result.value;
* obj.data.name = "Bob"; // Modify data
* console.log(obj.toString()); // Serialize with comments preserved
* }
* ```
*
* @example
* ```bash
* # CLI usage
* bun JSONCTCReadFileOp.ts config.jsonctc
* ```
*/
export class JSONCTCReadFileOp extends Op
{
name = 'JSONCTCReadFileOp';
constructor(private filePath: string)
{
super();
}
async run(_io?: IOContext): Promise<
| Success<JSONCTCObject>
| Failure<'fileNotFound' | 'readError' | 'parseError'>
>
{
await Promise.resolve();
try
{
// Read file synchronously
const content = fs.readFileSync(this.filePath, 'utf-8');
// Create JSONCTCObject from the content
try
{
const obj = new JSONCTCObject(content);
return this.succeed(obj);
}
catch (parseError: unknown)
{
return this.fail('parseError' as const, `Failed to parse JSONCTC: ${String(parseError)}`);
}
}
catch (error: unknown)
{
// Check if file not found
if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT')
{
return this.fail('fileNotFound' as const, `File not found: ${this.filePath}`);
}
// Other read errors
return this.fail('readError' as const, `Failed to read file: ${String(error)}`);
}
}
}
// CLI support
if (import.meta.main)
{
const args = process.argv.slice(2);
if (args.length < 1)
{
console.error('Usage: bun JSONCTCReadFileOp.ts <file>');
console.error('');
console.error('Examples:');
console.error(' bun JSONCTCReadFileOp.ts config.jsonctc');
console.error(' bun JSONCTCReadFileOp.ts /path/to/data.json');
process.exit(1);
}
const filePath = args[0]!;
const op = new JSONCTCReadFileOp(filePath);
const result = await op.run();
if (result.ok)
{
const obj = result.value;
console.log(JSON.stringify(obj.data, null, 2));
}
else
{
console.error(`Error: ${result.failure}`);
if (result.debugData)
{
console.error(result.debugData);
}
process.exit(1);
}
}