-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
333 lines (281 loc) · 12.1 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
/*
* 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";
var utils = require("./lib/utils");
var PLUGIN_KEY_PREFIX = "PLUGIN-";
// 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);
}
require("./lib/stdlog").setup({
vendor: "Adobe",
application: "Adobe Photoshop CC",
module: "Generator"
});
var util = require("util"),
config = require("./lib/config").getConfig(),
generator = require("./lib/generator"),
Q = require("q"),
optimist = require("optimist");
var optionParser = optimist["default"]({
"r" : "independent",
"m" : null,
"p" : 49494,
"h" : "localhost",
"P" : "password",
"i" : null,
"o" : null,
"f" : null,
});
var argv = optionParser
.usage("Run generator service.\nUsage: $0")
.describe({
"r": "launch reason, one of: independent, menu, metadata, alwayson",
"m": "menu ID of action that should be executed immediately after startup",
"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)",
"help": "display help message"
}).alias({
"r": "launchreason",
"m": "menu",
"p": "port",
"h": "host",
"P": "password",
"i": "input",
"o": "output",
"f": "pluginfolder",
}).argv;
if (argv.help) {
console.log(optimist.help());
process.exit(0);
}
function stop(exitCode, reason) {
if (!reason) {
reason = "no reason given";
}
console.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) {
console.warn("Potential problem with plugin at '" + absolutePath +
"': " + compatibility.message);
}
} catch (metadataLoadError) {
// Do nothing
}
return result;
}
// relative paths are resolved relative to the current working directory
var resolve = require("path").resolve,
fs = require("fs"),
absolutePath = resolve(process.cwd(), directory),
plugins = [],
potentialPlugin = null;
if (!fs.statSync(absolutePath).isDirectory()) {
console.error("Error: specified plugin path '%s' is not a directory", absolutePath);
return plugins;
}
console.log("Scanning for plugins in", absolutePath);
// 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 fs.statSync(absoluteChildPath).isDirectory();
})
.forEach(function (absolutePluginPath) {
potentialPlugin = verifyPluginAtPath(absolutePluginPath);
if (potentialPlugin) {
plugins.push(potentialPlugin);
}
});
}
return plugins;
}
if (!util.isArray(folders)) {
folders = [folders];
}
folders.forEach(function (f) {
try {
var currentPluginCount = allPlugins.length;
allPlugins = allPlugins.concat(listPluginsInDirectory(f));
if (currentPluginCount === allPlugins.length) {
// No plugins found in this directory
console.warn("No viable plugins were found in '" + f + "'");
}
} catch (e) {
console.error("Error processing plugin directory %s\n", f, e);
}
});
return allPlugins;
}
function setupGenerator() {
var deferred = Q.defer();
var theGenerator = generator.createGenerator();
// 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 () {
console.log("Exiting");
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.host = argv.host;
options.password = argv.password;
}
options.config = config;
theGenerator.start(options).done(
function () {
console.log("[init] Generator started!");
var semver = require("semver"),
totalPluginCount = 0,
pluginMap = {},
plugins = scanPluginDirectories(argv.pluginfolder, theGenerator);
// Ensure all plugins have a valid semver, then put them in to a map
// keyed on plugin name
plugins.forEach(function (p) {
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);
});
console.log("Processing plugin set: " + JSON.stringify(pluginSet));
for (i = 0; i < pluginSet.length; i++) {
try {
theGenerator.loadPlugin(pluginSet[i].path);
loaded = true;
} catch (loadingException) {
console.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() {
var os = require("os"),
versions = require("./lib/versions");
versions.logPackageInformation("[init]", __dirname);
versions.logGitInformation("[init]", __dirname);
// Record command line arguments
console.log("[init] Node.js version: %j", process.versions);
console.log("[init] OS: %s %s (%s), platform: %s", os.type(), os.release(), os.arch(), process.platform);
console.log("[init] unparsed command line: %j", process.argv);
console.log("[init] parsed command line: %j", argv);
// Start async process to initialize generator
setupGenerator().done(
function () {
console.log("Generator initialized");
},
function (err) {
stop(-3, "generator failed to initialize: " + err);
}
);
}
process.on("uncaughtException", function (err) {
if (err) {
if (err.stack) {
console.error(err.stack);
} else {
console.error(err);
}
}
stop(-1, "uncaught exception" + (err ? (": " + err.message) : "undefined"));
});
init();
}());