Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

simple-plist.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const bplistParser = require('bplist-parser')
  2. const bplistCreator = require('bplist-creator')
  3. const plist = require('plist')
  4. const fs = require('fs')
  5. function parse(aStringOrBuffer, aFile) {
  6. const firstByte = aStringOrBuffer[0]
  7. let results
  8. try {
  9. if (firstByte === 60 || firstByte === '<') {
  10. results = plist.parse(aStringOrBuffer.toString())
  11. } else if (firstByte === 98) {
  12. ;[results] = bplistParser.parseBuffer(aStringOrBuffer)
  13. } else if (aFile != null) {
  14. throw new Error(`Unable to determine format for '${aFile}'`)
  15. } else {
  16. throw new Error('Unable to determine format for plist aStringOrBuffer')
  17. }
  18. } catch (error) {
  19. throw new Error(`${aFile} has errors`)
  20. }
  21. return results
  22. }
  23. function readFileSync(aFile) {
  24. const contents = fs.readFileSync(aFile)
  25. if (contents.length === 0) {
  26. return {}
  27. }
  28. return parse(contents, aFile)
  29. }
  30. function readFile(aFile, callback) {
  31. fs.readFile(aFile, (err, contents) => {
  32. let results
  33. if (err) {
  34. callback(err)
  35. }
  36. try {
  37. results = parse(contents, aFile)
  38. callback(null, results)
  39. } catch (error) {
  40. callback(error)
  41. }
  42. })
  43. }
  44. function writeFileSync(aFile, anObject, options) {
  45. const data = plist.build(anObject)
  46. return fs.writeFileSync(aFile, data, options)
  47. }
  48. function writeFile(aFile, anObject, options, callback) {
  49. if (arguments.length === 3 && typeof options === 'function') {
  50. fs.writeFile(aFile, plist.build(anObject), options)
  51. } else {
  52. fs.writeFile(aFile, plist.build(anObject), options, callback)
  53. }
  54. }
  55. function writeBinaryFileSync(aFile, anObject, options) {
  56. return fs.writeFileSync(aFile, bplistCreator(anObject), options)
  57. }
  58. function writeBinaryFile(aFile, anObject, options, callback) {
  59. if (arguments.length === 3 && typeof options === 'function') {
  60. fs.writeFile(aFile, bplistCreator(anObject), options)
  61. } else {
  62. fs.writeFile(aFile, bplistCreator(anObject), options, callback)
  63. }
  64. }
  65. function stringify(anObject) {
  66. return plist.build(anObject)
  67. }
  68. module.exports = {
  69. bplistCreator,
  70. bplistParser,
  71. parse,
  72. plist,
  73. readFile,
  74. readFileSync,
  75. stringify,
  76. writeBinaryFile,
  77. writeBinaryFileSync,
  78. writeFile,
  79. writeFileSync
  80. }