style-dictionary 3.1.1 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/examples/advanced/assets-base64-embed/package.json +1 -1
  2. package/examples/advanced/auto-rebuild-watcher/package.json +1 -1
  3. package/examples/advanced/component-cti/package.json +1 -1
  4. package/examples/advanced/create-react-app/package.json +2 -2
  5. package/examples/advanced/create-react-native-app/package.json +1 -1
  6. package/examples/advanced/custom-file-header/package.json +1 -1
  7. package/examples/advanced/custom-filters/README.md +99 -0
  8. package/examples/advanced/custom-filters/build.js +27 -0
  9. package/examples/advanced/custom-filters/config.json +21 -0
  10. package/examples/advanced/custom-filters/package.json +20 -0
  11. package/examples/advanced/custom-filters/tokens/spacing.json +20 -0
  12. package/examples/advanced/custom-formats-with-templates/package.json +1 -1
  13. package/examples/advanced/custom-parser/package.json +1 -1
  14. package/examples/advanced/custom-transforms/package.json +1 -1
  15. package/examples/advanced/format-helpers/package.json +1 -1
  16. package/examples/advanced/matching-build-files/package.json +1 -1
  17. package/examples/advanced/multi-brand-multi-platform/package.json +1 -1
  18. package/examples/advanced/node-modules-as-config-and-properties/package.json +1 -1
  19. package/examples/advanced/npm-module/package.json +1 -1
  20. package/examples/advanced/referencing_aliasing/package.json +1 -1
  21. package/examples/advanced/s3/package.json +1 -1
  22. package/examples/advanced/tokens-deprecation/package.json +1 -1
  23. package/examples/advanced/transitive-transforms/package.json +1 -1
  24. package/examples/advanced/variables-in-outputs/package.json +1 -1
  25. package/examples/advanced/yaml-tokens/package.json +1 -1
  26. package/examples/basic/config.json +17 -3
  27. package/examples/complete/package.json +1 -1
  28. package/index.js +2 -2
  29. package/lib/common/filters.js +30 -0
  30. package/lib/common/formatHelpers/createPropertyFormatter.js +14 -2
  31. package/lib/common/formatHelpers/index.js +2 -1
  32. package/lib/common/formatHelpers/setSwiftFileProperties.js +55 -0
  33. package/lib/common/formats.js +77 -10
  34. package/lib/common/templates/ios-swift/{class.swift.template → any.swift.template} +6 -3
  35. package/package.json +24 -23
  36. package/types/index.d.ts +3 -2
  37. package/lib/common/templates/ios-swift/enum.swift.template +0 -26
@@ -11,6 +11,6 @@
11
11
  "author": "",
12
12
  "license": "Apache-2.0",
13
13
  "devDependencies": {
14
- "style-dictionary": "3.1.1"
14
+ "style-dictionary": "3.7.0"
15
15
  }
16
16
  }
@@ -17,6 +17,6 @@
17
17
  "license": "Apache-2.0",
18
18
  "devDependencies": {
19
19
  "chokidar-cli": "^1.2.0",
20
- "style-dictionary": "3.1.1"
20
+ "style-dictionary": "3.7.0"
21
21
  }
22
22
  }
@@ -15,6 +15,6 @@
15
15
  "author": "",
16
16
  "license": "Apache-2.0",
17
17
  "devDependencies": {
18
- "style-dictionary": "3.1.1"
18
+ "style-dictionary": "3.7.0"
19
19
  }
20
20
  }
@@ -3,14 +3,14 @@
3
3
  "description": "",
4
4
  "version": "1.0.0",
5
5
  "dependencies": {
6
- "node-sass": "^6.0.1",
6
+ "node-sass": "^7.0.0",
7
7
  "react": "^17.0.1",
8
8
  "react-dom": "^17.0.1",
9
9
  "react-scripts": "^4.0.3",
10
10
  "styled-components": "^5.3.0"
11
11
  },
12
12
  "devDependencies": {
13
- "style-dictionary": "3.1.1"
13
+ "style-dictionary": "3.7.0"
14
14
  },
15
15
  "resolutions": {
16
16
  "immer": "8.0.1",
@@ -27,7 +27,7 @@
27
27
  "babel-jest": "~25.2.6",
28
28
  "jest": "~25.2.6",
29
29
  "react-test-renderer": "~16.13.1",
30
- "style-dictionary": "3.1.1"
30
+ "style-dictionary": "3.7.0"
31
31
  },
