-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
332 lines (312 loc) · 8.89 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
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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const shell = require("shelljs");
const chalk = require("chalk");
const inquirer = require("inquirer");
const ora = require("ora");
shell.config.silent = true;
let ORA_SPINNER = {
interval: 80,
frames: [
" ⠋",
" ⠙",
" ⠚",
" ⠞",
" ⠖",
" ⠦",
" ⠴",
" ⠲",
" ⠳",
" ⠓"
]
};
let cwd = path.resolve("./");
let taskHotkeys = /win|linux/i.test(process.platform)
? "CONTROL + SHIFT + B"
: "COMMAND + SHIFT + B";
const config = {
alwaysAsk: false,
useLatest: true,
quiet: false,
here: false,
needsInstall: false,
fileName: "host",
customName: false
};
const apps = {
Illustrator: {
name: "Illustrator",
aliases: ["ilst", "ai", "illustrator"],
versions: ["2015.3", "2017", "2018", "2019", "2020"]
},
AfterEffects: {
name: "AfterEffects",
aliases: ["aeft", "ae", "after effects", "afterfx"],
versions: ["2018"]
},
Photoshop: {
name: "Photoshop",
aliases: ["phxs", "ps", "photoshop"],
versions: ["2015.3"]
},
InDesign: {
name: "InDesign",
aliases: ["idsn", "id", "indesign", "in design"],
versions: ["2015.3", "2018", "2019"]
},
Premiere: {
name: "Premiere",
aliases: ["ppro", "pp", "premiere", "premiere pro"],
versions: ["2018", "2019"]
},
Audition: {
name: "Audition",
aliases: ["audt", "au", "audition"],
versions: ["2015.2", "2017", "2018"]
}
};
let tsconfig = {
compileOnSave: true,
compilerOptions: {
outFile: "./host.jsx",
allowJs: true,
noLib: true,
types: []
},
files: ["host.ts"],
exclude: ["node_modules"]
};
async function init() {
let app,
args = process.argv;
checkForConfigArgs(args);
if (args.length) {
let fileCheck = args.filter((arg, i) => {
return i > 1 && /\w*\.(t|j)s(x?)$/.test(arg);
});
if (fileCheck.length)
config.fileName = fileCheck[0].replace(/\.(t|j)s(x?)/, "");
}
if (args.includes("help")) showHelp();
else {
let targPath = !config.here ? await findNearestParentPackage(cwd) : cwd;
config.needsInstall = await checkPackageForInstall(targPath);
if (args.length > 2) app = await findApp(args);
app = app ? app : await inquireAboutApp();
generateTSConfig(app, targPath);
writeDefaultFiles();
if (config.needsInstall) installTypes(targPath, app);
else showEndMessage(targPath, app);
}
}
async function inquireAboutApp() {
console.log("");
console.log(`Welcome! Let's get started.`);
console.log("");
let answers = await inquirer.prompt([
{
type: "list",
name: "app",
message: `Which app are you scripting for?`,
choices: Object.keys(apps).map(item => {
return {
name: item !== "AfterEffects" ? item : "After Effects",
value: item
};
})
},
{
type: "input",
name: "name",
message: `What should the name of the script file be?`
}
]);
config.fileName = answers.name.replace(" ", "-").replace(/\.(j|t)s(x?)/, "");
return apps[answers.app];
}
async function findApp(args) {
let result,
valids = args.filter(arg => {
return /^[a-z\s]*$/i.test(arg);
});
Object.keys(apps).forEach(app => {
let rx = new RegExp(apps[app].aliases.join("|"), "i");
valids.forEach(valid => {
if (rx.test(valid)) result = apps[app];
});
});
return result ? result : await inquireAboutApp();
}
function showHelp() {
console.log("");
console.log("Welcome!");
console.log("");
console.log(
`This package is best used when installed globally, but relies on a local package.json to install type definitions. If you haven't already, you should run ${chalk.yellow(
"npm init -y"
)} somewhere in your project.`
);
console.log("");
console.log(
`You can then run ${chalk.yellow(
"scriptopia"
)} at any time to get a prompt about the app and name of your resulting file.`
);
console.log("");
console.log(
`If you want to skip the prompts, you can optionally add the app name or file name after the command, like so:`
);
console.log("");
console.log(` - ${chalk.yellow("scriptopia ai test.jsx")}`);
console.log(` - ${chalk.yellow("scriptopia ilst myscript.ts")}`);
console.log(` - ${chalk.yellow("scriptopia illustrator somescript.js")}`);
console.log("");
console.log(
`You can see more details at ${chalk.blue(
`https://github.com/Inventsable/scriptopia`
)}`
);
console.log("");
}
function showEndMessage(thispath, app) {
let trail = differenceFromRoot(thispath);
let root = thispath.match(/[^\\|\/]*$/)[0];
let relpath = trail.travelDown.length ? `${root}/${trail.travelDown}` : ``;
relpath = relpath.replace(/\\/gm, "/");
let lengthcheck = relpath.split(/\\|\//);
if (lengthcheck.length == 2) relpath = relpath.match(/[^\\|\/]*$/)[0];
console.log("");
console.log(`✔ We're all done!`);
console.log("");
console.log(
`Now hit ${chalk.yellow(`${taskHotkeys}`)} and select ${chalk.bgBlue.black(
` tsc: watch - ${relpath.length ? relpath + "/" : ""}tsconfig.json `
)}`
);
console.log("");
console.log(
`You should now use ${chalk.green(
`${config.fileName}.ts`
)} to write code, but ${chalk.green(`${config.fileName}.jsx`)} to run in ${
app.name
}.`
);
console.log("");
}
function writeDefaultFiles() {
fs.writeFileSync(
`${cwd}/${config.fileName.replace(/\..*/, "")}.ts`,
`// Welcome to the world of Typescript!
// Be sure to run 'tsc: watch - [path to tsconfig]', then try writing 'app.' below:`
);
}
async function readDir(thispath) {
return new Promise((resolve, reject) => {
fs.readdir(path.resolve(thispath), { encoding: "utf-8" }, (err, files) => {
if (err) reject(err);
resolve(files);
});
});
}
function generateTSConfig(app, thispath) {
let version = config.useLatest
? app.versions[app.versions.length - 1]
: app.versions[0];
tsconfig.compilerOptions.outFile = `./${config.fileName}.jsx`;
tsconfig.compilerOptions.types = [`types-for-adobe/${app.name}/${version}`];
tsconfig.files = [`${config.fileName}.ts`];
fs.writeFileSync(
path.resolve(`${cwd}/tsconfig.json`),
JSON.stringify(tsconfig)
);
}
// Redundant, not using enough config args to justify this.
function checkForConfigArgs(args) {
if (!args.length) {
let valids = args.filter(arg => {
return /^\-[a-z]*$/i.test(arg);
});
if (!valids.length) return null;
let possibles = {
alwaysAsk: ["ask", "a"],
useLatest: ["latest", "l"],
quiet: ["quiet", "q"],
here: ["here", "h"]
};
valids.forEach(valid => {
Object.keys(possibles).forEach(key => {
possibles[key].forEach(term => {
config[key] = !config[key]
? new RegExp(`^\-${term}$`, "i").test(valid)
: true;
});
});
});
}
}
// Redundant, not needed
async function findNearestParentPackage(thispath) {
let children = await readDir(thispath);
if (!children.length || !children.includes("package.json"))
return await findNearestParentPackage(thispath.replace(/\/|\\\w*$/, ""));
else if (children.includes("package.json")) return thispath;
}
// Redundant since npm install automatically handles parenting chains
async function installTypes(thispath, app) {
// let paths = differenceFromRoot(thispath);
// shell.cd(paths.travelUp);
let spinner = ora({
text: `Installing types to ${
thispath.match(/[^\\|\/]*$/)[0]
}... (This may take a while)`,
spinner: ORA_SPINNER
}).start();
shell.exec("npm install ten-A/types-for-adobe --save-dev", () => {
console.log("");
spinner.stopAndPersist({
symbol: "",
text: `${chalk.black.bgBlue(` ✔ SUCCESSFULLY INSTALLED `)}`
});
showEndMessage(thispath, app);
});
}
async function checkPackageForInstall(thispath) {
let files = await readDir(thispath);
let found = false;
if (files.length && files.includes("package.json")) {
let manifest = JSON.parse(
fs.readFileSync(path.resolve(`${thispath}/package.json`), {
encoding: "utf-8"
})
);
let keys = [];
if (manifest.dependencies)
keys = [].concat(keys, Object.keys(manifest.dependencies));
if (manifest.devDependencies)
keys = [].concat(keys, Object.keys(manifest.devDependencies));
if (!keys.length) return true;
keys.forEach(dep => {
if (dep == "types-for-adobe") found = true;
});
}
return !found;
}
// Redundant, npm install automatically handles parent chaining
function differenceFromRoot(thispath) {
let remainder = cwd.replace(thispath, "");
if (!remainder.length) return { travelUp: "", travelDown: "" };
else {
remainder = remainder.replace(/(^\\|\/)|(\\|\/)$/, "");
let depths = remainder.split(/\\|\//);
return {
travelUp: depths
.map(d => {
return "..";
})
.join("/"),
travelDown: remainder
};
}
}
init();