-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathapp.js
386 lines (333 loc) · 14.7 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
(function () {
"use strict";
// Override the node module path to disallow loading packages (via require)
// that are outside of the generator and plugin folders.
global.whitelistedModuleFolders = [__dirname];
var Module = require("module").Module;
var nodeModulePaths = Module._nodeModulePaths.bind(Module);
Module._nodeModulePaths = function (from) {
return nodeModulePaths(from).filter(function (path) {
for (var i = 0; i < global.whitelistedModuleFolders.length; i++) {
var p = global.whitelistedModuleFolders[i];
if (path.substring(0, p.length) === p) {
return path;
}
}
});
};
var fs = require("fs"),
resolve = require("path").resolve,
util = require("util"),
optimist = require("optimist"),
Q = require("q"),
config = require("./lib/config").getConfig(),
generator = require("./lib/generator"),
stdlog = require("./lib/stdlog"),
logging = require("./lib/logging"),
utils = require("./lib/utils");
var PLUGIN_KEY_PREFIX = "PLUGIN-";
var _loggerManager = new logging.LoggerManager(logging.LOG_LEVEL_INFO),
_logStream = new logging.StreamFormatter(_loggerManager),
_logger = _loggerManager.createLogger("app");
// For backwards compatibility, set the logStream on the generator object
// even though I believe the only place this was previously referenced was in this file
generator.logStream = _logStream;
// On Windows, the bullet character is sometimes replaced with the bell character BEL (0x07).
// This causes Windows to make a beeping noise every time • is printed to the console.
// Use · instead. This needs to happen before adding stdlog to not affect the log files.
if (process.platform === "win32") {
utils.filterWriteStream(process.stdout, utils.replaceBullet);
utils.filterWriteStream(process.stderr, utils.replaceBullet);
}
var optionParser = optimist["default"]({
"p" : 49494,
"h" : "127.0.0.1",
"P" : "password",
"i" : null,
"o" : null,
"f" : null,
"photoshopVersion": null,
"photoshopPath": null,
"photoshopBinaryPath": null,
"photoshopLogPath": null,
"whiteListedPlugins": null
});
var argv = optionParser
.usage("Run generator service.\nUsage: $0")
.describe({
"p": "Photoshop server port",
"h": "Photoshop server host",
"P": "Photoshop server password",
"i": "file descriptor of input pipe",
"o": "file descriptor of output pipe",
"f": "folder to search for plugins (can be used multiple times)",
"v": "include verbose generator logging in stdout",
"photoshopVersion": "tell Generator PS's version so it isn't queried at startup (optional)",
"photoshopPath": "tell Generator PS's path so it isn't queried at startup (optional)",
"photoshopBinaryPath": "tell Generator PS's binary location so it isn't queried at startup (optional)",
"photoshopLogPath": "log root directory, required for generator built in mode",
"whiteListedPlugins": "A comma seperated list of plugin names that are ok to run (optional)",
"help": "display help message"
}).alias({
"p": "port",
"h": "host",
"P": "password",
"i": "input",
"o": "output",
"f": "pluginfolder",
"v": "verbose"
}).string("photoshopVersion")
.argv;
if (argv.help) {
console.log(optimist.help());
process.exit(0);
}
// Warn when photoshopLogPath not supplied.
// This is probably OK, and only applicable to "remote" generator development
if (!argv.photoshopLogPath) {
console.log(
"Generator did not receive a log location via 'photoshopLogPath' param; may use a non-standard location");
}
var logSettings = {
vendor: "Adobe",
application: "Adobe Photoshop Generator", // for fall-back log location when photoshopLogPath not given
logRoot: argv.photoshopLogPath,
module: "Generator",
verbose: argv.verbose
};
// Initialize log file writer
// This will tap stdout and read generator-logger's readable stream
stdlog.setup(logSettings, _logStream);
function stop(exitCode, reason) {
if (!reason) {
reason = "no reason given";
}
_logger.error("Exiting with code " + exitCode + ": " + reason);
process.exit(exitCode);
}
function scanPluginDirectories(folders, theGenerator) {
var allPlugins = [];
function listPluginsInDirectory(directory) {
function verifyPluginAtPath(absolutePath) {
var result = null,
metadata = null,
compatibility = null;
try {
metadata = theGenerator.getPluginMetadata(absolutePath);
compatibility = theGenerator.checkPluginCompatibility(metadata);
if (compatibility.compatible) {
result = {
path: absolutePath,
metadata: metadata
};
}
if (compatibility.message) {
_logger.warn("Potential problem with plugin at '" + absolutePath +
"': " + compatibility.message);
}
} catch (metadataLoadError) {
// Do nothing
}
return result;
}
function checkIfPathDirectory(absolutePath) {
var result = false;
try {
result = fs.statSync(absolutePath).isDirectory();
} catch (err) {
_logger.error(err);
}
return result;
}
// relative paths are resolved relative to the current working directory
var absolutePath = resolve(process.cwd(), directory),
plugins = [],
potentialPlugin = null;
if (!checkIfPathDirectory(absolutePath)) {
_logger.error("Error: specified plugin path '%s' is not a directory", absolutePath);
return plugins;
}
// First, try treating the directory as a plugin
potentialPlugin = verifyPluginAtPath(absolutePath);
if (potentialPlugin) {
plugins.push(potentialPlugin);
}
// If we didn't find a compatible plugin at the root level,
// then scan one level deep for plugins
if (plugins.length === 0) {
fs.readdirSync(absolutePath)
.map(function (child) {
return resolve(absolutePath, child);
})
.filter(function (absoluteChildPath) {
return checkIfPathDirectory(absoluteChildPath);
})
.forEach(function (absolutePluginPath) {
potentialPlugin = verifyPluginAtPath(absolutePluginPath);
if (potentialPlugin) {
plugins.push(potentialPlugin);
}
});
}
return plugins;
}
if (!util.isArray(folders)) {
folders = [folders];
}
folders.forEach(function (f) {
global.whitelistedModuleFolders.push(f);
try {
var currentPluginCount = allPlugins.length;
allPlugins = allPlugins.concat(listPluginsInDirectory(f));
if (currentPluginCount === allPlugins.length) {
// No plugins found in this directory
_logger.warn("No viable plugins were found in '" + f + "'");
}
} catch (e) {
_logger.error("Error processing plugin directory %s\n", f, e);
}
});
return allPlugins;
}
function setupGenerator() {
var deferred = Q.defer();
var theGenerator = generator.createGenerator(_loggerManager);
// NOTE: It *should* be the case that node automatically cleans up all pipes/sockets
// on exit. However, on node v0.10.15 mac 64-bit there seems to be a bug where
// the native-side process exit hangs if node is blocked on the read of a pipe.
// This meant that if Generator had an unhandled exception after starting to read
// from PS's pipe, the node process wouldn't fully exit until PS closed the pipe.
process.on("exit", function () {
if (theGenerator) {
theGenerator.shutdown();
}
});
theGenerator.on("close", function () {
setTimeout(function () {
stop(0, "Generator close event");
}, 1000);
});
var options = {};
if ((typeof argv.input === "number" && typeof argv.output === "number") ||
(typeof argv.input === "string" && typeof argv.output === "string")) {
options.inputFd = argv.input;
options.outputFd = argv.output;
options.password = null; // No encryption over pipes
} else if (typeof argv.port === "number" && argv.host && argv.password) {
options.port = argv.port;
options.hostname = argv.host;
options.password = argv.password;
}
if (argv.photoshopVersion && typeof argv.photoshopVersion === "string") {
options.photoshopVersion = argv.photoshopVersion;
}
if (argv.photoshopPath && typeof argv.photoshopPath === "string") {
options.photoshopPath = argv.photoshopPath;
}
if (argv.photoshopBinaryPath && typeof argv.photoshopBinaryPath === "string") {
options.photoshopBinaryPath = argv.photoshopBinaryPath;
}
options.config = config;
theGenerator.start(options).done(
function () {
var semver = require("semver"),
totalPluginCount = 0,
pluginMap = {},
plugins = scanPluginDirectories(argv.pluginfolder, theGenerator),
shouldWhiteList = typeof argv.whiteListedPlugins === "string",
whiteListedPlugins;
if (shouldWhiteList) {
whiteListedPlugins = argv.whiteListedPlugins.split(",").map(function (s) { return s.trim(); });
}
// Ensure all plugins have a valid semver, then put them in to a map
// keyed on plugin name
plugins.forEach(function (p) {
if (shouldWhiteList && whiteListedPlugins.indexOf(p.metadata.name) === -1) {
//if we are in "whitelist mode" skip any plugins that are not in the array
return;
}
if (!semver.valid(p.metadata.version)) {
p.metadata.version = "0.0.0";
}
if (!pluginMap[PLUGIN_KEY_PREFIX + p.metadata.name]) {
pluginMap[PLUGIN_KEY_PREFIX + p.metadata.name] = [];
}
pluginMap[PLUGIN_KEY_PREFIX + p.metadata.name].push(p);
});
// For each unique plugin name, try to load a plugin with that name
// in decending order of version
Object.keys(pluginMap).forEach(function (pluginSetKey) {
var pluginSet = pluginMap[pluginSetKey],
i,
loaded = false;
pluginSet.sort(function (a, b) {
return semver.rcompare(a.metadata.version, b.metadata.version);
});
for (i = 0; i < pluginSet.length; i++) {
try {
theGenerator.loadPlugin(pluginSet[i].path);
loaded = true;
} catch (loadingException) {
_logger.error("Unable to load plugin at '" + pluginSet[i].path + "': " +
loadingException.message);
}
if (loaded) {
totalPluginCount++;
break;
}
}
});
if (totalPluginCount === 0) {
// Without any plugins, Generator will never do anything. So, we exit.
deferred.reject("Generator requires at least one plugin to function, zero were loaded.");
} else {
deferred.resolve(theGenerator);
}
},
function (err) {
deferred.reject(err);
}
);
return deferred.promise;
}
function init() {
// Start async process to initialize generator
setupGenerator().fail(
function (err) {
stop(-3, "Generator failed to initialize: " + err);
});
}
process.on("uncaughtException", function (err) {
if (err) {
if (err.stack) {
_logger.error(err.stack);
} else {
_logger.error(err);
}
}
stop(-1, "Uncaught exception" + (err ? (": " + err.message) : "undefined"));
});
init();
}());