@@ -5,26 +5,26 @@ const fs = require('fs');
55const { promisify } = require ( 'util' ) ;
66const xml2js = require ( 'xml2js' ) ; // eslint-disable-line
77const ora = require ( 'ora' ) ; // eslint-disable-line
8+ const existingGlobals = require ( '../src/globals.json' ) ;
89
910const parseString = promisify ( xml2js . parseString ) ;
1011const readFile = promisify ( fs . readFile ) ;
1112const writeFile = promisify ( fs . writeFile ) ;
1213
13- const reduceToName = ( acc , def ) => {
14- const { name } = def . $ ;
15- if ( name !== 'global' ) return Object . assign ( { } , acc , { [ name ] : false } ) ;
14+ const reduceToName = arr =>
15+ arr . reduce ( ( acc , def ) => {
16+ const { name } = def . $ ;
17+ if ( name !== 'global' ) return { ...acc , [ name ] : false } ;
1618
17- const properties = def . elements [ 0 ] . property . reduce ( reduceToName , { } ) ;
18- const methods = def . elements [ 0 ] . method . reduce ( reduceToName , { } ) ;
19- return Object . assign ( { } , acc , properties , methods ) ;
20- } ;
21-
22- const reduceClassDef = arr => arr . reduce ( reduceToName , { } ) ;
19+ const properties = reduceToName ( def . elements [ 0 ] . property ) ;
20+ const methods = reduceToName ( def . elements [ 0 ] . method ) ;
21+ return { ...acc , ...properties , ...methods } ;
22+ } , { } ) ;
2323
2424const sortObj = obj =>
2525 Object . keys ( obj )
2626 . sort ( ( a , b ) => a . toLowerCase ( ) . localeCompare ( b . toLowerCase ( ) ) )
27- . reduce ( ( acc , key ) => Object . assign ( { } , acc , { [ key ] : obj [ key ] } ) , { } ) ;
27+ . reduce ( ( acc , key ) => ( { ... acc , [ key ] : obj [ key ] } ) , { } ) ;
2828
2929const staticJsGlobals = {
3030 // Fetched from $.global in Extendscript Tools and filtered
@@ -54,50 +54,65 @@ async function main() {
5454 const spinner = ora ( 'Generating data from xml' ) . start ( ) ;
5555
5656 try {
57- const data = {
58- base :
59- '/Library/Application Support/Adobe/Scripting Dictionaries CC/CommonFiles/javascript.xml' ,
60- indesign :
61- '/Users/adam/Library/Preferences/ExtendScript Toolkit/4.0/omv$indesign-13.064$13.0.xml' ,
62- illustrator :
63- '/Library/Application Support/Adobe/Scripting Dictionaries CC/Illustrator 2018/omv.xml' ,
64- photoshop :
65- '/Library/Application Support/Adobe/Scripting Dictionaries CC/photoshop/omv.xml' ,
66- scriptui :
67- '/Library/Application Support/Adobe/Scripting Dictionaries CC/CommonFiles/scriptui.xml' ,
68- } ;
69-
70- const xml = await Promise . all (
71- Object . keys ( data ) . map ( async key => {
72- const p = data [ key ] ;
73- const xmlString = await readFile ( p , 'utf8' ) ;
74- return [ key , xmlString ] ;
75- } ) ,
57+ const scriptingDir = path . join (
58+ '/Library' ,
59+ 'Application Support' ,
60+ 'Adobe' ,
61+ 'Scripting Dictionaries CC' ,
7662 ) ;
7763
64+ const paths = {
65+ base : path . join ( scriptingDir , 'CommonFiles' , 'javascript.xml' ) ,
66+ indesign : path . join (
67+ process . env . HOME ,
68+ '/Library' ,
69+ 'Preferences' ,
70+ 'ExtendScript Toolkit' ,
71+ '4.0' ,
72+ 'omv$indesign-13.064$13.0.xml' ,
73+ ) ,
74+ illustrator : path . join ( scriptingDir , 'Illustrator 2018' , 'omv.xml' ) ,
75+ photoshop : path . join ( scriptingDir , 'photoshop' , 'omv.xml' ) ,
76+ scriptui : path . join ( scriptingDir , 'CommonFiles' , 'scriptui.xml' ) ,
77+ } ;
78+
7879 const parsedXml = await Promise . all (
79- xml . map ( async ( [ key , xmlString ] ) => {
80- const parsed = await parseString ( xmlString ) ;
81- return [ key , parsed ] ;
80+ Object . keys ( paths ) . map ( async key => {
81+ try {
82+ const p = paths [ key ] ;
83+ const xml = await readFile ( p , 'utf8' ) ;
84+ const parsed = await parseString ( xml ) ;
85+ return [ key , parsed ] ;
86+ } catch ( err ) {
87+ return null ;
88+ }
8289 } ) ,
8390 ) ;
8491
85- const globals = parsedXml . reduce ( ( acc , [ key , x ] ) => {
92+ const globals = parsedXml . filter ( Boolean ) . reduce ( ( acc , [ key , x ] ) => {
8693 const { classdef } = x . dictionary . package [ 0 ] ;
87- const keys = reduceClassDef ( classdef ) ;
88-
89- if ( key === 'javascript' ) {
90- return Object . assign ( { } , acc , {
91- [ key ] : sortObj ( Object . assign ( { } , keys , staticJsGlobals ) ) ,
92- } ) ;
94+ const keys = reduceToName ( classdef ) ;
95+
96+ if ( key === 'base' ) {
97+ return {
98+ ...acc ,
99+ [ key ] : sortObj ( {
100+ ...existingGlobals [ key ] ,
101+ ...keys ,
102+ ...staticJsGlobals ,
103+ } ) ,
104+ } ;
93105 }
94106
95- return Object . assign ( { } , acc , { [ key ] : sortObj ( keys ) } ) ;
107+ return {
108+ ...acc ,
109+ [ key ] : sortObj ( { ...existingGlobals [ key ] , ...keys } ) ,
110+ } ;
96111 } , { } ) ;
97112
98113 await writeFile (
99- path . join ( __dirname , 'test .json' ) ,
100- JSON . stringify ( globals , null , 2 ) ,
114+ path . join ( __dirname , '..' , 'src' , 'globals .json') ,
115+ JSON . stringify ( { ... existingGlobals , ... globals } , null , 2 ) ,
101116 'utf8' ,
102117 ) ;
103118
0 commit comments