json-parser.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env node
  2. const path = require('path')
  3. const fs = require('fs')
  4. const operation = process.argv[2]
  5. const relativePath = process.argv[3]
  6. const jsonString = process.argv[4]
  7. const keyPathJson = process.argv[5]
  8. if (!operation || !relativePath) {
  9. process.stdout.write(JSON.stringify({
  10. success: false,
  11. error: 'Missing required parameters: operation and filePath'
  12. }) + '\n')
  13. process.exit(1)
  14. }
  15. const staticDir = process.env.STATIC_ROOT
  16. ? path.resolve(process.env.STATIC_ROOT)
  17. : path.resolve(__dirname, '..', 'static')
  18. const normalizedPath = path.normalize(relativePath).replace(/\\/g, '/')
  19. if (normalizedPath.includes('..') || normalizedPath.startsWith('/')) {
  20. process.stdout.write(JSON.stringify({
  21. success: false,
  22. error: 'Invalid file path'
  23. }) + '\n')
  24. process.exit(1)
  25. }
  26. const fileName = normalizedPath.endsWith('.json') ? normalizedPath : `${normalizedPath}.json`
  27. const filePath = path.resolve(staticDir, fileName)
  28. if (!filePath.startsWith(staticDir)) {
  29. process.stdout.write(JSON.stringify({
  30. success: false,
  31. error: 'File path must be within static directory'
  32. }) + '\n')
  33. process.exit(1)
  34. }
  35. function getNestedValue(obj, keyPath) {
  36. let current = obj
  37. for (const key of keyPath) {
  38. if (current === null || current === undefined) return undefined
  39. current = current[key]
  40. }
  41. return current
  42. }
  43. function setNestedValue(obj, keyPath, value) {
  44. let current = obj
  45. for (let i = 0; i < keyPath.length - 1; i++) {
  46. const key = keyPath[i]
  47. if (current[key] === null || current[key] === undefined) {
  48. current[key] = typeof keyPath[i + 1] === 'number' ? [] : {}
  49. }
  50. current = current[key]
  51. }
  52. current[keyPath[keyPath.length - 1]] = value
  53. }
  54. const dir = path.dirname(filePath)
  55. if (!fs.existsSync(dir)) {
  56. fs.mkdirSync(dir, { recursive: true })
  57. }
  58. let result
  59. if (operation === 'create') {
  60. if (!jsonString) {
  61. result = { success: false, error: 'Missing jsonString parameter for create operation' }
  62. } else {
  63. fs.writeFileSync(filePath, JSON.stringify(JSON.parse(jsonString), null, 2), 'utf8')
  64. result = { success: true, message: 'JSON file created successfully' }
  65. }
  66. } else if (operation === 'read') {
  67. if (!fs.existsSync(filePath)) {
  68. process.stdout.write('')
  69. process.exit(0)
  70. } else {
  71. const jsonData = JSON.parse(fs.readFileSync(filePath, 'utf8'))
  72. result = {
  73. success: true,
  74. data: keyPathJson ? getNestedValue(jsonData, JSON.parse(keyPathJson)) : jsonData
  75. }
  76. }
  77. } else if (operation === 'update') {
  78. if (!fs.existsSync(filePath)) {
  79. result = { success: false, error: 'JSON file does not exist' }
  80. } else if (!jsonString) {
  81. result = { success: false, error: 'Missing jsonString parameter for update operation' }
  82. } else {
  83. const jsonData = JSON.parse(fs.readFileSync(filePath, 'utf8'))
  84. const newValue = JSON.parse(jsonString)
  85. if (keyPathJson) {
  86. setNestedValue(jsonData, JSON.parse(keyPathJson), newValue)
  87. } else {
  88. Object.assign(jsonData, newValue)
  89. }
  90. fs.writeFileSync(filePath, JSON.stringify(jsonData, null, 2), 'utf8')
  91. result = { success: true, message: 'JSON file updated successfully' }
  92. }
  93. } else if (operation === 'check') {
  94. result = {
  95. success: true,
  96. exists: fs.existsSync(filePath)
  97. }
  98. } else {
  99. result = {
  100. success: false,
  101. error: `Unknown operation: ${operation}. Supported: create, read, update, check`
  102. }
  103. }
  104. process.stdout.write(JSON.stringify(result) + '\n')
  105. process.exit(result.success ? 0 : 1)