export const getSystemFontsLocation = ( ) : string | null => { const systemLibrary = NSFileManager . defaultManager ( ) . URLsForDirectory_inDomains ( NSLibraryDirectory , 8 ) [ 0 ] ; const systemLibraryPath = systemLibrary ? systemLibrary . absoluteString ( ) . replace ( 'file://' , '' ) : null ; const systemFonts = systemLibraryPath ? NSFileManager . defaultManager ( ) . fileExistsAtPath ( `${ systemLibraryPath }Fonts` ) : null ; if ( systemFonts ) { return `${ systemLibraryPath }Fonts/` ; } else { return null ; } } ; export const getUserFontsLocation = ( ) : string | null => { const userLibrary = NSFileManager . defaultManager ( ) . URLsForDirectory_inDomains ( NSLibraryDirectory , 1 ) [ 0 ] ; const userLibraryPath = userLibrary ? userLibrary . absoluteString ( ) . replace ( 'file://' , '' ) : null ; const userFonts = userLibraryPath ? NSFileManager . defaultManager ( ) . fileExistsAtPath ( `${ userLibraryPath }Fonts` ) : null ; if ( userFonts ) { return `${ userLibraryPath }Fonts/` ; } else { return null ; } } ; export const getSupplementalFontsLocation = ( ) : string | null => { const systemLibrary = NSFileManager . defaultManager ( ) . URLsForDirectory_inDomains ( NSLibraryDirectory , 8 ) [ 0 ] ; const systemLibraryPath = systemLibrary ? systemLibrary . absoluteString ( ) . replace ( 'file://' , '' ) : null ; const systemFonts = systemLibraryPath ? NSFileManager . defaultManager ( ) . fileExistsAtPath ( `${ systemLibraryPath }Fonts/Supplemental` ) : null ; if ( systemFonts ) { return `${ systemLibraryPath }Fonts/Supplemental/` ; } else { return null ; } } ; export const getUserFonts = ( ) : string [ ] | null => { const userFontsLoc = getUserFontsLocation ( ) ; if ( userFontsLoc ) { const userFonts = NSFileManager . defaultManager ( ) . contentsOfDirectoryAtPath_error ( userFontsLoc , nil ) ; return Array . from ( userFonts , ( item ) => { return String ( item ) ; } ) ; } else { return null ; } } ; export const getSystemFonts = ( ) : string [ ] | null => { const systemFontsLoc = getSystemFontsLocation ( ) ; if ( systemFontsLoc ) { const systemFonts = NSFileManager . defaultManager ( ) . contentsOfDirectoryAtPath_error ( systemFontsLoc , nil ) ; return Array . from ( systemFonts , ( item ) => { return String ( item ) ; } ) ; } else { return null ; } } ; export const getSupplimentalFonts = ( ) : string [ ] | null => { const supplementalFontsLoc = getSupplementalFontsLocation ( ) ; if ( supplementalFontsLoc ) { const supplementalFonts = NSFileManager . defaultManager ( ) . contentsOfDirectoryAtPath_error ( supplementalFontsLoc , nil ) ; return Array . from ( supplementalFonts , ( item ) => { return String ( item ) ; } ) ; } else { return null ; } } ; export const getAllFonts = ( ) : next . FontDir [ ] | null => { const userFontsLoc = getUserFontsLocation ( ) ; const systemFontsLoc = getSystemFontsLocation ( ) ; const supplementalFontsLoc = getSupplementalFontsLocation ( ) ; const userFonts = getUserFonts ( ) ; const systemFonts = getSystemFonts ( ) ; const supplementalFonts = getSupplimentalFonts ( ) ; const fontLocations = [ userFontsLoc , systemFontsLoc , supplementalFontsLoc ] ; const fontLocationContents = [ userFonts , systemFonts , supplementalFonts ] ; const availableFontLocations = fontLocations . filter ( ( fontDir ) => fontDir !== null ) ; if ( availableFontLocations . length > 0 ) { const allFonts = ( availableFontLocations as string [ ] ) . map ( ( fontLocation , index ) => { return { location : fontLocation , contents : fontLocationContents [ index ] } ; } ) ; return allFonts as next . FontDir [ ] ; } else { return null ; } } ; export const getRoot = ( context ) => { return context . scriptPath . stringByDeletingLastPathComponent ( ) . stringByDeletingLastPathComponent ( ) . stringByDeletingLastPathComponent ( ) ; } ; export const getFileContent = ( filePath ) => { return NSString . stringWithContentsOfFile_encoding_error ( filePath , 4 , nil ) ; } ; export const getSavePath = ( context ) => { let filePath = context . document . fileURL ( ) ? context . document . fileURL ( ) . path ( ) . stringByDeletingLastPathComponent ( ) : "~" ; let fileName = context . document . displayName ( ) . stringByDeletingPathExtension ( ) ; let savePanel = NSSavePanel . savePanel ( ) ; savePanel . setTitle ( "Export spec" ) ; savePanel . setNameFieldLabel ( "Export to:" ) ; savePanel . setPrompt ( "Export" ) ; savePanel . setCanCreateDirectories ( true ) ; savePanel . setNameFieldStringValue ( fileName ) ; if ( savePanel . runModal ( ) != NSOKButton ) { return false ; } return savePanel . URL ( ) . path ( ) ; } ; export const writeFile = ( options ) => { let content = NSString . stringWithString ( options . content ) ; let savePathName = [ ] ; NSFileManager . defaultManager ( ) . createDirectoryAtPath_withIntermediateDirectories_attributes_error ( options . path , true , nil , nil ) ; savePathName . push ( options . path , "/" , options . fileName ) ; savePathName = savePathName . join ( "" ) ; content . writeToFile_atomically_encoding_error ( savePathName , false , 4 , null ) ; } ; export const moveImages = ( images , savePath ) => { const imagesPath = `${ savePath }/images` ; NSFileManager . defaultManager ( ) . createDirectoryAtPath_attributes ( imagesPath , nil ) ; images . forEach ( ( image ) => { NSFileManager . defaultManager ( ) . moveItemAtPath_toPath_error ( image . src [ `1x` ] , `${ imagesPath }/${ image . id }.png` , nil ) ; NSFileManager . defaultManager ( ) . moveItemAtPath_toPath_error ( image . src [ `2x` ] , `${ imagesPath }/${ image . id }@2x.png` , nil ) ; } ) ; } ; export const moveFonts = ( fonts , savePath ) => { const fontsPath = `${ savePath }/fonts` ; NSFileManager . defaultManager ( ) . createDirectoryAtPath_attributes ( fontsPath , nil ) ; Object . keys ( fonts ) . forEach ( ( fontFamily ) => { const fontFiles = fonts [ `${ fontFamily }` ] ; const fontFamilyDir = `${ fontsPath }/${ fontFamily }` ; NSFileManager . defaultManager ( ) . createDirectoryAtPath_attributes ( fontFamilyDir , nil ) ; fontFiles . forEach ( ( fwp ) => { NSFileManager . defaultManager ( ) . moveItemAtPath_toPath_error ( fwp . path , `${ fontFamilyDir }/${ fwp . fileName }` , nil ) ; } ) ; } ) ; } ; export const moveSVGs = ( svgs , savePath ) => { const svgsPath = `${ savePath }/svgs` ; NSFileManager . defaultManager ( ) . createDirectoryAtPath_attributes ( svgsPath , nil ) ; svgs . forEach ( ( svg ) => { NSFileManager . defaultManager ( ) . moveItemAtPath_toPath_error ( svg . src , `${ svgsPath }/${ svg . id }.svg` , nil ) ; } ) ; } ; export const embedFont = ( fontFamily , fontFiles ) : string => { return fontFiles . reduce ( ( result , current ) => { const fontFace = `
      @font-face {
        font-family: '${ fontFamily }';
        font-weight: ${ current . font . weight };
        font-stretch: ${ current . font . stretch };
        font-style: ${ current . font . style };
        src: url("./fonts/${ fontFamily }/${ current . fileName }");
      }
    ` ; result = `${ result } ${ fontFace }` ; return result ; } , '' ) ; } ; export const embedFonts = ( fonts , styleContent ) : string => { return Object . keys ( fonts ) . reduce ( ( result , current ) => { const fontFiles = fonts [ `${ current }` ] ; const fontFamilyEmbedString = embedFont ( current , fontFiles ) ; result = `${ result } ${ fontFamilyEmbedString }` ; return result ; } , styleContent ) ; } ; export const embedTempFont = ( fontFamily , fontFiles ) : string => { return fontFiles . reduce ( ( result , current ) => { const fontFace = `
      @font-face {
        font-family: '${ fontFamily }';
        font-weight: ${ current . font . weight };
        font-stretch: ${ current . font . stretch };
        font-style: ${ current . font . style };
        src: url("file:/${ current . path }");
      }
    ` ; result = `${ result } ${ fontFace }` ; return result ; } , '' ) ; } ; export const embedTempFonts = ( fonts , styleContent ) : void => { return Object . keys ( fonts ) . reduce ( ( result , current ) => { const fontFiles = fonts [ `${ current }` ] ; const fontFamilyEmbedString = embedTempFont ( current , fontFiles ) ; result = `${ result } ${ fontFamilyEmbedString }` ; return result ; } , styleContent ) ; } ; export const getFinalStore = ( store ) => { let finalStore = { ... store } ; finalStore . images = store . images . map ( ( image ) => { return { id : image . id , src : { [ `1x` ] : `images/${ image . id }.png` , [ `2x` ] : `images/${ image . id }@2x.png` } } ; } ) ; finalStore . svgs = store . svgs . map ( ( svg ) => { return { id : svg . id , src : `svgs/${ svg . id }.svg` } ; } ) ; return JSON . stringify ( finalStore ) ; } ;