1 | "use strict";
|
2 | var __extends = (this && this.__extends) || function (d, b) {
|
3 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
4 | function __() { this.constructor = d; }
|
5 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
6 | };
|
7 | var nest_1 = require("./nest");
|
8 | var fileJob_1 = require("./../job/fileJob");
|
9 | var folderJob_1 = require("./../job/folderJob");
|
10 | var node_watch = require("node-watch"), fs = require("fs"), path_mod = require("path"), tmp = require("tmp"), mkdirp = require("mkdirp"), _ = require("lodash");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var FolderNest = (function (_super) {
|
16 | __extends(FolderNest, _super);
|
17 | function FolderNest(e, path, allowCreate) {
|
18 | var nest_name = path_mod.basename(path);
|
19 | _super.call(this, e, nest_name);
|
20 | this.allowCreate = allowCreate;
|
21 | this.checkDirectorySync(path);
|
22 | this.path = path;
|
23 | this.heldJobs = [];
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 | FolderNest.prototype.checkDirectorySync = function (directory) {
|
30 | var fn = this;
|
31 | try {
|
32 | fs.statSync(directory);
|
33 | }
|
34 | catch (e) {
|
35 | if (fn.allowCreate) {
|
36 | mkdirp.sync(directory);
|
37 | fn.e.log(1, "Directory \"" + directory + "\" was created since it did not already exist.", this);
|
38 | }
|
39 | else {
|
40 | fn.e.log(3, "Directory \"" + directory + "\" did not exist and was not created.", this);
|
41 | }
|
42 | }
|
43 | };
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | FolderNest.prototype.createJob = function (path, arrive) {
|
51 | if (arrive === void 0) { arrive = true; }
|
52 | var fl = this;
|
53 | var job;
|
54 |
|
55 | try {
|
56 | fs.accessSync(path, fs.F_OK);
|
57 |
|
58 | var path_stats = fs.lstatSync(path);
|
59 | if (path_stats.isDirectory()) {
|
60 | job = new folderJob_1.FolderJob(fl.e, path);
|
61 | job.createFiles(function () {
|
62 | if (arrive) {
|
63 |
|
64 | fl.arrive(job);
|
65 | }
|
66 | });
|
67 | }
|
68 | else if (path_stats.isFile()) {
|
69 | job = new fileJob_1.FileJob(fl.e, path);
|
70 | if (arrive) {
|
71 |
|
72 | fl.arrive(job);
|
73 | }
|
74 | }
|
75 | else {
|
76 | throw "Path is not a file or folder!";
|
77 | }
|
78 | }
|
79 | catch (e) {
|
80 |
|
81 | fl.e.log(0, "Job creation ignored because file did not exist.", fl);
|
82 | }
|
83 | return job;
|
84 | };
|
85 | |
86 |
|
87 |
|
88 |
|
89 | FolderNest.prototype.load = function (hold) {
|
90 | if (hold === void 0) { hold = false; }
|
91 | var fl = this;
|
92 | fs.readdir(fl.path, function (err, items) {
|
93 | if (items) {
|
94 | items = items.filter(function (item) { return !(/(^|\/)\.[^\/\.]/g).test(item); });
|
95 | items.forEach(function (filename) {
|
96 | var filepath = fl.path + path_mod.sep + filename;
|
97 | var job;
|
98 | if (hold === false) {
|
99 | fl.createJob(filepath, true);
|
100 | }
|
101 | else {
|
102 | job = fl.createJob(filepath, false);
|
103 | fl.holdJob(job);
|
104 | }
|
105 | });
|
106 | }
|
107 | });
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 | FolderNest.prototype.watch = function (hold) {
|
114 | if (hold === void 0) { hold = false; }
|
115 | var fl = this;
|
116 | var watch_options = {
|
117 | recursive: false
|
118 | };
|
119 | node_watch(fl.path, watch_options, function (filepath) {
|
120 | var job;
|
121 | if (hold === false) {
|
122 | job = fl.createJob(filepath, true);
|
123 | }
|
124 | else {
|
125 | job = fl.createJob(filepath, false);
|
126 | fl.holdJob(job);
|
127 | }
|
128 | });
|
129 | };
|
130 | |
131 |
|
132 |
|
133 | FolderNest.prototype.watchHold = function () {
|
134 | var fl = this;
|
135 | fl.load(true);
|
136 | fl.watch(true);
|
137 | };
|
138 | |
139 |
|
140 |
|
141 |
|
142 | FolderNest.prototype.arrive = function (job) {
|
143 | _super.prototype.arrive.call(this, job);
|
144 | };
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 | FolderNest.prototype.take = function (job, callback) {
|
151 |
|
152 | var new_path = this.path + "/" + job.getBasename();
|
153 | fs.renameSync(job.getPath(), new_path);
|
154 | job.setPath(new_path);
|
155 | callback(job);
|
156 | };
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 | FolderNest.prototype.getUnwatchedJobs = function () {
|
163 | var fl = this;
|
164 | var jobs = [];
|
165 | var fileArray = fs.readdirSync(fl.path);
|
166 | var items = fileArray.filter(function (item) { return !(/(^|\/)\.[^\/\.]/g).test(item); });
|
167 | items.forEach(function (filename) {
|
168 | var filepath = fl.path + path_mod.sep + filename;
|
169 | var job = fl.createJob(filepath, false);
|
170 | jobs.push(job);
|
171 |
|
172 | });
|
173 | return jobs;
|
174 | };
|
175 | |
176 |
|
177 |
|
178 |
|
179 | FolderNest.prototype.getHeldJobs = function () {
|
180 | return this.heldJobs;
|
181 | };
|
182 | |
183 |
|
184 |
|
185 |
|
186 | FolderNest.prototype.holdJob = function (job) {
|
187 | this.heldJobs.push(job);
|
188 | };
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | FolderNest.prototype.getHeldJob = function (jobId) {
|
217 | var f = this;
|
218 | var job = _.find(f.getHeldJobs(), function (j) { return j.getId() === jobId; });
|
219 | var jobIndex = _.findIndex(f.getHeldJobs(), function (j) { return j.getId() === jobId; });
|
220 | if (!job) {
|
221 | f.e.log(3, "Job ID " + jobId + " could not be found in the " + f.getHeldJobs().length + " pending held jobs.", f);
|
222 | }
|
223 | else {
|
224 | f.heldJobs.splice(jobIndex, 1);
|
225 | }
|
226 | return job;
|
227 | };
|
228 | return FolderNest;
|
229 | }(nest_1.Nest));
|
230 | exports.FolderNest = FolderNest;
|