|
1 | 1 | import { readFileSync, writeFileSync } from 'node:fs'; |
2 | 2 | import { parseArgs } from 'node:util'; |
3 | 3 |
|
4 | | -export function jsonEdit() { |
5 | | - const { positionals } = parseArgs({ |
6 | | - allowPositionals: true, |
7 | | - args: process.argv.slice(3), |
8 | | - }); |
| 4 | +const { positionals } = parseArgs({ |
| 5 | + allowPositionals: true, |
| 6 | +}); |
9 | 7 |
|
10 | | - const filename = positionals[0]; |
11 | | - const script = positionals[1]; |
| 8 | +const filename = positionals[0]; |
| 9 | +const script = positionals[1]; |
12 | 10 |
|
13 | | - if (!filename || !script) { |
14 | | - console.error('Usage: tool json-edit <filename> <script>'); |
15 | | - console.error('Example: tool json-edit package.json \'_.version = "1.2.3"\''); |
16 | | - process.exit(1); |
17 | | - } |
| 11 | +if (!filename || !script) { |
| 12 | + console.error('Usage: json-edit <filename> <script>'); |
| 13 | + console.error('Example: json-edit package.json \'_.version = "1.2.3"\''); |
| 14 | + process.exit(1); |
| 15 | +} |
18 | 16 |
|
19 | | - const json = JSON.parse(readFileSync(filename, 'utf-8')); |
20 | | - const func = new Function('_', script + '; return _;'); |
21 | | - const result = func(json); |
| 17 | +const json = JSON.parse(readFileSync(filename, 'utf-8')); |
| 18 | +const func = new Function('_', script + '; return _;'); |
| 19 | +const result = func(json); |
22 | 20 |
|
23 | | - writeFileSync(filename, JSON.stringify(result, null, 2) + '\n', 'utf-8'); |
24 | | -} |
| 21 | +writeFileSync(filename, JSON.stringify(result, null, 2) + '\n', 'utf-8'); |
0 commit comments