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