-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.js
241 lines (183 loc) · 6.64 KB
/
index.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
'use strict';
/*******************************************************************/
// DEPENDENCIES
/*******************************************************************/
const os = require('os'),
fs = require('fs'),
path = require('path'),
is = require('is-explicit').default,
uuid = require('uuid'),
Command = require('./lib/command');
/*******************************************************************/
// ERRORS
/*******************************************************************/
const Errors = {
UnsupportedPlatform : 'Cannot run After Effects commands in an environment it can\'t be installed in.',
BadExecuteArgument : 'execute expects a function or AfterEffectsCommand instance.',
ApplicationNotFound : 'Cannot execute command, After Effects could not be found in your application directory. Install After Effects in your application directory, or provide a path in program option.',
NoResult : 'Could not get results from After Effects. Ensure that Preferences > General > Allow Scripts to Write Files and Access Network is enabled.'
};
class AfterEffectsError extends Error {
constructor(message) {
super(message);
this.name = 'AfterEffectsError';
}
}
/*******************************************************************/
// SETUP
/*******************************************************************/
const options = {
errorHandling: true,
minify: false,
program: null,
includes: [
path.join(__dirname, '/lib/includes/console.jsx'),
path.join(__dirname, '/lib/includes/es5-shim.jsx'),
path.join(__dirname, '/lib/includes/get.jsx')
]
};
const platform = (() => {
const platform_name = os.platform();
if (platform_name === 'darwin') //mac
return require('./lib/platform-mac');
else if (platform_name.includes('win')) { //windows 32 or 64
return require('./lib/platform-win');
} else
throw new Error(Errors.UnsupportedPlatform);
})();
/*******************************************************************/
// HELPER
/*******************************************************************/
function prepare_command(input_args) {
let command = null;
const args = Array.prototype.slice.call(input_args);
const funcOrCommand = args.shift();
if (is(funcOrCommand, Command))
command = funcOrCommand;
if (is(funcOrCommand, Function))
command = new Command(funcOrCommand);
if (!command)
throw new Error(Errors.BadExecuteArgument);
command.arguments = args;
command.result_file = null;
return command;
}
function ensure_executable(command) {
if (!platform.canExecute(command))
throw new Error(Errors.ApplicationNotFound);
}
function prepare_script_path(scriptPath, command) {
if (!path.isAbsolute(scriptPath))
scriptPath = path.resolve(platform.scriptsDir(command), scriptPath);
if (path.extname(scriptPath) === '')
scriptPath += '.jsx';
return scriptPath;
}
function create_result_file_name(command) {
command.result_file = `ae-result-${uuid.v4()}.js`;
}
function get_results(command) {
if (!is(command.result_file, String))
return;
let results = {};
try {
//For macs, the javascript function inside After Effects that points toward
//the operating systems temp folder is slightly different than os.tmpdir,
//having a 'TemporaryItems' subfolder.
const sub_temp_dir = os.platform() === 'darwin' ? 'TemporaryItems' : '';
const jsfile = path.join(os.tmpdir(), sub_temp_dir, command.result_file);
results = require(jsfile);
fs.unlink(jsfile, function(err) {
if (err)
console.error (err)
});
command.result_file = null;
} catch (err) {
command.result_file = null;
return err;
}
if (is(results.logs, Array))
results.logs.forEach(log => process.stdout.write(`${log}\n`));
return results;
}
/*******************************************************************/
// INTERFACE
/*******************************************************************/
function execute(/*args*/) {
const command = prepare_command(arguments);
ensure_executable(command);
create_result_file_name(command);
return platform.execute(command)
//Handle Results
.then(() => new Promise((resolve,reject) => {
const results = get_results(command);
if (results == null)
resolve();
if (is(results, Error))
reject(Errors.NoResult);
if (is(results.returned, Error))
reject(new AfterEffectsError(results.returned.message));
else
resolve(results.returned);
}));
}
function executeSync(/*args*/) {
const command = prepare_command(arguments);
ensure_executable(command);
create_result_file_name(command);
platform.executeSync(command);
const results = get_results(command);
//Handle results
if (results == null)
return;
if (is(results, Error))
throw new Error(Errors.NoResult);
if (is(results.returned, Error))
throw new AfterEffectsError(results.returned.message);
else
return results.returned;
}
function create(funcOrCommand, scriptPath) {
//prepare command args shouldn't include scriptPath
const args = Array.prototype.slice.call(arguments, 2);
args.unshift(funcOrCommand);
const command = prepare_command(args);
scriptPath = prepare_script_path(scriptPath, command);
return new Promise((resolve, reject) => {
fs.writeFile(scriptPath, command.toString(), 'utf-8', (err) => {
if (err)
reject(err);
else
process.stdout.write(`Script written to ${scriptPath}\n`);
resolve(scriptPath);
});
});
}
function createSync(funcOrCommand, scriptPath) {
//prepare command args shouldn't include scriptPath
const args = Array.prototype.slice.call(arguments, 2);
args.unshift(funcOrCommand);
const command = prepare_command(args);
scriptPath = prepare_script_path(scriptPath, command);
fs.writeFileSync(scriptPath, command.toString(), 'utf-8');
process.stdout.write(`Script written to ${scriptPath}\n`);
return scriptPath;
}
/*******************************************************************/
// EXPORTS
/*******************************************************************/
module.exports = function() {
return executeSync.apply(null, arguments);
};
module.exports.execute = execute;
module.exports.executeSync = executeSync;
module.exports.create = create;
module.exports.createSync = createSync;
module.exports.options = options;
module.exports.Command = Command;
Object.defineProperty(module.exports, 'scriptsDir', {
//Pass in dummy command so we have access to the currently set program option, if one exists
get: () => platform.scriptsDir({ options: { program: module.exports.options.program }})
});
Object.preventExtensions(module.exports);
Object.preventExtensions(module.exports.options);