32
32
  "jest": {
33
33
  "preset": "react-native"
@@ -10,6 +10,6 @@
10
10
  "author": "",
11
11
  "license": "ISC",
12
12
  "devDependencies": {
13
- "style-dictionary": "3.1.1"
13
+ "style-dictionary": "3.7.0"
14
14
  }
15
15
  }
@@ -0,0 +1,99 @@
1
+ ## Filters
2
+
3
+ This example shows how to use built-in and custom filters to the design tokens.
4
+
5
+ Filters are functions that might remove according to some conditions a design token from the output distribution.
6
+
7
+ #### Running the example
8
+
9
+ First of all, set up the required dependencies running the command `npm install` in your local CLI environment (if you prefer to use *yarn* update the commands accordingly).
10
+
11
+ At this point, if you want to build the tokens you can run `npm run build`. This command will generate the files in the `build` folder.
12
+
13
+ #### How does it work a built-in filter?
14
+
15
+ Currently StyleDictionary supports just the following built-in filters:
16
+
17
+ - removePrivate
18
+
19
+ You have to apply it in the `config.json` file:
20
+
21
+ ```
22
+ "scss": {
23
+ "buildPath": "build/web/",
24
+ "files": [{
25
+ "destination": "colors.scss",
26
+ "filter": "removePrivate",
27
+ "format": "scss/variables"
28
+ }]
29
+ }
30
+ ```
31
+
32
+ The StyleDictionary will take care of filtering out proper design tokens from the source of truth:
33
+
34
+ ```
35
+ {
36
+ "color": {
37
+ "gray": {
38
+ "light" : {
39
+ "value": "#CCCCCC",
40
+ "group": "color",
41
+ "private": true
42
+ },
43
+ "medium": {
44
+ "value": "#999999",
45
+ "group": "color"
46
+ },
47
+ "dark" : {
48
+ "value": "#111111",
49
+ "group": "color"
50
+ }
51
+ },
52
+ }
53
+ }
54
+ ```
55
+
56
+ #### How does it work a custom filter?
57
+
58
+ To declare a custom **filter**, you have to call the `registerFilter` method:
59
+
60
+ ```
61
+ StyleDictionary.registerFilter({
62
+ name: 'isTextTransform',
63
+ matcher: function(token) {
64
+ return token.attributes.category === 'font' && token.value.includes['lowercase', 'uppercase]
65
+ }
66
+ });
67
+ ```
68
+
69
+ You have to apply it in the `config.json` file:
70
+
71
+ ```
72
+ "scss": {
73
+ "buildPath": "build/web/",
74
+ "files": [{
75
+ "destination": "fonts.scss",
76
+ "filter": "isTextTransform",
77
+ "format": "scss/variables"
78
+ }]
79
+ }
80
+ ```
81
+
82
+ The StyleDictionary will take care of filtering out proper design tokens from the source of truth:
83
+
84
+ ```
85
+ {
86
+ "fonts": {
87
+ "title-transform": {
88
+ "value": "uppercase", // included
89
+ "group": "font"
90
+ },
91
+ "title-size": {
92
+ "value": "36px", // excluded
93
+ "group": "font"
94
+ }
95
+ }
96
+ }
97
+ ```
98
+
99
+ More information can be found on the [documentation](https://amzn.github.io/style-dictionary/#/api?id=registerfilter).
@@ -0,0 +1,27 @@
1
+ const StyleDictionary = require('../../../index');
2
+
3
+ console.log('Build started...');
4
+ console.log('\n==============================================');
5
+
6
+
7
+ // REGISTER THE CUSTOM FILTERS
8
+
9
+ StyleDictionary.registerFilter({
10
+ name: 'removeBigSpacing',
11
+ matcher: function(token) {
12
+ return token.group === 'spacing' && token.value < 0.5
13
+ }
14
+ });
15
+
16
+ // APPLY THE CONFIGURATION
17
+ // IMPORTANT: the registration of custom transforms
18
+ // needs to be done _before_ applying the configuration
19
+ const StyleDictionaryExtended = StyleDictionary.extend(__dirname + '/config.json');
20
+
21
+
22
+ // FINALLY, BUILD ALL THE PLATFORMS
23
+ StyleDictionaryExtended.buildAllPlatforms();
24
+
25
+
26
+ console.log('\n==============================================');
27
+ console.log('\nBuild completed!');
@@ -0,0 +1,21 @@
1
+ {
2
+ "source": ["tokens/**/*.json"],
3
+ "platforms": {
4
+ "web": {
5
+ "buildPath": "build/web/",
6
+ "files": [{
7
+ "destination": "tokens.js",
8
+ "filter": "removeBigSpacing",
9
+ "format": "javascript/es6"
10
+ }]
11
+ },
12
+ "scss": {
13
+ "buildPath": "build/web/",
14
+ "files": [{
15
+ "destination": "tokens.scss",
16
+ "filter": "removeBigSpacing",
17
+ "format": "scss/variables"
18
+ }]
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "style-dictionary-filters",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "build/index.js",
6
+ "files": [
7
+ "build",
8
+ "properties"
9
+ ],
10
+ "scripts": {
11
+ "build": "node ./build.js",
12
+ "clean": "rm -rf build",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "",
16
+ "license": "Apache-2.0",
17
+ "devDependencies": {
18
+ "style-dictionary": "3.7.0"
19
+ }
20
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "margin": {
3
+ "small" : {
4
+ "value": "0.15",
5
+ "group": "spacing"
6
+ },
7
+ "medium": {
8
+ "value": "0.25",
9
+ "group": "spacing"
10
+ },
11
+ "large" : {
12
+ "value": "0.6",
13
+ "group": "spacing"
14
+ },
15
+ "extralarge" : {
16
+ "value": "1",
17
+ "group": "spacing"
18
+ }
19
+ }
20
+ }
@@ -18,6 +18,6 @@
18
18
  "handlebars": "^4.7.7",
19
19
  "lodash": "^4.17.21",
20
20
  "pug": "^3.0.2",
21
- "style-dictionary": "3.1.1"
21
+ "style-dictionary": "3.7.0"
22
22
  }
23
23
  }
@@ -9,6 +9,6 @@
9
9
  "author": "",
10
10
  "license": "Apache-2.0",
11
11
  "devDependencies": {
12
- "style-dictionary": "3.1.1"
12
+ "style-dictionary": "3.7.0"
13
13
  }
14
14
  }
@@ -15,6 +15,6 @@
15
15
  "author": "",
16
16
  "license": "Apache-2.0",
17
17
  "devDependencies": {
18
- "style-dictionary": "3.1.1"
18
+ "style-dictionary": "3.7.0"
19
19
  }
20
20
  }
