-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathformats.js
1265 lines (1188 loc) · 38.5 KB
/
formats.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const _template = require('lodash/template');
const GroupMessages = require('../utils/groupMessages');
const {
fileHeader,
formattedVariables,
getTypeScriptType,
iconsWithPrefix,
minifyDictionary,
sortByReference,
createPropertyFormatter,
sortByName,
setSwiftFileProperties,
setComposeObjectProperties
} = require('./formatHelpers');
const SASS_MAP_FORMAT_DEPRECATION_WARNINGS = GroupMessages.GROUP.SassMapFormatDeprecationWarnings;
/**
* @namespace Formats
*/
module.exports = {
/**
* Creates a CSS file with variable definitions based on the style dictionary
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @param {string} [options.selector] - Override the root css selector
* @example
* ```css
* :root {
* --color-background-base: #f0f0f0;
* --color-background-alt: #eeeeee;
* }
* ```
*/
'css/variables': function({dictionary, options={}, file}) {
const selector = options.selector ? options.selector : `:root`;
const { outputReferences } = options;
return fileHeader({file}) +
`${selector} {\n` +
formattedVariables({format: 'css', dictionary, outputReferences}) +
`\n}\n`;
},
/**
* Creates a SCSS file with a flat map based on the style dictionary
*
* Name the map by adding a 'mapName' attribute on the file object in your config.
*
* @memberof Formats
* @kind member
* @example
* ```scss
* $tokens: (
* 'color-background-base': #f0f0f0;
* 'color-background-alt': #eeeeee;
* )
* ```
*/
'scss/map-flat': function({dictionary, options, file}) {
const template = _template(fs.readFileSync(__dirname + '/templates/scss/map-flat.template'));
const { allTokens } = dictionary;
return template({allTokens, file, options, fileHeader});
},
// This will soon be removed, is left here only for backwards compatibility
'sass/map-flat': function({dictionary, options, file}) {
GroupMessages.add(SASS_MAP_FORMAT_DEPRECATION_WARNINGS, "sass/map-flat");
return module.exports['scss/map-flat']({dictionary, options, file});
},
/**
* Creates a SCSS file with a deep map based on the style dictionary.
*
* Name the map by adding a 'mapName' attribute on the file object in your config.
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @param {Boolean} [options.themeable=true] - Whether or not tokens should default to being themeable, if not otherwise specified per token.
* @example
* ```scss
* $color-background-base: #f0f0f0 !default;
* $color-background-alt: #eeeeee !default;
*
* $tokens: {
* 'color': (
* 'background': (
* 'base': $color-background-base,
* 'alt': $color-background-alt
* )
* )
* )
* ```
*/
'scss/map-deep': function({dictionary, options, file}) {
const mapTemplate = _template(fs.readFileSync(__dirname + '/templates/scss/map-deep.template'));
// Default the "themeable" option to true for backward compatibility.
const { outputReferences, themeable = true } = options;
return '\n' +
fileHeader({ file, commentStyle: 'long' }) +
formattedVariables({ format: 'sass', dictionary, outputReferences, themeable })
+ '\n' +
mapTemplate({dictionary, file});
},
// This will soon be removed, is left here only for backwards compatibility
'sass/map-deep': function({dictionary, options, file}) {
GroupMessages.add(SASS_MAP_FORMAT_DEPRECATION_WARNINGS, "sass/map-deep");
return module.exports['scss/map-deep']({dictionary, options, file});
},
/**
* Creates a SCSS file with variable definitions based on the style dictionary.
*
* Add `!default` to any variable by setting a `themeable: true` attribute in the token's definition.
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @param {Boolean} [options.themeable=false] - Whether or not tokens should default to being themeable, if not otherwise specified per token.
* @example
* ```scss
* $color-background-base: #f0f0f0;
* $color-background-alt: #eeeeee !default;
* ```
*/
'scss/variables': function({dictionary, options, file}) {
const { outputReferences, themeable = false } = options;
return fileHeader({file, commentStyle: 'short'}) +
formattedVariables({format: 'sass', dictionary, outputReferences, themeable}) + '\n';
},
/**
* Creates a SCSS file with variable definitions and helper classes for icons
*
* @memberof Formats
* @kind member
* @example
* ```scss
* $content-icon-email: '\E001';
* .icon.email:before { content:$content-icon-email; }
* ```
*/
'scss/icons': function({dictionary, options, file}) {
return fileHeader({file, commentStyle: 'short'}) + iconsWithPrefix('$', dictionary.allTokens, options);
},
/**
* Creates a LESS file with variable definitions based on the style dictionary
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @example
* ```less
* \@color-background-base: #f0f0f0;
* \@color-background-alt: #eeeeee;
* ```
*/
'less/variables': function({dictionary, options, file}) {
const { outputReferences } = options;
return fileHeader({file, commentStyle: 'short'}) +
formattedVariables({format: 'less', dictionary, outputReferences}) + '\n';
},
/**
* Creates a LESS file with variable definitions and helper classes for icons
*
* @memberof Formats
* @kind member
* @example
* ```less
* \@content-icon-email: '\E001';
* .icon.email:before { content:\@content-icon-email; }
* ```
*/
'less/icons': function({dictionary, options, file}) {
return fileHeader({file, commentStyle: 'short'}) + iconsWithPrefix('@', dictionary.allTokens, options);
},
/**
* Creates a Stylus file with variable definitions based on the style dictionary
*
* @memberof Formats
* @kind member
* @example
* ```stylus
* $color-background-base= #f0f0f0;
* $color-background-alt= #eeeeee;
* ```
*/
'stylus/variables': function({dictionary, options, file}) {
const { outputReferences } = options;
return fileHeader({file, commentStyle: 'short'}) +
formattedVariables({format: 'stylus', dictionary, outputReferences}) + '\n';
},
/**
* Creates a CommonJS module with the whole style dictionary
*
* @memberof Formats
* @kind member
* @example
* ```js
* module.exports = {
* color: {
* base: {
* red: {
* value: '#ff0000'
* }
* }
* }
* }
* ```
*/
'javascript/module': function({dictionary, file}) {
return fileHeader({file}) +
'module.exports = ' +
JSON.stringify(dictionary.tokens, null, 2) + ';\n';
},
/**
* Creates a CommonJS module with the whole style dictionary flattened to a single level.
*
* @memberof Formats
* @kind member
* @example
* ```js
* module.exports = {
* "ColorBaseRed": "#ff0000"
*}
*```
*/
'javascript/module-flat': function({dictionary, file}) {
return fileHeader({file}) +
'module.exports = ' + '{\n' + dictionary.allTokens.map(function(token) {
return ` "${token.name}": ${JSON.stringify(token.value)}`;
}).join(',\n') + '\n}' + ';\n';
},
/**
* Creates a JS file a global var that is a plain javascript object of the style dictionary.
* Name the variable by adding a 'name' attribute on the file object in your config.
*
* @memberof Formats
* @kind member
* @example
* ```js
* var StyleDictionary = {
* color: {
* base: {
* red: {
* value: '#ff0000'
* }
* }
* }
* }
* ```
*/
'javascript/object': function({dictionary, file}) {
return fileHeader({file}) +
'var ' +
(file.name || '_styleDictionary') +
' = ' +
JSON.stringify(dictionary.tokens, null, 2) +
';\n';
},
/**
* Creates a [UMD](https://github.com/umdjs/umd) module of the style
* dictionary. Name the module by adding a 'name' attribute on the file object
* in your config.
*
* @memberof Formats
* @kind member
* @example
* ```js
* (function(root, factory) {
* if (typeof module === "object" && module.exports) {
* module.exports = factory();
* } else if (typeof exports === "object") {
* exports["_styleDictionary"] = factory();
* } else if (typeof define === "function" && define.amd) {
* define([], factory);
* } else {
* root["_styleDictionary"] = factory();
* }
* }(this, function() {
* return {
* "color": {
* "red": {
* "value": "#FF0000"
* }
* }
* };
* }))
* ```
*/
'javascript/umd': function({dictionary, file}) {
var name = file.name || '_styleDictionary'
return fileHeader({file}) +
'(function(root, factory) {\n' +
' if (typeof module === "object" && module.exports) {\n' +
' module.exports = factory();\n' +
' } else if (typeof exports === "object") {\n' +
' exports["' + name + '"] = factory();\n' +
' } else if (typeof define === "function" && define.amd) {\n' +
' define([], factory);\n' +
' } else {\n' +
' root["' + name + '"] = factory();\n' +
' }\n' +
'}(this, function() {\n' +
' return ' + JSON.stringify(dictionary.tokens, null, 2) + ';\n' +
'}))\n'
},
/**
* Creates a ES6 module of the style dictionary.
*
* ```json
* {
* "platforms": {
* "js": {
* "transformGroup": "js",
* "files": [
* {
* "format": "javascript/es6",
* "destination": "colors.js",
* "filter": {
* "attributes": {
* "category": "color"
* }
* }
* }
* ]
* }
* }
* }
* ```
*
* @memberof Formats
* @kind member
* @example
* ```js
* export const ColorBackgroundBase = '#ffffff';
* export const ColorBackgroundAlt = '#fcfcfcfc';
* ```
*/
'javascript/es6': function({dictionary, file}) {
return fileHeader({file}) +
dictionary.allTokens.map(function(token) {
var to_ret = 'export const ' + token.name + ' = ' + JSON.stringify(token.value) + ';';
if (token.comment)
to_ret = to_ret.concat(' // ' + token.comment);
return to_ret;
}).join('\n') + '\n';
},
// TypeScript declarations
/**
* Creates TypeScript declarations for ES6 modules
*
* ```json
* {
* "platforms": {
* "ts": {
* "transformGroup": "js",
* "files": [
* {
* "format": "javascript/es6",
* "destination": "colors.js"
* },
* {
* "format": "typescript/es6-declarations",
* "destination": "colors.d.ts"
* }
* ]
* }
* }
* }
* ```
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {Boolean} [options.outputStringLiterals=false] - Whether or not to output literal types for string values
* @example
* ```typescript
* export const ColorBackgroundBase : string;
* export const ColorBackgroundAlt : string;
* ```
*/
'typescript/es6-declarations': function({dictionary, file, options}) {
return fileHeader({file}) +
dictionary.allProperties.map(function(prop) {
let to_ret_prop = '';
if (prop.comment)
to_ret_prop += '/** ' + prop.comment + ' */\n';
to_ret_prop += 'export const ' + prop.name + ' : ' + getTypeScriptType(prop.value, options) + ';';
return to_ret_prop;
}).join('\n') + '\n';
},
/**
* Creates TypeScript declarations for CommonJS module
*
* ```json
* {
* "platforms": {
* "ts": {
* "transformGroup": "js",
* "files": [
* {
* "format": "javascript/module",
* "destination": "colors.js"
* },
* {
* "format": "typescript/module-declarations",
* "destination": "colors.d.ts"
* }
* ]
* }
* }
* }
* ```
*
* @memberof Formats
* @kind member
* @example
* ```typescript
* export default tokens;
* declare interface DesignToken { value: string; name?: string; path?: string[]; comment?: string; attributes?: any; original?: any; }
* declare const tokens: {
* "color": {
* "red": DesignToken
* }
* }
* ```
*
* As you can see above example output this does not generate 100% accurate d.ts.
* This is a compromise between of what style-dictionary can do to help and not bloating the library with rarely used dependencies.
*
* Thankfully you can extend style-dictionary very easily:
*
* ```js
* const JsonToTS = require('json-to-ts');
* StyleDictionaryPackage.registerFormat({
* name: 'typescript/accurate-module-declarations',
* formatter: function({ dictionary }) {
* return 'declare const root: RootObject\n' +
* 'export default root\n' +
* JsonToTS(dictionary.properties).join('\n');
* },
* });
* ```
*/
'typescript/module-declarations': function({dictionary, file, options}) {
const {moduleName=`tokens`} = options;
function treeWalker(obj) {
let type = Object.create(null);
let has = Object.prototype.hasOwnProperty.bind(obj);
if (has('value')) {
type = 'DesignToken';
} else {
for (var k in obj) if (has(k)) {
switch (typeof obj[k]) {
case 'object':
type[k] = treeWalker(obj[k]);
}
}
}
return type;
}
const designTokenInterface = fs.readFileSync(
path.resolve(__dirname, `../../types/DesignToken.d.ts`), {encoding:'UTF-8'}
);
// get the first and last lines to add to the format by
// looking for the first and last single-line comment
const lines = designTokenInterface
.split('\n');
const firstLine = lines.indexOf(`//start`) + 1;
const lastLine = lines.indexOf(`//end`);
const output = fileHeader({file}) +
`export default ${moduleName};
declare ${lines.slice(firstLine, lastLine).join(`\n`)}
declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), null, 2)}`;
// JSON stringify will quote strings, because this is a type we need
// it unquoted.
return output.replace(/"DesignToken"/g, 'DesignToken') + '\n';
},
// Android templates
/**
* Creates a [resource](https://developer.android.com/guide/topics/resources/providing-resources) xml file. It is recommended to use a filter with this format
* as it is generally best practice in Android development to have resource files
* organized by type (color, dimension, string, etc.). However, a resource file
* with mixed resources will still work.
*
* This format will try to use the proper resource type for each token based on
* the category (color => color, size => dimen, etc.). However if you want to
* force a particular resource type you can provide a 'resourceType' attribute
* on the file configuration. You can also provide a 'resourceMap' if you
* don't use Style Dictionary's built-in CTI structure.
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @example
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <resources>
* <color name="color_base_red_5">#fffaf3f2</color>
* <color name="color_base_red_30">#fff0cccc</color>
* <dimen name="size_font_base">14sp</color>
* ```
*/
'android/resources': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/android/resources.template')
);
return template({dictionary, file, options, fileHeader});
},
/**
* Creates a color resource xml file with all the colors in your style dictionary.
*
* It is recommended to use the 'android/resources' format with a custom filter
* instead of this format:
*
* ```javascript
* format: 'android/resources',
* filter: {
* attributes: { category: 'color' }
* }
* ```
*
* @memberof Formats
* @kind member
* @example
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <resources>
* <color name="color_base_red_5">#fffaf3f2</color>
* <color name="color_base_red_30">#fff0cccc</color>
* <color name="color_base_red_60">#ffe19d9c</color>
* ```
*/
'android/colors': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/android/colors.template')
);
return template({dictionary, file, options, fileHeader});
},
/**
* Creates a dimen resource xml file with all the sizes in your style dictionary.
*
* It is recommended to use the 'android/resources' format with a custom filter
* instead of this format:
*
* ```javascript
* format: 'android/resources',
* filter: {
* attributes: { category: 'size' }
* }
* ```
*
* @memberof Formats
* @kind member
* @example
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <resources>
* <dimen name="size_padding_tiny">5.00dp</dimen>
* <dimen name="size_padding_small">10.00dp</dimen>
* <dimen name="size_padding_medium">15.00dp</dimen>
* ```
*/
'android/dimens': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/android/dimens.template')
);
return template({dictionary, file, options, fileHeader});
},
/**
* Creates a dimen resource xml file with all the font sizes in your style dictionary.
*
* It is recommended to use the 'android/resources' format with a custom filter
* instead of this format:
*
* ```javascript
* format: 'android/resources',
* filter: {
* attributes: { category: 'size' }
* }
* ```
*
* @memberof Formats
* @kind member
* @example
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <resources>
* <dimen name="size_font_tiny">10.00sp</dimen>
* <dimen name="size_font_small">13.00sp</dimen>
* <dimen name="size_font_medium">15.00sp</dimen>
* ```
*/
'android/fontDimens': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/android/fontDimens.template')
);
return template({dictionary, file, options, fileHeader});
},
/**
* Creates a resource xml file with all the integers in your style dictionary. It filters your
* design tokens by `token.attributes.category === 'time'`
*
* It is recommended to use the 'android/resources' format with a custom filter
* instead of this format:
*
* ```javascript
* format: 'android/resources',
* filter: {
* attributes: { category: 'time' }
* }
* ```
*
* @memberof Formats
* @kind member
* @todo Update the filter on this.
* @example
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <resources>
* <integer name="time_duration_short">1000</integer>
* <integer name="time_duration_medium">2000</integer>
* <integer name="time_duration_long">4000</integer>
* ```
*/
'android/integers': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/android/integers.template')
);
return template({dictionary, file, options, fileHeader});
},
/**
* Creates a resource xml file with all the strings in your style dictionary. Filters your
* design tokens by `token.attributes.category === 'content'`
*
* It is recommended to use the 'android/resources' format with a custom filter
* instead of this format:
*
* ```javascript
* format: 'android/resources',
* filter: {
* attributes: { category: 'content' }
* }
* ```
*
* @memberof Formats
* @kind member
* @example
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <resources>
* <string name="content_icon_email"></string>
* <string name="content_icon_chevron_down"></string>
* <string name="content_icon_chevron_up"></string>
* ```
*/
'android/strings': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/android/strings.template')
);
return template({dictionary, file, options, fileHeader});
},
// Compose templates
/**
* Creates a Kotlin file for Compose containing an object with a `val` for each property.
*
* @memberof Formats
* @kind member
* @param {String} className The name of the generated Kotlin object
* @param {String} packageName The package for the generated Kotlin object
* @param {Object} options
* @param {String[]} [options.import=['androidx.compose.ui.graphics.Color', 'androidx.compose.ui.unit.*']] - Modules to import. Can be a string or array of strings
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @example
* ```kotlin
* package com.example.tokens;
*
* import androidx.compose.ui.graphics.Color
*
* object StyleDictionary {
* val colorBaseRed5 = Color(0xFFFAF3F2)
* }
* ```
*/
'compose/object': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/compose/object.kt.template')
);
let allProperties;
const { outputReferences } = options;
const formatProperty = createPropertyFormatter({
outputReferences,
dictionary,
formatting: {
suffix: '',
commentStyle: 'none' // We will add the comment in the format template
}
});
if (outputReferences) {
allProperties = [...dictionary.allProperties].sort(sortByReference(dictionary));
} else {
allProperties = [...dictionary.allProperties].sort(sortByName);
}
options = setComposeObjectProperties(options);
return template({allProperties, file, options, formatProperty, fileHeader});
},
// iOS templates
/**
* Creates an Objective-C header file with macros for design tokens
*
* @memberof Formats
* @kind member
* @example
* ```objectivec
* #import <Foundation/Foundation.h>
* #import <UIKit/UIKit.h>
*
* #define ColorFontLink [UIColor colorWithRed:0.00f green:0.47f blue:0.80f alpha:1.00f]
* #define SizeFontTiny 176.00f
* ```
*/
'ios/macros': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/macros.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C plist file
*
* @memberof Formats
* @kind member
* @todo Fix this template and add example and usage
*/
'ios/plist': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/plist.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C implementation file of a style dictionary singleton class
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/singleton.m': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/singleton.m.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C header file of a style dictionary singleton class
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/singleton.h': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/singleton.h.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C header file of a static style dictionary class
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/static.h': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/static.h.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C implementation file of a static style dictionary class
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/static.m': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/static.m.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C header file of a color class
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/colors.h': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/colors.h.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C implementation file of a color class
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/colors.m': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/colors.m.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C header file of strings
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/strings.h': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/strings.h.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates an Objective-C implementation file of strings
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios/strings.m': function({dictionary, options, file}) {
const template = _template(
fs.readFileSync(__dirname + '/templates/ios/strings.m.template')
);
return template({ dictionary, options, file, fileHeader });
},
/**
* Creates a Swift implementation file of a class with values. It adds default `class` object type, `public` access control and `UIKit` import.
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {String} [options.accessControl=public] - Level of [access](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) of the generated swift object
* @param {String[]} [options.import=UIKit] - Modules to import. Can be a string or array of strings
* @param {String} [options.className] - The name of the generated Swift class
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @example
* ```swift
* public class StyleDictionary {
* public static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1)
* }
* ```
*/
'ios-swift/class.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, 'class', 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});
},
/**
* Creates a Swift implementation file of an enum with values. It adds default `enum` object type, `public` access control and `UIKit` import.
*
* @memberof Formats
* @kind member
* @param {Object} options
* @param {String} [options.accessControl=public] - Level of [access](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) of the generated swift object
* @param {String[]} [options.import=UIKit] - Modules to import. Can be a string or array of strings
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @example
* ```swift
* public enum StyleDictionary {
* public static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1)
* }
* ```
*/
'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);