file_task.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. let fs = require('fs');
  2. let Task = require('./task').Task;
  3. function isFileOrDirectory(t) {
  4. return (t instanceof FileTask ||
  5. t instanceof DirectoryTask);
  6. }
  7. function isFile(t) {
  8. return (t instanceof FileTask && !(t instanceof DirectoryTask));
  9. }
  10. /**
  11. @name jake
  12. @namespace jake
  13. */
  14. /**
  15. @name jake.FileTask
  16. @class`
  17. @extentds Task
  18. @description A Jake FileTask
  19. @param {String} name The name of the Task
  20. @param {Array} [prereqs] Prerequisites to be run before this task
  21. @param {Function} [action] The action to perform to create this file
  22. @param {Object} [opts]
  23. @param {Array} [opts.asyc=false] Perform this task asynchronously.
  24. If you flag a task with this option, you must call the global
  25. `complete` method inside the task's action, for execution to proceed
  26. to the next task.
  27. */
  28. class FileTask extends Task {
  29. constructor(...args) {
  30. super(...args);
  31. this.dummy = false;
  32. if (fs.existsSync(this.name)) {
  33. this.updateModTime();
  34. }
  35. else {
  36. this.modTime = null;
  37. }
  38. }
  39. isNeeded() {
  40. let prereqs = this.prereqs;
  41. let prereqName;
  42. let prereqTask;
  43. // No repeatsies
  44. if (this.taskStatus == Task.runStatuses.DONE) {
  45. return false;
  46. }
  47. // The always-make override
  48. else if (jake.program.opts['always-make']) {
  49. return true;
  50. }
  51. // Default case
  52. else {
  53. // We need either an existing file, or an action to create one.
  54. // First try grabbing the actual mod-time of the file
  55. try {
  56. this.updateModTime();
  57. }
  58. // Then fall back to looking for an action
  59. catch(e) {
  60. if (typeof this.action == 'function') {
  61. return true;
  62. }
  63. else {
  64. throw new Error('File-task ' + this.fullName + ' has no ' +
  65. 'existing file, and no action to create one.');
  66. }
  67. }
  68. // Compare mod-time of all the prereqs with its mod-time
  69. // If any prereqs are newer, need to run the action to update
  70. if (prereqs && prereqs.length) {
  71. for (let i = 0, ii = prereqs.length; i < ii; i++) {
  72. prereqName = prereqs[i];
  73. prereqTask = this.namespace.resolveTask(prereqName) ||
  74. jake.createPlaceholderFileTask(prereqName, this.namespace);
  75. // Run the action if:
  76. // 1. The prereq is a normal task (not file/dir)
  77. // 2. The prereq is a file-task with a mod-date more recent than
  78. // the one for this file/dir
  79. if (prereqTask) {
  80. if (!isFileOrDirectory(prereqTask) ||
  81. (isFile(prereqTask) && prereqTask.modTime > this.modTime)) {
  82. return true;
  83. }
  84. }
  85. }
  86. this.taskStatus = Task.runStatuses.DONE;
  87. return false;
  88. }
  89. // File/dir has no prereqs, and exists -- no need to run
  90. else {
  91. // Effectively done
  92. this.taskStatus = Task.runStatuses.DONE;
  93. return false;
  94. }
  95. }
  96. }
  97. updateModTime() {
  98. let stats = fs.statSync(this.name);
  99. this.modTime = stats.mtime;
  100. }
  101. complete() {
  102. if (!this.dummy) {
  103. this.updateModTime();
  104. }
  105. // Hackity hack
  106. Task.prototype.complete.apply(this, arguments);
  107. }
  108. }
  109. exports.FileTask = FileTask;
  110. // DirectoryTask is a subclass of FileTask, depends on it
  111. // being defined
  112. let DirectoryTask = require('./directory_task').DirectoryTask;