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

FileUpdater.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. 'use strict';
  18. var fs = require('fs-extra');
  19. var path = require('path');
  20. var minimatch = require('minimatch');
  21. /**
  22. * Logging callback used in the FileUpdater methods.
  23. * @callback loggingCallback
  24. * @param {string} message A message describing a single file update operation.
  25. */
  26. /**
  27. * Updates a target file or directory with a source file or directory. (Directory updates are
  28. * not recursive.) Stats for target and source items must be passed in. This is an internal
  29. * helper function used by other methods in this module.
  30. *
  31. * @param {?string} sourcePath Source file or directory to be used to update the
  32. * destination. If the source is null, then the destination is deleted if it exists.
  33. * @param {?fs.Stats} sourceStats An instance of fs.Stats for the source path, or null if
  34. * the source does not exist.
  35. * @param {string} targetPath Required destination file or directory to be updated. If it does
  36. * not exist, it will be created.
  37. * @param {?fs.Stats} targetStats An instance of fs.Stats for the target path, or null if
  38. * the target does not exist.
  39. * @param {Object} [options] Optional additional parameters for the update.
  40. * @param {string} [options.rootDir] Optional root directory (such as a project) to which target
  41. * and source path parameters are relative; may be omitted if the paths are absolute. The
  42. * rootDir is always omitted from any logged paths, to make the logs easier to read.
  43. * @param {boolean} [options.all] If true, all files are copied regardless of last-modified times.
  44. * Otherwise, a file is copied if the source's last-modified time is greather than or
  45. * equal to the target's last-modified time, or if the file sizes are different.
  46. * @param {loggingCallback} [log] Optional logging callback that takes a string message
  47. * describing any file operations that are performed.
  48. * @return {boolean} true if any changes were made, or false if the force flag is not set
  49. * and everything was up to date
  50. */
  51. function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats, options, log) {
  52. var updated = false;
  53. var rootDir = (options && options.rootDir) || '';
  54. var copyAll = (options && options.all) || false;
  55. var targetFullPath = path.join(rootDir || '', targetPath);
  56. if (sourceStats) {
  57. var sourceFullPath = path.join(rootDir || '', sourcePath);
  58. if (targetStats && (targetStats.isDirectory() !== sourceStats.isDirectory())) {
  59. // The target exists. But if the directory status doesn't match the source, delete it.
  60. log('delete ' + targetPath);
  61. fs.removeSync(targetFullPath);
  62. targetStats = null;
  63. updated = true;
  64. }
  65. if (!targetStats) {
  66. if (sourceStats.isDirectory()) {
  67. // The target directory does not exist, so it should be created.
  68. log('mkdir ' + targetPath);
  69. fs.ensureDirSync(targetFullPath);
  70. updated = true;
  71. } else if (sourceStats.isFile()) {
  72. // The target file does not exist, so it should be copied from the source.
  73. log('copy ' + sourcePath + ' ' + targetPath + (copyAll ? '' : ' (new file)'));
  74. fs.copySync(sourceFullPath, targetFullPath);
  75. updated = true;
  76. }
  77. } else if (sourceStats.isFile() && targetStats.isFile()) {
  78. // The source and target paths both exist and are files.
  79. if (copyAll) {
  80. // The caller specified all files should be copied.
  81. log('copy ' + sourcePath + ' ' + targetPath);
  82. fs.copySync(sourceFullPath, targetFullPath);
  83. updated = true;
  84. } else {
  85. // Copy if the source has been modified since it was copied to the target, or if
  86. // the file sizes are different. (The latter catches most cases in which something
  87. // was done to the file after copying.) Comparison is >= rather than > to allow
  88. // for timestamps lacking sub-second precision in some filesystems.
  89. if (sourceStats.mtime.getTime() >= targetStats.mtime.getTime() ||
  90. sourceStats.size !== targetStats.size) {
  91. log('copy ' + sourcePath + ' ' + targetPath + ' (updated file)');
  92. fs.copySync(sourceFullPath, targetFullPath);
  93. updated = true;
  94. }
  95. }
  96. }
  97. } else if (targetStats) {
  98. // The target exists but the source is null, so the target should be deleted.
  99. log('delete ' + targetPath + (copyAll ? '' : ' (no source)'));
  100. fs.removeSync(targetFullPath);
  101. updated = true;
  102. }
  103. return updated;
  104. }
  105. /**
  106. * Helper for updatePath and updatePaths functions. Queries stats for source and target
  107. * and ensures target directory exists before copying a file.
  108. */
  109. function updatePathInternal (sourcePath, targetPath, options, log) {
  110. var rootDir = (options && options.rootDir) || '';
  111. var targetFullPath = path.join(rootDir, targetPath);
  112. var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
  113. var sourceStats = null;
  114. if (sourcePath) {
  115. // A non-null source path was specified. It should exist.
  116. var sourceFullPath = path.join(rootDir, sourcePath);
  117. if (!fs.existsSync(sourceFullPath)) {
  118. throw new Error('Source path does not exist: ' + sourcePath);
  119. }
  120. sourceStats = fs.statSync(sourceFullPath);
  121. // Create the target's parent directory if it doesn't exist.
  122. var parentDir = path.dirname(targetFullPath);
  123. if (!fs.existsSync(parentDir)) {
  124. fs.ensureDirSync(parentDir);
  125. }
  126. }
  127. return updatePathWithStats(sourcePath, sourceStats, targetPath, targetStats, options, log);
  128. }
  129. /**
  130. * Updates a target file or directory with a source file or directory. (Directory updates are
  131. * not recursive.)
  132. *
  133. * @param {?string} sourcePath Source file or directory to be used to update the
  134. * destination. If the source is null, then the destination is deleted if it exists.
  135. * @param {string} targetPath Required destination file or directory to be updated. If it does
  136. * not exist, it will be created.
  137. * @param {Object} [options] Optional additional parameters for the update.
  138. * @param {string} [options.rootDir] Optional root directory (such as a project) to which target
  139. * and source path parameters are relative; may be omitted if the paths are absolute. The
  140. * rootDir is always omitted from any logged paths, to make the logs easier to read.
  141. * @param {boolean} [options.all] If true, all files are copied regardless of last-modified times.
  142. * Otherwise, a file is copied if the source's last-modified time is greather than or
  143. * equal to the target's last-modified time, or if the file sizes are different.
  144. * @param {loggingCallback} [log] Optional logging callback that takes a string message
  145. * describing any file operations that are performed.
  146. * @return {boolean} true if any changes were made, or false if the force flag is not set
  147. * and everything was up to date
  148. */
  149. function updatePath (sourcePath, targetPath, options, log) {
  150. if (sourcePath !== null && typeof sourcePath !== 'string') {
  151. throw new Error('A source path (or null) is required.');
  152. }
  153. if (!targetPath || typeof targetPath !== 'string') {
  154. throw new Error('A target path is required.');
  155. }
  156. log = log || function () { };
  157. return updatePathInternal(sourcePath, targetPath, options, log);
  158. }
  159. /**
  160. * Updates files and directories based on a mapping from target paths to source paths. Targets
  161. * with null sources in the map are deleted.
  162. *
  163. * @param {Object} pathMap A dictionary mapping from target paths to source paths.
  164. * @param {Object} [options] Optional additional parameters for the update.
  165. * @param {string} [options.rootDir] Optional root directory (such as a project) to which target
  166. * and source path parameters are relative; may be omitted if the paths are absolute. The
  167. * rootDir is always omitted from any logged paths, to make the logs easier to read.
  168. * @param {boolean} [options.all] If true, all files are copied regardless of last-modified times.
  169. * Otherwise, a file is copied if the source's last-modified time is greather than or
  170. * equal to the target's last-modified time, or if the file sizes are different.
  171. * @param {loggingCallback} [log] Optional logging callback that takes a string message
  172. * describing any file operations that are performed.
  173. * @return {boolean} true if any changes were made, or false if the force flag is not set
  174. * and everything was up to date
  175. */
  176. function updatePaths (pathMap, options, log) {
  177. if (!pathMap || typeof pathMap !== 'object' || Array.isArray(pathMap)) {
  178. throw new Error('An object mapping from target paths to source paths is required.');
  179. }
  180. log = log || function () { };
  181. var updated = false;
  182. // Iterate in sorted order to ensure directories are created before files under them.
  183. Object.keys(pathMap).sort().forEach(function (targetPath) {
  184. var sourcePath = pathMap[targetPath];
  185. updated = updatePathInternal(sourcePath, targetPath, options, log) || updated;
  186. });
  187. return updated;
  188. }
  189. /**
  190. * Updates a target directory with merged files and subdirectories from source directories.
  191. *
  192. * @param {string|string[]} sourceDirs Required source directory or array of source directories
  193. * to be merged into the target. The directories are listed in order of precedence; files in
  194. * directories later in the array supersede files in directories earlier in the array
  195. * (regardless of timestamps).
  196. * @param {string} targetDir Required destination directory to be updated. If it does not exist,
  197. * it will be created. If it exists, newer files from source directories will be copied over,
  198. * and files missing in the source directories will be deleted.
  199. * @param {Object} [options] Optional additional parameters for the update.
  200. * @param {string} [options.rootDir] Optional root directory (such as a project) to which target
  201. * and source path parameters are relative; may be omitted if the paths are absolute. The
  202. * rootDir is always omitted from any logged paths, to make the logs easier to read.
  203. * @param {boolean} [options.all] If true, all files are copied regardless of last-modified times.
  204. * Otherwise, a file is copied if the source's last-modified time is greather than or
  205. * equal to the target's last-modified time, or if the file sizes are different.
  206. * @param {string|string[]} [options.include] Optional glob string or array of glob strings that
  207. * are tested against both target and source relative paths to determine if they are included
  208. * in the merge-and-update. If unspecified, all items are included.
  209. * @param {string|string[]} [options.exclude] Optional glob string or array of glob strings that
  210. * are tested against both target and source relative paths to determine if they are excluded
  211. * from the merge-and-update. Exclusions override inclusions. If unspecified, no items are
  212. * excluded.
  213. * @param {loggingCallback} [log] Optional logging callback that takes a string message
  214. * describing any file operations that are performed.
  215. * @return {boolean} true if any changes were made, or false if the force flag is not set
  216. * and everything was up to date
  217. */
  218. function mergeAndUpdateDir (sourceDirs, targetDir, options, log) {
  219. if (sourceDirs && typeof sourceDirs === 'string') {
  220. sourceDirs = [ sourceDirs ];
  221. } else if (!Array.isArray(sourceDirs)) {
  222. throw new Error('A source directory path or array of paths is required.');
  223. }
  224. if (!targetDir || typeof targetDir !== 'string') {
  225. throw new Error('A target directory path is required.');
  226. }
  227. log = log || function () { };
  228. var rootDir = (options && options.rootDir) || '';
  229. var include = (options && options.include) || [ '**' ];
  230. if (typeof include === 'string') {
  231. include = [ include ];
  232. } else if (!Array.isArray(include)) {
  233. throw new Error('Include parameter must be a glob string or array of glob strings.');
  234. }
  235. var exclude = (options && options.exclude) || [];
  236. if (typeof exclude === 'string') {
  237. exclude = [ exclude ];
  238. } else if (!Array.isArray(exclude)) {
  239. throw new Error('Exclude parameter must be a glob string or array of glob strings.');
  240. }
  241. // Scan the files in each of the source directories.
  242. var sourceMaps = sourceDirs.map(function (sourceDir) {
  243. return path.join(rootDir, sourceDir);
  244. }).map(function (sourcePath) {
  245. if (!fs.existsSync(sourcePath)) {
  246. throw new Error('Source directory does not exist: ' + sourcePath);
  247. }
  248. return mapDirectory(rootDir, path.relative(rootDir, sourcePath), include, exclude);
  249. });
  250. // Scan the files in the target directory, if it exists.
  251. var targetMap = {};
  252. var targetFullPath = path.join(rootDir, targetDir);
  253. if (fs.existsSync(targetFullPath)) {
  254. targetMap = mapDirectory(rootDir, targetDir, include, exclude);
  255. }
  256. var pathMap = mergePathMaps(sourceMaps, targetMap, targetDir);
  257. var updated = false;
  258. // Iterate in sorted order to ensure directories are created before files under them.
  259. Object.keys(pathMap).sort().forEach(function (subPath) {
  260. var entry = pathMap[subPath];
  261. updated = updatePathWithStats(
  262. entry.sourcePath,
  263. entry.sourceStats,
  264. entry.targetPath,
  265. entry.targetStats,
  266. options,
  267. log) || updated;
  268. });
  269. return updated;
  270. }
  271. /**
  272. * Creates a dictionary map of all files and directories under a path.
  273. */
  274. function mapDirectory (rootDir, subDir, include, exclude) {
  275. var dirMap = { '': { subDir: subDir, stats: fs.statSync(path.join(rootDir, subDir)) } };
  276. mapSubdirectory(rootDir, subDir, '', include, exclude, dirMap);
  277. return dirMap;
  278. function mapSubdirectory (rootDir, subDir, relativeDir, include, exclude, dirMap) {
  279. var itemMapped = false;
  280. var items = fs.readdirSync(path.join(rootDir, subDir, relativeDir));
  281. items.forEach(function (item) {
  282. var relativePath = path.join(relativeDir, item);
  283. if (!matchGlobArray(relativePath, exclude)) {
  284. // Stats obtained here (required at least to know where to recurse in directories)
  285. // are saved for later, where the modified times may also be used. This minimizes
  286. // the number of file I/O operations performed.
  287. var fullPath = path.join(rootDir, subDir, relativePath);
  288. var stats = fs.statSync(fullPath);
  289. if (stats.isDirectory()) {
  290. // Directories are included if either something under them is included or they
  291. // match an include glob.
  292. if (mapSubdirectory(rootDir, subDir, relativePath, include, exclude, dirMap) ||
  293. matchGlobArray(relativePath, include)) {
  294. dirMap[relativePath] = { subDir: subDir, stats: stats };
  295. itemMapped = true;
  296. }
  297. } else if (stats.isFile()) {
  298. // Files are included only if they match an include glob.
  299. if (matchGlobArray(relativePath, include)) {
  300. dirMap[relativePath] = { subDir: subDir, stats: stats };
  301. itemMapped = true;
  302. }
  303. }
  304. }
  305. });
  306. return itemMapped;
  307. }
  308. function matchGlobArray (path, globs) {
  309. return globs.some(function (elem) {
  310. return minimatch(path, elem, { dot: true });
  311. });
  312. }
  313. }
  314. /**
  315. * Merges together multiple source maps and a target map into a single mapping from
  316. * relative paths to objects with target and source paths and stats.
  317. */
  318. function mergePathMaps (sourceMaps, targetMap, targetDir) {
  319. // Merge multiple source maps together, along with target path info.
  320. // Entries in later source maps override those in earlier source maps.
  321. // Target stats will be filled in below for targets that exist.
  322. var pathMap = {};
  323. sourceMaps.forEach(function (sourceMap) {
  324. Object.keys(sourceMap).forEach(function (sourceSubPath) {
  325. var sourceEntry = sourceMap[sourceSubPath];
  326. pathMap[sourceSubPath] = {
  327. targetPath: path.join(targetDir, sourceSubPath),
  328. targetStats: null,
  329. sourcePath: path.join(sourceEntry.subDir, sourceSubPath),
  330. sourceStats: sourceEntry.stats
  331. };
  332. });
  333. });
  334. // Fill in target stats for targets that exist, and create entries
  335. // for targets that don't have any corresponding sources.
  336. Object.keys(targetMap).forEach(function (subPath) {
  337. var entry = pathMap[subPath];
  338. if (entry) {
  339. entry.targetStats = targetMap[subPath].stats;
  340. } else {
  341. pathMap[subPath] = {
  342. targetPath: path.join(targetDir, subPath),
  343. targetStats: targetMap[subPath].stats,
  344. sourcePath: null,
  345. sourceStats: null
  346. };
  347. }
  348. });
  349. return pathMap;
  350. }
  351. module.exports = {
  352. updatePath: updatePath,
  353. updatePaths: updatePaths,
  354. mergeAndUpdateDir: mergeAndUpdateDir
  355. };