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.

file.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * archiver-utils
  3. *
  4. * Copyright (c) 2012-2014 Chris Talkington, contributors.
  5. * Licensed under the MIT license.
  6. * https://github.com/archiverjs/node-archiver/blob/master/LICENSE-MIT
  7. */
  8. var fs = require('graceful-fs');
  9. var path = require('path');
  10. var flatten = require('lodash.flatten');
  11. var difference = require('lodash.difference');
  12. var union = require('lodash.union');
  13. var isPlainObject = require('lodash.isplainobject');
  14. var glob = require('glob');
  15. var file = module.exports = {};
  16. var pathSeparatorRe = /[\/\\]/g;
  17. // Process specified wildcard glob patterns or filenames against a
  18. // callback, excluding and uniquing files in the result set.
  19. var processPatterns = function(patterns, fn) {
  20. // Filepaths to return.
  21. var result = [];
  22. // Iterate over flattened patterns array.
  23. flatten(patterns).forEach(function(pattern) {
  24. // If the first character is ! it should be omitted
  25. var exclusion = pattern.indexOf('!') === 0;
  26. // If the pattern is an exclusion, remove the !
  27. if (exclusion) { pattern = pattern.slice(1); }
  28. // Find all matching files for this pattern.
  29. var matches = fn(pattern);
  30. if (exclusion) {
  31. // If an exclusion, remove matching files.
  32. result = difference(result, matches);
  33. } else {
  34. // Otherwise add matching files.
  35. result = union(result, matches);
  36. }
  37. });
  38. return result;
  39. };
  40. // True if the file path exists.
  41. file.exists = function() {
  42. var filepath = path.join.apply(path, arguments);
  43. return fs.existsSync(filepath);
  44. };
  45. // Return an array of all file paths that match the given wildcard patterns.
  46. file.expand = function(...args) {
  47. // If the first argument is an options object, save those options to pass
  48. // into the File.prototype.glob.sync method.
  49. var options = isPlainObject(args[0]) ? args.shift() : {};
  50. // Use the first argument if it's an Array, otherwise convert the arguments
  51. // object to an array and use that.
  52. var patterns = Array.isArray(args[0]) ? args[0] : args;
  53. // Return empty set if there are no patterns or filepaths.
  54. if (patterns.length === 0) { return []; }
  55. // Return all matching filepaths.
  56. var matches = processPatterns(patterns, function(pattern) {
  57. // Find all matching files for this pattern.
  58. return glob.sync(pattern, options);
  59. });
  60. // Filter result set?
  61. if (options.filter) {
  62. matches = matches.filter(function(filepath) {
  63. filepath = path.join(options.cwd || '', filepath);
  64. try {
  65. if (typeof options.filter === 'function') {
  66. return options.filter(filepath);
  67. } else {
  68. // If the file is of the right type and exists, this should work.
  69. return fs.statSync(filepath)[options.filter]();
  70. }
  71. } catch(e) {
  72. // Otherwise, it's probably not the right type.
  73. return false;
  74. }
  75. });
  76. }
  77. return matches;
  78. };
  79. // Build a multi task "files" object dynamically.
  80. file.expandMapping = function(patterns, destBase, options) {
  81. options = Object.assign({
  82. rename: function(destBase, destPath) {
  83. return path.join(destBase || '', destPath);
  84. }
  85. }, options);
  86. var files = [];
  87. var fileByDest = {};
  88. // Find all files matching pattern, using passed-in options.
  89. file.expand(options, patterns).forEach(function(src) {
  90. var destPath = src;
  91. // Flatten?
  92. if (options.flatten) {
  93. destPath = path.basename(destPath);
  94. }
  95. // Change the extension?
  96. if (options.ext) {
  97. destPath = destPath.replace(/(\.[^\/]*)?$/, options.ext);
  98. }
  99. // Generate destination filename.
  100. var dest = options.rename(destBase, destPath, options);
  101. // Prepend cwd to src path if necessary.
  102. if (options.cwd) { src = path.join(options.cwd, src); }
  103. // Normalize filepaths to be unix-style.
  104. dest = dest.replace(pathSeparatorRe, '/');
  105. src = src.replace(pathSeparatorRe, '/');
  106. // Map correct src path to dest path.
  107. if (fileByDest[dest]) {
  108. // If dest already exists, push this src onto that dest's src array.
  109. fileByDest[dest].src.push(src);
  110. } else {
  111. // Otherwise create a new src-dest file mapping object.
  112. files.push({
  113. src: [src],
  114. dest: dest,
  115. });
  116. // And store a reference for later use.
  117. fileByDest[dest] = files[files.length - 1];
  118. }
  119. });
  120. return files;
  121. };
  122. // reusing bits of grunt's multi-task source normalization
  123. file.normalizeFilesArray = function(data) {
  124. var files = [];
  125. data.forEach(function(obj) {
  126. var prop;
  127. if ('src' in obj || 'dest' in obj) {
  128. files.push(obj);
  129. }
  130. });
  131. if (files.length === 0) {
  132. return [];
  133. }
  134. files = _(files).chain().forEach(function(obj) {
  135. if (!('src' in obj) || !obj.src) { return; }
  136. // Normalize .src properties to flattened array.
  137. if (Array.isArray(obj.src)) {
  138. obj.src = flatten(obj.src);
  139. } else {
  140. obj.src = [obj.src];
  141. }
  142. }).map(function(obj) {
  143. // Build options object, removing unwanted properties.
  144. var expandOptions = Object.assign({}, obj);
  145. delete expandOptions.src;
  146. delete expandOptions.dest;
  147. // Expand file mappings.
  148. if (obj.expand) {
  149. return file.expandMapping(obj.src, obj.dest, expandOptions).map(function(mapObj) {
  150. // Copy obj properties to result.
  151. var result = Object.assign({}, obj);
  152. // Make a clone of the orig obj available.
  153. result.orig = Object.assign({}, obj);
  154. // Set .src and .dest, processing both as templates.
  155. result.src = mapObj.src;
  156. result.dest = mapObj.dest;
  157. // Remove unwanted properties.
  158. ['expand', 'cwd', 'flatten', 'rename', 'ext'].forEach(function(prop) {
  159. delete result[prop];
  160. });
  161. return result;
  162. });
  163. }
  164. // Copy obj properties to result, adding an .orig property.
  165. var result = Object.assign({}, obj);
  166. // Make a clone of the orig obj available.
  167. result.orig = Object.assign({}, obj);
  168. if ('src' in result) {
  169. // Expose an expand-on-demand getter method as .src.
  170. Object.defineProperty(result, 'src', {
  171. enumerable: true,
  172. get: function fn() {
  173. var src;
  174. if (!('result' in fn)) {
  175. src = obj.src;
  176. // If src is an array, flatten it. Otherwise, make it into an array.
  177. src = Array.isArray(src) ? flatten(src) : [src];
  178. // Expand src files, memoizing result.
  179. fn.result = file.expand(expandOptions, src);
  180. }
  181. return fn.result;
  182. }
  183. });
  184. }
  185. if ('dest' in result) {
  186. result.dest = obj.dest;
  187. }
  188. return result;
  189. }).flatten().value();
  190. return files;
  191. };