Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add Multiple type of token in same file #965

Open
Vanals opened this issue Apr 19, 2023 · 1 comment
Open

Can't add Multiple type of token in same file #965

Vanals opened this issue Apr 19, 2023 · 1 comment

Comments

@Vanals
Copy link

Vanals commented Apr 19, 2023

Hi,

I am trying to add multiple types of tokens in the same file and ideally be able to specify formats.

Something like:

...
platforms: {
      swiftUI: {
        buildPath: `build/swiftUI/${brand}/`,
        transforms: ['color/UIColorSwift', 'colorUIColor'],
        files: [
          {
            destination: `fileName.swift`,
            format: 'ios-swift/enum.swift',
            className: 'colors',
            filter: {
              type: 'colors',
            },
          },
          {
            destination: `fileName.swift,
            format: 'ios-swift/enum.swift',
            className: 'FontSizes',
            filter: {
              type: 'fontSizes',
            },
          },
        ],
      },
    },
    ...
    
The `expectation` is that fileName.swift would contain

   import UIKit

public enum colors {
   public static let Apple = UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.0)
   public static let Facebook = UIColor(red: 0.094, green: 0.467, blue: 0.949, alpha: 1.0)
   public static let Github = UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.0)
   public static let GoogleBlue = UIColor(red: 0.259, green: 0.522, blue: 0.957, alpha: 1.0)
   public static let GoogleGreen = UIColor(red: 0.059, green: 0.616, blue: 0.345, alpha: 1.0)
   public static let GoogleRed = UIColor(red: 0.859, green: 0.267, blue: 0.216, alpha: 1.0)
   public static let GoogleYellow = UIColor(red: 0.957, green: 0.706, blue: 0.000, alpha: 1.0)
   }
   
public enum FontSizes {
   public static let fontSize140 = 56
}

But the outcome is

//
// NK-Dark.swift
//

// Do not edit directly
// Generated on Wed, 19 Apr 2023 09:27:35 GMT


import UIKit

public enum FontSizes {
    public static let fontSize140 = 56
}

So only the latest is in the file, which I believes overrides the first. Is there any chance I set up the config so both are kept in the same file?

Thanks

@chazzmoney
Copy link
Collaborator

chazzmoney commented Apr 25, 2023

So, there are three options - none of which are great.

  1. You could generate the entire dictionary instead of sectioned enums.

  2. You could output two distinct files and then configure a custom action to merge those files together after generation.

  3. You could create a custom format to accept an array of multiple enums and accept an array for classname and filters for the properties. The template which generates these (and would be a good places to start) is here:

https://github.com/amzn/style-dictionary/blob/aba09b503d8513e43d35e5b7b06f938a0fefef2f/lib/common/templates/ios-swift/any.swift.template

There will also be some additional work in terms of passing those array through these two functions:

'ios-swift/enum.swift': function({dictionary, options, file, platform}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios-swift/any.swift.template')
);
let allTokens;
const { outputReferences } = options;
options = setSwiftFileProperties(options, 'enum', platform.transformGroup);
const formatProperty = createPropertyFormatter({
outputReferences,
dictionary,
formatting: {
suffix: ''
}
});
if (outputReferences) {
allTokens = [...dictionary.allTokens].sort(sortByReference(dictionary));
} else {
allTokens = [...dictionary.allTokens].sort(sortByName);
}
return template({allTokens, file, options, formatProperty, fileHeader});
},

function setSwiftFileProperties(options, objectType, transformGroup) {
if (typeof options.objectType === 'undefined') {
if (typeof objectType === 'undefined') {
options.objectType = 'class';
} else {
options.objectType = objectType;
}
}
if (typeof options.import === 'undefined') {
if (typeof transformGroup === 'undefined') {
options.import = ['UIKit'];
} else if (['ios-swift', 'ios-swift-separate'].includes(transformGroup)) {
options.import = ['UIKit'];
} else {
// future swift-ui transformGroup to be added here
options.import = ['SwiftUI'];
}
} else if (typeof options.import === 'string') {
options.import = [options.import];
}
if (typeof options.accessControl === 'undefined') {
options.accessControl = 'public ';
} else {
if (options.accessControl !== "") {
options.accessControl = `${options.accessControl} `;
}
}
return options
}

I know none of these are great options, but those are the best ways to achieve what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants