UNPKG

4.88 kBJavaScriptView Raw
1"use strict";
2var __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};
7var job_1 = require("./job");
8var file_1 = require("./file");
9var node_path = require("path"), fs = require("fs");
10var FolderJob = (function (_super) {
11 __extends(FolderJob, _super);
12 /**
13 * FolderJob constructor
14 * @param e
15 * @param path
16 */
17 function FolderJob(e, path) {
18 _super.call(this, e, path);
19 this.path = path;
20 this.files = [];
21 this.getStatistics();
22 // verify path leads to a valid, readable file, handle error if not
23 }
24 FolderJob.prototype.getStatistics = function () {
25 this.basename = node_path.basename(this.getPath());
26 this.dirname = node_path.dirname(this.getPath());
27 };
28 /**
29 * Creates file objects for folder contents. Async operation returns a callback on completion.
30 * @param callback
31 */
32 FolderJob.prototype.createFiles = function (callback) {
33 var fl = this;
34 var folder_path = this.getPath();
35 fs.readdir(folder_path, function (err, items) {
36 items = items.filter(function (item) { return !(/(^|\/)\.[^\/\.]/g).test(item); });
37 items.forEach(function (filename) {
38 var filepath = folder_path + node_path.sep + filename;
39 var file = new file_1.File(fl.e, filepath);
40 fl.addFile(file);
41 });
42 callback();
43 });
44 };
45 /**
46 * Gets the job name.
47 * @returns {string}
48 */
49 FolderJob.prototype.getName = function () {
50 return this.getBasename();
51 };
52 /**
53 * Get the basename.
54 * @returns {string}
55 */
56 FolderJob.prototype.getBasename = function () {
57 return this.basename;
58 };
59 /**
60 * Get the directory name.
61 * @returns {string}
62 */
63 FolderJob.prototype.getDirname = function () {
64 return this.dirname;
65 };
66 /**
67 * Get the path.
68 * @returns {string}
69 */
70 FolderJob.prototype.getPath = function () {
71 return this.path;
72 };
73 /**
74 * Set a new path.
75 * @param path
76 */
77 FolderJob.prototype.setPath = function (path) {
78 this.path = path;
79 this.getStatistics();
80 };
81 /**
82 * Add a file object to the job.
83 * @param file
84 */
85 FolderJob.prototype.addFile = function (file) {
86 this.files.push(file);
87 this.e.log(0, "Adding file \"" + file.getName() + "\" to job.", this);
88 };
89 /**
90 * Get a file object from the job.
91 * @param index
92 * @returns {File}
93 */
94 FolderJob.prototype.getFile = function (index) {
95 return this.files[index];
96 };
97 /**
98 * Get all files associated with the job.
99 * @returns {File[]}
100 */
101 FolderJob.prototype.getFiles = function () {
102 return this.files;
103 };
104 /**
105 * Get the number of files in this folder.
106 * @returns {number}
107 */
108 FolderJob.prototype.count = function () {
109 return this.files.length;
110 };
111 /**
112 * Get the extension.
113 * @returns {null}
114 */
115 FolderJob.prototype.getExtension = function () {
116 return null;
117 };
118 /**
119 * Check if job is a folder.
120 * @returns {boolean}
121 */
122 FolderJob.prototype.isFolder = function () {
123 return true;
124 };
125 /**
126 * Check if job is a file.
127 * @returns {boolean}
128 */
129 FolderJob.prototype.isFile = function () {
130 return false;
131 };
132 /**
133 * Moves a folder to a nest. This is an asynchronous method which provides a callback on completion.
134 * @param destinationNest
135 * @param callback
136 */
137 FolderJob.prototype.move = function (destinationNest, callback) {
138 var fj = this;
139 try {
140 destinationNest.take(fj, function (new_path) {
141 fj.setPath(new_path);
142 fj.e.log(1, "Job \"" + fj.getName() + "\" was moved to Nest \"" + destinationNest.name + "\".", fj);
143 if (callback) {
144 callback();
145 }
146 });
147 }
148 catch (e) {
149 fj.e.log(3, "Job \"" + fj.getName() + "\" was not moved to Nest \"" + destinationNest.name + "\". " + e, fj);
150 if (callback) {
151 callback();
152 }
153 }
154 };
155 /**
156 * Renames the job folder, leaving its content's names alone.
157 * @param newName
158 */
159 FolderJob.prototype.rename = function (newName) {
160 var fj = this;
161 var new_path = fj.getDirname() + node_path.sep + newName;
162 fs.renameSync(fj.getPath(), new_path);
163 fj.setPath(new_path);
164 };
165 return FolderJob;
166}(job_1.Job));
167exports.FolderJob = FolderJob;