json-parser.js 3.7 KB

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