json-asty 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/json-asty.cli.js +118 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-asty",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Lossless JSON-to-AST Parser and AST-to-JSON Generator",
|
|
5
5
|
"keywords": [ "json", "ast", "parse", "generate" ],
|
|
6
6
|
"browser": "./lib/json-asty.browser.js",
|
|
7
7
|
"main": "./lib/json-asty.node.js",
|
|
8
8
|
"types": "./src/json-asty.d.ts",
|
|
9
|
+
"bin": { "json-asty": "./src/json-asty.cli.js" },
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
|
11
12
|
"url": "https://github.com/rse/json-asty.git"
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/*!
|
|
3
|
+
** JSON-ASTy -- Lossless JSON-to-AST Parser and AST-to-JSON Generator
|
|
4
|
+
** Copyright (c) 2018-2023 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
5
|
+
**
|
|
6
|
+
** Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
** a copy of this software and associated documentation files (the
|
|
8
|
+
** "Software"), to deal in the Software without restriction, including
|
|
9
|
+
** without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
** distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
** permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
** the following conditions:
|
|
13
|
+
**
|
|
14
|
+
** The above copyright notice and this permission notice shall be included
|
|
15
|
+
** in all copies or substantial portions of the Software.
|
|
16
|
+
**
|
|
17
|
+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
+
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
+
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
+
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
+
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const fs = require("fs")
|
|
27
|
+
const JsonAsty = require("..")
|
|
28
|
+
|
|
29
|
+
/* convert object path to XPath */
|
|
30
|
+
const opath2xpath = (opath) => {
|
|
31
|
+
let xpath = ""
|
|
32
|
+
const segments = opath.split(".")
|
|
33
|
+
for (const segment of segments)
|
|
34
|
+
xpath += `object / member [ / * [ pos() == 1 && @value == "${segment}" ] ] /`
|
|
35
|
+
xpath += " * [ pos() == 2 ]"
|
|
36
|
+
return xpath
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const get = async (file, path) => {
|
|
40
|
+
/* read JSON file */
|
|
41
|
+
const json = await fs.promises.readFile(file, { encoding: "utf8" })
|
|
42
|
+
|
|
43
|
+
/* parse JSON file */
|
|
44
|
+
const ast = JsonAsty.parse(json)
|
|
45
|
+
|
|
46
|
+
/* query JSON AST */
|
|
47
|
+
const query = opath2xpath(path)
|
|
48
|
+
const nodes = ast.query(query)
|
|
49
|
+
if (nodes.length === 0)
|
|
50
|
+
throw new Error(`path not found (no AST nodes matched): "${path}"`)
|
|
51
|
+
if (nodes.length > 1)
|
|
52
|
+
throw new Error(`path is ambigous (more than one AST node matched): "${path}"`)
|
|
53
|
+
const node = nodes[0]
|
|
54
|
+
|
|
55
|
+
/* deliver value */
|
|
56
|
+
const value = node.get("value")
|
|
57
|
+
process.stdout.write(value + "\n")
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const set = async (file, path, type, value) => {
|
|
61
|
+
/* read JSON file */
|
|
62
|
+
const json = await fs.promises.readFile(file, { encoding: "utf8" })
|
|
63
|
+
|
|
64
|
+
/* parse JSON file */
|
|
65
|
+
const ast = JsonAsty.parse(json)
|
|
66
|
+
|
|
67
|
+
/* query JSON AST */
|
|
68
|
+
const query = opath2xpath(path)
|
|
69
|
+
const nodes = ast.query(query)
|
|
70
|
+
if (nodes.length === 0)
|
|
71
|
+
throw new Error(`path not found (no AST nodes matched): "${path}"`)
|
|
72
|
+
if (nodes.length > 1)
|
|
73
|
+
throw new Error(`path is ambigous (more than one AST node matched): "${path}"`)
|
|
74
|
+
const node = nodes[0]
|
|
75
|
+
|
|
76
|
+
/* replace value */
|
|
77
|
+
if (type === "number")
|
|
78
|
+
value = parseInt(value)
|
|
79
|
+
else if (type === "boolean")
|
|
80
|
+
value = Boolean(value)
|
|
81
|
+
const nodeNew = node.create(type).set({ value })
|
|
82
|
+
node.parent().del(node).add(nodeNew)
|
|
83
|
+
|
|
84
|
+
/* save JSON file */
|
|
85
|
+
const jsonNew = JsonAsty.unparse(ast)
|
|
86
|
+
await fs.promises.writeFile(file, jsonNew, { encoding: "utf8" })
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
;(async () => {
|
|
90
|
+
try {
|
|
91
|
+
/* provide usage */
|
|
92
|
+
if (process.argv.length <= 2) {
|
|
93
|
+
process.stderr.write("json-asty: USAGE: json-asty get <json-file> <object-path>\n")
|
|
94
|
+
process.stderr.write("json-asty: USAGE: json-asty set <json-file> <object-path> <type> <value>\n")
|
|
95
|
+
process.exit(0)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* dispatch commands */
|
|
99
|
+
if (process.argv[2] === "get") {
|
|
100
|
+
if (process.argv.length !== (3 + 2))
|
|
101
|
+
throw new Error("invalid number of arguments (expected 2)")
|
|
102
|
+
await get(process.argv[3], process.argv[4])
|
|
103
|
+
}
|
|
104
|
+
else if (process.argv[2] === "set") {
|
|
105
|
+
if (process.argv.length !== (3 + 4))
|
|
106
|
+
throw new Error("invalid number of arguments (expected 4)")
|
|
107
|
+
await set(process.argv[3], process.argv[4], process.argv[5], process.argv[6])
|
|
108
|
+
}
|
|
109
|
+
else
|
|
110
|
+
throw new Error("invalid command (expected 'get' or 'set')")
|
|
111
|
+
process.exit(0)
|
|
112
|
+
}
|
|
113
|
+
catch (ex) {
|
|
114
|
+
process.stderr.write(`json-asty: ERROR: ${ex.toString()}\n`)
|
|
115
|
+
process.exit(1)
|
|
116
|
+
}
|
|
117
|
+
})()
|
|
118
|
+
|