Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict'
  2. var p = require('./minpath')
  3. var proc = require('./minproc')
  4. var buffer = require('is-buffer')
  5. module.exports = VFile
  6. var own = {}.hasOwnProperty
  7. // Order of setting (least specific to most), we need this because otherwise
  8. // `{stem: 'a', path: '~/b.js'}` would throw, as a path is needed before a
  9. // stem can be set.
  10. var order = ['history', 'path', 'basename', 'stem', 'extname', 'dirname']
  11. VFile.prototype.toString = toString
  12. // Access full path (`~/index.min.js`).
  13. Object.defineProperty(VFile.prototype, 'path', {get: getPath, set: setPath})
  14. // Access parent path (`~`).
  15. Object.defineProperty(VFile.prototype, 'dirname', {
  16. get: getDirname,
  17. set: setDirname
  18. })
  19. // Access basename (`index.min.js`).
  20. Object.defineProperty(VFile.prototype, 'basename', {
  21. get: getBasename,
  22. set: setBasename
  23. })
  24. // Access extname (`.js`).
  25. Object.defineProperty(VFile.prototype, 'extname', {
  26. get: getExtname,
  27. set: setExtname
  28. })
  29. // Access stem (`index.min`).
  30. Object.defineProperty(VFile.prototype, 'stem', {get: getStem, set: setStem})
  31. // Construct a new file.
  32. function VFile(options) {
  33. var prop
  34. var index
  35. if (!options) {
  36. options = {}
  37. } else if (typeof options === 'string' || buffer(options)) {
  38. options = {contents: options}
  39. } else if ('message' in options && 'messages' in options) {
  40. return options
  41. }
  42. if (!(this instanceof VFile)) {
  43. return new VFile(options)
  44. }
  45. this.data = {}
  46. this.messages = []
  47. this.history = []
  48. this.cwd = proc.cwd()
  49. // Set path related properties in the correct order.
  50. index = -1
  51. while (++index < order.length) {
  52. prop = order[index]
  53. if (own.call(options, prop)) {
  54. this[prop] = options[prop]
  55. }
  56. }
  57. // Set non-path related properties.
  58. for (prop in options) {
  59. if (order.indexOf(prop) < 0) {
  60. this[prop] = options[prop]
  61. }
  62. }
  63. }
  64. function getPath() {
  65. return this.history[this.history.length - 1]
  66. }
  67. function setPath(path) {
  68. assertNonEmpty(path, 'path')
  69. if (this.path !== path) {
  70. this.history.push(path)
  71. }
  72. }
  73. function getDirname() {
  74. return typeof this.path === 'string' ? p.dirname(this.path) : undefined
  75. }
  76. function setDirname(dirname) {
  77. assertPath(this.path, 'dirname')
  78. this.path = p.join(dirname || '', this.basename)
  79. }
  80. function getBasename() {
  81. return typeof this.path === 'string' ? p.basename(this.path) : undefined
  82. }
  83. function setBasename(basename) {
  84. assertNonEmpty(basename, 'basename')
  85. assertPart(basename, 'basename')
  86. this.path = p.join(this.dirname || '', basename)
  87. }
  88. function getExtname() {
  89. return typeof this.path === 'string' ? p.extname(this.path) : undefined
  90. }
  91. function setExtname(extname) {
  92. assertPart(extname, 'extname')
  93. assertPath(this.path, 'extname')
  94. if (extname) {
  95. if (extname.charCodeAt(0) !== 46 /* `.` */) {
  96. throw new Error('`extname` must start with `.`')
  97. }
  98. if (extname.indexOf('.', 1) > -1) {
  99. throw new Error('`extname` cannot contain multiple dots')
  100. }
  101. }
  102. this.path = p.join(this.dirname, this.stem + (extname || ''))
  103. }
  104. function getStem() {
  105. return typeof this.path === 'string'
  106. ? p.basename(this.path, this.extname)
  107. : undefined
  108. }
  109. function setStem(stem) {
  110. assertNonEmpty(stem, 'stem')
  111. assertPart(stem, 'stem')
  112. this.path = p.join(this.dirname || '', stem + (this.extname || ''))
  113. }
  114. // Get the value of the file.
  115. function toString(encoding) {
  116. return (this.contents || '').toString(encoding)
  117. }
  118. // Assert that `part` is not a path (i.e., does not contain `p.sep`).
  119. function assertPart(part, name) {
  120. if (part && part.indexOf(p.sep) > -1) {
  121. throw new Error(
  122. '`' + name + '` cannot be a path: did not expect `' + p.sep + '`'
  123. )
  124. }
  125. }
  126. // Assert that `part` is not empty.
  127. function assertNonEmpty(part, name) {
  128. if (!part) {
  129. throw new Error('`' + name + '` cannot be empty')
  130. }
  131. }
  132. // Assert `path` exists.
  133. function assertPath(path, name) {
  134. if (!path) {
  135. throw new Error('Setting `' + name + '` requires `path` to be set too')
  136. }
  137. }