@@ -10,6 +10,6 @@
10
10
  "author": "",
11
11
  "license": "Apache-2.0",
12
12
  "devDependencies": {
13
- "style-dictionary": "3.1.1"
13
+ "style-dictionary": "3.7.0"
14
14
  }
15
15
  }
@@ -16,6 +16,6 @@
16
16
  "author": "Kelly Harrop <kn.harrop@gmail.com>",
17
17
  "license": "Apache-2.0",
18
18
  "devDependencies": {
19
- "style-dictionary": "3.1.1"
19
+ "style-dictionary": "3.7.0"
20
20
  }
21
21
  }
@@ -15,6 +15,6 @@
15
15
  "author": "",
16
16
  "license": "Apache-2.0",
17
17
  "devDependencies": {
18
- "style-dictionary": "3.1.1"
18
+ "style-dictionary": "3.7.0"
19
19
  }
20
20
  }
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "homepage": "https://github.com/dbanksdesign/style-dictionary-node#readme",
21
21
  "devDependencies": {
22
- "style-dictionary": "3.1.1",
22
+ "style-dictionary": "3.7.0",
23
23
  "tinycolor2": "^1.4.1"
24
24
  }
25
25
  }
@@ -16,6 +16,6 @@
16
16
  "author": "",
17
17
  "license": "Apache-2.0",
18
18
  "devDependencies": {
19
- "style-dictionary": "3.1.1"
19
+ "style-dictionary": "3.7.0"
20
20
  }
21
21
  }
@@ -15,6 +15,6 @@
15
15
  "author": "",
16
16
  "license": "Apache-2.0",
17
17
  "devDependencies": {
18
- "style-dictionary": "3.1.1"
18
+ "style-dictionary": "3.7.0"
19
19
  }
20
20
  }
@@ -15,6 +15,6 @@
15
15
  "devDependencies": {
16
16
  "aws-sdk": "^2.7.21",
17
17
  "fs-extra": "^1.0.0",
18
- "style-dictionary": "3.1.1"
18
+ "style-dictionary": "3.7.0"
19
19
  }
20
20
  }
@@ -16,6 +16,6 @@
16
16
  "license": "Apache-2.0",
17
17
  "devDependencies": {
18
18
  "lodash": "^4.17.11",
19
- "style-dictionary": "3.1.1"
19
+ "style-dictionary": "3.7.0"
20
20
  }
21
21
  }
@@ -11,6 +11,6 @@
11
11
  "license": "ISC",
12
12
  "devDependencies": {
13
13
  "chroma-js": "^2.1.0",
14
- "style-dictionary": "3.1.1"
14
+ "style-dictionary": "3.7.0"
15
15
  }
16
16
  }
@@ -10,6 +10,6 @@
10
10
  "author": "",
11
11
  "license": "MIT",
12
12
  "devDependencies": {
13
- "style-dictionary": "3.1.1"
13
+ "style-dictionary": "3.7.0"
14
14
  }
15
15
  }
@@ -9,7 +9,7 @@
9
9
  "author": "",
10
10
  "license": "Apache-2.0",
11
11
  "devDependencies": {
12
- "style-dictionary": "3.1.1",
12
+ "style-dictionary": "3.7.0",
13
13
  "yaml": "^1.10.0"
14
14
  }
15
15
  }
@@ -95,10 +95,25 @@
95
95
  "transformGroup": "ios-swift",
96
96
  "buildPath": "build/ios-swift/",
97
97
  "files": [{
98
- "destination": "StyleDictionary.swift",
98
+ "destination": "StyleDictionary+Class.swift",
99
99
  "format": "ios-swift/class.swift",
100
- "className": "StyleDictionary",
100
+ "className": "StyleDictionaryClass",
101
101
  "filter": {}
102
+ },{
103
+ "destination": "StyleDictionary+Enum.swift",
104
+ "format": "ios-swift/enum.swift",
105
+ "className": "StyleDictionaryEnum",
106
+ "filter": {}
107
+ },{
108
+ "destination": "StyleDictionary+Struct.swift",
109
+ "format": "ios-swift/any.swift",
110
+ "className": "StyleDictionaryStruct",
111
+ "filter": {},
112
+ "options": {
113
+ "imports": "SwiftUI",
114
+ "objectType": "struct",
115
+ "accessControl": "internal"
116
+ }
102
117
  }]
103
118
  },
104
119
  "ios-swift-separate-enums": {
@@ -117,7 +132,6 @@
117
132
  "destination": "StyleDictionarySize.swift",
118
133
  "format": "ios-swift/enum.swift",
119
134
  "className": "StyleDictionarySize",
120
- "type": "float",
121
135
  "filter": {
122
136
  "attributes": {
123
137
  "category": "size"
@@ -11,6 +11,6 @@
11
11
  "author": "",
12
12
  "license": "Apache-2.0",
13
13
  "devDependencies": {
14
- "style-dictionary": "2.9.0"
14
+ "style-dictionary": "latest"
15
15
  }
16
16
  }
package/index.js CHANGED
@@ -43,8 +43,8 @@ var StyleDictionary = {
43
43
  format: require('./lib/common/formats'),
44
44
  action: require('./lib/common/actions'),
45
45
  formatHelpers: require('./lib/common/formatHelpers'),
46
- filter: {}, // we need to initialise the object, since we don't have built-in filters
47
- parsers: [], // ditto ^
46
+ filter: require('./lib/common/filters'),
47
+ parsers: [], // we need to initialise the array, since we don't have built-in parsers
48
48
  fileHeader: {},
49
49
 
50
50
  registerTransform: require('./lib/register/transform'),
@@ -0,0 +1,30 @@
1
+ /*
2
+ * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
+ * the License. A copy of the License is located at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
+ * and limitations under the License.
12
+ */
13
+
14
+ /**
15
+ * @namespace Filters
16
+ */
17
+
18
+ module.exports = {
19
+ /**
20
+ * Remove a token from the ditribution output if it contains a key `private` set to true
21
+ *
22
+ * @memberof Filters
23
+ *
24
+ * @param {Object} token
25
+ * @returns {Boolean}
26
+ */
27
+ removePrivate: function(token) {
28
+ return (token && token.private) ? false : true
29
+ }
30
+ }
@@ -42,13 +42,21 @@ const defaultFormatting = {
42
42
  * ```
43
43
  * @param {Object} options
44
44
  * @param {Boolean} options.outputReferences - Whether or not to output references. You will want to pass this from the `options` object sent to the formatter function.
45
+ * @param {Boolean} options.outputReferenceFallbacks - Whether or not to output css variable fallback values when using output references. You will want to pass this from the `options` object sent to the formatter function.
45
46
  * @param {Dictionary} options.dictionary - The dictionary object sent to the formatter function
46
47
  * @param {String} options.format - Available formats are: 'css', 'sass', 'less', and 'stylus'. If you want to customize the format and can't use one of those predefined formats, use the `formatting` option
47
48
  * @param {Object} options.formatting - Custom formatting properties that define parts of a declaration line in code. The configurable strings are: prefix, indentation, separator, suffix, and commentStyle. Those are used to generate a line like this: `${indentation}${prefix}${prop.name}${separator} ${prop.value}${suffix}`
48
49
  * @param {Boolean} options.themeable [false] - Whether tokens should default to being themeable.
49
50
  * @returns {Function}
50
51
  */
51
- function createPropertyFormatter({ outputReferences, dictionary, format, formatting = {}, themeable = false }) {
52
+ function createPropertyFormatter({
53
+ outputReferences = false,
54
+ outputReferenceFallbacks = false,
55
+ dictionary,
56
+ format,
57
+ formatting = {},
58
+ themeable = false
59
+ }) {
52
60
  let {prefix, commentStyle, indentation, separator, suffix} = Object.assign({}, defaultFormatting, formatting);
53
61
 
54
62
  switch(format) {
@@ -106,7 +114,11 @@ function createPropertyFormatter({ outputReferences, dictionary, format, formatt
106
114
  if (ref.value && ref.name) {
107
115
  value = value.replace(ref.value, function() {
108
116
  if (format === 'css') {
109
- return `var(${prefix}${ref.name})`
117
+ if (outputReferenceFallbacks) {
118
+ return `var(${prefix}${ref.name}, ${ref.value})`;
119
+ } else {
120
+ return `var(${prefix}${ref.name})`;
121
+ }
110
122
  } else {
111
123
  return `${prefix}${ref.name}`;
112
124
  }
@@ -23,5 +23,6 @@
23
23
  iconsWithPrefix: require('./iconsWithPrefix'),
24
24
  sortByReference: require('./sortByReference'),
25
25
  sortByName: require('./sortByName'),
26
- minifyDictionary: require('./minifyDictionary')
26
+ minifyDictionary: require('./minifyDictionary'),
27
+ setSwiftFileProperties: require('./setSwiftFileProperties')
27
28
  }
@@ -0,0 +1,55 @@
1
+ /*
2
+ * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
+ * the License. A copy of the License is located at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
+ * and limitations under the License.
12
+ */
13
+
14
+ /**
15
+ * Outputs an object with swift format configurations. Sets import, object type and access control.
16
+ * @memberof module:formatHelpers
17
+ * @param {Object} options - The options object declared at configuration
18
+ * @param {String} objectType - The type of the object in the final file. Could be a class, enum, struct, etc.
19
+ * @param {String} transformGroup - The transformGroup of the file, so it can be applied proper import
20
+ * @returns {Object}
21
+ */
22
+ function setSwiftFileProperties(options, objectType, transformGroup) {
23
+ if (typeof options.objectType === 'undefined') {
24
+ if (typeof objectType === 'undefined') {
25
+ options.objectType = 'class';
26
+ } else {
27
+ options.objectType = objectType;
28
+ }
29
+ }
30
+
31
+ if (typeof options.import === 'undefined') {
32
+ if (typeof transformGroup === 'undefined') {
33
+ options.import = ['UIKit'];
34
+ } else if (['ios-swift', 'ios-swift-separate'].includes(transformGroup)) {
35
+ options.import = ['UIKit'];
36
+ } else {
37
+ // future swift-ui transformGroup to be added here
38
+ options.import = ['SwiftUI'];
39
+ }
40
+ } else if (typeof options.import === 'string') {
41
+ options.import = [options.import];
42
+ }
43
+
44
+ if (typeof options.accessControl === 'undefined') {
45
+ options.accessControl = 'public ';
46
+ } else {
47
+ if (options.accessControl !== "") {
48
+ options.accessControl = `${options.accessControl} `;
49
+ }
50
+ }
51
+
52
+ return options
53
+ }
54
+
55
+ module.exports = setSwiftFileProperties;
@@ -15,7 +15,7 @@ const fs = require('fs');
15
15
  const path = require('path');
16
16
  const _template = require('lodash/template');
17
17
  const GroupMessages = require('../utils/groupMessages');
18
- const { fileHeader, formattedVariables, getTypeScriptType, iconsWithPrefix, minifyDictionary, sortByReference, createPropertyFormatter, sortByName } = require('./formatHelpers');
18
+ const { fileHeader, formattedVariables, getTypeScriptType, iconsWithPrefix, minifyDictionary, sortByReference, createPropertyFormatter, sortByName, setSwiftFileProperties } = require('./formatHelpers');
19
19
 
20
20
  const SASS_MAP_FORMAT_DEPRECATION_WARNINGS = GroupMessages.GROUP.SassMapFormatDeprecationWarnings;
21
21
 
@@ -107,7 +107,7 @@ module.exports = {
107
107
 
108
108
  // Default the "themeable" option to true for backward compatibility.
109
109
  const { outputReferences, themeable = true } = options;
110
- return '\n' +
110
+ return '\n' +
111
111
  fileHeader({ file, commentStyle: 'long' }) +
112
112
  formattedVariables({ format: 'sass', dictionary, outputReferences, themeable })
113
113
  + '\n' +
@@ -744,6 +744,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
744
744
  },
745
745
 
746
746
  // iOS templates
747
+
747
748
  /**
748
749
  * Creates an Objective-C header file with macros for design tokens
749
750
  *
@@ -902,11 +903,13 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
902
903
  },
903
904
 
904
905
  /**
905
- * Creates a Swift implementation file of a class with values
906
+ * Creates a Swift implementation file of a class with values. It adds default `class` object type, `public` access control and `UIKit` import.
906
907
  *
907
908
  * @memberof Formats
908
909
  * @kind member
909
910
  * @param {Object} options
911
+ * @param {String} [options.accessControl=public] - Level of [access](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) of the generated swift object
912
+ * @param {String[]} [options.import=UIKit] - Modules to import. Can be a string or array of string
910
913
  * @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
911
914
  * @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
912
915
  * @example
@@ -916,12 +919,13 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
916
919
  * }
917
920
  * ```
918
921
  */
919
- 'ios-swift/class.swift': function({dictionary, options, file}) {
922
+ 'ios-swift/class.swift': function({dictionary, options, file, platform}) {
920
923
  const template = _template(
921
- fs.readFileSync(__dirname + '/templates/ios-swift/class.swift.template')
924
+ fs.readFileSync(__dirname + '/templates/ios-swift/any.swift.template')
922
925
  );
923
926
  let allTokens;
924
927
  const { outputReferences } = options;
928
+ options = setSwiftFileProperties(options, 'class', platform.transformGroup);
925
929
  const formatProperty = createPropertyFormatter({
926
930
  outputReferences,
927
931
  dictionary,
@@ -940,18 +944,29 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
940
944
  },
941
945
 
942
946
  /**
943
- * Creates a Swift implementation file of an enum with values
947
+ * Creates a Swift implementation file of an enum with values. It adds default `enum` object type, `public` access control and `UIKit` import.
944
948
  *
945
949
  * @memberof Formats
946
950
  * @kind member
947
- * @todo Add example and usage
951
+ * @param {Object} options
952
+ * @param {String} [options.accessControl=public] - Level of [access](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) of the generated swift object
953
+ * @param {String[]} [options.import=UIKit] - Modules to import. Can be a string or array of string
954
+ * @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
955
+ * @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
956
+ * @example
957
+ * ```swift
958
+ * public enum StyleDictionary {
959
+ * public static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1)
960
+ * }
961
+ * ```
948
962
  */
949
- 'ios-swift/enum.swift': function({dictionary, options, file}) {
963
+ 'ios-swift/enum.swift': function({dictionary, options, file, platform}) {
950
964
  const template = _template(
951
- fs.readFileSync(__dirname + '/templates/ios-swift/enum.swift.template')
965
+ fs.readFileSync(__dirname + '/templates/ios-swift/any.swift.template')
952
966
  );
953
967
  let allTokens;
954
968
  const { outputReferences } = options;
969
+ options = setSwiftFileProperties(options, 'enum', platform.transformGroup);
955
970
  const formatProperty = createPropertyFormatter({
956
971
  outputReferences,
957
972
  dictionary,
@@ -963,10 +978,62 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
963
978
  if (outputReferences) {
964
979
  allTokens = [...dictionary.allTokens].sort(sortByReference(dictionary));
965
980
  } else {
966
- allTokens = [...dictionary.allTokens].sort(sortByName)
981
+ allTokens = [...dictionary.allTokens].sort(sortByName);
967
982
  }
968
983
  return template({allTokens, file, options, formatProperty, fileHeader});
969
984
  },
985
+
986
+ /**
987
+ * Creates a Swift implementation file of any given type with values. It has by default `class` object type, `public` access control and `UIKit` import.
988
+ *
989
+ * ```javascript
990
+ * format: 'ios-swift/any.swift',
991
+ * import: ['UIKit', 'AnotherModule'],
992
+ * objectType: 'struct',
993
+ * accessControl: 'internal',
994
+ * ```
995
+ *
996
+ * @memberof Formats
997
+ * @kind member
998
+ * @param {Object} options
999
+ * @param {String} [options.accessControl=public] - Level of [access](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) of the generated swift object
1000
+ * @param {String[]} [options.import=UIKit] - Modules to import. Can be a string or array of strings
1001
+ * @param {String} [options.objectType=class] - The type of the generated Swift object
1002
+ * @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
1003
+ * @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
1004
+ * @example
1005
+ * ```swift
1006
+ * import UIKit
1007
+ * import AnotherModule
1008
+ *
1009
+ * internal struct StyleDictionary {
1010
+ * internal static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1)
1011
+ * }
1012
+ * ```
1013
+ */
1014
+ 'ios-swift/any.swift': function({ dictionary, options, file, platform }) {
1015
+ const template = _template(
1016
+ fs.readFileSync(__dirname + '/templates/ios-swift/any.swift.template')
1017
+ );
1018
+ let allTokens;
1019
+ const { outputReferences } = options;
1020
+ options = setSwiftFileProperties(options, options.objectType, platform.transformGroup);
1021
+ const formatProperty = createPropertyFormatter({
1022
+ outputReferences,
1023
+ dictionary,
1024
+ formatting: {
1025
+ suffix: ''
1026
+ }
1027
+ });
1028
+
1029
+ if (outputReferences) {
1030
+ allTokens = [...dictionary.allTokens].sort(sortByReference(dictionary));
1031
+ } else {
1032
+ allTokens = [...dictionary.allTokens].sort(sortByName);
1033
+ }
1034
+ return template({allTokens, file, options, formatProperty, fileHeader});
1035
+ },
1036
+
970
1037
  // Css templates
971
1038
 
972
1039
  /**
@@ -17,10 +17,13 @@
17
17
  // <%= file.destination %>
18
18
  //
19
19
  <%= fileHeader({file, commentStyle: 'short'}) %>
20
- import UIKit
20
+ <%= options.import.map(function(item) {
21
+ return 'import ' + item
22
+ }).join('\n')
23
+ %>
21
24
 
22
- public class <%= file.className %> {
25
+ <%= options.accessControl %><%= options.objectType %> <%= file.className %> {
23
26
  <%= allTokens.map(function(prop) {
24
- return 'public static let ' + formatProperty(prop);
27
+ return options.accessControl + 'static let ' + formatProperty(prop);
25
28
  }).join('\n ') %>
26
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "style-dictionary",
3
- "version": "3.1.1",
3
+ "version": "3.7.0",
4
4
  "description": "Style once, use everywhere. A build system for creating cross-platform styles.",
5
5
  "keywords": [
6
6
  "style dictionary",
@@ -44,7 +44,8 @@
44
44
  "generate-docs": "node ./scripts/generateDocs.js",
45
45
  "serve-docs": "docsify serve docs -p 3000 -P 12345",
46
46
  "install-cli": "npm install -g $(npm pack)",
47
- "release": "git add . && standard-version -a"
47
+ "release": "git add . && standard-version -a",
48
+ "prepare": "husky install"
48
49
  },
49
50
  "standard-version": {
50
51
  "scripts": {
@@ -119,36 +120,36 @@
119
120
  "dependencies": {
120
121
  "chalk": "^4.0.0",
121
122
  "change-case": "^4.1.2",
122
- "commander": "^5.1.0",
123
- "fs-extra": "^8.1.0",
124
- "glob": "^7.1.6",
123
+ "commander": "^8.3.0",
124
+ "fs-extra": "^10.0.0",
125
+ "glob": "^7.2.0",
126
+ "json5": "^2.2.0",
125
127
  "jsonc-parser": "^3.0.0",
126
- "json5": "^2.1.3",
127
128
  "lodash": "^4.17.15",
128
129
  "tinycolor2": "^1.4.1"
129
130
  },
130
131
  "devDependencies": {
131
- "@babel/preset-env": "^7.10.2",
132
- "@commitlint/cli": "^11.0.0",
133
- "@commitlint/config-conventional": "^11.0.0",
134
- "babel-jest": "^26.0.1",
132
+ "@babel/preset-env": "^7.16.11",
133
+ "@commitlint/cli": "^16.1.0",
134
+ "@commitlint/config-conventional": "^16.0.0",
135
+ "babel-jest": "^27.4.6",
135
136
  "babel-preset-env": "^1.7.0",
136
- "docsify": "^4.12.1",
137
+ "docsify": "^4.12.2",
137
138
  "docsify-cli": "^4.4.3",
138
- "eslint": "^6.8.0",
139
- "eslint-plugin-jest": "^24.1.3",
140
- "husky": "^4.2.5",
139
+ "eslint": "^8.7.0",
140
+ "eslint-plugin-jest": "^26.0.0",
141
+ "husky": "^7.0.0",
141
142
  "istanbul": "^0.4.5",
142
- "jest": "^26.6.3",
143
+ "jest": "^27.4.7",
143
144
  "jsdoc-escape-at": "^1.0.1",
144
- "jsdoc-to-markdown": "^7.0.1",
145
+ "jsdoc-to-markdown": "^7.1.0",
145
146
  "json5-jest": "^1.0.1",
146
- "less": "^3.11.2",
147
- "lint-staged": "^10.2.7",
148
- "node-sass": "^6.0.1",
149
- "standard-version": "^9.0.0",
150
- "stylus": "^0.54.8",
151
- "tsd": "^0.15.1",
152
- "yaml": "^1.10.0"
147
+ "less": "^4.1.2",
148
+ "lint-staged": "^12.3.1",
149
+ "node-sass": "^7.0.1",
150
+ "standard-version": "^9.3.2",
151
+ "stylus": "^0.56.0",
152
+ "tsd": "^0.19.1",
153
+ "yaml": "^1.10.2"
153
154
  }
154
155
  }
package/types/index.d.ts CHANGED
@@ -20,7 +20,7 @@ import {Dictionary as _Dictionary} from './Dictionary';
20
20
  import {File as _File} from './File';
21
21
  import {FileHeader as _FileHeader} from './FileHeader';
22
22
  import {Filter as _Filter} from './Filter';
23
- import {Format as _Format} from './Format';
23
+ import {Format as _Format, Formatter as _Formatter } from './Format';
24
24
  import {FormatHelpers as _FormatHelpers} from './FormatHelpers';
25
25
  import {Options as _Options} from './Options';
26
26
  import {Parser as _Parser} from './Parser';
@@ -44,6 +44,7 @@ declare namespace StyleDictionary {
44
44
  type Filter = _Filter;
45
45
  type Format = _Format;
46
46
  type FormatHelpers = _FormatHelpers;
47
+ type Formatter = _Formatter;
47
48
  type Options = _Options;
48
49
  type Parser = _Parser;
49
50
  type Platform = _Platform;
@@ -63,7 +64,7 @@ declare namespace StyleDictionary {
63
64
 
64
65
  transform: Record<string, Transform>;
65
66
  transformGroup: Record<string, TransformGroup['transforms']>;
66
- format: Record<string, Format>;
67
+ format: Record<string, Formatter>;
67
68
  action: Record<string, Action>;
68
69
  filter: Record<string, Filter>;
69
70
  fileHeader: Record<string, FileHeader>;
@@ -1,26 +0,0 @@
1
- <%
2
- //
3
- // Copyright 2019 Alaska Air Group or its affiliates. All Rights Reserved.
4
- //
5
- // Licensed under the Apache License, Version 2.0 (the "License").
6
- // You may not use this file except in compliance with the License.
7
- // A copy of the License is located at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // or in the "license" file accompanying this file. This file is distributed
12
- // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13
- // express or implied. See the License for the specific language governing
14
- // permissions and limitations under the License.
15
- %>
16
- //
17
- // <%= file.destination %>
18
- //
19
- <%= fileHeader({file, commentStyle: 'short'}) %>
20
- import UIKit
21
-
22
- public enum <%= file.className %> {
23
- <%= allTokens.map(function(prop) {
24
- return 'public static let ' + formatProperty(prop);
25
- }).join('\n ') %>
26
- }