UNPKG

6.67 kBJavaScriptView Raw
1"use strict";
2var tunnel_1 = require("./tunnel/tunnel");
3var ftpNest_1 = require("./nest/ftpNest");
4var folderNest_1 = require("./nest/folderNest");
5var environment_1 = require("./environment/environment");
6var webhookNest_1 = require("./nest/webhookNest");
7var autoFolderNest_1 = require("./nest/autoFolderNest");
8var s3Nest_1 = require("./nest/s3Nest");
9/**
10 * Expose `Antfarm`.
11 */
12var Antfarm = (function () {
13 /**
14 * Antfarm constructor
15 * @param options Antfarm options
16 */
17 function Antfarm(options) {
18 this.e = new environment_1.Environment(options);
19 this.e.log(1, "Started antfarm", this);
20 }
21 Antfarm.prototype.version = function () {
22 return "0.0.1";
23 };
24 /**
25 * Factory method which returns a Tunnel.
26 * @param name
27 * @returns {Tunnel}
28 */
29 Antfarm.prototype.createTunnel = function (name) {
30 return new tunnel_1.Tunnel(this.e, name);
31 };
32 /**
33 * Factory method which returns a FolderNest.
34 * @param path Path of the folder.
35 * @param allowCreate Optional boolean flag to allow creation of folder if it does not exist.
36 * @returns {FolderNest}
37 * #### Example
38 * ```js
39 * var out_folder = af.createFolderNest("/Users/dominick/Desktop/My Folder/");
40 * ```
41 */
42 Antfarm.prototype.createFolderNest = function (path, allowCreate) {
43 if (allowCreate === void 0) { allowCreate = false; }
44 return new folderNest_1.FolderNest(this.e, path, allowCreate);
45 };
46 /**
47 * Factory method which returns an AutoFolderNest. If the auto managed directory does not exist, it is created.
48 * @param hierarchy Path of the folder as a string or an array of strings as path segments.
49 * @returns {AutoFolderNest}
50 *
51 * #### Example
52 * ```js
53 * af.createAutoFolderNest("outfolder")
54 * // /Users/dominick/My Automanaged Directory/outfolder
55 * ```
56 * #### Example
57 * ```js
58 * af.createAutoFolderNest(["proofing", "others"])
59 * // /Users/dominick/My Automanaged Directory/proofing/others
60 * ```
61 */
62 Antfarm.prototype.createAutoFolderNest = function (hierarchy) {
63 return new autoFolderNest_1.AutoFolderNest(this.e, hierarchy);
64 };
65 /**
66 * Factory method which returns an FtpNest.
67 * @param host Hostname or IP address of the FTP server.
68 * @param port Port number of the FTP server.
69 * @param username FTP account username.
70 * @param password FTP account password.
71 * @param checkEvery Frequency of re-checking FTP in minutes.
72 * @returns {FtpNest}
73 * #### Example
74 * ```js
75 * // Check FTP directory every 2 minutes
76 * var my_ftp = af.createFtpNest("ftp.example.com", 21, "", "", 2);
77 * ```
78 */
79 Antfarm.prototype.createFtpNest = function (host, port, username, password, checkEvery) {
80 if (port === void 0) { port = 21; }
81 if (username === void 0) { username = ""; }
82 if (password === void 0) { password = ""; }
83 if (checkEvery === void 0) { checkEvery = 10; }
84 return new ftpNest_1.FtpNest(this.e, host, port, username, password, checkEvery);
85 };
86 /**
87 * Factory method to create and return an S3 nest.
88 * @param bucket
89 * @param keyPrefix
90 * @param checkEvery
91 * @param allowCreation
92 * @returns {S3Nest}
93 * ```js
94 * var bucket = af.createS3Nest("my-bucket-name", "", 1, true);
95 * ```
96 */
97 Antfarm.prototype.createS3Nest = function (bucket, keyPrefix, checkEvery, allowCreation) {
98 if (checkEvery === void 0) { checkEvery = 5; }
99 if (allowCreation === void 0) { allowCreation = false; }
100 return new s3Nest_1.S3Nest(this.e, bucket, keyPrefix, checkEvery, allowCreation);
101 };
102 /**
103 * Factory method which returns a WebhookNest.
104 * @param path The path which is generated in the webhook's route. You can supply a string or array of strings.
105 * @param httpMethod HTTP method for this webhook. Choose "all" for any HTTP method.
106 * @param handleRequest Optional callback function to handle the request, for sending a custom response.
107 * @returns {WebhookNest}
108 *
109 * #### Example
110 * ```js
111 * var webhook = af.createWebhookNest(["proof", "create"], "post");
112 * ```
113 *
114 * #### Example returning custom response
115 * ```js
116 * var webhook = af.createWebhookNest(["proof", "create"], "post", function(req, res, job, nest){
117 * res.setHeader("Content-Type", "application/json; charset=utf-8");
118 * res.end(JSON.stringify({
119 * job_name: job.getName(),
120 * job_id: job.getId(),
121 * message: "Proof created!"
122 * }));
123 * });
124 * ```
125 */
126 Antfarm.prototype.createWebhookNest = function (path, httpMethod, handleRequest) {
127 if (httpMethod === void 0) { httpMethod = "all"; }
128 return new webhookNest_1.WebhookNest(this.e, path, httpMethod, handleRequest);
129 };
130 /**
131 * Load an entire directory of workflow modules.
132 * @param directory Path to the workflow modules.
133 * #### Example
134 * ```js
135 * af.loadDir("./workflows");
136 * ```
137 */
138 Antfarm.prototype.loadDir = function (directory) {
139 var af = this;
140 var workflows = require("require-dir-all")(directory, {
141 _parentsToSkip: 1,
142 indexAsParent: true,
143 throwNoDir: true
144 });
145 var loaded_counter = 0;
146 for (var workflow in workflows) {
147 try {
148 new workflows[workflow](af);
149 loaded_counter++;
150 }
151 catch (e) {
152 af.e.log(3, "Couldn't load workflow module \"" + workflow + "\". " + e, af);
153 }
154 }
155 af.e.log(1, "Loaded " + loaded_counter + " workflows.", af);
156 };
157 /**
158 * Log messages into the antfarm logger.
159 * @param type {number} The log level. 0 = debug, 1 = info, 2 = warning, 3 = error
160 * @param message {string} Log message.
161 * @param actor {any} Instance which triggers the action being logged.
162 * @param instances {any[]} Array of of other involved instances.
163 * #### Example
164 * ```js
165 * job.e.log(1, `Transferred to Tunnel "${tunnel.getName()}".`, job, [oldTunnel]);
166 * ```
167 */
168 Antfarm.prototype.log = function (type, message, actor, instances) {
169 if (instances === void 0) { instances = []; }
170 var af = this;
171 af.e.log(type, message, actor, instances);
172 };
173 return Antfarm;
174}());
175exports.Antfarm = Antfarm;
176module.exports = Antfarm;