Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

simple-plist.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(error)
  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. if (err) {
  33. return callback(err)
  34. }
  35. let results
  36. try {
  37. results = parse(contents, aFile)
  38. } catch (error) {
  39. return callback(error)
  40. }
  41. return callback(null, results)
  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. }