1 let fs = require('fs');
2 let Task = require('./task').Task;
4 function isFileOrDirectory(t) {
5 return (t instanceof FileTask ||
6 t instanceof DirectoryTask);
10 return (t instanceof FileTask && !(t instanceof DirectoryTask));
21 @description A Jake FileTask
23 @param {String} name The name of the Task
24 @param {Array} [prereqs] Prerequisites to be run before this task
25 @param {Function} [action] The action to perform to create this file
26 @param {Object} [opts]
27 @param {Array} [opts.asyc=false] Perform this task asynchronously.
28 If you flag a task with this option, you must call the global
29 `complete` method inside the task's action, for execution to proceed
32 class FileTask extends Task {
33 constructor(...args) {
36 if (fs.existsSync(this.name)) {
45 let prereqs = this.prereqs;
50 if (this.taskStatus == Task.runStatuses.DONE) {
53 // The always-make override
54 else if (jake.program.opts['always-make']) {
60 // We need either an existing file, or an action to create one.
61 // First try grabbing the actual mod-time of the file
65 // Then fall back to looking for an action
67 if (typeof this.action == 'function') {
71 throw new Error('File-task ' + this.fullName + ' has no ' +
72 'existing file, and no action to create one.');
76 // Compare mod-time of all the prereqs with its mod-time
77 // If any prereqs are newer, need to run the action to update
78 if (prereqs && prereqs.length) {
79 for (let i = 0, ii = prereqs.length; i < ii; i++) {
80 prereqName = prereqs[i];
81 prereqTask = this.namespace.resolveTask(prereqName) ||
82 jake.createPlaceholderFileTask(prereqName, this.namespace);
84 // 1. The prereq is a normal task (not file/dir)
85 // 2. The prereq is a file-task with a mod-date more recent than
86 // the one for this file/dir
88 if (!isFileOrDirectory(prereqTask) ||
89 (isFile(prereqTask) && prereqTask.modTime > this.modTime)) {
95 // File/dir has no prereqs, and exists -- no need to run
98 this.taskStatus = Task.runStatuses.DONE;
105 let stats = fs.statSync(this.name);
106 this.modTime = stats.mtime;
111 this.updateModTime();
114 Task.prototype.complete.apply(this, arguments);
119 exports.FileTask = FileTask;
121 // DirectoryTask is a subclass of FileTask, depends on it
123 let DirectoryTask = require('./directory_task').DirectoryTask;