json-parser.js 3.3 KB

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